Practical Examples
Below are real-world scripts demonstrating how to combine the features of uniswap-python to achieve complex tasks.
Example 1: Estimating True Price Impact (Slippage)
This script demonstrates how to estimate the price impact of a trade before executing it.
Price impact is the difference between the theoretical spot market price and the execution price you will actually pay due to the depth of liquidity available. The estimate_price_impact method intelligently subtracts the protocol fee from this calculation so you see the true slippage caused by pool depth.
import os
from web3 import Web3
from uniswap import Uniswap
from uniswap.fee import FeeTier
# Initialize Read-Only Client
provider = os.environ.get("PROVIDER")
uniswap = Uniswap(address=None, private_key=None, version=3, provider=provider)
# Token Addresses
ETH = Web3.to_checksum_address("0x0000000000000000000000000000000000000000")
VXV = Web3.to_checksum_address("0x7d29a64504629172a429e64183d6673b9dacbfce")
def check_impact(qty_eth: int):
"""Checks price impact for a specific order size."""
# 1. Estimate Impact
impact = uniswap.estimate_price_impact(
token_in=ETH,
token_out=VXV,
amount_in=qty_eth,
fee=FeeTier.TIER_10000 # VXV pool uses 1% fee tier
)
# 2. Format as a percentage
impact_percentage = round(impact * 100, 3)
eth_amount = qty_eth / 10**18
print(f"Impact for buying VXV with {eth_amount} ETH: {impact_percentage}%")
if __name__ == "__main__":
print("--- Checking V3 Liquidity Impact ---")
# Small order: 1 ETH
check_impact(1 * 10**18)
# Large order: 100 ETH
# Notice how the impact percentage increases significantly
check_impact(100 * 10**18)
Example 2: Comparing Prices Across Versions (Arbitrage Checker)
Because Uniswap V2 and V3 operate as completely separate smart contract ecosystems, the price of an asset can temporarily differ between them. This creates arbitrage opportunities.
This script initializes two clients to compare the price of an asset across V2 and V3.
import os
from web3 import Web3
from uniswap import Uniswap
from uniswap.fee import FeeTier
# Token Addresses
ETH = "0x0000000000000000000000000000000000000000"
USDC = Web3.to_checksum_address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
# Amount to check (1 ETH)
qty = 1 * 10**18
def compare_versions():
provider = os.environ.get("PROVIDER")
# Initialize V2 and V3 clients
uni_v2 = Uniswap(address=None, private_key=None, version=2, provider=provider)
uni_v3 = Uniswap(address=None, private_key=None, version=3, provider=provider)
# Get V2 Price
price_v2_raw = uni_v2.get_price_input(ETH, USDC, qty)
price_v2 = price_v2_raw / 10**6 # USDC has 6 decimals
# Get V3 Price (Requires Fee Tier, standard for USDC/ETH is 0.05%)
price_v3_raw = uni_v3.get_price_input(ETH, USDC, qty, fee=FeeTier.TIER_500)
price_v3 = price_v3_raw / 10**6
print(f"V2 Price for 1 ETH: {price_v2} USDC")
print(f"V3 Price for 1 ETH: {price_v3} USDC")
diff = abs(price_v2 - price_v3)
print(f"Price Difference: {round(diff, 2)} USDC")
if __name__ == "__main__":
compare_versions()
These examples can be expanded to build automated trading algorithms, liquidity management dashboards, and more.