How Getting Ethereum Account Balance and Transactions

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.

Order of steps

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

Get the WatchData API key to make calls to the endpoints

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.

Call the first endpoint: Account Balance

In this example, we will use the method eth_getBalance to get the balance of vitalik.eth account using WatchData API.

Example query:

{
    "jsonrpc": "2.0",
    "method": "eth_getBalance",
    "id": 1,
    "params": [
        "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
        "latest"
    ]
}

Response received:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": "0xbe25c909e01dbcf7be"
}

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.

Call the second endpoint: Transaction History

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:

{
    "id": 1,
    "jsonrpc": "2.0",
    "method": "watch_getTransfersByAddress",
    "params": [
        {
            "addresses": "0x2Ec9436c5fc5ecCEF819B388519B9171521E9C56",
            "contractAddresses": "native",
            "offset": 0,
            "limit": 100,
            "fromBlock": 0
        }
    ]
}

Let’s analyze the separate lines from the query in more detail to better understand how it works.

"contractAddresses": "native",

This line means that we want to get the result exactly in the native Eth token, not in other ERC-20 tokens.

"offset": 0,
"limit": 100

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:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": [
        {
            "blockNumber": 12279273,
            "blockTimestamp": "2021-04-20T20:37:59",
            "transactionHash": "0x73e52ca9ce843fab7026c3849fbcfa64489ea36f6f718ca77c45110ae7385b28",
            "transactionIndex": 91,
            "logIndex": null,
            "contractAddress": null,
            "transferType": "trx",
            "from": "0xd4b237ed855e152bb9c8e108821f72584bdbe8c1",
            "to": "0x2ec9436c5fc5eccef819b388519b9171521e9c56",
            "value": 150000000000000000,
            "fee": 4874100000000000,
            "asset": "ETH",
            "decimals": 18
        },
        {
            "blockNumber": 12279428,
            "blockTimestamp": "2021-04-20T21:12:52",
            "transactionHash": "0x7390dba880298062bb8b9a343b3792cc4d7b0e9637018ecf06a8bda663735171",
            "transactionIndex": 95,
            "logIndex": null,
            "contractAddress": null,
            "transferType": "trx",
            "from": "0x2ec9436c5fc5eccef819b388519b9171521e9c56",
            "to": "0xd4b237ed855e152bb9c8e108821f72584bdbe8c1",
            "value": 145968000000000000,
            "fee": 4032000000000000,
            "asset": "ETH",
            "decimals": 18
        },
        {
            "blockNumber": 12355870,
            "blockTimestamp": "2021-05-02T16:38:56",
            "transactionHash": "0xda196993775574d8c484a650a3c243242a7ec111062ec704d809521374f957f1",
            "transactionIndex": 121,
            "logIndex": null,
            "contractAddress": null,
            "transferType": "trx",
            "from": "0xd4b237ed855e152bb9c8e108821f72584bdbe8c1",
            "to": "0x2ec9436c5fc5eccef819b388519b9171521e9c56",
            "value": 20000000000000000,
            "fee": 882000000000000,
            "asset": "ETH",
            "decimals": 18
        },
        {
            "blockNumber": 12355876,
            "blockTimestamp": "2021-05-02T16:39:39",
            "transactionHash": "0xedfbd7001b8fc068daeff64e2020a4099489a7ed80efc33755bfc3ada3a43860",
            "transactionIndex": 199,
            "logIndex": null,
            "contractAddress": null,
            "transferType": "trx",
            "from": "0x2ec9436c5fc5eccef819b388519b9171521e9c56",
            "to": "0xa5409ec958c83c3f309868babaca7c86dcb077c1",
            "value": 0,
            "fee": 17892090000000000,
            "asset": "ETH",
            "decimals": 18
        },
        {
            "blockNumber": 12355879,
            "blockTimestamp": "2021-05-02T16:40:37",
            "transactionHash": "0x55e750b4e070b02e5ea6336081c291ef492f7c54b37ebd515d00c02bc233cbdc",
            "transactionIndex": 104,
            "logIndex": null,
            "contractAddress": null,
            "transferType": "trx",
            "from": "0x2ec9436c5fc5eccef819b388519b9171521e9c56",
            "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
            "value": 0,
            "fee": 2063198000000000,
            "asset": "ETH",
            "decimals": 18
        }
    ]
}

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.

Last updated