LFJ Developer Docs
  • Liquidity Book
  • Introduction
  • LB V2.2 Key Changes
  • Guides
    • Swap Tokens
    • Add/Remove Liquidity
    • Tracking Volume
    • Tracking Pool Balances
    • Finding The Best Quote
    • Byte32 Decoding
    • Price From Bin Id
    • Bin Id From Price
    • Finding Liquidity Depth
    • User Balances
  • Concepts
    • Concentrated Liquidity
    • Bin Math
    • Bin Liquidity
    • Swaps
    • Fees
    • Oracle
  • Contracts
    • Interfaces
      • ILBLegacyFactory
      • ILBLegacyToken
      • ILBLegacyPair
      • ILBLegacyRouter
      • ILBFlashLoanCallback
      • IPendingOwnable
      • IJoeFactory
      • IJoePair
      • IJoeRouter01
      • IJoeRouter02
      • IWNATIVE
      • ILBFactory
      • ILBHooks
      • ILBPair
      • ILBRouter
      • ILBToken
    • Libraries
      • Math
        • BitMath
        • Encoded
        • LiquidityConfigurations
        • PackedUint128Math
        • SafeCast
        • SampleMath
        • TreeMath
        • Uint128x128Math
        • Uint256x256Math
      • BinHelper
      • Clone
      • Constants
      • FeeHelper
      • Hooks
      • ImmutableClone
      • JoeLibrary
      • OracleHelper
      • PairParameterHelper
      • PriceHelper
      • ReentrancyGuardUpgradeable
      • TokenHelper
    • LBBaseHooks
    • LBFactory
    • LBPair
    • LBQuoter
    • LBRouter
    • LBToken
  • Deployment Addresses
    • Avalanche C-Chain
    • Fuji Testnet
    • Arbitrum One
    • Binance Smart Chain
    • Binance Smart Chain Testnet
    • Ethereum Mainnet
    • Monad Testnet
  • SDK
    • Introduction
    • Making a Trade
    • Adding Liquidity
    • Removing Liquidity
  • Audits
  • AMM
    • Joe V1 Contracts
    • Joe V1 Audits
  • LFJ DEX API
    • Dex Analytics
    • Pools
    • Rewards
    • User
    • User Lifetime Stats
    • Vaults
    • Models
  • LFJ Aggregator API
    • Default
    • Models
Powered by GitBook
On this page
  • Introduction
  • Find The Best Quote From Amount In
  • Code Example
  • Find The Best Quote From Amount Out
  • Code Example
  1. Guides

Finding The Best Quote

Introduction

Finding the optimal quote for a givien tokens path can be obtained through the LBQuoter contract. This contract will find the best combinations of swaps accross the V1, V2, V2.1 and V2.2 pairs in order to optimise cost.

The rest of the document describes:

  • Which functions to call to get the optimal quote from an input and output amount.

  • Code examples that illustrate use cases.

Find The Best Quote From Amount In

This function retrieves the best route of swaps for a given input amount.

function findBestPathFromAmountIn(address[] calldata route, uint128 amountIn)
    public
    view
    returns (Quote memory quote)

The output of this function is quite substantial. Here are the specifications to understand it better:

struct Quote {
    address[] route; // Path in the form of an array of token address to go through
    address[] pairs; // Address of the different pairs to do through
    uint256[] binSteps; // Bin step for each pair (0 for V1 pairs)
    ILBRouter.Version[] versions; // Versions to use for each pair
    uint128[] amounts; // The amounts for every step of the swap
    uint128[] virtualAmountsWithoutSlippage; // The virtual amounts of every step of the swap without slippage
    uint128[] fees; // The fees to pay for every step of the swap
}

Code Example

In this example, we want to swap 10 USDC to USDT. A possible route is to go through the WAVAX route in between:

address[] memory tokenPath = new address[](3);
tokenPath[0] = USDC;
tokenPath[1] = WAVAX;
tokenPath[2] = USDT;

uint128 amountIn = 10 * 10e6;

LBQuoter.Quote memory quote = quoter.findBestPathFromAmountIn(tokenPath, amountIn);

Find The Best Quote From Amount Out

This function retrieves the best route of swaps for a given output amount.

function findBestPathFromAmountOut(address[] calldata route, uint128 amountOut)
    public
    view
    returns (Quote memory quote)

Code Example

In this example, we want to own 10 USDC and we would like to know how many USDT is required to perform the if we want to go through the WAVAX route:

address[] memory tokenPath = new address[](3);
tokenPath[0] = USDT;
tokenPath[1] = WAVAX;
tokenPath[2] = USDC;

uint128 amountOut = 10 * 10e6;

LBQuoter.Quote memory quote = quoter.findBestPathFromAmountOut(tokenPath, amountOut);
PreviousTracking Pool BalancesNextByte32 Decoding

Last updated 9 days ago