uniswap_v2.pair

Documentation for eth_defi.uniswap_v2.pair Python module.

Uniswap v2 pair info.

  • See PairDetails for a helper class to get price and other data from the trading pairs of :Uniswap v2 like DEXes

Functions

fetch_pair_details(web3, pair_contact_address)

Get pair info for PancakeSwap, others.

Classes

PairDetails

Uniswap v2 trading pair info.

class PairDetails

Bases: object

Uniswap v2 trading pair info.

An example usage how to get the latest price of a pair on PancakeSwap. The PairDetails class will do an automatic conversion of prices to human-readable, decimal format:

from web3 import Web3, HTTPProvider

from eth_defi.chain import install_chain_middleware
from eth_defi.uniswap_v2.pair import fetch_pair_details

web3 = Web3(HTTPProvider("https://bsc-dataseed.bnbchain.org"))

print(f"Connected to chain {web3.eth.chain_id}")

# BNB Chain does its own things
install_chain_middleware(web3)

# Find pair addresses on TradingStrategy.ai
# https://tradingstrategy.ai/trading-view/binance/pancakeswap-v2/bnb-busd
pair_address = "0x58f876857a02d6762e0101bb5c46a8c1ed44dc16"
wbnb = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
busd = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56"

# PancakeSwap has this low level encoded token0/token1 as BNB/BUSD
# in human-readable token order
# and we do not need to swap around
reverse_token_order = False

pair = fetch_pair_details(
    web3,
    pair_address,
    reverse_token_order,
)

assert pair.token0.address == wbnb
assert pair.token1.address == busd

price = pair.get_current_mid_price()

# Assume 1 BUSD = 1 USD
print(f"The current price of PancakeSwap BNB/BUSD is {price:.4f} USD")
contract: web3.contract.contract.Contract

Pool contract

https://docs.uniswap.org/contracts/v2/reference/smart-contracts/pair#getreserves

token0: eth_defi.token.TokenDetails

One pair of tokens

token1: eth_defi.token.TokenDetails

One pair of tokens

reverse_token_order: Optional[bool]

Store the human readable token order on this data.

If false then pair reads as token0 symbol (WETH) - token1 symbol (USDC).

If true then pair reads as token1 symbol (USDC) - token0 symbol (WETH).

property address: eth_typing.evm.HexAddress

Get pair contract address

property checksum_free_address: str

Get pair contract address, all lowercase.

get_base_token()

Get human-ordered base token.

Return type

eth_defi.token.TokenDetails

get_quote_token()

Get human-ordered quote token.

Return type

eth_defi.token.TokenDetails

convert_price_to_human(reserve0, reserve1, reverse_token_order=None)

Convert the price obtained through Sync event

Parameters
  • reverse_token_order

    Decide token order for human (base, quote token) order. If set, assume quote token is token0.

    IF set to None, use value from the data.

  • reserve0 (int) –

  • reserve1 (int) –

get_current_mid_price()

Return the price in this pool.

Calls getReserves() over JSON-RPC and calculate the current price basede on the pair reserves.

See https://docs.uniswap.org/contracts/v2/reference/smart-contracts/pair#getreserves

Returns

Quote token / base token price in human digestible form

Return type

decimal.Decimal

__init__(contract, token0, token1, reverse_token_order=None)
Parameters
Return type

None

fetch_pair_details(web3, pair_contact_address, reverse_token_order=None, base_token_address=None, quote_token_address=None)

Get pair info for PancakeSwap, others.

See also PairDetails.

Parameters
  • web3 – Web3 instance

  • pair_contact_address (Union[str, eth_typing.evm.HexAddress]) – Smart contract address of trading pair

  • reverse_token_order (Optional[bool]) –

    Set the human readable token order.

    See :py:class`PairDetails` for more info.

  • base_token_address (Optional[str]) – Automatically determine token order from addresses.

  • quote_token_address (Optional[str]) – Automatically determine token order from addresses.

Return type

eth_defi.uniswap_v2.pair.PairDetails