The Graph 🛠️ Querying andIndexing Smart Contract Data on Ethereum with The Graph
ETHGlobal·Mon, Oct 9, 2023, 12:00 AM
Join Simon Emanuel Schmid from The Graph for a workshop titled "Querying andIndexing Smart Contract Data on Ethereum with The Graph ". Welcome to ETHGlobal Waterloo, where it all began! Returning to Canada's tech hub, we're thrilled to bring together international enthusiasts and local talent for an unforgettable hackathon experience. Don't miss out on this exciting opportunity to showcase your skills, collaborate, and innovate with the power of Ethereum. Be a part of the journey that continues to shape the Ethereum ecosystem. Secure your spot today and let's build together at the heart of Canada's tech scene! ----------------- Follow us: https://twitter.com/ETHGlobal Check us out: https://ethglobal.com Email us at: hello@ethglobal.com
Transcript
foreign [Music] 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 how to work with this um Depending on time we can also go into the subgraph development by then yeah I think it's it's called to have just also that that was never recorded it's upgraph development stuff it's recorded from time to time uh anyways let's get it started so um query and indexing smart contract data on ethereum with the graph um I start here really with the basics um that is what is actually in the 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 um just rows and columns with uh with data in it when we look at the nft contract it's my uh main example because it's such simple in in terms of Technology um you you would have like in a very simple way you have an ID the token ID of um of the nft and then you have an owner which is a theorem 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 it that's the data view on it right um if you wanna so yeah the problem is with the data is that the smart contract and the blockchain it it usually holds the the state as of now like the if it is a good tool to to see which who has which nft Google owns Which nft but it is not very good um in in knowing uh who owns the nft 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 which the graph we have a history but um it will go a little bit deeper the second thing that the smart contract does um besides uh holding holding state or data 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 a smart contract and that's also why it's called smart contract um because it the the data is um yeah it 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 the cube by cryptography uh that means that like all the transactions they are they are in blocks and then the hash is coded on all all over that those transactions that makes a block and then the Block it's all always um 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 around not on one server it is basically copied over 1000 or 100 ten thousand hundred thousand times all across the world that makes it so uh censorship 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 not Global jurisdiction where you could enforce such a thing um that makes it very very interesting but going forward anything goes the blockchain as one thing we 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 Dennis Martin tracked as I said before is the data and the logic 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 with your secret key or your wallet so that um everybody knows that if this is electric transaction and um and then the transactions included on the blockchain we will look at that later on um but there is a problem with the data on the blockchain as I said before um but the the user interfaces as we initially saw um they want to read they want to write to the blockchain and as we saw before in this instant 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 and the gas is consumed by the miners or now no now now it's the chrome stickers provider validators and then that's why you pay and by paying 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 fund its own business well the same is is also based from Alchemy if you if you do a lot of requests to outcome your infuro you need to pay for them um so they're jumping into it but the protocol by itself doesn't incentivize reading but 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 is with the ethereum blockchain especially is that we have all these the smart contracts or dabs um that has 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 um 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 it takes a lot of time um that's not something that you want to have in the front end and this is also what we see currently or before the graph that front-ends to this is like really code from a project that I am that that I was advising um how they were trying to get the data out um so for those that understand um JavaScript or typescript that reads as follows um do you get you try to get a contract but all these await methods especially those that we see here and and these here they all pay 200 to 300 milliseconds to resolve because you 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 that 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 the why the graph exists so for for one to get the historical data but for tools 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 humanipfs 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 com soon pm um on those stories you see currently one point or one building queries per day or even a little bit more depends um so it is really powering um the a lot of the Decks that we see currently uni 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 also eccentric indexing Network protocol so um there are more than 180 indexes I think currently worldwide owned by individuals oops um and that that means these are independent indexes that that run with things so it is similar to what we talked about before but ethereum or a blockchain general is that we have a distribute Network um there is this redundancy so if a subgraph is indexed by uh one indexer across the world if one goes down another is still around um that also gives you fast and cheap reliable access to that data and um yeah the whole mission is that we we go towards a global open apis 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 to do here so quick uh into the subgraph 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 this Google Slides just multiple of them so database consists of multiple tables and you can Define the tables as As You Wish um then you have the mappings which is hit this gear here 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 the 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 necessary by itself the indexes that need to index the top graphs 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 a stop graph so what happens is that we saw this mess of transactions that are just layered on top of each other that they are um created like very nice and neat buckets for for each of the protocol in a 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 query so we send one query that has a has a round trip of 100 to 300 milliseconds 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 like all my presentations are open uh you can you can use them even if you want to give the presentation just to someone maybe you want to remove the extra node logo then but uh yeah it's probably good 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 and in between is this indexing protocol that is incentivized and built for data reading and that sits in between and reads data from the blockchain and stores in in this indexing layer all right so how does this 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 that adapt can be like in his purest sense adapt is just HTML CSS and JavaScript um that defines that app it 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 units for upper open LP position whatever cool thing you can do it's all in the transactions transactions they emit events um and with events the smart track basically tells the outside world what what happened we can um look into this later what an event is but that's basic basically what what smart contracts emits so you can watch the blockchain or watch smart contracts for those events and these events are they're interested into the graph node which will um bandwidth according to those events around map paint which is uh in a best webassembly module and put it put then the Staffing store so here's is the database um where where the data is stored for later retrieval then the other way uh that they had also in this image from both is that the depth the depth also needs queries so if you go to UNI swap um and the analytics page it it needs to load data somewhere so it sends queries to the graphical API which sits on top of the graph node and that graphic API will get data from the store and then um send it back to the web so it's very um very neat right um also a very funny uh use that we used to displace this one like um how sub crops work so you have this mess of uh transactions again and then magically is over now they are sorted into this this pocket so that you know what's going on left half and overview 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 the sorry they are writing very sophisticated subgraphs um okay um and I also send you this bringing unit checked that's Pro currently for me the best entry point I mean you can also go to the graph oops grass.com and go here to graph Explorer and then you see also stop cross here um but the one from like these are the ones that that threat for themselves or also some of the sorry see they are also in the tops here um but sub graphs here are very good so we can for example say call um let's have a look at it I know so so they have some on the decent price Network and some on the hosted service um so you can look through them and and find out they have like I think it's hundreds of graphs what is it okay since then production area 47 and 74. 47 on the development and 100 and them six deployments cos Y is that because they deploy to different chains but see that's curves 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 search for this one is the crypto Punk subgraph um yeah you need to be careful to take the one of the highest creation because somehow a lot of 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 if if that's not graph but um what I like to do and that's where the where I think it makes sense to see this Workshop is actually open up a a very um a little bit more sophisticated playground although this a revamp of this one is in the works but I can show you how this work works so what we do 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 square go here then you can go to uh explore like this and paste it now the problem with sub graphs on the decentralized network is that you need to have an API key and that's inside of that URL is display solder for the API key and what we first need to find one right so let's do this together so what you do is you go to Studio actually connect the wallet and I will just create a new account and then um sign in and yeah I used to sorry to to upload some test subgroups but what's important for you is that you go to API keys and not sub graphs and then you create a new API key you can click here actually like a demo and then that can receive free queries that do this by giving an email okay so I can claim the quiz nice and now I have two thousand pre-quarters on this API key that's very good and I already have the eight pack in here so I can just copy here go back go here and paste it boom um yeah feel free to just abuse that one I mean 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 is because it is um various um strongly typed query language that we see um we can have to explore on the right so you can also have the docks a graphql query has so called root types and here we have the root type query and 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 um the cryptopunk subgraph it has accounts which are horses of punks then we have the actual punks and we have metadata of the punk and all of this you see there is not this 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 we will look into this and then the result is an account or a just one account or a list of accounts same with punks and received account punks metadata trades tasks bits 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 punks 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 normal apps cool and I think you can try a lot sale so cool they say the largest sale ever happened is this one five eight two two for um 8000 Eve always always funny for me to see like the 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 will be cool we got here to the sales and I like this Explorer we will have soon something like this in in also on the on the graph.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 to 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 I want to see also the amount I actually want to see the transaction and then we maybe want to have to from and the 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 and try to figure out what actually what data is this here and then I can send it 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 this but that's the second one um maybe I should also get the punk oh yeah that's it again and you can remove this one actually have a little bit better View so five eight to two yes that's what we saw here for 8K but a funny thing is that actually the highest sale ever is this one okay okay it's interface let's open and let's try again yeah okay cool and why is why is that what happened here so when we go to way this but by the way that usually it's um in a way so what happened here this is interesting no um we start 8K is the highest sale 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 pound 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 graphs perfect for you can say ah there is this transaction hash we go to either scan okay and actually new compute so like autocomplete's not there yet and um and let's start to look into the thing and what you see here is that um and so someone uh made a flash loan I think so here we see um uh someone got or what is this five hundred million die in a Flash Slowdown from I think that's why we end up with this weird number and then buys Punk and then it gives it gives it all back and sells Punk again this person tried to uh fake this transaction and we can and interestingly uh on the official side the sentence or this um but they talk about it in an interesting way so we can try to see the punk with the number and go here and yeah see they say okay this uh blah blah they are flash loan and they sends written on their website yeah cool um yeah I think this explains how to query the graph
Automatic transcript — names and jargon may be misspelled.