Send Transaction from Instructions
Like
sendTransactionWithSigners
from@solana/helpers
Sends a transaction containing one or more instructions. The transaction will be signed by the fee payer.
Returns: Promise<Signature>
- The unique transaction signature that can be used to look up the transaction
const signature = await connection.sendTransactionFromInstructions({
feePayer: wallet,
instructions: [instruction1, instruction2],
commitment: "confirmed",
skipPreflight: true,
maximumClientSideRetries: 0,
abortSignal: null,
});
Options
feePayer
:KeyPairSigner
- The account that will pay for the transaction’s feesinstructions
:Array<IInstruction>
- Array of instructions to include in the transactioncommitment
:Commitment
(optional) - Desired commitment level . Can be"processed"
,"confirmed"
(default), or"finalized"
.skipPreflight
:boolean
(optional) - Whether to skip preflight transaction checks. Enable to reduce latency, disable for more safety (default: true)maximumClientSideRetries
:number
(optional) - Maximum number of times to retry if the transaction fails. Useful for handling temporary network issues (default: 0)abortSignal
:AbortSignal | null
(optional) - Signal to abort the transaction. Use this to implement timeouts or cancel pending transactions (default: null)
Example
Here’s an example of sending a transaction with a SOL transfer instruction:
const feePayer = await connection.createWallet({
airdropAmount: lamports(1n * SOL)
});
const recipient = await generateKeyPairSigner();
// Create an instruction to transfer SOL
const transferInstruction = getTransferSolInstruction({
amount: lamports(0.1n * SOL),
destination: recipient.address,
source: feePayer
});
// Send the transaction with the transfer instruction
const signature = await connection.sendTransactionFromInstructions({
feePayer,
instructions: [transferInstruction],
maximumClientSideRetries: 3
});
console.log(`Transaction successful: ${signature}`);
console.log(`Explorer link: ${connection.getExplorerLink("tx", signature)}`);
You can also send multiple instructions in a single transaction:
// Create instructions to transfer SOL to multiple recipients
const transferInstructions = recipients.map(recipient =>
getTransferSolInstruction({
amount: lamports(0.1n * SOL),
destination: recipient.address,
source: feePayer
})
);
// Send all transfers in one transaction
const signature = await connection.sendTransactionFromInstructions({
feePayer,
instructions: transferInstructions,
commitment: "confirmed"
});
What Happens Automatically
The function will automatically:
- Get a recent blockhash
- Add compute budget instructions if needed
- Sign the transaction with the fee payer
- Set a priority fee, using your RPC provider’s priority fee estimation tools (depending on RPC provider).
- Send and confirm the transaction,
- Retry the transaction if requested and needed
Error Handling
If the intruction fails, the function will throw an error.
error.message
is extracted from logs, so should always be useful errors. You should never see custom program error 0xSomeHexCode
.
Error messages take the format of programName.instructionHandler: message
, for example:
TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb.TransferChecked: insufficient funds
8jR5GeNzeweq35Uo84kGP3v1NcBaZWH5u62k7PxN4T2y.RefundOffer: A has one constraint was violated
11111111111111111111111111111111.Allocate: account already in use
If you see an error that doesn’t take this format, please file a bug!
See also: Send and Confirm Transaction, Get Recent Signature Confirmation