Skip to content

Commit

Permalink
Fix compilation errors - need some advice on error codes though as i'…
Browse files Browse the repository at this point in the history
…m not convinced it's a great experience for the client
  • Loading branch information
ml-james committed Oct 28, 2024
1 parent b14b7c1 commit a4ae191
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.lmax.solana4j.client.api.AccountInfo;
import com.lmax.solana4j.client.api.Blockhash;
import com.lmax.solana4j.client.api.SolanaApi;
import com.lmax.solana4j.client.api.SolanaClientError;
import com.lmax.solana4j.client.api.SolanaClientResponse;
import com.lmax.solana4j.client.api.TokenAmount;
import com.lmax.solana4j.client.api.TransactionResponse;
import com.lmax.solana4j.domain.TestKeyPair;
import com.lmax.solana4j.domain.TestPublicKey;
Expand All @@ -19,6 +19,7 @@
import com.lmax.solana4j.transactionblobs.V0TransactionBlobFactory;

import java.util.List;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Collectors;

public class SolanaDriver
Expand All @@ -33,35 +34,35 @@ public SolanaDriver(final SolanaApi solanaApi)

public String requestAirdrop(final TestPublicKey address, final long amountLamports)
{
return solanaApi.requestAirdrop(address.getPublicKeyBase58(), amountLamports);
return retryingClient(solanaApi.requestAirdrop(address.getPublicKeyBase58(), amountLamports));
}

public TransactionResponse getTransactionResponse(final String transactionSignature)
{
return solanaApi.getTransaction(transactionSignature);
return retryingClient(solanaApi.getTransaction(transactionSignature));
}

public long getSlot()
{
return solanaApi.getSlot();
return retryingClient(solanaApi.getSlot());
}

public long getBlockHeight()
{
return solanaApi.getBlockHeight();
return retryingClient(solanaApi.getBlockHeight());
}

public AccountInfo getAccountInfo(final TestPublicKey address)
{
return solanaApi.getAccountInfo(address.getPublicKeyBase58());
return retryingClient(solanaApi.getAccountInfo(address.getPublicKeyBase58()));
}

public String createAddressLookupTable(final ProgramDerivedAddress programDerivedAddress,
final TestKeyPair authority,
final TestKeyPair payer,
final Slot slot)
{
final Blockhash recentBlockhash = solanaApi.getLatestBlockhash();
final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().createAddressLookupTable(
programDerivedAddress,
Expand All @@ -71,7 +72,7 @@ public String createAddressLookupTable(final ProgramDerivedAddress programDerive
payer.getSolana4jPublicKey(),
List.of(payer, authority));

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String extendAddressLookupTable(final TestPublicKey addressLookupTable,
Expand All @@ -80,7 +81,7 @@ public String extendAddressLookupTable(final TestPublicKey addressLookupTable,
final List<TestPublicKey> addressesToAdd,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash recentBlockhash = solanaApi.getLatestBlockhash();
final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().extendAddressLookupTable(
addressLookupTable.getSolana4jPublicKey(),
Expand All @@ -91,7 +92,7 @@ public String extendAddressLookupTable(final TestPublicKey addressLookupTable,
List.of(payer, authority),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String createMintAccount(
Expand All @@ -104,8 +105,8 @@ public String createMintAccount(
final int accountSpan,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash recentBlockhash = solanaApi.getLatestBlockhash();
final long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan);
final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash());
final long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan));

final String transactionBlob = getTransactionFactory().createMintAccount(
tokenProgram,
Expand All @@ -121,7 +122,7 @@ public String createMintAccount(
addressLookupTables
);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String mintTo(
Expand All @@ -133,7 +134,7 @@ public String mintTo(
final TestKeyPair payer,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash recentBlockhash = solanaApi.getLatestBlockhash();
final Blockhash recentBlockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().mintTo(
tokenProgram,
Expand All @@ -146,7 +147,7 @@ public String mintTo(
addressLookupTables
);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String createTokenAccount(
Expand All @@ -158,8 +159,8 @@ public String createTokenAccount(
final int accountSpan,
final List<AddressLookupTable> addressLookupTables)
{
final Long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan);
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan));
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().createTokenAccount(
tokenProgram,
Expand All @@ -173,17 +174,17 @@ public String createTokenAccount(
List.of(payer, account),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public long getBalance(final String address)
{
return solanaApi.getBalance(address);
return retryingClient(solanaApi.getBalance(address));
}

public String getTokenBalance(final String address)
public TokenAmount getTokenBalance(final String address)
{
return solanaApi.getTokenAccountBalance(address);
return retryingClient(solanaApi.getTokenAccountBalance(address));
}

public String createNonceAccount(
Expand All @@ -193,8 +194,8 @@ public String createNonceAccount(
final int accountSpan,
final List<AddressLookupTable> addressLookupTables)
{
final Long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan);
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan));
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().createNonce(
nonceAccount.getSolana4jPublicKey(),
Expand All @@ -206,7 +207,7 @@ public String createNonceAccount(
List.of(payer, nonceAccount, nonceAuthority),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String createMultiSigAccount(
Expand All @@ -218,8 +219,8 @@ public String createMultiSigAccount(
final TestKeyPair payer,
final List<AddressLookupTable> addressLookupTables)
{
final Long rentExemption = solanaApi.getMinimumBalanceForRentExemption(accountSpan);
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Long rentExemption = retryingClient(solanaApi.getMinimumBalanceForRentExemption(accountSpan));
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().createMultiSigAccount(
tokenProgram,
Expand All @@ -233,7 +234,7 @@ public String createMultiSigAccount(
List.of(payer, account),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String advanceNonce(
Expand All @@ -242,7 +243,7 @@ public String advanceNonce(
final TestKeyPair payer,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().advanceNonce(
account.getSolana4jPublicKey(),
Expand All @@ -252,7 +253,7 @@ public String advanceNonce(
List.of(payer, authority),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String tokenTransfer(
Expand All @@ -265,7 +266,7 @@ public String tokenTransfer(
final List<TestKeyPair> signers,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().tokenTransfer(
tokenProgram,
Expand All @@ -278,7 +279,7 @@ public String tokenTransfer(
signers,
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String transfer(
Expand All @@ -288,7 +289,7 @@ public String transfer(
final TestKeyPair payer,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().solTransfer(
from.getSolana4jPublicKey(),
Expand All @@ -299,7 +300,7 @@ public String transfer(
List.of(payer, from),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String createAssociatedTokenAccount(
Expand All @@ -311,7 +312,7 @@ public String createAssociatedTokenAccount(
final TestKeyPair payer,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().createAssociatedTokenAccount(
tokenProgram,
Expand All @@ -324,7 +325,7 @@ public String createAssociatedTokenAccount(
List.of(payer),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String setTokenAccountAuthority(
Expand All @@ -337,7 +338,7 @@ public String setTokenAccountAuthority(
final List<TestKeyPair> signers,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().setTokenAccountAuthority(
tokenProgram,
Expand All @@ -350,12 +351,12 @@ public String setTokenAccountAuthority(
signers,
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob);
return retryingClient(solanaApi.sendTransaction(transactionBlob));
}

public String setComputeUnits(final int computeUnitLimit, final long computeUnitPrice, final TestKeyPair payer)
{
final Blockhash blockhash = solanaApi.getLatestBlockhash();
final Blockhash blockhash = retryingClient(solanaApi.getLatestBlockhash());

final String transactionBlob = getTransactionFactory().setComputeUnits(
computeUnitLimit,
Expand Down Expand Up @@ -416,6 +417,17 @@ private TransactionBlobFactory getTransactionFactory()

private <T> T retryingClient(final SolanaClientResponse<T> response)
{
return response.getResponse();
for (int i = 0; i < 10; i++)
{
if (response.getError() == null)
{
return response.getResponse();
}
else
{
LockSupport.parkNanos(100_000);
}
}
throw new RuntimeException("Unable to get a response after retry.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ public void waitForSlot(final String... args)
new RequiredArg("slot")
);

Waiter.waitFor(Condition.isTrue(() ->
{
long slot = solanaDriver.getSlot();
slot > params.valueAsLong("slot"))
}));
Waiter.waitFor(Condition.isTrue(() -> solanaDriver.getSlot() > params.valueAsLong("slot")));
}

public void createMintAccount(final String... args)
Expand Down Expand Up @@ -308,7 +304,7 @@ public void tokenBalance(final String... args)
final TestPublicKey address = testContext.data(TestDataType.TEST_PUBLIC_KEY).lookup(params.value("address"));
final String amount = params.value("amount");

Waiter.waitFor(Condition.isEqualTo(amount, () -> solanaDriver.getTokenBalance(address.getPublicKeyBase58())));
Waiter.waitFor(Condition.isEqualTo(amount, () -> solanaDriver.getTokenBalance(address.getPublicKeyBase58()).getUiAmountString()));
}

public void solBalance(final String... args)
Expand Down

0 comments on commit a4ae191

Please sign in to comment.