030ffice-logoZwart4

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:

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 client

from ethers import Web3


Set your account's private key

account = "0x123456789012345678901234567890abcdef"


Set the Etherscan API URL

api_url = "


Create a new Web3 instance

w3 = Web3(Web3.HTTPProvider(api_url))


Get your Ethereum account balance

balance = w3.eth.getBalance(account)


Print the balance

print(f"Account Balance: {balance}")


Sign a transaction using the API MetaMask

tx_hash = "0x123456789012345678901234567890abcdef"

signer = w3.eth.account.signTx(tx_hash, account, {"gasPrice": 20.5})


Printing the signature

print(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 response

tx_details = w3.eth.getTransaction(tx_hash).decode("utf-8")


Parsing Etherscan data into a Python dictionary

data = json.loads(tx_details)


Print the parsed data

print(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 client

from ethers import Web3


Set the private key your account

account = "0x123456789012345678901234567890abcdef"


Set the Etherscan API URL

api_url = "


Create a new Web3 instance

w3 = Web3(Web3.HTTPProvider(api_url))


Get your Ethereum account balance

balance = w3.eth.getBalance(account)


Sign a transaction using the MetaMask API

tx_hash = "0x123456789012345678901234567890abcdef"

signer = w3.eth.account.signTx(tx_hash, account, {"gasPrice": 20.5})


Print the signature

print(signer)


Get transaction details from the MetaMask API response

tx_details = w3.eth.getTransaction(tx_hash).decode("utf-8")


Parse Etherscan data into a Python dictionary

data = json.loads(tx_details)


Print the parsed data

print(data)

Note that this code assumes you have replaced YOUR_PROJECT_ID with your current Infura project ID.

solana verify

Leave a Reply

Your email address will not be published. Required fields are marked *