sendTransaction
Installation
npm install @karrotcapital/spiderswap
Usage
Initialization
First, initialize the 'Spiderswap'
class with your API key.
import Spiderswap from '@karrotcapital/spiderswap';
// OR
const Spiderswap = require('@karrotcapital/spiderswap');
const spiderswap = new Spiderswap(ApiKey);
Method: sendTransaction
Description
The 'sendTransaction'
method sends a Solana transaction to the blockchain, handling retries and confirmation.
Parameters
'rpcEndpoint'
(string): The RPC endpoint to send the transaction to.'transaction'
(VersionedTransaction): The transaction object to be sent.'privateKey'
(string): The private key of the sender, encoded in base58.'additionalSigners'
(Array<string> | null, default null): The additional signers that need to sign the transaction for it to be sent.'retries'
(number, default 3): The amount of retries this function will try to do to execute the transaction.
Returns
'Promise<string>'
: A promise that resolves to the transaction signature.
Example
This example demonstrates how to use 'getSwap'
, 'createTransaction'
, and 'sendTransaction'
methods.
async function performSwapCreateAndSendTransaction() {
try {
// Get the swap data
const swapResult = await spiderswap.swap.getSwap(
'ownerAddress',
'fromMintAddress',
'toMintAddress',
1, // Amount in tokens
0.5, // Slippage percentage
'provider',
'pool',
9 // fromMintDecimals
);
console.log('Swap data:', swapResult);
const { base64Transaction, signers } = swapResult;
// Create the transaction
const transaction = await spiderswap.swap.createTransaction(base64Transaction);
console.log('Transaction:', transaction);
// Send the transaction
const signature = await spiderswap.swap.sendTransaction(
'https://api.mainnet-beta.solana.com',
transaction,
'privateKeyInBase58',
signers,
3
);
console.log('Transaction signature:', signature);
} catch (error) {
console.error('Error performing swap, creating and sending transaction:', error);
}
}
performSwapCreateAndSendTransaction();
Implementation Details
This method attempts to send the transaction multiple times, updating the blockhash and signing the transaction each time. If it fails after a set number of retries, it throws an error.
Last updated