Ethereum: convert a chain into an indexed list to Python
As mentioned, the use of the “Requests” library is not enough for this task. You will have to manually analyze the JSON response of Ethereum’s API. Here is an article that goes through how to achieve this:
Premise
Before starting, be sure to have the following installed:
- Python 3.x
Ethers.py
library (available in pypi)
- A bitcoin wallet or a test account with an Ethereum address
Install the required library
If you still don’t have Ethers.py
, install it with PIP:
`Bash
Pip Install Ethers.py
Conversion of a chain to an indexed list
Here is a step by step guide on how to convert a chain that represents a hexadecimal value into an indexed list (for example, a list of whole numbers) in Python:
- Obtain the URL API Ethereum
: You will need to obtain the [Ethereum Labs] ( Replace
your_pi_Key
with your real key:
`Python
Import requests
API_Key = "Your_api_Key"
API_URL = F " & from = 0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000
- Send a get Get Api : Use the
applications to send a Geta API to Ethereum:
Python
Import requests
URL = API_URL
ANSWER = REQUESTS.GET (URL)
- Analyze the answer JSON : The API returns a JSON object, which we will have to analyze in a Python dictionary using the
JSON ()
::
`Python
Data = Json.Loads (Answer.text)
- Remove the number of transactions : We are interested in the number of transactions that can be performed in our specific address (0x …). Remove this value from the JSON answer:
`Python
transaction_count = data ['result'] [0] ['number']
- Turn an indexed list : Convert the hexadecimal chain (
" 0x ... "
) into a complete number, then to a list of whole numbers usingMap ()
function: function:
`Python
indexed_list = [int (hex_value) for hex_value in data ['result'] [0] ['hex']]
Put everything together
Here is the complete code:
`Python
Import requests
Import Json
API_Key = "Your_api_Key"
URL = f " & FROM = 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
ANSWER = REQUESTS.GET (URL)
Data = Json.Loads (Answer.text)
transaction_count = data ['result'] [0] ['number']
indexed_list = [int (hex_value) for hex_value in data ['result'] [0] ['hex']]
print (indexed_list)
Example of use of cases
Suppose you have the next hexadecimal chain that represents an Ethereum address:
`Bash
0x1234567890ABCDEF
You can use this chain to turn it into an indexed list as:
`Python
indexed_list = [int ("0x" + hex_value) for hex_value in data ['result'] [0] ['hex']]]]]
print (indexed_list)
Exit:
`
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
`
In this example, the hexadecimal chain becomes an integer, then a list of entire numbers that represent the index values.