Submit a transaction to Stellar RPC using the JavaScript SDK
Here is a simple, rudimentary looping mechanism to submit a transaction to Stellar RPC and wait for a result.
import { Transaction, FeeBumpTransaction } from "@stellar/stellar-sdk";
import { Server, Api } from "@stellar/stellar-sdk/rpc";
const RPC_SERVER = "https://soroban-testnet.stellar.org/";
const server = new Server(RPC_SERVER);
// Submits a tx and then polls for its status until a timeout is reached.
async function submitTx(
tx: Transaction | FeeBumpTransaction,
): Promise<Api.GetTransactionResponse> {
return server
.sendTransaction(tx)
.then(async (reply) => {
if (reply.status !== "PENDING") {
throw reply;
}
return server.pollTransaction(reply.hash, {
sleepStrategy: (_iter: number) => 500,
attempts: 5,
});
})
.then((finalStatus) => {
switch (finalStatus.status) {
case Api.GetTransactionStatus.FAILED:
case Api.GetTransactionStatus.NOT_FOUND:
throw tmpStatus;
case Api.GetTransactionStatus.SUCCESS:
return status;
}
});
}
Remember: You should always handle errors gracefully! This is a fail-hard and fail-fast approach that should only be used in these examples.
Guides in this category:
📄️ Create an account
Learn about creating Stellar accounts, keypairs, funding, and account basics.
📄️ Send to and receive payments from Contract Accounts
Learn to send payments to and receive payments from Contract Accounts on the Stellar network.
📄️ Send and receive payments
Learn to send payments and watch for received payments on the Stellar network.
📄️ Channel accounts
Create channel accounts to submit transactions to the network at a high rate.
📄️ Claimable balances
Split a payment into two parts by creating a claimable balance.
📄️ Clawbacks
Use clawbacks to burn a specific amount of a clawback-enabled asset from a trustline or claimable balance.
📄️ Fee-bump transactions
Use fee-bump transactions to pay for transaction fees on behalf of another account without re-signing the transaction.
📄️ Sponsored reserves
Use sponsored reserves to pay for base reserves on behalf of another account.
📄️ Path payments
Send a payment where the asset received differs from the asset sent.
📄️ Pooled accounts: muxed accounts and memos
Use muxed accounts to differentiate between individual accounts in a pooled account.
📄️ Install and deploy a smart contract with code
Install and deploy a smart contract with code.
📄️ Invoke a contract function in a transaction using SDKs
Use the Stellar SDK to create, simulate, and assemble a transaction.
📄️ simulateTransaction RPC method guide
simulateTransaction examples and tutorials guide.
📄️ Submit a transaction to Stellar RPC using the JavaScript SDK
Use a looping mechanism to submit a transaction to the RPC.
📄️ Upload WebAssembly (Wasm) bytecode using code
Upload the Wasm of the contract using js-stellar-sdk.