estimate_buy_price

Documentation for eth_defi.uniswap_v2.fees.estimate_buy_price function.

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

Estimate how much we are going to need to pay when doing buy.

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_price(
    uniswap_v2,
    weth,
    usdc,
    1*10**18,
)
assert amount_eth / 1e18 == pytest.approx(0.28488156127668085)
Parameters
  • 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

  • quantity (int) – How much of the base token we want to buy

  • 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 base token to receive

Return type

int