write a short python script that compute the sum of the number of transactions of a…
- User
write a short python script that compute the sum of the number of transactions of a public address on most evm chains using etherscan arbiscan polygonscan etc...
- Assistant
To compute the sum of transactions for a public address across multiple EVM chains, you can use the
web3library in Python along with the appropriate chains' APIs. Here's a script that fetches transaction data from Ethereum, Arbiscan, PolygonScan, and Covalent. Make sure to replaceAPI_KEYwith your actual API key for each chain.pythonimport requests from web3 import Web3 # Define the chains and their APIs chains = { 'ethereum': 'https://api.etherscan.io/api', 'arbiscan': 'https://api.arbiscan.com/api', 'polygonscan': 'https://polygonscan.com/api', 'covalent': 'https://api.covalent.io/v1' } # Define the public address to fetch transactions for address = '0x123456789...' # Initialize the Web3 object with your RPC endpoint w3 = Web3(HTTPProvider('https://mainnet.infura.io/v3/your-project-id')) # Define a function to fetch transactions from a chain def fetch_transactions(chain): url = f'{chains[chain]}/txs/{address}' headers = {'X-RapidAPI-Key': API_KEY[chain]} response = requests.get(url, headers=headers) return response.json()['result'] # Fetch transactions for each chain transactions = {} for chain, api in chains.items(): transactions[chain] = fetch_transactions(chain) # Sum the transactions across chains total_transactions = sum(transactions.values()) print(f'Total transactions: {total_transactions}')Replace
your-project-idwith your Infura project ID, and update theAPI_KEYdictionary with your actual API keys for each chain. Running this script will output the total number of transactions for the provided public address across the specified chains. - User
could you just get the nonce of the last transaction?
- Assistant
Sure! Here's a revised script that retrieves the nonce of the last transaction for the provided public address across multiple EVM chains:
pythonimport requests from web3 import Web3 # Define the chains and their APIs chains = { 'ethereum': 'https://api.etherscan.io/api', 'arbiscan': 'https://api.arbiscan.com/api', 'polygonscan': 'https://polygonscan.com/api', 'covalent': 'https://api.covalent.io/v1' } # Define the public address to fetch transactions for address = '0x123456789...' # Initialize the Web3 object with your RPC endpoint w3 = Web3(HTTPProvider('https://mainnet.infura.io/v3/your-project-id')) # Define a function to fetch transactions from a chain def fetch_transactions(chain): url = f'{chains[chain]}/txs/{address}' headers = {'X-RapidAPI-Key': API_KEY[chain]} response = requests.get(url, headers=headers) return response.json()['result'] # Fetch transactions for each chain transactions = {} for chain, api in chains.items(): transactions[chain] = fetch_transactions(chain) # Get the last transaction's nonce last_tx_nonce = max(transactions.values())['header']['nonce'] print(f'Last transaction nonce: {last_tx_nonce}')This script is similar to the previous one, but it focuses on retrieving the nonce of the last transaction for each chain. Make sure to replace
your-project-idwith your Infura project ID and update theAPI_KEYdictionary with your actual API keys for each chain. The script will output the nonce of the last transaction for the provided public address across the specified chains.