Documentation for eth_defi.uniswap_v2.fees Python module.

Uniswap v2 token price, fee and price impact calculations.

Mostly lifted from Uniswap-v2-py MIT licensed by Asynctomatic.

A short example how to get started:

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(

# 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(
    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")


estimate_buy_price(uniswap, base_token, ...)

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

estimate_buy_price_decimals(uniswap, ...[, ...])

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

estimate_buy_quantity(uniswap, base_token, ...)

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

estimate_buy_received_amount_raw(uniswap, ...)

Estimate how much we receive for a certain cash amount.

estimate_sell_price(uniswap, base_token, ...)

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

estimate_sell_price_decimals(uniswap, ...[, ...])

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

estimate_sell_received_amount_raw(uniswap, ...)

Estimate how much cash we receive for a certain quantity of tokens sold.



A helper class to estimate Uniswap fees.