Featured image: Coding SQL Query. Source Unsplash.

Welcome!

What you must know already

Basic anyblock.tools SQL queries

<technology>_<chain>_<network></network></chain></technology>

Find the latest block

SELECT * FROM block ORDER BY number DESC LIMIT 1
SELECT max(number) FROM block

Find events for a given block

SELECT * FROM event
WHERE event.block_number=7075271

Find calls for a transaction hash

SELECT * FROM call
WHERE call.hash = '0xadd837afa5b68987eb9f0167ad65cbb8131f57da84db56a19acf4a5a98bd35da'

Find transactions for a given contract

SELECT * FROM tx
WHERE tx.to='0xB97048628DB6B661D4C2aA833e95Dbe1A905B280'
LIMIT 100

Find specific events for a given contract

SELECT arg->'scaled', arg->'num'
FROM "event",jsonb_array_elements(args) arg
WHERE event = 'Transfer' AND address = '0xB97048628DB6B661D4C2aA833e95Dbe1A905B280'
AND (arg->'num')::numeric > 1000000000000000000
LIMIT 100

Find the latest 10 DAI Transfer events and extract sender, receiver, and value from JSON

SELECT
*,
args->0->>'hex' as "from",
args->1->>'hex' as "to",
CAST(args->2->'scaled' AS NUMERIC) AS "value"
FROM event
WHERE address = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359'
AND event = 'Transfer'
ORDER BY timestamp DESC
LIMIT 10

Where to go from here

Sascha Göbel — Co-Founder of Anyblock

Interested or questions?

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