Skip to main content

Sending transactions

Creating a transaction

Before you can sign and send transactions, you must first create a Transaction.

The code snippet below demonstrates how to create a Transaction using the @solana/web3.js library:

const web3 = require("@solana/web3.js");

// Define network connection
const network = "<NETWORK_URL>";
const connection = new web3.Connection(network);

// Generate keypairs
const fromKeypair = web3.Keypair.generate();
const toKeypair = web3.Keypair.generate();

// Get blockhash
const { blockhash } = await connection.getLatestBlockhash();

// Create transaction
const transaction = new web3.Transaction({
feePayer: fromKeypair,
recentBlockhash: blockhash
});

// Add transaction instructions
transaction.add(
web3.SystemProgram.transfer({
fromPubkey: fromKeypair.publicKey,
toPubkey: toKeypair.publicKey,
lamports: web3.LAMPORTS_PER_SOL
})
);

info

You can learn more about creating transactions on Solana by visiting the Solana documentation.

Signing a transaction

To sign a transaction, call the signTransaction method on the provider, passing a Transaction as an argument.

const signedTransaction = await window.coinbaseSolana.signTransaction(transaction);

Sending a transaction

To send a transaction, call the sendTransaction method on the provider, passing a Transaction as an argument.

const { signature } = await window.coinbaseSolana.sendTransaction(transaction);

Signing and sending a transaction

To sign and send a transaction, call the signAndSendTransaction method on the provider, passing a Transaction as an argument.

const { signature } = await window.coinbaseSolana.signAndSendTransaction(transaction);

Signing all transactions

To sign a batch of transactions, call the signAllTransactions method on the provider, passing a list of Transactions as an argument.

const signedTransactions = await window.coinbaseSolana.signAllTransactions(transactions);

Was this helpful?