How Getting Ethereum Account Balance and Transactions
Last updated
Last updated
Weβre going to take apart two sets of data used in creating an Ethereum wallet.
Current Balance β The last Eth amount for the account.
Address transactions β Historical Eth transactions for the account.
What superpowers this gives you:
Instantly view the balance of any address in Ethereum without third-party involvement.
No need to run or sync a node or client.
Integrate into the dApp you create without any fuss.
1. Get the WatchData API key to make calls to the endpoints 2. Call the first endpoint: Account Balance 3. Call the second endpoint: Transaction History
To create an API key, you need to do the following steps: On the Dashboard.watchdata.io tab, click Create API key.
Your API key will be automatically created.
In the example, weβll show instruction for sending queries via Postman. In addition to Postman, you can use data from the WatchData API in any application or Web3 SDKβs, using post and curl queries.
First, after you get the API key you have to connect to the blockchain.
For this you can copy your API key, or copy the HTTP Link at once, which will speed up your work several times! The URL of the previously selected chain + your API Key is automatically substituted in the HTTP Link.
Go to Postman and create a new request.
The request type is POST, in the Request url field enter the address specified above. All you need to do is insert the previously generated link
Now everything is ready to send a request and receive data from the API.
In this example, we will use the method eth_getBalance to get the balance of vitalik.eth account using WatchData API.
Example query:
Response received:
In the answer we are interested in the line βresultβ: β0xbe25c909e01dbcf7beβ which gave us Vitalikβs account balance in hexadecimal notation. In order to decode this value into the usual Ethereum balance, we need to perform a couple of simple actions.
First you need to convert hex to decimal. You can do it here.
After we converted to decimals we see the account balance in Wei. In order to convert Wei to Eth letβs divide this number by 10ΒΉβΈ.
Done. Letβs compare the result with the balance on Etherscan and make sure the data is correct.
In this example we will not use Vitalikβs wallet address, otherwise we will get a response to the query that takes very many lines. For the example we will take a random address 0x2Ec9436c5fc5ecCEF819B388519B9171521E9C56 with little transaction history. We will use the watch_getTransfersByAddress method to get the transaction history.
Example query:
Letβs analyze the separate lines from the query in more detail to better understand how it works.
This line means that we want to get the result exactly in the native Eth token, not in other ERC-20 tokens.
If a LIMIT number is specified, the result returns no more than the specified number of lines (less may be if the query itself produced fewer lines). LIMIT ALL equals no LIMIT instruction. OFFSET specifies to skip the specified number of lines before starting to output lines.
Received response with a history of 5 transactions:
Great :) Now you know how to get current balance in Eth and transaction history from blockchain. In further articles we will consider additional methods used when creating a crypto wallet.