getQuote

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

Description

The 'getQuote' method retrieves a quote for a token swap, converting the amount to lamports and the slippage to basis points (bps) before making the API call.

Parameters

  • '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 basis points (bps).

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

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

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

Returns

'Promise<quoteResponseMap>': A promise that resolves to a 'quoteResponseMap' object containing the quote data.

'quoteResponseMap' Type

export type quoteMap = {
    inputMint: string;
    inAmount: any;
    outputMint: string;
    outAmount: any;
    minimumReceived: any;
    slippage: number;
    priceImpactPercent: any;
}

Example

async function getQuote() {
    try {
        const quoteData = await spiderswap.quote.getQuote(
            'fromMintAddress',
            'toMintAddress',
            1,         // Amount in tokens
            0.5,       // Slippage percentage
            'providerName',
            9,         // fromMintDecimals
            9          // toMintDecimals
        );
        console.log('Quote data:', quoteData);
    } catch (error) {
        console.error('Error fetching quote data:', error);
    }
}

getQuote();

Implementation Details

This method converts the 'amount' to lamports and the 'slippage' to basis points (bps) before calling the 'getQuoteRaw' method to fetch the quote data from the API. It then converts the amounts back to their original units and constructs a 'quoteResponseMap' object.

Last updated