Call a uniswap function using Web3.py in Python

Uniswap is a decentralized exchange running on the Ethereum blockchain. It is a set of Solidity smart contracts that operate together to create a trading exchange. This exchange has no operators, no administrators and is currently executing more trades per day then many large centralized exchanges.

Lets use Web3.py in Python to call one of these contracts to obtain data from Uniswap. The Python code below uses Web3.py and calls the Uniswap contract on Ethereum to obtain:

  • a count of all the trading pairs on uniswap
  • a list of all token trading pair addresses that are used for pricing

Since we are reading data from the blockchain there is no gas fee.

import json
from web3 import Web3

#this program queries for all of the uniswap pair addresses and their token supply

infura_url = 'INSERTYOURINFRAURLHERE'
web3 = Web3(Web3.HTTPProvider(infura_url))

# uniswap_Factory
factory_abi = json.loads('[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"","type":"uint256"}],"name":"PairCreated","type":"event"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feeTo","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"getPair","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeTo","type":"address"}],"name":"setFeeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')
factory_address = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'
factory_contract = web3.eth.contract(address=factory_address, abi=factory_abi)


#returns a count of all the trading pairs on uniswap
allPairsLength = factory_contract.functions.allPairsLength().call()
print(allPairsLength)

# get the uniswap pair address for pricing

The above code returns a count of trading pairs on uniswap. Now run the code below to get a list of all the token pair addresses used for pricing a token pair and their total supply. Use the total count of trading pairs that printed in the code above and type it in the “for” statement parameter below.

for i in range(1, PUTTHECOUNTOFTRADINGPAIRSHERE):
    allPairs_address = factory_contract.functions.allPairs(i).call()
    contract = web3.eth.contract(address=allPairs_address, abi=pairs_abi)
    symbol = contract.functions.name().call()
    supply = contract.functions.totalSupply().call()
    print(allPairs_address, supply)

Next – Get market data from uniswap using Web3.py in Python

Ledger Nano X - The secure hardware wallet

Leave a Reply