sendTransaction
Submit a real transaction to the Stellar network. This is the only way to make changes on-chain.
Unlike Horizon, this does not wait for transaction completion. It simply validates and enqueues the transaction. Clients should call getTransaction
to learn about transaction success/failure.
This supports all transactions, not only smart contract-related transactions.
Params
(1)Please note that parameter structure within the request must contain named parameters as a by-name object, and not as positional arguments in a by-position array
1. transaction (required)
The signed transaction to broadcast for inclusion in a ledger.
A Stellar TransactionEnvelope (as a base64-encoded string)
Result
(sendTransactionResult)Transaction status and network state. The result will include if the transaction was successfully enqueued, and information about the current ledger.
Transaction hash (as a hex-encoded string)
The current status of the transaction by hash.
The sequence number of the latest ledger known to Stellar RPC at the time it handled the request.
The unix timestamp of the close time of the latest ledger known to Stellar RPC at the time it handled the request.
(optional) If the transaction status is ERROR
, this will be a base64 encoded string of the raw TransactionResult XDR struct containing details on why stellar-core rejected the transaction.
(optional) If the transaction status is ERROR
, this field may be present with an array of base64 encoded strings. Each string will decode to a raw DiagnosticEvent XDR struct containing details on why stellar-core rejected the transaction.
Examples
Submitting a valid transaction using the sendTransaction
method, resulting in a PENDING
status.
Request
- cURL
- JavaScript
- Python
- JSON
curl -X POST \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"id": 8675309,
"method": "sendTransaction",
"params": {
"transaction": "AAAAAgAAAAAg4dbAxsGAGICfBG3iT2cKGYQ6hK4sJWzZ6or1C5v6GAAAAGQAJsOiAAAADQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAACgAAAAVIZWxsbwAAAAAAAAEAAAAMU29yb2JhbiBEb2NzAAAAAAAAAAELm/oYAAAAQATr6Ghp/DNO7S6JjEFwcJ9a+dvI6NJr7I/2eQttvoovjQ8te4zKKaapC3mbmx6ld6YKL5T81mxs45TjzdG5zw0="
}
}' \
https://soroban-testnet.stellar.org | jq
let requestBody = {
"jsonrpc": "2.0",
"id": 8675309,
"method": "sendTransaction",
"params": {
"transaction": "AAAAAgAAAAAg4dbAxsGAGICfBG3iT2cKGYQ6hK4sJWzZ6or1C5v6GAAAAGQAJsOiAAAADQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAACgAAAAVIZWxsbwAAAAAAAAEAAAAMU29yb2JhbiBEb2NzAAAAAAAAAAELm/oYAAAAQATr6Ghp/DNO7S6JjEFwcJ9a+dvI6NJr7I/2eQttvoovjQ8te4zKKaapC3mbmx6ld6YKL5T81mxs45TjzdG5zw0="
}
}
let res = await fetch('https://soroban-testnet.stellar.org', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
})
let json = await res.json()
console.log(json)
import json, requests
res = requests.post(https://soroban-testnet.stellar.org, json={
"jsonrpc": "2.0",
"id": 8675309,
"method": "sendTransaction",
"params": {
"transaction": "AAAAAgAAAAAg4dbAxsGAGICfBG3iT2cKGYQ6hK4sJWzZ6or1C5v6GAAAAGQAJsOiAAAADQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAACgAAAAVIZWxsbwAAAAAAAAEAAAAMU29yb2JhbiBEb2NzAAAAAAAAAAELm/oYAAAAQATr6Ghp/DNO7S6JjEFwcJ9a+dvI6NJr7I/2eQttvoovjQ8te4zKKaapC3mbmx6ld6YKL5T81mxs45TjzdG5zw0="
}
})
print(json.dumps(res.json(), indent=4))
{
"jsonrpc": "2.0",
"id": 8675309,
"method": "sendTransaction",
"params": {
"transaction": "AAAAAgAAAAAg4dbAxsGAGICfBG3iT2cKGYQ6hK4sJWzZ6or1C5v6GAAAAGQAJsOiAAAADQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAACgAAAAVIZWxsbwAAAAAAAAEAAAAMU29yb2JhbiBEb2NzAAAAAAAAAAELm/oYAAAAQATr6Ghp/DNO7S6JjEFwcJ9a+dvI6NJr7I/2eQttvoovjQ8te4zKKaapC3mbmx6ld6YKL5T81mxs45TjzdG5zw0="
}
}
Result
{
"jsonrpc": "2.0",
"id": 8675309,
"result": {
"status": "PENDING",
"hash": "d8ec9b68780314ffdfdfc2194b1b35dd27d7303c3bceaef6447e31631a1419dc",
"latestLedger": 2553978,
"latestLedgerCloseTime": "1700159337"
}
}
Guía del SDK
El ejemplo anterior está enviando una transacción utilizando métodos RPC directamente. Si estás utilizando el SDK de Stellar para crear aplicaciones, puedes usar las funciones nativas para obtener la misma información.
- Python
- JavaScript
- Java
# pip install --upgrade stellar-sdk
from stellar_sdk import SorobanServer, soroban_rpc, Keypair, Network, TransactionBuilder, scval
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(root_keypair.public_key)
# native token contract (XLM)
contract_id = "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC"
transaction = (
TransactionBuilder(
source_account=root_account,
network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
base_fee=100,
)
# Transfer 1 native token to GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H
# https://developers.stellar.org/docs/tokens/token-interface
.append_invoke_contract_function_op(contract_id, "transfer", [
scval.to_address(root_keypair.public_key), # from
scval.to_address("GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"), # to
scval.to_int128(1 * 10 ** 7) # amount, 1 XLM, decimal places are 7
])
.set_timeout(30)
.build()
)
transaction = server.prepare_transaction(transaction)
transaction.sign(root_keypair)
return server.send_transaction(transaction)
response = send_transaction()
print("status", response.status)
print("hash:", response.hash)
print("status:", response.status)
print("errorResultXdr:", response.error_result_xdr)
// 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 {
// native token contract (XLM)
const contractId =
"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC";
const sourceSecretKey =
"SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const contract = new StellarSdk.Contract(contractId);
const sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecretKey);
const accountId = sourceKeypair.publicKey();
const account = await server.getAccount(accountId);
const fee = StellarSdk.BASE_FEE;
// Transfer 1 native token to GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H
// https://developers.stellar.org/docs/tokens/token-interface
const transaction = new StellarSdk.TransactionBuilder(account, { fee })
.setNetworkPassphrase(StellarSdk.Networks.TESTNET)
.setTimeout(30)
.addOperation(
contract.call(
"transfer",
StellarSdk.nativeToScVal(accountId, { type: "address" }), // from
StellarSdk.nativeToScVal(
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
{ type: "address" },
), // to
StellarSdk.nativeToScVal("10000000", { type: "i128" }), // amount, 1 XLM, decimal places are 7
),
)
.build();
server
.prepareTransaction(transaction)
.then((result) => {
result.sign(sourceKeypair);
return server.sendTransaction(result);
})
.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();
// https://github.com/lightsail-network/java-stellar-sdk?tab=readme-ov-file#installation
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.operations.InvokeHostFunctionOperation;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.scval.Scv;
import java.math.BigInteger;
import java.util.Arrays;
public class SendTransactionExample {
public static void main(String[] args) {
SorobanServer server = new SorobanServer("https://soroban-testnet.stellar.org");
try {
KeyPair sourceKeyPair =
KeyPair.fromSecretSeed("SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
TransactionBuilderAccount sourceAccount = server.getAccount(sourceKeyPair.getAccountId());
// native token contract (XLM)
String contractId = "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC";
// Transfer 1 native token to GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H
// https://developers.stellar.org/docs/tokens/token-interface
org.stellar.sdk.operations.InvokeHostFunctionOperation operation =
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
contractId,
"transfer",
Arrays.asList(
Scv.toAddress(sourceKeyPair.getAccountId()), // from
Scv.toAddress(
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"), // to
Scv.toInt128(
BigInteger.valueOf(10000000)) // amount, 1 XLM, decimal places are 7
))
.build();
Transaction transaction =
new TransactionBuilder(sourceAccount, Network.TESTNET)
.setBaseFee(100)
.addOperation(operation)
.setTimeout(30)
.build();
transaction = server.prepareTransaction(transaction);
// 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();
}
}
}