Indexing blockchain data fast
ETHCC 7 · 11 Jul 2024 · 44:59
Jason Smythe presents Envio's tools for indexing blockchain data quickly. He says its HyperIndex indexer processed the Uniswap ETH-USDC pool in 9 minutes 40 seconds, which he puts at 104 times faster than comparison tools. The speed comes from HyperSync, a purpose-built query layer written in Rust that stores data in Parquet column files, instead of relying on standard JSON-RPC calls to nodes. He also covers HyperRPC and demos Python scripts that scan logs from block zero and compute USDC transfer volume. In Q&A he explains that Envio ingests chains through RPC endpoints and runs its own nodes for popular chains.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Hello everyone. I am really excited to show you how to index um events really fast uh using some tools that I've been building for the last little while. I definitely do not need to join that meeting. Um so yeah, I've been building Envio for a while and one of the big um focuses of of Envio is making it really fast. So this is a little um sneak peek into a benchmark that we did of um hyper index, which is one of the products um that we've been building in our set of tools and um our data stack. Um and yeah, a little bit about me, uh I seem to always wear colorful shirts, that's a thing.
You can see my little avatar in the corner, that's um from a previous project, but I still think it's fun and quite represent uh representative of me. Um I'm from Cape Town, um and I love uh yeah, doing things outdoors and hiking, etc. But uh most importantly, I love building really high quality tools. Um so yeah, uh the team is a very uh dev-heavy team, so it's by builders uh for builders, and um we got to the point of creating an indexer out of our own frustrations. Um we've been uh very heavy users of the graph for many years for uh through multiple projects and previous previous startups.
Um and the previous project was called Float Capital. Um so this is the very old uh UI that we had that was running on the graph, and um our second version. In fact, I'm lying. This is the version of the UI that we um had running on our very own indexer. Um and this indexer slowly evolved into Envio, and um we put a pause to this DeFi protocol, and uh we're now focusing full time on um on Envio. Um so yeah, once again, um coming back to this picture to index the Uniswap ETHUSDC pool.
Um, our indexer took uh nine minutes and 40 seconds, and you can see the time of uh two other tools as a comparison. Uh so we're 104 times faster. So for comparison uh for floats uh due to the number of events we had, um our indexer on the graph took about a week to sync, and um when we used to move to our own system, it would take 1.6 hours, which is a massive speed up. Um we have stickers, by the way.
These are our stickers. I don't know how many I have here. Um rushing too much in the morning, but uh we've got great stickers and big dev energy. Um so I've spoken about um why we need it and how much faster it is, but how do we actually do it? What's the secret source behind it? Um traditionally a lot of indexing has been built on uh the RPC. Um so RPC methods get sent to a blockchain node, they process it and return back the data. Um but this is quite slow.
There's um the network and bandwidth costs. Um RPC is a um a JSON um ETH RPC is is JSON format, so you're sending over big strings and you sending through a lot of queries. Um the other thing is um blockchain nodes are designed for execution. Um they're not designed for fast querying. Um obviously there's a lot of improvements people are trying to make to make different nodes faster for querying, but they still uh they won't beat something that's purpose built for being faster query, and that's something that we've built. So that's hypersync.
Um it's yeah, I I've written mega fast, mega flexible, but I would say it's hyper fast, hyperflexible. And um this is some of the technologies that we've uh used. Uh it's built in Rust. Uh we've had a massive focus on doing uh every optimization we can possibly think of. Um there's a lot of fun rabbit holes we can chat about if you come find me afterwards. Um and then we store all the data in parquet files in buckets, and we have uh indexes on all of those buckets. So you can scan the entire chain from block zero to current extremely fast.
You can find a needle in a haystack, proverbally. So yeah, and it's a non-consensus client, so we don't worry about all of those other problems. The only thing that our node cares about is fetching data, retrieving data fast and filtering it. Yeah, so a little bit behind under the under the hood, we use Parquet, which are which is a column la format file storage layout, and that allows us to filter and process through columns really fast because we can load an entire column in one chunk of memory, whereas uh row-oriented databases like Postgres, you have to load the entire row so you'd go through through that whole row just to get the the number. Um we um also use uh Rust.
Uh we also use Ref, but more specifically, it's more to illustrate how fast Rust really is. Um there's a lot of good use cases for it, and hypersync is the engine behind hyperindex. Um hypersync is an API. Um hypersync only serves raw data, but users typically care about process data, they care about a data that's been aggregated, filtered, and enriched, um, and we've built a really convenient tool called hyperindex to do that. And hyperindex is the tool that I was showing in those benchmarks against the graph.
If you're familiar with using any of those other tools that we are comparing with, you'll find it really easy to use hyperindex. We have handler functions that take in the events, and then you can load things from the database and save things to the database and keep your stata up to date. And then we have another project which is really interesting called Hyper RPC. So it's basically building RPC as much of the RPC interface as we can on top of the our high performance query nodes. So most notably the ETH get logs method is extremely high performant.
You can do a ETH get logs query from block zero till current block, which is quite insane. Obviously, if there's billions of logs that that uh fetches, you'd have to split that up. Um but a lot of existing um RPC providers have between 500 or maybe 2000 on the very high-end block ranges. Um here's a few um uh queries that uh my uh co-founder has done, and uh, you know, I suggest you follow him on on Twitter because he'll keep publishing new ones, but I'll go through some specific examples, and here are some uh some of the feedback that we've got. Um so yeah, please give us a try.
And without further ado, let's go into demo mode. Um I'm gonna first demo that's bigger than I thought it would be. Um I'm gonna first demo this. Uh yeah, sorry, wrong tab. Um yeah, let's open VS Code so I can scroll around. Um I'm gonna first step through some very simple hyper sync um scripts.
So this is a simple Python script using our Python library. We have um JavaScript, Rust, and Golang libraries. Also, I'm gonna show uh just Python and JavaScript at the moment for accessibility. And um here you can see you specify the URL to get to hypersync over here. And um here we've specified um it's the eigenlayer slasher, but you can specify any address, and uh this query will get all of the logs from that address from block zero to uh 90 million. Um so yeah, we can quickly uh run that.
Let's run it here. Um Python, and you'll see how quickly it actually runs through all of the all of the logs. So it um it only returned nine logs uh but it scanned the entire chain, so that's really powerful, and then we'll go through some examples where we scan uh many logs and do that also fast. So it makes both scanning for a small number of logs and a large number of logs really fast.
Um the next example which uh actually shows you the shape of a query. Um I'll show you here. Let's do um USDC transfer volume. So this script has uh sorry two parts. Uh the first part collects all of the data we care about, and then the second script um processes that data and it saves the file in parquet. Um I'll I'll share the link to get this code so you can run it locally.
It does take a few minutes, um, and I don't want to use all the time on just this this demo. Um but here you can see uh you specify the USDC address, uh you specify the transfer topic, and you select what you want to receive back. So we're getting the block number from the transaction and all of the data related to the events. And then over here we actually create the streaming config.
So we stream the data, and over here we write the file the parquet files of all the data in this folder. So if you look here, I've got a parquet viewing plugin, which is really convenient. And you can see here's all the data. And what was the other one I wanted to do? Yeah, so this is also a really nice example.
In JavaScript doing something very similar, you can see we've got the address, we've got the topics and the log selection here, and we collect the results into parquet. And what's really nice is with parquet, you can use various tools to process the data. You can use Python or almost any language, but here I'm literally just doing it in the in the in this plugin. You can write SQL statements using DuckDB.
So if I increase that a little bit, oops, then uh I'm filtering to only see transfers where the value is um below a certain amount. Obviously, you can use scripts for this. Um but yeah, let's remove that, and here we have all the data. Okay, um now let's move on to another uh use case over here. Um I've got this um GitHub repo called EVM address event summary.
Um sorry EBM yeah, this one. So here's the here's the page if you want to give it a give it a clone, and um this repo has two folders in it. Um we've got the um all the transfers, so this looks at ERC20 uh transfers and eth transfers, and this folder looks at NFT transfers. Um if we go into the scripts, it is a little bit more complicated, um but it's still easy to follow.
First thing the user does is they provide an Ethereum address that they want to um uh to filter, which is the address of of uh NFT contract, and then um here's the query, we get the um we get all events where the address matches and the topic matches for transfers, and we um we iterate through that. Um basically we stop iterating when we stop receiving data, and this um for example is able to process and retrieve all of the um the transfers that occur for crypto kiddies, which is uh a nostalgic project for me.
Um so yeah, let's do the let's do the demo. Um let's grab it. So yeah, this beginning part because Crypto Kitties actually uh didn't use the standard ERC20 format for their events, um, so they didn't index both fields, which is fine because our tool is able to handle that. So I'll click enter, and you'll see it whizzing past.
I'll move on because it does take about three minutes. Um scan through and process and save all the owners of every um crypto kiddy, and there's um about 5.7 million crypto kiddie transfers in total, so it's uh blisteringly fast, is I guess the only way I could say it. Um so it basically populates this um this object. We have the address, a set of all the token IDs that that user owns, and the number of transfers into this address and transfers out.
Um if you look at the important logic, here it is. Um it removes it from it removes it from the person who's sending the token, so they no longer have that token. It adds it to the token that's receiving it, and then we just increment the the transfer count. Um so yeah, honestly, super simple. Um these four lines are basically the crux of what this uh this little script is doing.
Um while this is chugging along, um I'll show you another script which includes a really cool feature that we have, which is to get traces. Uh so I've only been showing you logs, uh transactions and blocks. Um traces basically um define the whole call stack. So you can now get internal ETH transfers, which is sometimes uh challenging. So to do that, um we specify specify slightly bigger queries.
So we're starting from block zero, so we're getting every single uh transfer, and um here we've got the uh transfer event topic for ERC20, and here we've got all traces to and from an address. So if someone receives a payment in an internal transaction, we can capture that, and we're also getting the transactions, and the things we care about are the gas price. So we grab the effective gas price and the gas used by the transaction.
We get all the data that we need from the ERC20 token, and we get the value that was transferred by any call, whether it's internal or external. And yeah, we stream the data just like we're doing here. This is almost near the end. Um just be warned it'll um spit out a large amount of text because it's uh literally every um every uh token in existence. Um so yeah, let's let's run this in the meantime.
Um yeah, this was an interesting address. I it's just a random address from uh from Etherscan, but it it has quite a number of internal transactions that you can only uh discover through traces. So here this is if we go to internal transactions, you'll see it's got uh like yeah a fair amount. Um so there's ETH moving in and out of this address uh internally, and um here we can see the scripts uh ran in uh five seconds and um you can see a basic summary of of all the balances really quick.
Um I have uh another address which I think is quite cool. Um this is the biggest address I could personally find. Um it's uh the Kraken number seven. Yeah, I don't know exactly what they do with it, but I guess deposits and withdrawals and stuff all go through here. So this address has uh yeah, a crazy crazy number of transactions. Um this is just external transactions, it's got even more internal transactions. Um, and I did record a video because it takes about um six minutes to run.
Uh that's the wrong folder. Here we go. Um so yeah, I run it run that contract, and just under five uh under six seconds later, um, it'll print the balance of that address, which is really cool. Um so yeah, um what I've been showing you is how you can get raw data really fast and and process through it. Um and oh it didn't pause at the end.
Um and the sort of main point is that with uh data processing, the biggest bottleneck often is getting the raw data. Um computers are fast normally if you doing simple processing of that data, uh that isn't really the bottleneck. Um so we've made a really big unlock uh through through hypersync. Uh what I would like to show you next for the remaining bit of um the presentation, or actually are there any questions on on hypersync or things I should zoom in on? I I have many questions actually.
Yeah, please. Um if you're if you're running it locally, like what are the requirements in terms of like speculative machine. Yeah, gotcha. Um yeah, so we haven't open sourced the code for hypersync. Um so yeah, we're happy to license it potentially, um, or things like that. Um we ourselves are running it on bare metal uh for high performance. Um the storage costs are actually really not that bad. Um for example running an arbitrum.
I'm probably gonna mix up the numbers exactly here, but um running like an arbitrum full node takes uh I think like close to three terabytes. Um but running um our node with uh logs etc. for Arbitrum um was I think like 500 gigabytes or something, so like a massive amount of compression. Um that depends on what you index. Uh yeah you should identify what you want to indexed prior to running. Yeah, so we index everything um but uh we don't have traces for every blockchain yet uh traces are yeah a a l a very new feature um but yeah we've got all the in uh the data pre-indexed um so there's a a lot of compression I think um typically if you want to build an application where you know exactly what data you care about you would probably want to populate tables just with the data you care about so uh I don't know let's let's say etherscan um like they already have indexed this address so imagine you came to this page and it took six minutes to load this page because it's so big uh so like Ether Etherscan for example uh yeah I also don't know what their database structure is but they've got a lot of partitions and and things um so yeah um yeah last little example I think I had a tab open for it uh yeah here we go so I'll go out um so there's a we have a a simple tool to create an indexer which will save you from going into the low level things that I've been showing you in this presentation.
Um it's a tool called Envio so you can install it npmi dash gnvio. Um we have a v2 coming out really soon um but it hasn't been released. So I'm gonna show you the code from v2 but run v1 um just so things are smooth. So NVO init. Okay, I could have given it a different name. I'll do it in this folder.
We have three languages that you can use, they all JavaScript related languages. Let's do TypeScript. So if you want to build on fuel, you can choose that. And we have some templates which are nice examples to start with, but contract import is really nice. I'll show you why. You can use a local ABI or you can use a block explorer and here we go. Let's do Ethereum Mainnet.
Sorry, I've I've forgotten the address here. So this is the ENS address. If I paste that shouldn't be there, if I paste this address address here, it'll actually pull the ABI from various sources like Ether Scan if the contract's verified. Oh sorry, I clicked enter by mistake.
Deselect or select events. I want to index, and then yeah, I can add more networks. It's a multi-chain indexer. But for now, let's just do this one contract. And now installs all the dependencies, the node modules, etc. And yeah, we have an indexer. So if I do PNPM dev, the indexer will start running. And this sorry, the address that I indexed is the ENS registrar. So we can now give it a few seconds.
I don't know, let it get up to a couple thousand events. Okay, yeah, that's enough. And then open that. And yeah, you can see I was fiddling around with it. Yeah, I this is my ENS name, but it's um yeah, obviously I haven't we haven't scanned far enough yet. Um let's just remove these filters and just show this query.
Yeah, you can see all of these ENS names. The owner and uh the date that it expires, and this is the data that comes directly from the name registered event. We also have uh name renewed. Um we can run that and uh we have ownership transferred. Um so literally, this is how quick it is to build an API for your app.
Um and the great thing is it actually generates all the code for you. Um so if if we go uh in here, oh yeah, let's just do it in here. Um we've got the source folder, and here's the code that that we have. Um so it's defined all these handlers for us, and you can add whatever logic you want. Uh you can change the schema so you can um I don't know if you can have let's say type owner um ens names and make this uh an array of strings and um yeah, yeah, it does need an ID.
So yeah, if we wanted to be able to look up all the ENS names belonging to this user, we could create this in our schema and edit our handlers, um which is super cool. Um yeah, that's uh the main uh part of the demo done. I'd like to zoom in on anything uh anyone has questions about yeah. I think you're reasonable, but what chains are you wanting?
Yeah, so we we've got a list in our docs. Um we on a lot of chains. Um.dev. I think we're on a uh just over 45 chains. Uh quite a few of them are test networks. Um but if we go here, um endpoints. This yeah, these are all the chains we have. Um so yeah, give give it a browse. It's um like I said, I think 46 maybe, 47 uh chains.
Um yeah, and if you have a new chain that you would like us to index, just message us. Um the team is quite good at syncing new chains as you can see. Uh another question. I know you have not much experience about querying on uh on chain data, but uh is it possible to query like a specific method? Like if I do the name and I do want to see all the transactions within this method. Got you. So um on your smart contract you have a method called I don't know, call me, and you want to know when call me is is called. Yeah, it's possible.
So you would um I don't have a uh example prepared, but I can I can talk you through how you'd do it. Um so if I go to the uh the um the docs here, um you would get it on the traces and it would be in the call data. So if we uh scroll down, sorry. Um that's transactions. Here we go.
On traces we have um yeah, the input and um uh yeah, so you'd you would be able to get the input, the address that was called. Um yeah, um basically from that you'd be able to uh get all of the calls on a contract and then filter out the ones that you that you care about um yeah using the SIG hash as someone mentioned. Yeah.
Great presentation. Uh I wonder two things is possible to fetch metadata like ID of this, and it's possible to do that having on the blocking way. Um second question. Um EAS, like a what data make the CMUS code data taste. Yeah, yeah, good question. So we we aren't very restrictive in terms of how you use the tools. So if you're using um hypersync directly, uh you can implement it however you want.
Uh we only store blockchain data, so we don't store IPFS data, etc. Um if you want to implement it in hyper um index, you know, like in here, uh we have um asynchronous handlers, so you can uh send out the asynchronous processes on the side, but um at you're right, that would slow down the index so if it blocks um an event from from happening. So what you can have is a co-process that populates another table on the deck of the database based on other data. That would that would be a good way of doing it.
So there is a lot of flexibility. Um the other approach uh that we've seen people use really well is to implement uh some kind of of local cache. Um we've seen a lot of people use text files. Uh I know it sounds primitive, but honestly it works. Um so for um uh uh the Sabia um indexer, they they need the token metadata, uh sorry not token metadata, they need the name and symbol of the token um which they need to call from from the URC20 contract, and they they've just they just have a JSON file, and as the indexer runs, it updates that JSON file, and it's a very quick uh lookup. So the first time they run the indexer, it's slow because it has to fetch the data, but then you cache it.
Um so you can you can use clever caching also to speed things up. Is there an example of how do you use it? Uh yeah, yeah, yeah. Um Sablio this one. Um in their apps. Um yeah, I think 40 data change on the simple data. Uh uh decoder, yeah. So you were talking about decoding bytes into a string or yeah, so the contract actually seems to byte string and the contract controls that just project. Uh ah, got you.
Uh yeah, I I think that. Like using view em. Using Envio. Yeah, yeah. So you uh I don't think we have a function for that, but you could implement it quite quite easily. Alright, thank you.
Sure. So here is the cache uh source. Yeah, of course I can't find it so easily. Where is it? Um yeah, they've they've got a file called cache somewhere. Um but she has the repo if you if you guys want to dig around. Um cool. I think that's about time. Another question. Yeah. Do you do you run your own node or do you report then just or use internal process? Use the mic.
Yeah, do you yeah, do you run the nodes on your side, or did you just implement like different kind of node that's implements the RPC interface and you store the data elsewhere behind the scenes? Yeah, no, good good question. We don't run nodes for every single chain we support, but the um the popular chains uh we do because it uh gives us more assurance on uh on uptime, etc. Um we uh ingest via RPC. So uh to in uh you know you know to uh integrate a new network, all we need is an RPC endpoint that has the um get um uh what's it called?
Uh get block uh get transaction receipts and get block. So basically those two methods uh give us almost all of the data, and then if if we want to in um ingest traces, uh we have to they have to um implement the the trace method. Uh we use the Aragon trace method at the moment. Um I'm trying to think what uh yeah, does that answer the question? Yeah. Okay, thank you very much. Now I think if we want to stay on time, we will have to interrupt here and uh yeah.
Thanks for watching. Maybe you can follow up uh later outside if you want. Thank you.
Related talks
- Zero To Dapp
- Cyfrin Updraft: Full-Stack Web3 Development Crash Course
- Covalent 🛠 Building in Web3 with One Unified API
- Human-Readable Push Notifications on Ethereum
- Building Augur - Lessons Learned by Scott Bigelow (Devcon4)
- BuidlGuidl - Intro to Ethereum Development, Build & Deploy your first dApp
- Full-Stack dApp Development with Scaffold-ETH and The Graph, Day 4
- Infura & Frontends: Hack with ethers.js, ethLogs & ERC 20's – Michael Godsey
- Simplifying EVM Development with Curvegrid's MultiBaas Platform I ETHGlobal Taipei 2025