getSwapRaw

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

Description

The 'getSwapRaw' method facilitates token swaps by fetching raw swap data from the SpiderSwap API.

Parameters

  • 'owner' (string): The wallet address of the owner initiating the swap.

  • 'fromMint' (string): The mint address of the token being swapped from.

  • 'toMint' (string): The mint address of the token being swapped to.

  • 'amount' (number): The amount of the 'fromMint' token to swap, in lamports.

  • 'slippage' (number): The acceptable slippage percentage for the swap, in bps.

  • 'provider' (string): The liquidity provider.

  • 'pool' (string | null, optional): The specific pool for the swap (default is null).

Returns

'Promise<swapResponse>': A promise that resolves to a base64-encoded string containing the raw swap data.

'swapResponse' Type

export type swapResponse = {
    base64Transaction: string,
    signers: Array<string>,
}

Example

async function performSwap() {
    try {
        const swapResult = await spiderswap.swap.getSwapRaw(
            'ownerAddress',
            'fromMintAddress',
            'toMintAddress',
            1000000,  // Amount in lamports
            50, // Amount in bps
            'provider',
            'pool'
        );
        console.log('Swap data:', swapResult);
    } catch (error) {
        console.error('Error performing swap:', error);
    }
}

performSwap();

Error Handling

The method throws an error if the swap data cannot be fetched, logging the error details for debugging.

Last updated