Skip to content
LogoLogo

Get DEX Buy Quote

dex.getBuyQuote

Gets the amount of input tokens needed to buy an exact output amount.

Usage

import {  } from './viem.config'
 
const  = await ..({
  : 100000000n,
  : '0x20c0000000000000000000000000000000000000',
  : '0x20c0000000000000000000000000000000000001',
})
 
.('Amount in:', )
Amount in: 100100000n

Recipes

Fund an Exact Invoice Payment

Combine dex.getBuyQuote with dex.buySync to pay an invoice denominated in a token you do not hold, deriving maxAmountIn from a fresh quote plus a tolerance.

import {  } from './viem.config'
 
const  = '0x20c0000000000000000000000000000000000000'
const  = '0x20c0000000000000000000000000000000000001'
const  = 2_500_000000n
 
const  = await ..({
  : ,
  : ,
  : ,
})
 
const {  } = await ..({
  : ,
  : ( * 1_005n) / 1_000n, // quote + 0.5% tolerance
  : ,
  : ,
})

Return Value

type ReturnType = bigint

The amount of tokenIn needed to buy the specified amountOut.

Parameters

amountOut

  • Type: bigint

Amount of tokenOut to buy.

tokenIn

  • Type: Address | bigint

Token to spend: a TIP-20 token id or a contract address.

tokenOut

  • Type: Address | bigint

Token to buy: a TIP-20 token id or a contract address.

blockNumber (optional)

  • Type: bigint

Block number to read the state from.

blockOverrides (optional)

  • Type: BlockOverrides

Block overrides to apply to the state.

blockTag (optional)

  • Type: BlockTag

Block tag to read the state from.

stateOverride (optional)

  • Type: StateOverride

State override to apply.