Class SorobanServer
- All Implemented Interfaces:
Closeable,AutoCloseable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumRepresents the "durability keyspace" that this ledger key belongs to, checkgetContractData(java.lang.String, org.stellar.sdk.xdr.SCVal, org.stellar.sdk.SorobanServer.Durability)for more details. -
Constructor Summary
ConstructorsConstructorDescriptionSorobanServer(String serverURI) Creates a new SorobanServer instance.SorobanServer(String serverURI, okhttp3.OkHttpClient httpClient) Creates a new SorobanServer instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()getAccount(String accountId) Fetch a minimal set of current info about a Stellar account.getContractData(String contractId, SCVal key, SorobanServer.Durability durability) Reads the current value of contract data ledger entries directly.getEvents(GetEventsRequest getEventsRequest) Fetches all events that match the givenGetEventsRequest.General node health check.Fetches the latest ledger meta info from network which Soroban-RPC is connected to.getLedgerEntries(Collection<LedgerKey> keys) Reads the current value of ledger entries directly.Fetches metadata about the network which Soroban-RPC is connected to.getTransaction(String hash) Fetch the details of a submitted transaction.prepareTransaction(Transaction transaction) Submit a trial contract invocation, first run a simulation of the contract invocation as defined on the incoming transaction, and apply the results to a new copy of the transaction which is then returned.prepareTransaction(Transaction transaction, SimulateTransactionResponse simulateTransactionResponse) Prepare the transaction using the simulateTransaction obtained by the user in advance, apply the simulateTransaction results to a new copy of the transaction which is then returned.sendTransaction(Transaction transaction) Submit a real transaction to the Stellar network.simulateTransaction(Transaction transaction) An alias forsimulateTransaction(Transaction, SimulateTransactionRequest.ResourceConfig)with no resource leeway.simulateTransaction(Transaction transaction, SimulateTransactionRequest.ResourceConfig resourceConfig) Submit a trial contract invocation to get back return values, expected ledger footprint, expected authorizations, and expected costs.
-
Constructor Details
-
SorobanServer
Creates a new SorobanServer instance.- Parameters:
serverURI- The URI of the Soroban-RPC instance to connect to.
-
SorobanServer
Creates a new SorobanServer instance.- Parameters:
serverURI- The URI of the Soroban-RPC instance to connect to.httpClient- TheOkHttpClientinstance to use for requests.
-
-
Method Details
-
getAccount
public TransactionBuilderAccount getAccount(String accountId) throws IOException, AccountNotFoundException, SorobanRpcErrorResponse Fetch a minimal set of current info about a Stellar account. Needed to get the current sequence number for the account, so you can build a successful transaction withTransactionBuilder.- Parameters:
accountId- The public address of the account to load.- Returns:
- An
Accountobject containing the sequence number and current state of the account. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.AccountNotFoundException- If the account does not exist on the network. You may need to fund it first.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.
-
getHealth
General node health check.- Returns:
- A
GetHealthResponseobject containing the health check result. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
getContractData
public Optional<GetLedgerEntriesResponse.LedgerEntryResult> getContractData(String contractId, SCVal key, SorobanServer.Durability durability) throws IOException, SorobanRpcErrorResponse Reads the current value of contract data ledger entries directly.- Parameters:
contractId- The contract ID containing the data to load. Encoded as Stellar Contract Address. e.g. "CCJZ5DGASBWQXR5MPFCJXMBI333XE5U3FSJTNQU7RIKE3P5GN2K2WYD5"key- The key of the contract data to load.durability- The "durability keyspace" that this ledger key belongs to, which is eitherSorobanServer.Durability.TEMPORARYorSorobanServer.Durability.PERSISTENT.- Returns:
- A
GetLedgerEntriesResponse.LedgerEntryResultobject containing the ledger entry result. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.
-
getLedgerEntries
public GetLedgerEntriesResponse getLedgerEntries(Collection<LedgerKey> keys) throws IOException, SorobanRpcErrorResponse Reads the current value of ledger entries directly.Allows you to directly inspect the current state of contracts, contract's code, or any other ledger entries.
- Parameters:
keys- The key of the contract data to load, at least one key must be provided.- Returns:
- A
GetLedgerEntriesResponseobject containing the current values. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
getTransaction
public GetTransactionResponse getTransaction(String hash) throws IOException, SorobanRpcErrorResponse Fetch the details of a submitted transaction.When submitting a transaction, client should poll this to tell when the transaction has completed.
- Parameters:
hash- The hash of the transaction to check. Encoded as a hex string.- Returns:
- A
GetTransactionResponseobject containing the transaction status, result, and other details. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
getEvents
public GetEventsResponse getEvents(GetEventsRequest getEventsRequest) throws IOException, SorobanRpcErrorResponse Fetches all events that match the givenGetEventsRequest.- Parameters:
getEventsRequest- TheGetEventsRequestto use for the request.- Returns:
- A
GetEventsResponseobject containing the events that match the request. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
getNetwork
Fetches metadata about the network which Soroban-RPC is connected to.- Returns:
- A
GetNetworkResponseobject containing the network metadata. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
getLatestLedger
Fetches the latest ledger meta info from network which Soroban-RPC is connected to.- Returns:
- A
GetLatestLedgerResponseobject containing the latest ledger meta info. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
simulateTransaction
public SimulateTransactionResponse simulateTransaction(Transaction transaction, @Nullable SimulateTransactionRequest.ResourceConfig resourceConfig) throws IOException, SorobanRpcErrorResponse Submit a trial contract invocation to get back return values, expected ledger footprint, expected authorizations, and expected costs.- Parameters:
transaction- The transaction to simulate. It should include exactly one operation, which must be one ofInvokeHostFunctionOperation,ExtendFootprintTTLOperation, orRestoreFootprintOperation. Any provided footprint will be ignored.resourceConfig- Additional resource include in the simulation.- Returns:
- A
SimulateTransactionResponseobject containing the cost, footprint, result/auth requirements (if applicable), and error of the transaction. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
simulateTransaction
public SimulateTransactionResponse simulateTransaction(Transaction transaction) throws IOException, SorobanRpcErrorResponse An alias forsimulateTransaction(Transaction, SimulateTransactionRequest.ResourceConfig)with no resource leeway.- Throws:
IOExceptionSorobanRpcErrorResponse
-
prepareTransaction
public Transaction prepareTransaction(Transaction transaction) throws IOException, SorobanRpcErrorResponse, PrepareTransactionException Submit a trial contract invocation, first run a simulation of the contract invocation as defined on the incoming transaction, and apply the results to a new copy of the transaction which is then returned. Setting the ledger footprint and authorization, so the resulting transaction is ready for signing and sending.The returned transaction will also have an updated fee that is the sum of fee set on incoming transaction with the contract resource fees estimated from simulation. It is advisable to check the fee on returned transaction and validate or take appropriate measures for interaction with user to confirm it is acceptable.
You can call the
simulateTransaction(org.stellar.sdk.Transaction, org.stellar.sdk.requests.sorobanrpc.SimulateTransactionRequest.ResourceConfig)method directly first if you want to inspect estimated fees for a given transaction in detail first, if that is of importance.- Parameters:
transaction- The transaction to prepare. It should include exactly one operation, which must be one ofInvokeHostFunctionOperation,ExtendFootprintTTLOperation, orRestoreFootprintOperation. Any provided footprint will be ignored. You can useTransaction.isSorobanTransaction()to check if a transaction is a Soroban transaction. Any provided footprint will be overwritten. However, if your operation has existing auth entries, they will be preferred over ALL auth entries from the simulation. In other words, if you include auth entries, you don't care about the auth returned from the simulation. Other fields (footprint, etc.) will be filled as normal.- Returns:
- Returns a copy of the
Transaction, with the expected authorizations (in the case of invocation) and ledger footprint added. The transaction fee will also automatically be padded with the contract's minimum resource fees discovered from the simulation. - Throws:
PrepareTransactionException- If preparing the transaction fails.IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.
-
prepareTransaction
public Transaction prepareTransaction(Transaction transaction, SimulateTransactionResponse simulateTransactionResponse) throws PrepareTransactionException Prepare the transaction using the simulateTransaction obtained by the user in advance, apply the simulateTransaction results to a new copy of the transaction which is then returned. Setting the ledger footprint and authorization, so the resulting transaction is ready for signing and sending.The returned transaction will also have an updated fee that is the sum of fee set on incoming transaction with the contract resource fees estimated from simulation. It is advisable to check the fee on returned transaction and validate or take appropriate measures for interaction with user to confirm it is acceptable.
- Parameters:
transaction- The transaction to prepare. It should include exactly one operation, which must be one ofInvokeHostFunctionOperation,ExtendFootprintTTLOperation, orRestoreFootprintOperation. Any provided footprint will be ignored. You can useTransaction.isSorobanTransaction()to check if a transaction is a Soroban transaction. Any provided footprint will be overwritten. However, if your operation has existing auth entries, they will be preferred over ALL auth entries from the simulation. In other words, if you include auth entries, you don't care about the auth returned from the simulation. Other fields (footprint, etc.) will be filled as normal.simulateTransactionResponse- TheSimulateTransactionResponseto use for preparing the transaction.- Returns:
- Returns a copy of the
Transaction, with the expected authorizations (in the case of invocation) and ledger footprint added. The transaction fee will also automatically be padded with the contract's minimum resource fees discovered from the simulation. - Throws:
PrepareTransactionException- If preparing the transaction fails.
-
sendTransaction
public SendTransactionResponse sendTransaction(Transaction transaction) throws IOException, SorobanRpcErrorResponse Submit a real transaction to the Stellar network. This is the only way to make changes "on-chain". Unlike Horizon, Soroban-RPC does not wait for transaction completion. It simply validates the transaction and enqueues it. Clients should callgetTransaction(java.lang.String)to learn about transaction's status.- Parameters:
transaction- The transaction to submit.- Returns:
- A
SendTransactionResponseobject containing some details about the transaction that was submitted. - Throws:
IOException- If the request could not be executed due to cancellation, a connectivity problem or timeout. Because networks can fail during an exchange, it is possible that the remote server accepted the request before the failure.SorobanRpcErrorResponse- If the Soroban-RPC instance returns an error response.- See Also:
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-