getSwap

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

Description

The 'getSwap' method facilitates token swaps by fetching swap data from the SpiderSwap API, converting the amount to lamports and the slippage to basis points (bps) before making the API call.

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.

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

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

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

  • 'fromMintDecimals'(number, optional): The decimal places for the 'fromMint' token (default is 9).

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.getSwap(
            'ownerAddress',
            'fromMintAddress',
            'toMintAddress',
            1,        // Amount in tokens
            0.5,      // Slippage percentage
            'provider',
            'pool',
            9         // fromMintDecimals
        );
        console.log('Swap data:', swapResult);
    } catch (error) {
        console.error('Error performing swap:', error);
    }
}

performSwap();

Error Handling

The method relies on 'getSwapRaw' for error handling, throwing an error if the swap data cannot be fetched and logging the error details for debugging.

Last updated