diff --git a/client/src/main/java/com/lmax/solana4j/client/api/SolanaApi.java b/client/src/main/java/com/lmax/solana4j/client/api/SolanaApi.java index b1659ca8..69c7991e 100644 --- a/client/src/main/java/com/lmax/solana4j/client/api/SolanaApi.java +++ b/client/src/main/java/com/lmax/solana4j/client/api/SolanaApi.java @@ -103,4 +103,6 @@ public interface SolanaApi * @throws SolanaJsonRpcClientException if there is an error with the JSON-RPC request */ SolanaClientResponse getMinimumBalanceForRentExemption(int size) throws SolanaJsonRpcClientException; + +// SolanaClientResponse simulateTransaction(String transaction) throws SolanaJsonRpcClientException; } diff --git a/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClient.java b/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClient.java index 134bcb6b..d64d2b2e 100644 --- a/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClient.java +++ b/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClient.java @@ -20,6 +20,7 @@ import java.time.Duration; import java.util.Locale; import java.util.Map; +import java.util.function.Function; public class SolanaJsonRpcClient implements SolanaApi { @@ -50,179 +51,153 @@ public SolanaJsonRpcClient(final String rpcUrl, final Duration connectionTimeout @Override public SolanaClientResponse requestAirdrop(final String address, final long amountLamports) throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, "requestAirdrop", address, amountLamports); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess()); + return queryForObject( + new TypeReference>() + { + }, + "requestAirdrop", + dto -> dto, + address, + amountLamports); } @Override public SolanaClientResponse getTransaction(final String transactionSignature) throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, + return queryForObject(new TypeReference>() + { + }, "getTransaction", + dto -> dto, transactionSignature, Map.of( "econding", "jsonParsed", "maxSupportedTransactionVersion", 0 ) ); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess()); } @Override public SolanaClientResponse sendTransaction(final String transactionBlob) throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, + return queryForObject(new TypeReference>() + { + }, "sendTransaction", + dto -> dto, transactionBlob, Map.of("preflightCommitment", Commitment.FINALIZED.toString().toLowerCase(Locale.UK)) ); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess()); } @Override public SolanaClientResponse getBalance(final String address) throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, + return queryForObject(new TypeReference<>() + { + }, "getBalance", + BalanceDTO::getValue, address); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess().getValue()); } @Override public SolanaClientResponse getTokenAccountBalance(final String address) throws SolanaJsonRpcClientException { - final var result = queryForObject( - new TypeReference>() + return queryForObject( + new TypeReference<>() { }, "getTokenAccountBalance", + TokenAmountDTO::getValue, address); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess().getValue()); } @Override public SolanaClientResponse getAccountInfo(final String address) throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, + return queryForObject(new TypeReference>() + { + }, "getAccountInfo", + AccountInfoDTO::getValue, address, Map.of("encoding", "base64") ); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess().getValue()); } @Override public SolanaClientResponse getBlockHeight() throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, "getBlockHeight"); + return queryForObject(new TypeReference>() + { + }, + "getBlockHeight", + dto -> dto); - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess()); } @Override public SolanaClientResponse getSlot() throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, "getSlot"); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess()); + return queryForObject(new TypeReference>() + { + }, + "getSlot", + dto -> dto); } @Override public SolanaClientResponse getLatestBlockhash() throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, "getLatestBlockhash"); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess().getValue()); + return queryForObject(new TypeReference>() + { + }, + "getLatestBlockhash", + BlockhashDTO::getValue); } @Override public SolanaClientResponse getMinimumBalanceForRentExemption(final int size) throws SolanaJsonRpcClientException { - final var result = queryForObject(new TypeReference>() - { - }, "getMinimumBalanceForRentExemption", size); - - if (result.isError()) - { - return new SolanaJsonRpcClientResponse<>(result.getError().getErrorCode()); - } - - return new SolanaJsonRpcClientResponse<>(result.getSuccess()); + return queryForObject( + new TypeReference>() + { + }, + "getMinimumBalanceForRentExemption", + dto -> dto, + size); } - private Result queryForObject(final TypeReference> type, final String method, final Object... params) throws SolanaJsonRpcClientException +// @Override +// public SolanaClientResponse simulateTransaction(final String transaction) throws SolanaJsonRpcClientException +// { +// return queryForObject( +// new TypeReference>() +// { +// }, +// "simulateTransaction", +// SimulateTransactionResponseDTO::getValue, +// transaction, +// Map.of("encoding", "base64", "replaceRecentBlockhash", true)); +// } + + private SolanaClientResponse queryForObject( + final TypeReference> type, + final String method, + final Function dtoMapper, + final Object... params) throws SolanaJsonRpcClientException { final HttpRequest request = prepareRequest(method, params); final HttpResponse httpResponse = sendRequest(request); - return decodeResponse(type, httpResponse); + final Result response = decodeResponse(type, httpResponse); + if (response.isError()) + { + return SolanaJsonRpcClientResponse.creatErrorResponse(response.getError()); + } + + return SolanaJsonRpcClientResponse.createSuccessResponse(dtoMapper.apply(response.getSuccess())); } private HttpRequest prepareRequest(final String method, final Object[] params) throws SolanaJsonRpcClientException diff --git a/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClientResponse.java b/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClientResponse.java index 5154a8cf..ea42ac87 100644 --- a/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClientResponse.java +++ b/client/src/main/java/com/lmax/solana4j/client/jsonrpc/SolanaJsonRpcClientResponse.java @@ -3,21 +3,25 @@ import com.lmax.solana4j.client.api.SolanaClientError; import com.lmax.solana4j.client.api.SolanaClientResponse; -class SolanaJsonRpcClientResponse implements SolanaClientResponse +final class SolanaJsonRpcClientResponse implements SolanaClientResponse { private final T response; private final SolanaClientError error; - SolanaJsonRpcClientResponse(final T response) + private SolanaJsonRpcClientResponse(final T response, final SolanaClientError error) { this.response = response; - this.error = null; + this.error = error; } - SolanaJsonRpcClientResponse(final SolanaClientError error) + static SolanaClientResponse createSuccessResponse(final T response) { - this.response = null; - this.error = error; + return new SolanaJsonRpcClientResponse<>(response, null); + } + + static SolanaClientResponse creatErrorResponse(final SolanaClientError error) + { + return new SolanaJsonRpcClientResponse<>(null, error); } @Override diff --git a/client/src/main/java/com/lmax/solana4j/client/jsonrpc/TransactionDetailLevel.java b/client/src/main/java/com/lmax/solana4j/client/jsonrpc/TransactionDetailLevel.java new file mode 100644 index 00000000..18f89cdf --- /dev/null +++ b/client/src/main/java/com/lmax/solana4j/client/jsonrpc/TransactionDetailLevel.java @@ -0,0 +1,9 @@ +package com.lmax.solana4j.client.jsonrpc; + +enum TransactionDetailLevel +{ + FULL, + ACCOUNTS, + SIGNATURES, + NONE +} \ No newline at end of file diff --git a/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java b/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java index 7c2a4a5a..7eef2592 100644 --- a/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java +++ b/message-encoding/src/test-support/java/com/lmax/solana4j/SolanaNodeDsl.java @@ -723,7 +723,7 @@ public void setComputeUnits(final String... args) else { assertThat(solanaClientResponse.isSuccess()).withFailMessage( - "We expected a succes but received a an error from the client." + "We expected a success but received a an error from the client." ).isTrue(); Waiter.waitFor(Condition.isNotNull(() -> solanaDriver.getTransactionResponse(solanaClientResponse.getResponse())));