diff --git a/docs/data/rpc/api-reference/methods/sendTransaction.mdx b/docs/data/rpc/api-reference/methods/sendTransaction.mdx index 52440c7e49..e413de5166 100644 --- a/docs/data/rpc/api-reference/methods/sendTransaction.mdx +++ b/docs/data/rpc/api-reference/methods/sendTransaction.mdx @@ -4,5 +4,136 @@ description: Submits a transaction --- import { RpcMethod } from "@site/src/components/RpcMethod"; +import { CodeExample } from "@site/src/components/CodeExample"; + +### SDK Guide + +The example above is sending a transaction using RPC methods directly. If you are using the Stellar SDK to build applications, you can use the native functions to get the same information. + +
+
+ + +```python +# pip install --upgrade stellar-sdk +from stellar_sdk import SorobanServer, soroban_rpc, Account, Asset, Keypair, Network, TransactionBuilder + +def send_transaction() -> soroban_rpc.SendTransactionResponse: + server = SorobanServer(server_url='https://soroban-testnet.stellar.org', client=None) + + root_keypair = Keypair.from_secret( + "SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + ) + root_account = server.load_account("GBSBL6FBPX5UHKL4AZCPUU6PXKUBYMKRUN3L4YQ4V2CCWSE7YMN2HYPB") + contract_id = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE" + transaction = ( + TransactionBuilder( + source_account=root_account, + network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, + base_fee=100, + ) + .append_invoke_contract_function_op(contract_id,"increment") + # mark this transaction as valid only for the next 30 seconds + .set_timeout(30) + .build() + ) + transaction.sign(root_keypair) + response = server.send_transaction(transaction) + return response + +response = send_transaction() + +print("status", response.status) +print("hash:", response.hash); +print("status:", response.status); +print("errorResultXdr:", response.error_result_xdr); +``` + +```js +// yarn add @stellar/stellar-sdk +import * as StellarSdk from "@stellar/stellar-sdk"; + +import { Server } from "@stellar/stellar-sdk/rpc"; +const server = new Server("https://soroban-testnet.stellar.org"); + +async function sendTransaction() { + try { + const contractId = + "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE"; + const sourceSecretKey = + "SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; + const contract = new StellarSdk.Contract(contractId); + const accountId = + "GBSBL6FBPX5UHKL4AZCPUU6PXKUBYMKRUN3L4YQ4V2CCWSE7YMN2HYPB"; + const account = await server.getAccount(accountId); + const fee = StellarSdk.BASE_FEE; + const transaction = new StellarSdk.TransactionBuilder(account, { fee }) + .setNetworkPassphrase(StellarSdk.Networks.TESTNET) + .setTimeout(30) + .addOperation(contract.call("increment")) + .build(); + + const sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecretKey); + transaction.sign(sourceKeypair); + + server.sendTransaction(transaction).then((result) => { + console.log("hash:", result.hash); + console.log("status:", result.status); + console.log("errorResultXdr:", result.errorResultXdr); + }); + } catch (error) { + console.error("Error fetching transaction:", error); + } +} + +sendTransaction(); +``` + +```java +// implementation 'network.lightsail:stellar-sdk:0.44.0' +import org.stellar.sdk.AccountConverter; +import org.stellar.sdk.InvokeHostFunctionOperation; +import org.stellar.sdk.KeyPair; +import org.stellar.sdk.Network; +import org.stellar.sdk.SorobanServer; +import org.stellar.sdk.Transaction; +import org.stellar.sdk.TransactionBuilder; +import org.stellar.sdk.TransactionBuilderAccount; +import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse; + +public class SendTransactionExample { + + public static void main(String[] args) { + SorobanServer server = new SorobanServer("https://soroban-testnet.stellar.org"); + try { + TransactionBuilderAccount account = server.getAccount("GBSBL6FBPX5UHKL4AZCPUU6PXKUBYMKRUN3L4YQ4V2CCWSE7YMN2HYPB"); + KeyPair sourceKeyPair = KeyPair.fromSecretSeed("SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); + String contractId = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE"; + InvokeHostFunctionOperation operation = InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(contractId, "increment", null).build(); + // Build the transaction + Transaction transaction = new TransactionBuilder(AccountConverter.enableMuxed(), account, Network.TESTNET) + .addOperation(operation) + .build(); + + // Sign the transaction + transaction.sign(sourceKeyPair); + + // Send the transaction using the SorobanServer + SendTransactionResponse response = server.sendTransaction(transaction); + System.out.println(response.getStatus()); + System.out.println(response.getHash()); + System.out.println(response.getLatestLedger()); + System.out.println(response.getLatestLedgerCloseTime()); + } catch (Exception e) { + System.err.println("An error has occurred:"); + e.printStackTrace(); + } + } +} + +``` + + +