Skip to content

Commit

Permalink
add integration test for solTransfer
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-james committed Jun 19, 2024
1 parent 2031d2b commit fcca28f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void shouldCreateNonce(final String messageEncoding)
solana.createKeyPair("account");
solana.createKeyPair("authority");

solana.airdrop("address: authority", "amountSol: 10");
solana.airdrop("authority", "10");
solana.createNonceAccount("account", "authority", "payer");
}

Expand All @@ -38,4 +38,20 @@ void shouldAdvanceNonce(final String messageEncoding)

solana.advanceNonce("account", "authority", "payer");
}

@ParameterizedMessageEncodingTest
void shouldTransferSol(final String messageEncoding)
{
solana.setMessageEncoding(messageEncoding);

solana.createKeyPair("from");
solana.createKeyPair("to");

solana.airdrop("from", "10");

solana.transfer("from", "to", "1", "payer");

solana.balance("from", "9");
solana.balance("to", "1");
}
}
21 changes: 21 additions & 0 deletions src/test-support/java/com/lmax/solana4j/SolanaDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,27 @@ public String tokenTransfer(
return solanaApi.sendTransaction(transactionBlob, Commitment.FINALIZED);
}

public String transfer(
final TestKeyPair from,
final TestPublicKey to,
final long amount,
final TestKeyPair payer,
final List<AddressLookupTable> addressLookupTables)
{
final Blockhash blockhash = solanaApi.getRecentBlockHash();

final String transactionBlob = getTransactionFactory().solTransfer(
from.getSolana4jPublicKey(),
to.getSolana4jPublicKey(),
amount,
Solana.blockhash(blockhash.getBytes()),
payer.getSolana4jPublicKey(),
List.of(payer, from),
addressLookupTables);

return solanaApi.sendTransaction(transactionBlob, Commitment.FINALIZED);
}

public void setMessageEncoding(final String messageEncoding)
{
if (messageEncoding.equals("V0"))
Expand Down
41 changes: 40 additions & 1 deletion src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void airdrop(final String... args)
new OptionalArg("rememberTransactionId"));

final TestPublicKey address = testContext.getPublicKey(params.value("address"));
final Sol sol = new Sol(Long.parseLong(params.value("amountSol")));
final Sol sol = new Sol(params.valueAsLong("amountSol"));

final String transactionSignature = solanaDriver.requestAirdrop(address, sol.lamports());
new Waiter().waitFor(new IsNotNullAssertion<>(() -> solanaDriver.getTransactionResponse(transactionSignature, FINALIZED).getTransaction()));
Expand Down Expand Up @@ -285,6 +285,21 @@ public void tokenBalance(final String... args)
new Waiter().waitFor(new IsEqualToAssertion<>(amount, () -> solanaDriver.getTokenBalance(address.getPublicKeyBase58(), commitment)));
}

public void balance(final String... args)
{
final DslParams params = DslParams.create(
args,
new RequiredArg("address"),
new RequiredArg("amountSol"),
new OptionalArg("commitment").setDefault("FINALIZED").setAllowedValues(Commitment.class));

final TestPublicKey address = testContext.getPublicKey(params.value("address"));
final Sol sol = new Sol(params.valueAsLong("amountSol"));
final Commitment commitment = params.valueAs("commitment", Commitment.class);

new Waiter().waitFor(new IsEqualToAssertion<>(sol.lamports(), () -> solanaDriver.getBalance(address.getPublicKeyBase58(), commitment)));
}

public void tokenTransfer(final String... args)
{
final DslParams params = DslParams.create(
Expand Down Expand Up @@ -356,4 +371,28 @@ public void advanceNonce(final String... args)

new Waiter().waitFor(new IsNotNullAssertion<>(() -> solanaDriver.getTransactionResponse(transactionSignature, FINALIZED).getTransaction()));
}

public void transfer(final String... args)
{
final DslParams params = DslParams.create(
args,
new RequiredArg("from"),
new RequiredArg("to"),
new RequiredArg("amountSol"),
new RequiredArg("payer"),
new OptionalArg("addressLookupTables").setAllowMultipleValues()
);

final TestKeyPair from = testContext.getKeyPair(params.value("from"));
final TestPublicKey to = testContext.getPublicKey(params.value("to"));
final Sol sol = new Sol(params.valueAsLong("amountSol"));
final TestKeyPair payer = testContext.getKeyPair(params.value("payer"));
final List<AddressLookupTable> addressLookupTables = params.valuesAsList("addressLookupTables").stream()
.map(testContext::getAddressLookupTable)
.toList();

final String transactionSignature = solanaDriver.transfer(from, to, sol.lamports(), payer, addressLookupTables);

new Waiter().waitFor(new IsNotNullAssertion<>(() -> solanaDriver.getTransactionResponse(transactionSignature, FINALIZED).getTransaction()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String solTransfer(
to,
amount
))
.payer(from)
.payer(payer)
.seal()
.unsigned()
.build();
Expand Down

0 comments on commit fcca28f

Please sign in to comment.