Karrot - Spiderswap Package
  • Introduction
  • How to Obtain Your API Key
  • NPM
    • Swap Overview
      • getSwapRaw
      • getSwap
      • createTransaction
      • sendTransaction
    • Quote Overview
      • getQuoteRaw
      • getQuote
      • bulkQuote
  • PHP
    • Coming Soon
  • Python
    • Swap Overview
      • get_swap_raw
      • get_swap
      • create_transaction
      • send_transaction
    • Quote Overview
      • get_quote_raw
      • get_quote
      • bulk_quote
Powered by GitBook
On this page
  • Installation
  • Usage
  • Initialization
  • Method: createTransaction
  1. NPM
  2. Swap Overview

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'.

PreviousgetSwapNextsendTransaction

Last updated 11 months ago