Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for calling Soroban RPC server. #492

Merged
merged 16 commits into from
Aug 2, 2023
9 changes: 9 additions & 0 deletions src/main/java/org/stellar/sdk/AbstractTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public Network getNetwork() {
return mNetwork;
}

/**
* Gets the {@link AccountConverter} for this transaction.
*
* @return the {@link AccountConverter} object
*/
public AccountConverter getAccountConverter() {
return accountConverter;
}

/**
* Gets read only list(immutable) of the signatures on transaction.
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/stellar/sdk/AccountNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.stellar.sdk;

import lombok.Getter;

/** Exception thrown when trying to load an account that doesn't exist on the Stellar network. */
@Getter
public class AccountNotFoundException extends Exception {
// The account that was not found.
private final String accountId;

public AccountNotFoundException(String accountId) {
super("Account not found, accountId: " + accountId);
this.accountId = accountId;
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/stellar/sdk/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Network {
public static final Network PUBLIC =
new Network("Public Global Stellar Network ; September 2015");
public static final Network TESTNET = new Network("Test SDF Network ; September 2015");
public static final Network FUTURENET = new Network("Test SDF Future Network ; October 2022");
public static final Network STANDALONE = new Network("Standalone Network ; February 2017");
public static final Network SANDBOX =
new Network("Local Sandbox Stellar Network ; September 2022");

private final String networkPassphrase;

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/stellar/sdk/PrepareTransactionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.stellar.sdk;

import lombok.Getter;
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;

/** Exception thrown when preparing a transaction failed. */
@Getter
public class PrepareTransactionException extends Exception {
// The response returned by the Soroban-RPC instance when simulating the transaction.
private final SimulateTransactionResponse simulateTransactionResponse;

public PrepareTransactionException(
String message, SimulateTransactionResponse simulateTransactionResponse) {
super(message);
this.simulateTransactionResponse = simulateTransactionResponse;
}
}
526 changes: 526 additions & 0 deletions src/main/java/org/stellar/sdk/SorobanServer.java

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/main/java/org/stellar/sdk/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,20 @@ public Builder(TransactionBuilderAccount sourceAccount, Network network) {
super(sourceAccount, network);
}
}

/**
* Returns true if this transaction is a Soroban transaction.
*
* @return true if this transaction is a Soroban transaction.
*/
public boolean isSorobanTransaction() {
if (mOperations.length != 1) {
return false;
}

Operation op = mOperations[0];
return op instanceof InvokeHostFunctionOperation
|| op instanceof BumpSequenceOperation
|| op instanceof RestoreFootprintOperation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.stellar.sdk.requests.sorobanrpc;

import com.google.gson.annotations.SerializedName;

/** Represents the type of event. */
public enum EventFilterType {
@SerializedName("system")
SYSTEM,
@SerializedName("contract")
CONTRACT,
@SerializedName("diagnostic")
DIAGNOSTIC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.stellar.sdk.requests.sorobanrpc;

import java.util.Collection;
import lombok.Builder;
import lombok.NonNull;
import lombok.Singular;
import lombok.Value;

/**
* Request for JSON-RPC method getEvents.
*
* @see <a href="https://soroban.stellar.org/api/methods/getEvents#parameters"
* target="_blank">getEvents documentation</a>
*/
@Value
@Builder(toBuilder = true)
public class GetEventsRequest {
@NonNull String startLedger;

@Singular("filter")
Collection<EventFilter> filters;

PaginationOptions pagination;

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

String cursor;
}

@Builder(toBuilder = true)
@Value
public static class EventFilter {
EventFilterType type;

Collection<String> contractIds;

@Singular("topic")
Collection<Collection<String>> topics;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.stellar.sdk.requests.sorobanrpc;

import java.util.Collection;
import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Request for JSON-RPC method getLedgerEntries.
*
* @see <a href="https://soroban.stellar.org/api/methods/getLedgerEntries#parameters"
* target="_blank">getLedgerEntries documentation</a>
*/
@AllArgsConstructor
@Value
public class GetLedgerEntriesRequest {
Collection<String> keys;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.stellar.sdk.requests.sorobanrpc;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Request for JSON-RPC method getTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/getTransaction#parameters"
* target="_blank">getTransaction documentation</a>
*/
@Value
@AllArgsConstructor
public class GetTransactionRequest {
String hash;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.stellar.sdk.requests.sorobanrpc;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Request for JSON-RPC method sendTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/sendTransaction#parameters"
* target="_blank">sendTransaction documentation</a>
*/
@AllArgsConstructor
@Value
public class SendTransactionRequest {
String transaction;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.stellar.sdk.requests.sorobanrpc;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Request for JSON-RPC method simulateTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/simulateTransaction#parameters"
* target="_blank">simulateTransaction documentation</a>
*/
@AllArgsConstructor
@Value
public class SimulateTransactionRequest {
String transaction;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.stellar.sdk.requests.sorobanrpc;

import lombok.Getter;

/**
* Throws when Soroban-RPC instance responds with error.
*
* @see <a href="https://www.jsonrpc.org/specification#error_object" target="_blank">JSON-RPC 2.0
* Specification - Error object<a>
*/
@Getter
public class SorobanRpcErrorResponse extends Exception {
private final Integer code;

private final String message;

private final String data;

public SorobanRpcErrorResponse(Integer code, String message, String data) {
super(message);
this.code = code;
this.message = message;
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.stellar.sdk.requests.sorobanrpc;

import com.google.gson.annotations.SerializedName;
import lombok.RequiredArgsConstructor;
import lombok.Value;

/**
* Represent the request sent to Soroban-RPC.
*
* @see <a href="https://www.jsonrpc.org/specification#request_object" target="_blank">JSON-RPC 2.0
* Specification - Request object<a>
*/
@RequiredArgsConstructor
@Value
public class SorobanRpcRequest<T> {
overcat marked this conversation as resolved.
Show resolved Hide resolved
@SerializedName("jsonrpc")
String jsonRpc = "2.0";

String id;

String method;

T params;
}
2 changes: 2 additions & 0 deletions src/main/java/org/stellar/sdk/responses/GsonSingleton.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.stellar.sdk.responses;

import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static Gson getInstance() {
.registerTypeAdapter(
claimableBalancePageType.getType(),
new PageDeserializer<ClaimableBalanceResponse>(claimableBalancePageType))
.registerTypeAdapter(ImmutableList.class, new ImmutableListDeserializer())
.create();
}
return instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.stellar.sdk.responses.sorobanrpc;

import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import lombok.Value;
import org.stellar.sdk.requests.sorobanrpc.EventFilterType;

/**
* Response for JSON-RPC method getEvents.
*
* @see <a href="https://soroban.stellar.org/api/methods/getEvents#returns"
* target="_blank">getEvents documentation</a>
*/
@AllArgsConstructor
@Value
public class GetEventsResponse {
ImmutableList<EventInfo> events;

Long latestLedger;

@AllArgsConstructor
@Value
public static class EventInfo {
EventFilterType type;

Integer ledger;

String ledgerClosedAt;

String contractId;

String id;

String pagingToken;

ImmutableList<String> topic;

EventInfoValue value;

Boolean inSuccessfulContractCall;
}

@AllArgsConstructor
@Value
public static class EventInfoValue {
String xdr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.stellar.sdk.responses.sorobanrpc;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Response for JSON-RPC method getHealth.
*
* @see <a href="https://soroban.stellar.org/api/methods/getHealth#returns"
* target="_blank">getHealth documentation</a>
*/
@AllArgsConstructor
@Value
public class GetHealthResponse {
String status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.stellar.sdk.responses.sorobanrpc;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Response for JSON-RPC method getLatestLedger.
*
* @see <a href="https://soroban.stellar.org/api/methods/getLatestLedger#returns"
* target="_blank">getLatestLedger documentation</a>
*/
@AllArgsConstructor
@Value
public class GetLatestLedgerResponse {
String id;

Integer protocolVersion;

Integer sequence;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.stellar.sdk.responses.sorobanrpc;

import com.google.common.collect.ImmutableList;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Response for JSON-RPC method getLedgerEntries.
*
* @see <a href="https://soroban.stellar.org/api/methods/getLedgerEntries#returns"
* target="_blank">getLedgerEntries documentation</a>
*/
@AllArgsConstructor
@Value
public class GetLedgerEntriesResponse {
ImmutableList<LedgerEntryResult> entries;

Long latestLedger;

@AllArgsConstructor
@Value
public static class LedgerEntryResult {
String key;

String xdr;

@SerializedName("lastModifiedLedgerSeq")
Long lastModifiedLedger;
}
}
Loading
Loading