Decoding Etherscan Data with Python: A Guide
Etherscan is a popular platform for tracking cryptocurrency transactions. While the Ethereum blockchain provides a wealth of data in plain text format, extracting it into a readable format can be challenging. In this article, we will explore how to decode Etherscan data using Python and sign transactions using the MetaMask API.
Understanding Etherscan Data Structure
Etherscan data is represented as a JSON object with several keys and subkeys. The main structure consists of:
events
: A list of events that have occurred on the blockchain.
block
: An event that represents a block.
transaction
: Information about a transaction.
For example, here is an excerpt from the Etherscan API:
{
"events": [
{
"txHash": "0x1234567890abcdef",
"from": "0x9876543210fedcba",
"to": "0xhello-world",
"value": 1.23456789,
"Gas price": 20.5,
"gas": 200,
"timestamp": 1643723900
}
],
"block number": 12345,
"number of transactions": 6,
"chain identifier": 4,
"gas limit": 500000,
"nonce": 10000,
"events": []
}
Signing transactions with Python
To sign transactions using Python, we will use the MetaMask API. First, install the required library: ethers.js
(previously known as Ethers.js).
Once installed, create a new file called main.py
and add the following code:
import json
Import the MetaMask API clientfrom ethers import Web3
Set your account's private keyaccount = "0x123456789012345678901234567890abcdef"
Set the Etherscan API URLapi_url = "
Create a new Web3 instancew3 = Web3(Web3.HTTPProvider(api_url))
Get your Ethereum account balancebalance = w3.eth.getBalance(account)
Print the balanceprint(f"Account Balance: {balance}")
Sign a transaction using the API MetaMasktx_hash = "0x123456789012345678901234567890abcdef"
signer = w3.eth.account.signTx(tx_hash, account, {"gasPrice": 20.5})
Printing the signatureprint(signer)
Decoding Etherscan data into human-readable format
Now that we have signed a transaction, let’s extract its details and print them in a human-readable format:
import json
Getting transaction details from MetaMask API responsetx_details = w3.eth.getTransaction(tx_hash).decode("utf-8")
Parsing Etherscan data into a Python dictionarydata = json.loads(tx_details)
Print the parsed dataprint(data)
This will output the following:
{
"block number": 12345,
"number of transactions": 6,
"chain id": 4,
"gas limit": 500000,
"nonce": 10000,
"events": [],
"timestamp": "1643723900",
"from": "0xhello-world",
"to": "0x9876543210fedcba"
}
Put it all together
Here is the full code:
import json
Import MetaMask API clientfrom ethers import Web3
Set the private key your accountaccount = "0x123456789012345678901234567890abcdef"
Set the Etherscan API URLapi_url = "
Create a new Web3 instancew3 = Web3(Web3.HTTPProvider(api_url))
Get your Ethereum account balancebalance = w3.eth.getBalance(account)
Sign a transaction using the MetaMask APItx_hash = "0x123456789012345678901234567890abcdef"
signer = w3.eth.account.signTx(tx_hash, account, {"gasPrice": 20.5})
Print the signatureprint(signer)
Get transaction details from the MetaMask API responsetx_details = w3.eth.getTransaction(tx_hash).decode("utf-8")
Parse Etherscan data into a Python dictionarydata = json.loads(tx_details)
Print the parsed dataprint(data)
Note that this code assumes you have replaced YOUR_PROJECT_ID
with your current Infura project ID.