Quoting Prices
Before executing any trade on an automated market maker (AMM) like Uniswap, it is crucial to query the expected return. uniswap-python provides robust methods to quote prices, derived directly from on-chain smart contract data.
Important: All pricing methods in this library return values as integers in the token's smallest denomination (e.g., Wei). You must understand the decimal precision of the tokens you are querying to interpret the results.
Understanding FeeTier (V3 Only)
In Uniswap V1 and V2, the fee was a static 0.3% applied to all pools. In V3, liquidity providers can create pools with different fee tiers for the same token pair. When quoting prices on a V3 client, you must explicitly pass the fee argument.
from uniswap.fee import FeeTier
FeeTier.TIER_100 # 0.01% - Best for highly correlated assets (e.g., USDC/USDT)
FeeTier.TIER_500 # 0.05% - Best for large-cap assets (e.g., ETH/USDC)
FeeTier.TIER_3000 # 0.30% - The standard tier for most pairs
FeeTier.TIER_10000 # 1.00% - Best for exotic or highly volatile tokens
Failing to provide a fee tier in V3 will raise an InvalidFeeTier exception.
Exact Input: get_price_input
Use this method when you know exactly how much you want to spend. It returns the maximum amount of output_token you will receive for a given amount of input_token.
# Scenario: I want to spend exactly 500 USDC. How much WBTC will I get?
# USDC has 6 decimals
usdc_qty = 500 * 10**6
expected_wbtc = uniswap.get_price_input(
token0=USDC,
token1=WBTC,
qty=usdc_qty,
fee=FeeTier.TIER_3000
)
# WBTC has 8 decimals
print(f"For 500 USDC, you will receive {expected_wbtc / 10**8} WBTC")
Exact Output: get_price_output
Use this method when you know exactly how much you want to receive. It returns the minimum amount of input_token you need to supply to buy the exact amount of output_token.
# Scenario: I need to buy exactly 10 UNI tokens. How much ETH will it cost?
# UNI has 18 decimals
uni_qty = 10 * 10**18
cost_in_eth = uniswap.get_price_output(
token0=ETH,
token1=UNI,
qty=uni_qty,
fee=FeeTier.TIER_3000
)
print(f"Buying 10 UNI will cost {cost_in_eth / 10**18} ETH")
Spot Price: get_raw_price
The get_price_input and get_price_output methods simulate actual trades; therefore, their results account for slippage based on the size of your order against the pool's liquidity.
If you want the theoretical spot price (the ratio of reserves in V2, or the decoded sqrtPriceX96 in V3) regardless of order size, use get_raw_price.
# Get the spot price of ETH quoted in DAI
spot_price = uniswap.get_raw_price(ETH, DAI, fee=FeeTier.TIER_3000)
print(f"Current Spot Price: {spot_price} DAI per ETH")
Best Practice: Comparing the output of
get_raw_priceagainstget_price_inputallows you to calculate the exact price impact of your trade size. See the Examples page for a script demonstrating this.
Custom Routing
By default, if a direct pair does not exist (or has no liquidity), Uniswap attempts to route the trade. uniswap-python assumes a simple hop through WETH if no route is provided.
If you know a better multi-hop route (e.g., Token A -> USDC -> Token B), you can pass a list of addresses to the route parameter.
Note: Custom routing via the route parameter is currently fully supported on V2. V3 custom routing requires specific path encoding which is actively being developed.
Dealing with Decimals
Tokens on Ethereum do not use floating-point numbers. They use large integers combined with a decimals identifier.
If you do not know a token's decimals, uniswap-python can query the contract for you:
# Fetch token metadata from the blockchain
link_token = uniswap.get_token(LINK_ADDRESS)
print(link_token.symbol) # Output: LINK
print(link_token.decimals) # Output: 18
# Now you can safely format your inputs/outputs
qty = 50 * (10 ** link_token.decimals)
Common Pitfall: USDC and USDT use 6 decimals, WBTC uses 8, while most others (ETH, DAI, UNI) use 18. Always verify decimals before constructing logic around prices.