estimate_buy_quantity

Documentation for eth_defi.uniswap_v2.fees.estimate_buy_quantity function.

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

Estimate how many tokens we are going to receive when doing a buy.

Good for doing a price impact calculations.

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

Example:

# Estimate how much ETH we will receive for 500 USDC.
# In this case the pool ETH price is $1700 so this should be below ~1/4 of ETH
amount_eth = estimate_buy_quantity(
    uniswap_v2,
    weth,
    usdc,
    500*10**18,
)
assert amount_eth / 1e18 == pytest.approx(0.28488156127668085)
Parameters
  • quantity (int) – How much of the quote token we have to use

  • 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

Returns

Expected base token to receive

Return type

int