How To Get Historical Transactions On Polygon?
๐Ÿงป

How To Get Historical Transactions On Polygon?

Created
Jun 14, 2022
Tags
Ethereum
Updated
Jun 14, 2022
Ethereum Layer 2 scaling solutions, like Polygon, have allowed developers to take advantage of low transaction costs and far faster confirmation times. However, this introduces a new challenge for devs: how to process and store large swaths of blockchain data. Traditionally, developers had to spin up, manage, and index across their own nodes to build databases. This left developers constrained by an expensive and slow solution that ultimately limits their appsโ€™ feature sets regardless of whether they are deployed on Layer 1 or Layer 2 solutions.
Code snippet:
import json from web3 import Web3 import requests try: ALCHEMY_KEY = "Rt4_MeHEE8mQWVi-uSppHNSDmOG" except: print("Please insert your Alchemy API Key!") w3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.g.alchemy.com/v2/"+ALCHEMY_KEY)) # includes the standard ERC20 ABI info ERC20_ABI = json.loads('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],......{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]') # noqa: 501 # configures web3 to point towards the USDT token address # make sure that these addresses are Ethereum-checksummed! (use this tool: https://ethsum.netlify.app/ to make sure) USDT_ADDRESS = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F' FROM_ADDRESS = '0x5350E1068f0E138ff306990B16fA4910d970c692' TO_ADDRESS = '0x9d2b758E3ffd2569c6956676fAE7f8B71A53Ffb5' from_Block = '0x16C5376' to_Block = '0x16C537A' usdt = w3.eth.contract(address=USDT_ADDRESS, abi=ERC20_ABI) transfer_json = requests.post('https://polygon-mainnet.g.alchemy.com/v2/'+ALCHEMY_KEY, json={"jsonrpc": "2.0","id": 0,"method": "alchemy_getAssetTransfers","params": [{"fromBlock": from_Block,"toBlock": to_Block,"fromAddress": FROM_ADDRESS,"toAddress": TO_ADDRESS,"contractAddresses": [USDT_ADDRESS],"category": ["erc20"]}]}) json_response = transfer_json.json() #print(json_response) transfer_val = (json_response['result']['transfers'][0]['rawContract']['value']) # convert hexadecimal make to decimal format val = (int(transfer_val, 16)) print("USDT TRANSFERED:") print("--> FROM: " + FROM_ADDRESS) print("--> TO: " + TO_ADDRESS) # unit conversion! print(val/1000000)
Read more here: