Getting a quote

Quote Interface Overview

The Quote interface comprises a set of functions that allow you to obtain estimated swap amounts based on various trade scenarios. It takes into account the trade type, tokens being swapped, token reserves, and trade amounts to provide accurate estimates. Before utilizing the Quote interface, familiarize yourself with the following core components:

TradeType Enum

The TradeType enum defines the types of trades that can be executed:

  • EXACT_INPUT: Specifies that the input token amount is fixed, and the output token amount may vary.

  • EXACT_OUTPUT: Specifies that the output token amount is fixed, and the input token amount may vary.

PoolState Type

The PoolState type encapsulates the state of a liquidity pool and consists of the following properties:

  • reserve: The reserve amount of the token being traded.

  • ethReserve: The Ether (ETH) reserve amount in the pool.

SwapAmount Type

The SwapAmount type represents the result of a swap and includes:

  • amount: The estimated output token amount after the swap.

  • priceImpact: An indication of the price impact of the swap, represented as a percentage.

Using the Quote Interface

To utilize the Quote interface and obtain swap amount estimates, you'll need to call the getSwapAmounts function. The function signature is as follows:

function getSwapAmounts<
  T extends TradeType,
  P extends PoolState | undefined = PoolState | undefined,
>(
  xfai: Xfai,
  tradeType: T,
  tokenIn: {
    token: Token;
    state: P;
  },
  tokenOut: {
    token: Token;
    state: P extends undefined ? PoolState : PoolState | undefined;
  },
  amount: BigNumber,
): SwapAmount

Example Usage

Here's an example usage of the getSwapAmounts function:

import { Xfai, Token, PoolState, TradeType, SwapAmount, getPoolState, getPoolFromToken } from '@xfai-labs/sdk';
import { BigNumber } from 'ethers';

// Create an instance of the Xfai protocol
const xfai = new Xfai(...);

// Define trade parameters
const tradeType = TradeType.EXACT_INPUT;
const inputToken: Token = Token('0x...'); // Input token instance
const outputToken: Token = Token('0x...'); // Output token instance

// Get the input pool using the getPoolFromToken function which uses token address and create2 to generate the pool object
const inputPool = getPoolFromToken(xfai, inputToken);
const outputPool = getPoolFromToken(xfai, outputToken);

const tokenIn: { token: Token; state: PoolState } = {
  token: inputToken,
  state: await getPoolState(xfai, inputPool), // Pool state for input token
};
const tokenOut: { token: Token; state: PoolState } = {
  token: outputToken,
  state: await getPoolState(xfai, outputPool), // Pool state for output token
};
const amount = new BigNumber('1000'); // Amount of input tokens

// Calculate swap amount using the Quote interface
const swapAmount: SwapAmount = getSwapAmounts(xfai, tradeType, tokenIn, tokenOut, amount);

console.log('Estimated Output Amount:', swapAmount.amount.toString());
console.log('Price Impact:', swapAmount.priceImpact.toFixed(2) + '%');

If you want to swap the native token (e.g., Ether), you can use xfai.nativeToken as the input token. The native token or the wrapped version of the native token does not require pool state information, and you can include it in the tokenIn parameter as follows:

const tokenIn: { token: Token; state: undefined } = {
  token: xfai.nativeToken, // Use xfai.nativeToken for native token (xfai.wrappedNativeToken for the wrapped version fo the native token)
  state: undefined, // No need for state for native token
};

Last updated