Skip to content

Commit

Permalink
Add default OptionalParams to existing client - ready for testing non…
Browse files Browse the repository at this point in the history
… default params as input
  • Loading branch information
ml-james committed Nov 1, 2024
1 parent 6fac5e0 commit 37de451
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.lmax.solana4j.client.jsonrpc;

import com.lmax.solana4j.client.api.Commitment;
import com.lmax.solana4j.client.api.SolanaClientOptionalParams;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

// https://solana.com/docs/rpc/http/getaccountinfo
Expand All @@ -11,25 +16,6 @@ class GetAccountInfoContractTest extends SolanaClientIntegrationTestBase
@Test
void shouldGetAccountInfo() throws SolanaJsonRpcClientException
{
// {
// "jsonrpc" : "2.0",
// "result" : {
// "context" : {
// "apiVersion" : "1.18.25",
// "slot" : 479
// },
// "value" : {
// "data" : [ "", "base64" ],
// "executable" : false,
// "lamports" : 600000,
// "owner" : "11111111111111111111111111111111",
// "rentEpoch" : 18446744073709551615,
// "space" : 0
// }
// },
// "id" : 4
// }

final var accountInfo = api.getAccountInfo(payerAccount).getResponse();

assertThat(accountInfo.getOwner()).isEqualTo("11111111111111111111111111111111");
Expand All @@ -42,43 +28,65 @@ void shouldGetAccountInfo() throws SolanaJsonRpcClientException
}

@Test
@Disabled
void shouldGetTokenAccountInfo() throws SolanaJsonRpcClientException
void shouldGetAccountInfoWithOptionalParams() throws SolanaJsonRpcClientException
{
// {
// "jsonrpc" : "2.0",
// "result" : {
// "context" : {
// "apiVersion" : "1.18.25",
// "slot" : 479
// },
// "value" : {
// "data" : [ "", "base64" ],
// "executable" : false,
// "lamports" : 600000,
// "owner" : "11111111111111111111111111111111",
// "rentEpoch" : 18446744073709551615,
// "space" : 0
// }
// },
// "id" : 4
// }
final SolanaClientOptionalParams params = new SolanaJsonRpcClientOptionalParams();
params.addParam("commitment", Commitment.FINALIZED.name().toLowerCase(Locale.UK));
params.addParam("encoding", "base64");
params.addParam("dataSlice", Map.of("length", 10, "offset", 0));
params.addParam("minContextSlot", 1);

final var accountInfo = api.getAccountInfo(payerAccount, params).getResponse();

assertThat(accountInfo.getOwner()).isEqualTo("11111111111111111111111111111111");
assertThat(accountInfo.getData().get(0)).isEqualTo("");
assertThat(accountInfo.getData().get(1)).isEqualTo("base64");
assertThat(accountInfo.getLamports()).isEqualTo(600000L);
assertThat(accountInfo.isExecutable()).isEqualTo(false);
assertThat(accountInfo.getRentEpoch()).isEqualTo("18446744073709551615");
assertThat(accountInfo.getSpace()).isEqualTo(0);
}

@Test
void shouldGetTokenAccountInfo() throws SolanaJsonRpcClientException
{
final var tokenAccountInfo = api.getAccountInfo(tokenAccount).getResponse();

assertThat(tokenAccountInfo.getOwner()).isEqualTo("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
assertThat(tokenAccountInfo.getData().get(0)).isEqualTo(
"DiEMlfEgHelAEAAXQrGw4/3hg4TDygNdPgaE1JMaak97Idi7jw+W6DSraz1ENBeREhm4FdP/BAJ9rBwqRQtkB2Q" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
assertThat(tokenAccountInfo.getData().get(1)).isEqualTo("base64");
assertThat(tokenAccountInfo.getLamports()).isEqualTo(400000L);
assertThat(tokenAccountInfo.isExecutable()).isEqualTo(false);
assertThat(tokenAccountInfo.getRentEpoch()).isEqualTo("18446744073709551615");
assertThat(tokenAccountInfo.getSpace()).isEqualTo(165);
}

@Test
void shouldGetTokenAccountInfoWithOptionalParams() throws SolanaJsonRpcClientException
{
final SolanaClientOptionalParams params = new SolanaJsonRpcClientOptionalParams();
params.addParam("commitment", Commitment.FINALIZED.name().toLowerCase(Locale.UK));
params.addParam("encoding", "base64");
params.addParam("dataSlice", Map.of("length", 10, "offset", 10));
params.addParam("minContextSlot", 1);

final var tokenAccountInfo = api.getAccountInfo(tokenAccount, params).getResponse();

// assertThat(tokenAccountInfo.getOwner()).isEqualTo("11111111111111111111111111111111");
// assertThat(tokenAccountInfo.getData().get(0)).isEqualTo("");
// assertThat(tokenAccountInfo.getData().get(1)).isEqualTo("base64");
// assertThat(tokenAccountInfo.getLamports()).isEqualTo(600000L);
// assertThat(tokenAccountInfo.isExecutable()).isEqualTo(false);
// assertThat(tokenAccountInfo.getRentEpoch()).isEqualTo("18446744073709551615");
// assertThat(tokenAccountInfo.getSpace()).isEqualTo(0);
assertThat(tokenAccountInfo.getOwner()).isEqualTo("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
assertThat(tokenAccountInfo.getData().get(0)).isEqualTo("ABdCsbDj/eGDhA==");
assertThat(tokenAccountInfo.getData().get(1)).isEqualTo("base64");
assertThat(tokenAccountInfo.getLamports()).isEqualTo(400000L);
assertThat(tokenAccountInfo.isExecutable()).isEqualTo(false);
assertThat(tokenAccountInfo.getRentEpoch()).isEqualTo("18446744073709551615");
assertThat(tokenAccountInfo.getSpace()).isEqualTo(165);
}

@Test
@Disabled
void shouldThrowRpcExceptionForAccount()
void shouldThrowRpcExceptionForUnknownAccount()
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,8 @@ class GetTokenAccountBalanceContractTest extends SolanaClientIntegrationTestBase
@Test
void shouldGetTokenAccountBalance() throws SolanaJsonRpcClientException
{
// {
// "jsonrpc" : "2.0",
// "result" : {
// "context" : {
// "apiVersion" : "2.0.14",
// "slot" : 258
// },
// "value" : {
// "amount" : "100",
// "decimals" : 18,
// "uiAmount" : 1.0E-16,
// "uiAmountString" : "0.0000000000000001"
// }
// },
// "id" : 4
// }

final var tokenAccountBalance = api.getTokenAccountBalance(tokenAccount).getResponse();

assertThat(tokenAccountBalance.getAmount()).isEqualTo("100");
assertThat(tokenAccountBalance.getDecimals()).isEqualTo(18);
assertThat(tokenAccountBalance.getUiAmountString()).isEqualTo("0.0000000000000001");
Expand Down
106 changes: 104 additions & 2 deletions client/src/main/java/com/lmax/solana4j/client/api/SolanaApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ public interface SolanaApi
/**
* Sends a transaction to the Solana blockchain.
* The transaction is encoded as a base64-encoded string (transaction blob).
* Additional optional parameters can be passed to customize the transaction request.
*
* @param transactionBlob the base64-encoded string representing the transaction
* @param optionalParams a map of optional parameters to customize the transaction request,
* such as `skipPreflight`, `preflightCommitment`, or other Solana JSON-RPC options
* @return the signature of the transaction, which is a base58-encoded string
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<String> sendTransaction(String transactionBlob, SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Sends a transaction to the Solana blockchain without optional parameters.
*
* @param transactionBlob the base64-encoded string representing the transaction
* @return the signature of the transaction, which is a base58-encoded string
Expand All @@ -28,17 +40,38 @@ public interface SolanaApi
*/
SolanaClientResponse<TransactionResponse> getTransaction(String transactionSignature) throws SolanaJsonRpcClientException;

/**
* Retrieves the transaction response for a given transaction signature with optional parameters.
*
* @param transactionSignature the base58-encoded signature of the transaction
* @param optionalParams optional parameters for customizing the request
* @return the {@link TransactionResponse} containing details of the transaction
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<TransactionResponse> getTransaction(String transactionSignature, SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Requests an airdrop of lamports to the specified address.
* This is used for test purposes to receive lamports (the smallest unit of SOL).
*
* @param address the base58-encoded public key of the recipient account
* @param address the base58-encoded public key of the recipient account
* @param amountLamports the amount of lamports to be airdropped
* @return the transaction signature as a base58-encoded string
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<String> requestAirdrop(String address, long amountLamports) throws SolanaJsonRpcClientException;

/**
* Requests an airdrop of lamports to the specified address with optional parameters.
*
* @param address the base58-encoded public key of the recipient account
* @param amountLamports the amount of lamports to be airdropped
* @param optionalParams optional parameters for customizing the request
* @return the transaction signature as a base58-encoded string
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<String> requestAirdrop(String address, long amountLamports, SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Retrieves the balance of an account in lamports.
*
Expand All @@ -48,6 +81,16 @@ public interface SolanaApi
*/
SolanaClientResponse<Long> getBalance(String address) throws SolanaJsonRpcClientException;

/**
* Retrieves the balance of an account in lamports with optional parameters.
*
* @param address the base58-encoded public key of the account
* @param optionalParams optional parameters for customizing the request
* @return the balance of the account in lamports
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<Long> getBalance(String address, SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Retrieves the token account balance of an SPL token account.
*
Expand All @@ -57,6 +100,16 @@ public interface SolanaApi
*/
SolanaClientResponse<TokenAmount> getTokenAccountBalance(String address) throws SolanaJsonRpcClientException;

/**
* Retrieves the token account balance of an SPL token account with optional parameters.
*
* @param address the base58-encoded public key of the token account
* @param optionalParams optional parameters for customizing the request
* @return the {@link TokenAmount} representing the token balance of the account
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<TokenAmount> getTokenAccountBalance(String address, SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Retrieves the account information for the specified address.
* The account information includes the balance, ownership, and data stored in the account.
Expand All @@ -67,6 +120,16 @@ public interface SolanaApi
*/
SolanaClientResponse<AccountInfo> getAccountInfo(String address) throws SolanaJsonRpcClientException;

/**
* Retrieves the account information for the specified address with optional parameters.
*
* @param address the base58-encoded public key of the account
* @param params optional parameters for customizing the request
* @return the {@link AccountInfo} containing details of the account
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<AccountInfo> getAccountInfo(String address, SolanaClientOptionalParams params) throws SolanaJsonRpcClientException;

/**
* Retrieves the current block height of the Solana blockchain.
* The block height represents the number of blocks preceding the current block.
Expand All @@ -76,6 +139,15 @@ public interface SolanaApi
*/
SolanaClientResponse<Long> getBlockHeight() throws SolanaJsonRpcClientException;

/**
* Retrieves the current block height with optional parameters.
*
* @param optionalParams optional parameters for customizing the request
* @return the current block height
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<Long> getBlockHeight(SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Retrieves the current slot number.
* A slot is a specific point in time or block on the Solana blockchain.
Expand All @@ -85,6 +157,15 @@ public interface SolanaApi
*/
SolanaClientResponse<Long> getSlot() throws SolanaJsonRpcClientException;

/**
* Retrieves the current slot number with optional parameters.
*
* @param optionalParams optional parameters for customizing the request
* @return the current slot number
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<Long> getSlot(SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Retrieves the most recent blockhash.
* The blockhash ensures that a transaction is processed within a specific time window.
Expand All @@ -94,6 +175,15 @@ public interface SolanaApi
*/
SolanaClientResponse<Blockhash> getLatestBlockhash() throws SolanaJsonRpcClientException;

/**
* Retrieves the most recent blockhash with optional parameters.
*
* @param optionalParams optional parameters for customizing the request
* @return the {@link Blockhash} representing the most recent blockhash
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<Blockhash> getLatestBlockhash(SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;

/**
* Retrieves the minimum balance required for rent exemption for an account of the given size.
* This is the minimum balance needed to ensure the account is rent-exempt.
Expand All @@ -103,4 +193,16 @@ public interface SolanaApi
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<Long> getMinimumBalanceForRentExemption(int size) throws SolanaJsonRpcClientException;
}

/**
* Retrieves the minimum balance required for rent exemption for an account of the given size,
* with additional parameters to customize the request.
* This is the minimum balance needed to ensure the account is rent-exempt.
*
* @param size the size of the account in bytes
* @param optionalParams additional parameters to customize the rent exemption query
* @return the minimum balance in lamports for rent exemption
* @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request
*/
SolanaClientResponse<Long> getMinimumBalanceForRentExemption(int size, SolanaClientOptionalParams optionalParams) throws SolanaJsonRpcClientException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lmax.solana4j.client.api;

import java.util.Map;

/**
* Interface representing optional parameters for customizing Solana JSON-RPC requests.
* Implementations of this interface allow adding parameters that modify request behavior.
*/
public interface SolanaClientOptionalParams
{
/**
* Adds an optional parameter to the request.
* This can be used to pass additional customization options supported by the Solana JSON-RPC API.
*
* @param key the name of the parameter to add (e.g., "commitment", "encoding")
* @param value the value of the parameter, which can be any supported data type
*/
void addParam(String key, Object value);

/**
* Retrieves all the optional parameters as a map.
* This map can be used to access the parameters set for a given request.
*
* @return a map containing all the key-value pairs for optional parameters
*/
Map<String, Object> getParams();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lmax.solana4j.client.api;

import java.util.List;

/**
* Represents a transaction on the Solana blockchain.
* This interface provides access to the metadata of the transaction and the core transaction data.
Expand All @@ -15,10 +17,10 @@ public interface Transaction
TransactionMetadata getMeta();

/**
* Returns the core transaction data.
* The transaction data contains the instructions, signatures, and other essential components that define the transaction.
* Returns the transaction data and the encoding used.
* The transaction data contains essential components such as instructions, signatures, and account keys involved in the transaction.
*
* @return the {@link TransactionData} object representing the core transaction data
* @return a list representing the core transaction data and its encoding
*/
TransactionData getTransaction();
List<String> getTransaction();
}
Loading

0 comments on commit 37de451

Please sign in to comment.