Last active
August 11, 2022 18:29
-
-
Save ljlabs/714d6cc27aacdf2809a7fb19b2f79565 to your computer and use it in GitHub Desktop.
This script will print the price of ETH in USDC on the Sushi Swap DEX on the polygon network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from web3 import Web3 | |
infura_url = 'https://polygon-rpc.com' | |
web3 = Web3(Web3.HTTPProvider(infura_url)) | |
abi = [{"constant": True, "inputs": [], "name": "getReserves", | |
"outputs": [{"internalType": "uint112", "name": "_reserve0", | |
"type": "uint112"}, | |
{"internalType": "uint112", "name": "_reserve1", | |
"type": "uint112"}, {"internalType": "uint32", | |
"name": "_blockTimestampLast", | |
"type": "uint32"}], | |
"payable": False, "stateMutability": "view", | |
"type": "function"}] | |
pool_address = Web3.toChecksumAddress('0x34965ba0ac2451a34a0471f04cca3f990b8dea27') | |
liquidity_pool_contract = web3.eth.contract(address=pool_address, abi=abi) | |
# Prices | |
reserves = liquidity_pool_contract.functions.getReserves().call() | |
usdc = reserves[0] * 10e-6 # according to https://polygonscan.com/address/0x2791bca1f2de4661ed88a30c99a7a9449aa84174#readProxyContract | |
wraped_eth = reserves[1] * 10e-18 # according to https://polygonscan.com/address/0x7ceb23fd6bc0add59e62ac25578270cff1b9f619#readContract | |
print(usdc / wraped_eth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line 23 - 24 I supported the decimals that each token is using, to get an accurate token price to be displayed.
I got the number of decimal places for each token in the comments to the right, this used polygon scan to read the contracts, and tell me the number of decimal places being used.