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 for Ethereum mainnet Uniswap:
import os from web3 import Web3 from eth_defi.provider.multi_provider import create_multi_provider_web3 from eth_defi.uniswap_v2.deployment import fetch_deployment from eth_defi.uniswap_v2.fees import estimate_buy_price from eth_defi.uniswap_v2.pair import fetch_pair_details # Default to Ankr free JSON-RPC endpoint if one not given # https://eth.public-rpc.com/ web3 = create_multi_provider_web3(os.environ.get("JSON_RPC_ETHEREUM", "https://eth.public-rpc.com")) assert web3.eth.chain_id == 1, f"We are not on Ethereum mainnet, got {web3.eth.chain_id}" uniswap_v2 = fetch_deployment( web3=web3, factory_address="0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f", router_address="0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", init_code_hash='0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f', ) # Uniswap v2's USDC-WETH needs to be reserved to WETH-USDC in human logic # https://tradingstrategy.ai/trading-view/ethereum/uniswap-v2/eth-usdc pair = fetch_pair_details(web3, "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc", reverse_token_order=True) print(f"Uniswap v2 pool is {pair.contract.address}, https://tradingstrategy.ai/trading-view/ethereum/uniswap-v2/eth-usdc") print("Base token is", pair.get_base_token()) print("Quote token is", pair.get_quote_token()) raw_price = estimate_buy_price( uniswap_v2, base_token=pair.get_base_token().contract, quote_token=pair.get_quote_token().contract, quantity=1 * 10**18, # 1 WETH = 1000000000000000000 wei ) # Convert raw USDC Solidity amount -> human USDC human_price = pair.get_quote_token().convert_to_decimals(raw_price) print(f"Price is {human_price} ETH/USD")
Example for unit testing:
# 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