From 057161e967779544365b3ae58e58a76686d8ed0e Mon Sep 17 00:00:00 2001 From: Johnpaul Date: Fri, 21 Jun 2024 00:26:10 +0100 Subject: [PATCH 1/2] add sdk examples to send transaction --- .../api-reference/methods/sendTransaction.mdx | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/docs/data/rpc/api-reference/methods/sendTransaction.mdx b/docs/data/rpc/api-reference/methods/sendTransaction.mdx index 52440c7e49..8391e7b000 100644 --- a/docs/data/rpc/api-reference/methods/sendTransaction.mdx +++ b/docs/data/rpc/api-reference/methods/sendTransaction.mdx @@ -4,5 +4,143 @@ 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( + "SA6XHAH4GNLRWWWF6TEVEWNS44CBNFAJWHWOPZCVZOUXSQA7BOYN7XHC" + ) + # Create an Account object from an address and sequence number. + root_account = Account(account=root_keypair.public_key, sequence=1) + + transaction = ( + TransactionBuilder( + source_account=root_account, + # If you want to submit to pubnet, you need to change `network_passphrase` to `Network.PUBLIC_NETWORK_PASSPHRASE` + network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, + base_fee=100, + ) + .append_payment_op( # add a payment operation to the transaction + destination="GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW", + asset=Asset.native(), + amount="125.5", + ) + .append_set_options_op( # add a set options operation to the transaction + home_domain="overcat.me" + ) + .set_timeout(30) + .build() + ) # mark this transaction as valid only for the next 30 seconds + response = server.send_transaction(transaction) + return response + +response = send_transaction() + +print("response", response) +``` + +```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 contract = new StellarSdk.Contract(contractId); + + const fee = StellarSdk.BASE_FEE; + const transaction = new StellarSdk.TransactionBuilder(account, { fee }) + //.setNetworkPassphrase(StellarSdk.Networks.PUBLIC) + .setNetworkPassphrase(StellarSdk.Networks.FUTURENET) + .setTimeout(30) // valid for the next 30s + // Add an operation to call increment() on the contract + .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.Account; +import org.stellar.sdk.AccountConverter; +import org.stellar.sdk.CreateAccountOperation; +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.TransactionPreconditions; +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 { + KeyPair sourceKeyPair = KeyPair.fromSecretSeed("SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); + long sequenceNumber = 2908908335136768L; + Account account + = new Account("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR", sequenceNumber); + + // Build the transaction + Transaction transaction = new TransactionBuilder(AccountConverter.enableMuxed(), account, Network.TESTNET) + .addOperation( + new CreateAccountOperation.Builder( + "GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR", "2000") + .build()) + .setTimeout(TransactionPreconditions.TIMEOUT_INFINITE) + .build(); + + // Sign the transaction + transaction.sign(sourceKeyPair); + + // Send the transaction using the SorobanServer + SendTransactionResponse response = server.sendTransaction(transaction); + // getter functions for the response object are available in java + 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(); + } + } +} +``` + + +
From e286611dc727db01fb284075b170f23c37be1992 Mon Sep 17 00:00:00 2001 From: Johnpaul Date: Mon, 24 Jun 2024 19:40:06 +0100 Subject: [PATCH 2/2] fix issues with doc code samples --- .../api-reference/methods/sendTransaction.mdx | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/docs/data/rpc/api-reference/methods/sendTransaction.mdx b/docs/data/rpc/api-reference/methods/sendTransaction.mdx index 8391e7b000..e413de5166 100644 --- a/docs/data/rpc/api-reference/methods/sendTransaction.mdx +++ b/docs/data/rpc/api-reference/methods/sendTransaction.mdx @@ -12,6 +12,7 @@ import { CodeExample } from "@site/src/components/CodeExample"; 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. +
@@ -23,35 +24,31 @@ def send_transaction() -> soroban_rpc.SendTransactionResponse: server = SorobanServer(server_url='https://soroban-testnet.stellar.org', client=None) root_keypair = Keypair.from_secret( - "SA6XHAH4GNLRWWWF6TEVEWNS44CBNFAJWHWOPZCVZOUXSQA7BOYN7XHC" + "SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) - # Create an Account object from an address and sequence number. - root_account = Account(account=root_keypair.public_key, sequence=1) - + root_account = server.load_account("GBSBL6FBPX5UHKL4AZCPUU6PXKUBYMKRUN3L4YQ4V2CCWSE7YMN2HYPB") + contract_id = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE" transaction = ( TransactionBuilder( source_account=root_account, - # If you want to submit to pubnet, you need to change `network_passphrase` to `Network.PUBLIC_NETWORK_PASSPHRASE` network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE, base_fee=100, ) - .append_payment_op( # add a payment operation to the transaction - destination="GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW", - asset=Asset.native(), - amount="125.5", - ) - .append_set_options_op( # add a set options operation to the transaction - home_domain="overcat.me" - ) + .append_invoke_contract_function_op(contract_id,"increment") + # mark this transaction as valid only for the next 30 seconds .set_timeout(30) .build() - ) # mark this transaction as valid only for the next 30 seconds + ) + transaction.sign(root_keypair) response = server.send_transaction(transaction) return response response = send_transaction() -print("response", response) +print("status", response.status) +print("hash:", response.hash); +print("status:", response.status); +print("errorResultXdr:", response.error_result_xdr); ``` ```js @@ -65,14 +62,16 @@ 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.PUBLIC) - .setNetworkPassphrase(StellarSdk.Networks.FUTURENET) - .setTimeout(30) // valid for the next 30s - // Add an operation to call increment() on the contract + .setNetworkPassphrase(StellarSdk.Networks.TESTNET) + .setTimeout(30) .addOperation(contract.call("increment")) .build(); @@ -94,15 +93,14 @@ sendTransaction(); ```java // implementation 'network.lightsail:stellar-sdk:0.44.0' -import org.stellar.sdk.Account; import org.stellar.sdk.AccountConverter; -import org.stellar.sdk.CreateAccountOperation; +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.TransactionPreconditions; +import org.stellar.sdk.TransactionBuilderAccount; import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse; public class SendTransactionExample { @@ -110,18 +108,13 @@ 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"); - long sequenceNumber = 2908908335136768L; - Account account - = new Account("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR", sequenceNumber); - + String contractId = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE"; + InvokeHostFunctionOperation operation = InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(contractId, "increment", null).build(); // Build the transaction Transaction transaction = new TransactionBuilder(AccountConverter.enableMuxed(), account, Network.TESTNET) - .addOperation( - new CreateAccountOperation.Builder( - "GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR", "2000") - .build()) - .setTimeout(TransactionPreconditions.TIMEOUT_INFINITE) + .addOperation(operation) .build(); // Sign the transaction @@ -129,7 +122,6 @@ public class SendTransactionExample { // Send the transaction using the SorobanServer SendTransactionResponse response = server.sendTransaction(transaction); - // getter functions for the response object are available in java System.out.println(response.getStatus()); System.out.println(response.getHash()); System.out.println(response.getLatestLedger()); @@ -140,6 +132,7 @@ public class SendTransactionExample { } } } + ```