estimate_sell_price

Documentation for eth_defi.uniswap_v2.fees.estimate_sell_price function.

estimate_sell_price(uniswap, base_token, quote_token, quantity, *, fee=30, slippage=0, intermediate_token=None)[source]

Estimate how much we are going to get paid when doing a sell.

Calls the on-chain contract to get the current liquidity and estimates the the price based on it.

Note

The price of an asset depends on how much you are selling it. More you sell, more there will be price impact.

To get a price of an asset, ask for quantity 1 of it:

# Create the trading pair and add iint(10_000 * amounts[-1] // (10_000 - slippage))nitial liquidity for price 1700 USDC/ETH
deploy_trading_pair(
    web3,
    deployer,
    uniswap_v2,
    weth,
    usdc,
    1_000 * 10**18,  # 1000 ETH liquidity
    1_700_000 * 10**6,  # 1.7M USDC liquidity
)

# Estimate the price of selling 1 ETH
usdc_per_eth = estimate_sell_price(
    uniswap_v2,
    weth,
    usdc,
    1 * 10**18,  # 1 ETH
)
price_as_usd = usdc_per_eth / 1e6
assert price_as_usd == pytest.approx(1693.2118677678354)
Parameters
  • quantity (int) – How much of the base token we want to sell

  • uniswap (eth_defi.uniswap_v2.deployment.UniswapV2Deployment) – Uniswap v2 deployment

  • base_token (web3.contract.contract.Contract) – Base token of the trading pair

  • quote_token (web3.contract.contract.Contract) – Quote token of the trading pair

  • fee (int) – Trading fee express in bps, default = 30 bps (0.3%)

  • slippage (float) – Slippage express in bps

  • intermediate_token (Optional[web3.contract.contract.Contract]) –

Returns

Expected quote token amount to receive

Return type

int