The Graph 🛠Querying and Indexing Smart Contract Data on Ethereum
ETHGlobal·Mon, Oct 9, 2023, 12:00 AM
Join Simon Emanuel Schmid from The Graph for a talk titled "Querying and Indexing Smart Contract Data on Ethereum". As new users flock to web3, critical infrastructure advancements are needed to scale. Scaling Ethereum is your chance to shape the future of Ethereum. Join us March 10th-29th for three weeks of hacking and summits featuring teams like Gnosis Chain, Scroll, Hyperlane, UMA, IPFS/Filecoin, and more. ----------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
foreign [Applause] [Music] or 20 minutes um but I have prepared a special one for you that is the less technical um thing like how to actually query data and how to find the data and how to work with this um Depending on time we can also go into the subgraph development by them yeah I think it's uh it's cool to ask this also that was never recorded subclass development and stuff it's recorded from Turkey pound uh anyways let's get it started so um querying and indexing smart contract data on ethereum with the graph um I start here really with the basics um that is what is actually been a smart contract anyways um and I have like an interesting uh like an example you can think about of a smart contract that it has two things basically it has storage like data and you can think about the storage units more contract as a simple Google Sheets or Excel sheet where you have some just rows and columns with uh with data in it when we look at the nft contract that's my main example because it's such simple in in terms of Technology um you you would have like you know in a very simple way you have an ID the token ID of um what's the nft and then you have an owner which is in ethereum address and then you might or might not have a metadata URI and other stuff but but in a nutshell and the nft the es621 contract defines token ID and an owner so that's that's the data view on it right um if you yeah the problem is with the data is that the smart contract and the blockchain it it usually holds the state as of now right it is a good tool to to see which who has which nft who owns Which nft but it is not very good um in in knowing uh who owns the NXT before and that's exactly what's going on here um that's a problem that we don't we don't see um who owned before in the smart contract state by itself um this is where the graph jumps in uh that it actually helps to also find a way into the history of smart contract and on top of that making the current state actually are also easier to access so you can think about without the graph it is just this and with the graph we have a history but um we will go a little bit deeper the second thing that the smart contract does um besides uh holding holding state or date down is um it also has rules how to change that date done and that's very important because you don't want like have everybody just changing who owns Which nft it follows certain rules um which can be encoded in the smart Contracting that's also why it's called smart contract um because it the data is um yeah can only be changed according to the rules so let's Define quickly the concept of a blockchain again um so they basically the evolution is a distributed data storage um secured by cryptography uh that means that like all the transactions they are in there in blocks and then the hash is coded onto all all over that those transactions that makes a block and then the Block it's all always um a link to its pre previous block that makes the whole thing tamper-proof so you can never change one thing um distribute means that it is the blockchain is not runs not on one server it is basically copied over 1000 or 100 top ten thousand hundred thousand times all across the world that makes it so uh tend to ship resistant that you would basically need to shut down every single node that runs the ethereum blockchain on it or whatever blockchain in order to stop it and that's basically impossible um because we have node uh Global jurisdiction where you could enforce such a thing um that makes it very very interesting but going forward you think of the blockchain as one thing you do not care about that it is distributed across all all these machines so we think of it as a logical uh one unit so that's just the blockchain right and then the smart contract as I said before is the data and the load check how to change the data that is stored on blockchain and how does it work if you want to change the data you send a transaction to the blockchain which is then distributed across all these um copies but um and you you sign it cryptographically if you if your secret key or your wallet so that um everybody knows that it's this is a lectured transaction and um and then the transactions included on the blockchain we will look at that later on um but there is a problem next update on the blockchain as I said before um but the the user interfaces and we initially saw um they want to read or they want to write to the blockchain and as you saw before it is insane device so if you send the transaction to the blockchain you pay some gas or you have to put some gas on top of it the gas is consumed by by the miners or now no now now it's the growing State provider validators and then that's why you pay and by pending it for it the whole thing works um but when you want to read from the blockchain there's no incentivization for anybody to provide you that data per se not in the protocol um so that's why when you go on either scan you see a lot of advertisements um which sometimes are also a little bit Shady in order so that ethers can can spawn its own business or the same is is also built from Alchemy if you if you do a lot of requests to Alchemy or infuro you need to pay for them um so they're jumping into it but the protocol by itself doesn't incentivize reading right that's a big problem going back to the um analogy from before each of these things here is one transaction and also a bigger problem existing serum blockchain especially is that we have all these the smart contracts adapts and that have stuff on the blockchain and every time someone sends a transaction it just laid on top of each other so in the end if you wanna if you want to see okay what was actually going on let's say in and foundation and you would need to oops then you would need to go through all these transactions and and pick the ones out that are according to foundation and that's quite tedious work and takes a lot of time um without something that you want to have in the front end and this is also uh what we see currently or before the graph that front ends do this is not really code from a project that I am that that was advising um how they were trying to get the data out um so for those that don't stand um JavaScript or typescript that reads as follows um do you get you try to get a contract but all these overweight methods especially those that we see here and and these here they all pay 200 to 300 milliseconds to resolve because they always call back to some some node um which you usually need to pay somehow that they give you that that data and then you wait and you go through so this code if you are if you own let's say 10 or 20 nfts easily gets 5 to 10 or 20 seconds to load just to show you uh a list of the tokens that you hold and that this is this is like super inefficient and um yeah users just move away after a while because they say like whatever I don't have time to wait 20 seconds for my stuff to resolve um yes and that's actually now we had like already two um reasons or by the graph exists so for for one to get historical data but for two also to have a very good access to the data um yeah on the website you probably know it's indexing protocol so creating metrics like if you might BFFs there's also support for our beef um near Cosmos and and others so in the end uh the graph will index all blockchains more con soon pm um on those terrorists is the currently one point or one billion queries per day or even a little bit more depends um so it is really powering um the a lot of the depths that we see currently unit swap is the most famous example that uses but we are also kind of others like enzyme or audios or n and stable um yeah there are a bunch of them um yeah and it's not only the host service it's also a centralized indexing Network protocol so um there are more than 180 indexes I think currently worldwide owned by individuals oops um and that means these are independent indexes that run the things so it is similar to what we talked about before but if you're more blockchain General is that we have a distribute Network um there is this redundancy so if a sub block is indexed by one indexer across the world which one goes down another is still around um it also gives you fast and cheap reliable access to that data and um yeah and the whole mission is that we we go over to Global open API of the data that we need um yeah and it's very cool and in the end we can now build truly decentralized apps right sorry that's what we try to do here so quick uh into the sample so I told you before about sub graphs sub graphs in general are just a database um a database is the same as you saw before in these Google Slides just multiple of them so database consist of multiple tables and you can Define the tables as As You Wish um then you have the mappings which is hit this gear View and that's the logic of how to get data out of the blockchain into that database and in the end you you make it available so and to subgraph by itself it's only um subgraph by itself is only the code that defines how this is done but it doesn't do it necessarily by itself the indexes that need to index the subgraphs which by indexing that means they create the database and they run the code how to fill that database so um yeah that's the quick concept of sub graph so what happens is that we saw this mess of transactions that are just there on top of each other that they are um created like very nice neat buckets for for each of the protocol in the database per each protocol so we can we can easily query the data and make much more sense about what's going on um that in the end we end up with uh to to go back to the screenshot from before then we have a very nice and neat graphql created so we send one query that has uh has a round trip of 100 to 300 minutes seconds usually uh it's much quicker that's what we expect um yeah to get that data out all right um I was just reading the chat so I will also share with you the presentation afterwards I all my presentations are open uh you can you can use them even if you want to give that presentation to someone maybe you want to remove the edge to node logo then but that yeah it's public goods um so we end up with this uh modern depth architecture where we have the user interface on top and then uh the blockchain on the one that'd be new already in between is this indexing protocol that is incentivized and and built for data really and that it sits in between and reads data from the blockchain and stores you know in this indexing layer all right so how does it look like then or how can you think about it this is the um the illustration that we that's the official one how the graph node works so again on top is the depth adapt can be like in his purest sense adapt is H is just HTML CSS and JavaScript um that defines the depth could be also other programming language but that's all you currently see um and actually talked before when they send transactions to the smart contracts uh they are change you can change state so um that is buying an nft on an nft Marketplace or swapping something on unit swap or open LP position whatever cool thing you can do in the transactions transactions they emit events um and meet with events smart contract basically tells the outside world what what happened we can um look into this later what an event is but that's basically basically what what smart contracts and me so you can watch the blockchain or watch smart contracts for those events these events are they're ingested into the graph node which will um bandwidth according to those events around Matt pin which is uh in a best webassembly module and put it put then the stuff into the store so here's is the database um where where the data is stored for later retrieval then the other way that we had also in this image from both is that the depth the depth also needs queries so if you go to UNI swap um at the analytics page it needs to load data somewhere so it sends queries to the graphical API which sits on top of the graph node and that autograph API will get data from the store and then um extend back to the deck so it's very um very neat rack and also a very funny uh use that we used to displace this one like um how sub crops work do you have this mess of uh transactions again and then magically somehow they are sorted into this this pocket so that they know what's going on that Avenue all right so now when you think okay it's very cool but uh now how can I actually find data to query the best thing in my opinion is this sub graphs that XY set from Missouri they are writing very sophisticated subgraphs um okay um that also send you this thing here in the chat that's Pro currently for me the best entry point I mean you can also go to the graphs class.com and go here to the graph Explorer and then you see also the sub graphs here um but the one from like these are the ones that that thread for themselves or also something sorry see there also in the tops here but sub graphs here are very good so we can for example say cool um let's have a look at I know so so there are some on the decent price Network and someone that was a service um so you can look through them and and find out they have like I think it's 100 subscribe so that's it okay since then production area in 47 174 7 on the development and 100 and um 60 deployment because why is that because they deploy to different chains see that's curve they are usually the same on all cool um so when you want to look into one of those another good example actually is uh let's switch to this one is the crypto Punk subgraph um yeah you need to be careful to take the one with the high exploration because somehow all people just deployed sub graphs that's a downside of a decentralized network um now if you want to query that subcraft then you can just go here and go into the playground and start to play around and wish wish that subcloud but um what I like to do and that's where the where I think it makes sense to see this Workshop is to actually open up a they're very um a little bit more sophisticated playground although there's a revamp of this one is in the works but I can show you how this work works so what we do if we if you want to query a sub graph on decentralized network what you need is kind of this is this query URL you can copy the SQL go here then you can watch and go to uh um explore like this and and paste it now the problem with subgraphs on the decent trans network is that you need to have an API King and that's inside of that URL is displaying solder for the API key and what we first need to find one right so let's do this together what you do is you go to the sub graph Studio actually and connect the wallet and client will just create a new account the comic and then sign in and yeah I used to try to to upload some desktop crops but what's important for you is that you go to API keys and not subclass and then create a new API key you can click here let's say like uh demo and then I can receive free queries that will this by giving an email area I think these are options okay so I can claim the queries nice and now I have two a thousand free queers on this aircraft key that's very good and I already have the eight practice here so I can just copy here go back over here I can paste it wait um yeah feel free to just abused everyone like everybody can have thousand three queries cool um now what we have here is on the top we have the sub graph and on the bottom we have um the Explorer and the documentation so the cool thing about graphql it's because it is um very interesting it's a um that's strongly typed query language that we see um we can have this explore on the right you can also have the docs a graphql query has so called root types and here we have the root type query a root type subscription all the subscriptions do not work currently so we go with query now if you go on query then you see a detailed documentation about what you can query um so in this example when you look about look at the cryptopunk subgraph it has accounts which are uh all those of pumps then we have the actual punks and we have met the data of the punk and all of this you see the result is repeating pattern of account and then accounts with s so this is the first one is to get just one and then you need to provide an ID or you can have many and then you can have a filter let me look into this and then the result is an account or at least one account or a list of accounts same as punks and C account punks metadata trades asks and contracts and so on so forth like that's going on so now what do we want to do a funny thing to do with the crypto pause actually is to say I want to know which was actually the most expensive sale ever happened on Punk and uh you could go to um crypto punks normal apps here acrylic cool and I think you can try a large state so cool they say the largest sale ever happened is this one five eight two two uh for um 8000 even always always funny for me to see that with people actually pay that much money for a pixel but yeah that's how it is um now we could try to reproduce this one um and that'll be cool we go here to the sales and I like this Explorer we will have soon something like this in also on the on the graphs.com um it always looks the same we can say okay what we want to do we can actually order by the amount of the same and all the direction we make it descending so that says um it will sort the list of results by amount and the highest amount would be on the top and then we can say okay in which hash it was it happened we can say which uh ID it is and we want to see also the amount I actually want to see the transaction range and then we maybe want to have to from second to two cool let's see and see this is for me a very explorative that's why it's also called Explorer I can just um click around here and and read stuff and trying to figure out what actually what data is here and then I can send it cool now we see um so the highest according to that chakra uh is actually this one and funny enough when we go back here we see this is 8K and it's just but that's the second one um maybe I should also get the oh yeah okay the current woman that sent this again and we can do this one which is better View so five eight to two yes that's what we saw here for 8K but and funny thing is that actually the highest sale ever is this one good interface and let's try again yeah and why is why is that what happened here so when we go to eat the way just by the way that usually it's um in a way so what happened here this is interesting oh um B Store 8K is the highest say and that's what they say here but actually um we also see when we look on the blockchain um that it is uh 124 k e for one Punk um and now you wonder like why is it displayed here and we can start to do investigations and that's what I think is is the graph perfect for and say ah there is this transaction hash we go to either scan actually new compute so like autocomplete's not there yet and um and let's start to look into the say and what you see here is that um and so someone uh made a flash loan I think okay we see um uh someone got oh what is this five hundred million die in a flexible detention that's why we end up with this uh weird number and then buys a punk and then it gives it gives it all back and sells Punk girl this person tried to uh fake this transaction and we can and interestingly uh on the official side then sentence or this um but they talk about it and you know an interesting way so we can try to see the punk what's the number and go here yeah see they say I gave this uh blah blah they are flash loan and this answered on their website yeah cool um yeah I think this explains how to query the graph foreign
Automatic transcript — names and jargon may be misspelled.