Skip to content
LogoLogo

Get DEX Sell Quote

dex.getSellQuote

Gets the output amount received for selling an exact input amount.

Usage

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

Recipes

Convert an Entire Balance with a Slippage Bound

Combine dex.getSellQuote with token.getBalance and dex.sellSync to sweep a full balance into your settlement token, bounding the output with a fresh quote.

import {  } from './viem.config'
 
const  = '0x20c0000000000000000000000000000000000000'
const  = '0x20c0000000000000000000000000000000000001'
 
const  = await ..({ :  })
 
const  = await ..({
  : .,
  : ,
  : ,
})
 
const {  } = await ..({
  : .,
  : ( * 995n) / 1_000n, // quote - 0.5% tolerance
  : ,
  : ,
})

Halt Automated Conversions When the Rate Drifts

Use a fixed probe quote to derive the pair's effective conversion rate, and pause scheduled conversions when it moves past your tolerance.

import {  } from './viem.config'
 
const  = 10_000_000000n
 
const  = await ..({
  : ,
  : '0x20c0000000000000000000000000000000000001',
  : '0x20c0000000000000000000000000000000000000',
})
 
const  = ( * 10_000n) / 
 
if ( < 9_980n)
  throw new (`alphaUSD rate ${} bps: pausing conversions`)

Return Value

type ReturnType = bigint

The amount of tokenOut received for selling the specified amountIn.

Parameters

amountIn

  • Type: bigint

Amount of tokenIn to sell.

tokenIn

  • Type: Address | bigint

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

tokenOut

  • Type: Address | bigint

Token to receive: 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.