Featured image: Neon handshake. Source Unsplash.

Welcome!

Within this tutorial, you will learn how to retrieve and analyze data from the Ethereum Blockchain with the help of the anyblock.tools API.

You will learn

  • How to access anyblock.tools
  • How to use different methods to query the anyblock.tools API
  • How to write a basic query returning some events
  • How a return object is structured and which data it returns
  • How to filter events for a specific contract or a specific event type
  • How to sort the events by block number

What you must know already

This tutorial is written for programmers, who have some experience with JSON, Rest-APIs, and the basic structure of HTTP-requests.

What you need

If you want to play around with the HTTP-requests, you should install an HTTP client. The good ol’ terminal users might use cURL. For advanced usage and a graphical UI, we recommend using Postman. We provide copy-pasteable commands for cURL throughout the tutorial, so if you want to follow along, it is advisable to install the software first.

Create an anyblock.tools query step-by-step

Retrieve all events indexed by anyblock.tools

On the anyblock.tools endpoint ethereum/ethereum/mainnet/es/event/search, you can query all events from the Ethereum mainnet.

curl -X POST \
https://api.anyblock.tools/ethereum/ethereum/mainnet/es/event/search/ \
-H' Authorization: Bearer $mytoken' \
-H' Content-Type: application/json'
"hits":{
"total":69502921,
"max_score":1,
"hits":[
...
]
}
{
"_index":"ethereum_2",
"_type":"event",
"_id":"0x92c1b864051b9e6758ab217bc70e0d8641d5f830e16b0a7d15ba78ef2356ba9c_e_52",
"_score":1,
"_routing":"0x251d33d4ab03fb675bb2d09304a4aca28b943373c0bd8dbc85402d9e23f4f061",
"_parent":"0x92c1b864051b9e6758ab217bc70e0d8641d5f830e16b0a7d15ba78ef2356ba9c",
"_source":{
"args":[
{
"name":"hash",
"value.hex":"b'eb8dd23ef00be18cb4a263b4271e2f9c28bb47a239f179001691f6e887a6ed47'",
"value.num":null,
"value.scaled":null,
"value.type":"bytes32",
"pos":0
},
{
"name":"registrationDate",
"value.hex":"0x59948642",
"value.num":1502905922,
"value.type":"uint256",
"pos":1,
"value.scaled":null
}
],
"event":"AuctionStarted",
"logIndex":{
"num":52,
"raw":"0x34"
},
"transactionIndex":{
"num":92,
"raw":"0x5c"
},
"transactionHash":"0x92c1b864051b9e6758ab217bc70e0d8641d5f830e16b0a7d15ba78ef2356ba9c",
"address":"0x6090a6e47849629b7245dfa1ca21d94cd15878ef",
"blockHash":"0x251d33d4ab03fb675bb2d09304a4aca28b943373c0bd8dbc85402d9e23f4f061",
"blockNumber":{
"num":4145267,
"raw":"0x3f4073"
},
"error":null,
"str":"AuctionStarted(b\"\\xeb\\x8d\\xd2>\\xf0\\x0b\\xe1\\x8c\\xb4\\xa2c\\xb4'\\x1e/\\x9c(\\xbbG\\xa29\\xf1y\\x00\\x16\\x91\\xf6\\xe8\\x87\\xa6\\xedG\", 1502905922)",
"timestamp":"2017-08-11T17:52:02"
}
}
  • "blockNumber" – the block, where it was omitted
  • "timestamp" – approximate timestamp, when it was included in the blockchain

Filter events from a specific contract

You are probably interested in filtering for events that belong to a specific smart contract.

{
"query":{
"bool":{
"filter":{
"term":{
"address":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
}
}
}
}
}
curl -X POST \
https://api.anyblock.tools/ethereum/ethereum/mainnet/es/event/search/ \
-H' Authorization: Bearer d2560f14-1935-44e7-ad3e-a1718dc03bd2' \
-H' Content-Type: application/json'
-d '{
"query":{
"bool":{
"filter":
{
"term":{
"address":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
}
}
}
}
}'

Filter for a specific type of event

Now every event under the hits.hits keyword originates from the contract of interest. but there are still different types of events present in the queries result.

contractERC20Events{ eventApproval(addressindexedsrc,addressindexedguy,uintwad); eventTransfer(addressindexedsrc,addressindexeddst,uintwad); }
{ "query":{ "bool":{ "filter":[ { "term":{ "event.raw":"Transfer" } }, { "term":{ "address":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359" } } ] } } }
curl -X POST \
https://api.anyblock.tools/ethereum/ethereum/mainnet/es/event/search/ \
-H 'Authorization: Bearer d2560f14-1935-44e7-ad3e-a1718dc03bd2' \
-H 'Content-Type: application/json'
-d '{
"query":{
"bool":{
"filter":[
{
"term":{
"event.raw":"Transfer"
}
},
{
"term":{
"address":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
}
}
]
}
}
}'

Retrieving sorted results

You may notice that the "timestamp" of the events is outdated and that they are not sorted by their "blockNumber".

{ "query":{ "bool":{ "filter":[ { "term":{ "event.raw":"Transfer" } }, { "term":{ "address":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359" } } ] } }, "sort":{ "blockNumber.num":{ "order":"desc" } }, "size":5 }
curl -X POST \
https://api.anyblock.tools/ethereum/ethereum/mainnet/es/event/search/ \
-H 'Authorization: Bearer d2560f14-1935-44e7-ad3e-a1718dc03bd2' \
-H 'Content-Type: application/json'
-d '{
"query":{
"bool":{
"filter":[
{
"term":{
"event.raw":"Transfer"
}
},
{
"term":{
"address":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
}
}
]
}
},
"sort":{
"blockNumber.num":{
"order":"desc"
}
},
"size":5
}'

Restricting result size

In the last query, we specified the "size" parameter with a value of 5. This will limit the number of retrieved events to 5. For testing queries, it is advisable to set this to a small number.

Where to go from here

The best starting point is the Elasticsearch documentation. There you’ll learn how to construct more complex filter queries or how to combine filters with boolean logic.

Sascha Göbel — Co-Founder of Anyblock

Interested or questions?

Sascha Göbel
(Co-Founder)
sascha@anyblockanalytics.com
+49 6131 3272372

Anyblock Analytics is a German blockchain solution provider. We offer consulting, tools and data to integrate business processes with blockchain.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store