createTransaction

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: createTransaction

Description

The 'createTransaction' method creates a Solana transaction from a base64-encoded string containing the swap transaction data.

Parameters

  • 'swapTransactionData' (string): The base64-encoded string containing the swap transaction data.

Returns

'Promise<VersionedTransaction>': A promise that resolves to a 'VersionedTransaction' object.

Example

This example demonstrates using both 'getSwap' and 'createTransaction' methods.

async function performSwapAndCreateTransaction() {
    try {
        // Get the swap data
        const swapResult = await spiderswap.swap.getSwap(
            'ownerAddress',
            'fromMintAddress',
            'toMintAddress',
            1,        // Amount in tokens
            0.5,      // Slippage percentage
            'provider', // orca
            'pool',
            9
        );
        console.log('Swap data:', swapResult);
        
        const { base64Transaction, signers } = swapResult;

        // Create the transaction
        const transaction = await spiderswap.swap.createTransaction(base64Transaction);
        console.log('Transaction:', transaction);
    } catch (error) {
        console.error('Error performing swap and creating transaction:', error);
    }
}

performSwapAndCreateTransaction();

Implementation Details

This method converts the 'swapTransactionData' from a base64-encoded string to a buffer and deserializes it into a 'VersionedTransaction'.

Last updated