Skip to content

Commit

Permalink
feat: add support for Soroban PRC's getTransactions API.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Jul 25, 2024
1 parent ef4cb68 commit 42849d2
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/main/java/org/stellar/sdk/SorobanServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.stellar.sdk.requests.sorobanrpc.GetEventsRequest;
import org.stellar.sdk.requests.sorobanrpc.GetLedgerEntriesRequest;
import org.stellar.sdk.requests.sorobanrpc.GetTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.GetTransactionsRequest;
import org.stellar.sdk.requests.sorobanrpc.SendTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.SimulateTransactionRequest;
import org.stellar.sdk.requests.sorobanrpc.SorobanRpcRequest;
Expand All @@ -41,6 +42,7 @@
import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse;
import org.stellar.sdk.responses.sorobanrpc.GetNetworkResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionsResponse;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SorobanRpcResponse;
Expand Down Expand Up @@ -160,8 +162,8 @@ public GetHealthResponse getHealth() {
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getHealth" target="_blank">getHealth
* documentation</a>
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getFeeStats"
* target="_blank">getFeeStats documentation</a>
*/
public GetFeeStatsResponse getFeeStats() {
return this.<Void, GetFeeStatsResponse>sendRequest(
Expand Down Expand Up @@ -287,6 +289,31 @@ public GetTransactionResponse getTransaction(String hash) {
"getTransaction", params, new TypeToken<SorobanRpcResponse<GetTransactionResponse>>() {});
}

/**
* Gets a detailed list of transactions starting from the user specified starting point that you
* can paginate as long as the pages fall within the history retention of their corresponding RPC
* provider.
*
* @param getTransactionsRequest The {@link GetEventsRequest} to use for the request.
* @return A {@link GetTransactionsResponse} object containing the transactions that match the
* request.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws SorobanRpcException If the Soroban-RPC instance returns an error response.
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransactions"
* target="_blank">getTransactions documentation</a>
*/
public GetTransactionsResponse getTransactions(GetTransactionsRequest getTransactionsRequest) {
return this.sendRequest(
"getTransactions",
getTransactionsRequest,
new TypeToken<SorobanRpcResponse<GetTransactionsResponse>>() {});
}

/**
* Fetches all events that match the given {@link GetEventsRequest}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.stellar.sdk.requests.sorobanrpc;

import lombok.Builder;
import lombok.NonNull;
import lombok.Value;

/**
* Request for JSON-RPC method getTransactions.
*
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransactions"
* target="_blank">getEvents documentation</a>
*/
@Value
@Builder(toBuilder = true)
public class GetTransactionsRequest {
@NonNull Long startLedger;

PaginationOptions pagination;

@Value
@Builder(toBuilder = true)
public static class PaginationOptions {
Long limit;

String cursor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.stellar.sdk.responses.sorobanrpc;

import java.util.List;
import java.util.stream.Collectors;
import lombok.Value;
import org.stellar.sdk.Util;
import org.stellar.sdk.xdr.DiagnosticEvent;
import org.stellar.sdk.xdr.TransactionEnvelope;
import org.stellar.sdk.xdr.TransactionMeta;
import org.stellar.sdk.xdr.TransactionResult;

/**
* Response for JSON-RPC method getTransactions.
*
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransactions"
* target="_blank">getTransactions documentation</a>
*/
@Value
public class GetTransactionsResponse {
List<Transaction> transactions;
Long latestLedger;
Long latestLedgerCloseTimestamp;
Long oldestLedger;
Long oldestLedgerCloseTimestamp;
String cursor;

@Value
public static class Transaction {
TransactionStatus status;
Integer applicationOrder;
Boolean feeBump;

/** The field can be parsed as {@link org.stellar.sdk.xdr.TransactionEnvelope} object. */
String envelopeXdr;

/** The field can be parsed as {@link org.stellar.sdk.xdr.TransactionResult} object. */
String resultXdr;

/** The field can be parsed as {@link org.stellar.sdk.xdr.TransactionMeta} object. */
String resultMetaXdr;

Long ledger;
Long createdAt;
List<String> diagnosticEventsXdr;

/**
* Parses the {@code envelopeXdr} field from a string to an {@link
* org.stellar.sdk.xdr.TransactionEnvelope} object.
*
* @return the parsed {@link org.stellar.sdk.xdr.TransactionEnvelope} object
*/
public TransactionEnvelope parseEnvelopeXdr() {
return Util.parseXdr(envelopeXdr, TransactionEnvelope::fromXdrBase64);
}

/**
* Parses the {@code resultXdr} field from a string to an {@link
* org.stellar.sdk.xdr.TransactionResult} object.
*
* @return the parsed {@link org.stellar.sdk.xdr.TransactionResult} object
*/
public TransactionResult parseResultXdr() {
return Util.parseXdr(resultXdr, TransactionResult::fromXdrBase64);
}

/**
* Parses the {@code resultMetaXdr} field from a string to an {@link
* org.stellar.sdk.xdr.TransactionMeta} object.
*
* @return the parsed {@link org.stellar.sdk.xdr.TransactionMeta} object
*/
public TransactionMeta parseResultMetaXdr() {
return Util.parseXdr(resultMetaXdr, TransactionMeta::fromXdrBase64);
}

/**
* Parses the {@code diagnosticEventsXdr} field from a list of strings to a list of {@link
* org.stellar.sdk.xdr.DiagnosticEvent} objects.
*
* @return a list of parsed {@link org.stellar.sdk.xdr.DiagnosticEvent} objects
*/
public List<DiagnosticEvent> parseDiagnosticEventsXdr() {
if (diagnosticEventsXdr == null) {
return null;
}
return diagnosticEventsXdr.stream()
.map(xdr -> Util.parseXdr(xdr, DiagnosticEvent::fromXdrBase64))
.collect(Collectors.toList());
}
}

public enum TransactionStatus {
SUCCESS,
FAILED
}
}
Loading

0 comments on commit 42849d2

Please sign in to comment.