getSwapBest

Installation

npm install @quickscope/sdk

Usage

Initialization

First, initialize the Quickscope class with your location ("frankfurt" or "nyc") and your API key:

import Quickscope from '@quickscope/sdk';
// OR
const Quickscope = require('@quickscope/sdk');

const quickscope = new Quickscope("frankfurt", "your-api-key");

Method: getSwapBest

Description

The getSwapBest method generates a ready-to-sign, base64-encoded Solana transaction that performs a token swap using the best available quote from all supported liquidity providers. It handles route selection, slippage calculation, and transaction building in a single step.

Use this method when you want the fastest path from quote to transaction without specifying providers or pools manually.


Parameters

{
  payer: string;
  from: string;
  to: string;
  amount: number;
  slippage: number;
  priorityFee: number;
  useJito: boolean;
}
  • payer: (string) – The wallet address (base58) that will pay for and sign the transaction.
  • from: (string) – The mint address of the token being swapped from.
  • to: (string) – The mint address of the token being swapped to.
  • amount: (number) – The input amount in base units (e.g., lamports).
  • slippage: (number) – Acceptable slippage in basis points (e.g., 50 = 0.5%).
  • priorityFee: (number) – Optional micro-lamport priority fee. Can be set to 0 to disable.
  • useJito: (boolean) – If true, includes the transaction in a Jito bundle for better execution guarantees.

Returns

Promise<swapResponse>: A promise that resolves to a swapResponse object containing the transaction or an error.

swapResponse Type

export type swapResponse = {
  swap?: {
    base64Transaction: string;
  };
  error?: string;
  error_code?: string;
}

Example

async function fetchBestSwapTx() {
    try {
        const swap = await quickscope.v1.swap.getSwapBest({
            payer: 'E3BmYnxm9LYo2ca9mY7zBr5Say5YUjo17QdoRxPk9yTG', // your wallet address
            from: 'So11111111111111111111111111111111111111112', // SOL
            to: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',   // USDC
            amount: 1000000,
            slippage: 100,       // 1%
            priorityFee: 50000,  // 0.00005 SOL
            useJito: true
        });

        console.log('Swap transaction:', swap.swap?.base64Transaction);

        // Example output:
        // {
        //   swap: {
        //     base64Transaction: "AAABAgMEBQYH..."
        //   }
        // }
    } catch (error) {
        console.error('Error generating swap transaction:', error);
    }
}

fetchBestSwapTx();

Error Handling

This method throws or returns an error if:

  • No valid quote can be found for the token pair
  • One or more input fields are missing or malformed
  • The backend fails to build a transaction

You should wrap this call in a try/catch block and optionally check for the presence of swap in the response: