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: getQuote
  1. NPM
  2. Quote Overview

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.

PreviousgetQuoteRawNextbulkQuote

Last updated 11 months ago