Skip to content

Commit

Permalink
Add all but the last test for simulate transaction contract test
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-james committed Nov 23, 2024
1 parent a5c9740 commit 0cb11c8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

// https://solana.com/docs/rpc/http/simulatetransaction
@Disabled
final class SimulateTransactionContractTest extends SolanaClientIntegrationTestBase
{
private String transactionBlobBase58;
private String transactionBlobBase64;
private String mintToTransactionBlobBase58;
private String mintToTransactionBlobBase64;
private String transactionBlobBase64BadSignatures;
private String transactionBlobBase64ReplaceBlockhash;

Expand All @@ -28,7 +27,7 @@ void beforeEach() throws SolanaJsonRpcClientException
{
final String latestBlockhash = SOLANA_API.getLatestBlockhash().getResponse().getBlockhashBase58();

final byte[] successfulTransactionBytes = Solana4jJsonRpcTestHelper.createMintToTransactionBlob(
final byte[] successfulMintToTransactionBytes = Solana4jJsonRpcTestHelper.createMintToTransactionBlob(
Solana.account(PAYER),
Solana.blockhash(latestBlockhash),
Solana.account(TOKEN_MINT),
Expand All @@ -40,8 +39,8 @@ void beforeEach() throws SolanaJsonRpcClientException
)
);

transactionBlobBase58 = SolanaEncoding.encodeBase58(successfulTransactionBytes);
transactionBlobBase64 = Base64.getEncoder().encodeToString(successfulTransactionBytes);
mintToTransactionBlobBase58 = SolanaEncoding.encodeBase58(successfulMintToTransactionBytes);
mintToTransactionBlobBase64 = Base64.getEncoder().encodeToString(successfulMintToTransactionBytes);

final byte[] badSignaturesTransactionBytes = Solana4jJsonRpcTestHelper.createMintToTransactionBlob(
Solana.account(PAYER),
Expand Down Expand Up @@ -75,32 +74,62 @@ void beforeEach() throws SolanaJsonRpcClientException
@Test
void shouldSimulateTransactionDefaultOptionalParams() throws SolanaJsonRpcClientException
{
final var response = SOLANA_API.simulateTransaction(transactionBlobBase64).getResponse();
final var response = SOLANA_API.simulateTransaction(mintToTransactionBlobBase64).getResponse();

Assertions.assertThat(response.getUnitsConsumed()).isEqualTo(958);
Assertions.assertThat(response.getLogs()).hasSize(4);
Assertions.assertThat(response.getInnerInstructions()).isNull();
Assertions.assertThat(response.getReplacementBlockhash()).isNull();
Assertions.assertThat(response.getAccounts()).isNull();
Assertions.assertThat(response.getReturnData()).isNull();
Assertions.assertThat(response.getLogs()).hasSize(4);
Assertions.assertThat(response.getErr()).isNull();
}

@Test
void shouldSimulateTransactionSigVerifyOptionalParam() throws SolanaJsonRpcClientException
void shouldSimulateTransactionSigVerifyTrueOptionalParam() throws SolanaJsonRpcClientException
{
final SolanaClientOptionalParams optionalParams = new SolanaJsonRpcClientOptionalParams();
optionalParams.addParam("encoding", "base64");
optionalParams.addParam("sigVerify", true);

final var response = SOLANA_API.simulateTransaction(transactionBlobBase64BadSignatures);
final var response = SOLANA_API.simulateTransaction(transactionBlobBase64BadSignatures, optionalParams);

Assertions.assertThat(response.isSuccess()).isFalse();
// error because the signatures are invalid
Assertions.assertThat(response.getError().getErrorCode()).isEqualTo(-32003L);
Assertions.assertThat(response.getError().getErrorMessage()).isEqualTo("Transaction signature verification failure");
}

@Test
void shouldSimulateTransactionSigVerifyFalseOptionalParam() throws SolanaJsonRpcClientException
{
final SolanaClientOptionalParams optionalParams = new SolanaJsonRpcClientOptionalParams();
optionalParams.addParam("encoding", "base64");
optionalParams.addParam("sigVerify", false);

final var response = SOLANA_API.simulateTransaction(transactionBlobBase64BadSignatures, optionalParams);

// sigVerify false so not looking at signatures
Assertions.assertThat(response.isSuccess()).isTrue();
}

@Test
void shouldSimulateTransactionReplaceRecentBlockhashFalseOptionalParam() throws SolanaJsonRpcClientException
{
final SolanaClientOptionalParams optionalParams = new SolanaJsonRpcClientOptionalParams();
optionalParams.addParam("encoding", "base64");
optionalParams.addParam("replaceRecentBlockhash", false);

final var response = SOLANA_API.simulateTransaction(transactionBlobBase64ReplaceBlockhash, optionalParams);

Assertions.assertThat(response.isSuccess()).isTrue();
// error because the invalid blockhash not replaced
Assertions.assertThat(response.getResponse().getErr()).isNotNull();
}


@Test
void shouldSimulateTransactionReplaceRecentBlockhashOptionalParam() throws SolanaJsonRpcClientException
void shouldSimulateTransactionReplaceRecentBlockhashTrueOptionalParam() throws SolanaJsonRpcClientException
{
final SolanaClientOptionalParams optionalParams = new SolanaJsonRpcClientOptionalParams();
optionalParams.addParam("encoding", "base64");
Expand All @@ -109,6 +138,8 @@ void shouldSimulateTransactionReplaceRecentBlockhashOptionalParam() throws Solan
final var response = SOLANA_API.simulateTransaction(transactionBlobBase64ReplaceBlockhash, optionalParams);

Assertions.assertThat(response.isSuccess()).isTrue();
// no error because the invalid blockhash replaced
Assertions.assertThat(response.getResponse().getErr()).isNull();
}

@Test
Expand All @@ -117,7 +148,7 @@ void shouldSimulateTransactionEncodingBase58OptionalParam() throws SolanaJsonRpc
final SolanaClientOptionalParams optionalParams = new SolanaJsonRpcClientOptionalParams();
optionalParams.addParam("encoding", "base58");

final var response = SOLANA_API.simulateTransaction(transactionBlobBase58, optionalParams);
final var response = SOLANA_API.simulateTransaction(mintToTransactionBlobBase58, optionalParams);

Assertions.assertThat(response.getResponse().getUnitsConsumed()).isEqualTo(958);
Assertions.assertThat(response.getResponse().getLogs()).hasSize(4);
Expand All @@ -131,19 +162,23 @@ void shouldSimulateTransactionInnerInstructionsOptionalParam() throws SolanaJson
optionalParams.addParam("encoding", "base64");
optionalParams.addParam("innerInstructions", true);

final var response = SOLANA_API.simulateTransaction(transactionBlobBase64, optionalParams);
final var response = SOLANA_API.simulateTransaction(mintToTransactionBlobBase64, optionalParams);

// both fields require cross program interactions deemed out of scope of this test
// inner instructions will be empty and return data will be null
Assertions.assertThat(response.getResponse().getInnerInstructions()).hasSize(0);
Assertions.assertThat(response.getResponse().getReturnData()).isNull();
}

@Test
@Disabled
void shouldSimulateTransactionAccountsConfigurationOptionalParam() throws SolanaJsonRpcClientException
{
final SolanaClientOptionalParams optionalParams = new SolanaJsonRpcClientOptionalParams();
optionalParams.addParam("encoding", "base64");
optionalParams.addParam("accounts", Map.of("encoding", "base64", "address", List.of(TOKEN_ACCOUNT_1)));

final var response = SOLANA_API.simulateTransaction(transactionBlobBase64, optionalParams);
final var response = SOLANA_API.simulateTransaction(mintToTransactionBlobBase64, optionalParams);

assertThat(response.isSuccess()).isTrue();
}
Expand All @@ -155,7 +190,7 @@ void shouldReturnErrorForMinContextSlotNotReached() throws SolanaJsonRpcClientEx
optionalParams.addParam("encoding", "base64");
optionalParams.addParam("minContextSlot", 10000000000L);

final var response = SOLANA_API.sendTransaction(transactionBlobBase64, optionalParams);
final var response = SOLANA_API.sendTransaction(mintToTransactionBlobBase64, optionalParams);

Assertions.assertThat(response.isSuccess()).isFalse();
Assertions.assertThat(response.getError().getErrorCode()).isEqualTo(-32016L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ static byte[] createMintToTransactionBlob(
List.of(destination))
)
.payer(payer)
.seal();
.seal()
.unsigned()
.build();

final SignedMessageBuilder signedMessageBuilder = Solana.forSigning(buffer);
for (final Signer signer : signers)
{
signedMessageBuilder.by(signer.signer, (transaction, signature) -> BouncyCastleSigner.sign(signer.privateKey, transaction, signature));
}

signedMessageBuilder.build();

return ByteBufferPrimitiveArray.copy(buffer);
Expand Down Expand Up @@ -70,14 +71,15 @@ static byte[] createTransferTokenTransactionBlob(
signers.stream().map(x -> x.signer).collect(Collectors.toList()))
)
.payer(payer)
.seal();
.seal()
.unsigned()
.build();

final SignedMessageBuilder signedMessageBuilder = Solana.forSigning(buffer);
for (final Signer signer : signers)
{
signedMessageBuilder.by(signer.signer, (transaction, signature) -> BouncyCastleSigner.sign(signer.privateKey, transaction, signature));
}

signedMessageBuilder.build();

return ByteBufferPrimitiveArray.copy(buffer);
Expand Down

0 comments on commit 0cb11c8

Please sign in to comment.