The Graph 🛠 Quickstart to Subgraph Development & Querying - Simon Emanuel Schmid
ETHGlobal·Sun, Oct 9, 2022, 12:00 AM
Speaker
Join Simon Emanuel Schmid of The Graph for a workshop titled "Quickstart to Subgraph Development & Querying". ETHGlobal hosted ETHMexico in Mexico City 🇲🇽 - our first IRL hackathon in LATAM in over four years. This year hackers had the chance to build the future of web3 with a chance to win over $150k+ in prizes from teams like WalletConnect, dYdX, AAVE Grants DAO, as well as experience talks from guest like Vitalik Buterin and Aya Miyaguchi! Thank you to the 580+ attendees from 27 countries who joined us for this unforgettable weekend of learning and hacking. For Mexico, 65% of hackers were from LATAM, and 46% were new to web3! Thank you for being a part of it! ❤️ After ETHMexico, we are heading straight into a full month of learning & building 🏗 Get ready for ETHOnline, our largest online event of the year! --------------------------------------------------- Follow us: https://twitter.com/ETHGlobal Check us out: https://ethglobal.com Email us at: hello@ethglobal.com
Transcript
[Applause] [Music] nice this feels cool i mean like i flew in from europe yesterday and this for me my body is probably kind of giving that talk at an after hour at six a.m in the morning but uh let's do this eat sleep hack repeat um yeah what i will do is kind of a quick start to sub graph development and also querying i mean the problem with a lot of deaths when we talk with them about the graph is like they are a little bit afraid about the technology because the graph is such a powerful thing but it's also it seems like at the first sight overwhelming like how get into it and how can i use it and what exactly does it i think it's cool everybody uses it but i don't know how so i want to kind of reduce that fear and give you kind of um you know a quick intro so let's have a quick repetition about what is actually a smart contract so i think about a smart contract actually in in the thing that is just data so it's structured data that's lives on the blockchain it's immutable or kind of actually mutable and then you have rules how to change that data that's a smart contract basically and the smart contract by itself it always knows about its current state who owns which nft right now but it does not really know about historical state but blockchain knows but the smart contract by itself knows only about the current state so let's look at that example so if you look at the you know standard nft smart contract this is the data like you have just a list with uh with token ids owners and then some random metadata uris if you're lucky then they're not on google cloud but um yeah and that's that's the state you can always create this market like who owns which which um token but what you don't know like by just looking at the state of the smart contract who owned it before so that's problem especially for nfts like there's this big word about the promenades but also other stuff that's interesting that when we want to look into the past like for example on uni swap how did the prices of a pair kind of develop or how did the tvl of a certain protocol uh change over time and very interesting data that but you need to look into into the past of the smart contract state right so it would be nice to have kind of something like a time machine right that you can just kind of go back in time and see like how was the state of that smart contract yesterday or like before yesterday a week ago and so on so forth so that's one way how you could look at what the graph is it's kind of it gives you this possibility for this time machine but not only that or kind of to understand it a little bit better first i said like the smart contracts are basically data and rules how to change the data and if you want to change the data what you do is you send a transaction to the blockchain so that that data actually gets changed right and that's what we look at with the graph we look at the transactions um another angle on the whole thing is when you start to develop depths so initially kind of in the early days of ethereum there was this idea of having the user interface you know just html css and javascript deployed somewhere on a decentralized storage censorship resistance and so on then you run your own blockchain on your computer right and then you download that front end on your computer and start to run and start to interact directly um but yeah there was a problem that because the blockchain by itself is right optimized and the json rpc interface that you use to interact with your node is also kind of it's it's for writing data mostly the reading stuff is a little bit kind of afterthought but like the modern frontends that we see today like usually you read data right you want to see kind of how did the token price develop who traded which and what's going on and there was there was not really really a thing that's also why like a lot of centralized companies jump in to fill the gap to kind of provide that data in somehow in an efficient way but we want to be decentralized right so in the wild we see code like this i mean this was actually one of the projects that i worked with kind of looking at the code and and for those that understand javascript you see like all these await statements those are then kind of the the loop through an arbitrary big number of balances and all these await statements they get 200 300 400 milliseconds to resolve and if you have like 10 nfds or of 20 nfts here and all of those are awaited and then in the end kind of this simple page that just shows you how your nfts take seconds to load which which is a super bad experience right like um so which how should it be like you should have like a user interface on top that actually writes the blockchain and that you have kind of this indexing query protocol in the middle that gives you this data efficiently so in the end you can write queries that look like this right kind of very nice and uh you know i love graphql because it's such a nice language so this does actually the same and like to send this query it's kind of a matter of milliseconds so so this is actually where the graph comes in you know it's kind of these apis for for this vibrant decentralized future like on the host service currently we have roughly one billion yeah billion queries per day uh big names like audios uni swap m stable enzyme used to graph it's kind of yeah it's it's well well used and and now there's also kind of this decentralized indexing uh network protocol launched like one year ago so you were not not only able to kind of use the centralized hosted service which which was a proof of of concept but you can really have like decentralized subgraphs and data so there is kind of this network with 170 indexers that are kind of uh individuals there's this red redundancy across the world it's fast cheaper while reliable and like we're going towards this global open api so in the end you can build truly decentralized steps but again so to get back to to what's what we can do here is like um there are two bounties from the graph and i'll show you how to get those now like there's the the best use of an existing sub-graph which is a little bit easier uh so we have a a winner and a runner-up so the winner gets 2k and the runner 500 and then the best new subgraph which is more heavy but also more cool to do is um it's the same so it's kind of a little bit of strategy so what do you want to do do you want to do the easy stuff but you have more competition or do you want to do the hard stuff so but it takes you more time but that's up to you but we're happy to help bonus points for the charging is if you go with the new technologies like substream i do not talk about this in that talk but you can read online or you use the decentralized graph network right so how do we do it um yeah a subgraph explained again is kind of this thing very very right at kind of a data structure it's it's actually a database for those who know about databases and then you have instruction set so you look at the blockchain what's going on there and then you start to write in the database so you have it easily retrievable later on um so you can think about the transactions in the blockchain are kind of a mess you know everything is kind of in on the same thing what you want to do is kind of you want to kind of have like the databases per protocol so you can easily query it and make sense out of it um so roughly that how it works on top you have to dab you as we said like you send transactions a smart contract to change the state which will emit some events the graph node kind of having the information from the subgraph will take those events put it into a database so that on the other side you can then just query that database and have quickly your front end so in the end like a sub graph it's more or less what's going on here you have this mess of uh of different stuff and magically it kind of is organized in a way that you can reuse um now you have learned about the graph so you can now scan that qr code and tell all the people that you learned about the graph marketing team told me to bring in the slides so you could do it or not three oh one more a few more you want to do it nice um you got it cool thanks so nft subgraph development like i always use nfts as an example because like you understand it's like it's the super easy thing so how does an nft contract look like you have a transfer event which is the most important one actually and then you have those functions kind of balance off owner which are read functions and you have to transfer from which is a write function um more or less that's it there are there is some other stuff around with allowances but in a nutshell that that's kind of nft you just have some contract that even emits that event and kind of provides those functions it's very simple but that's also the beauty because you can build so much out of it but it's also easy then to index so i have some tips tricks here but we skip through these because i want to show you also how we can query but the slides are online so it's more kind of a reference for you so you can think about like what queries do i really want before you start to write your subgraph for nfts it's kind of the how many nfts are in total who owes what what's the pronouns we have kind of a rough schema how you would model that in a database so you have a contract which represents the collection you have an account which are the owners then you have the nfts which are then linked to the account and also the contract then you can store all the events for later reference and store the metadata if you have it and trades also if you have it that's roughly how i would model it and then what you do is kind of your install dependencies find the contract and then you start to initialize and then you just deploy it to the studio and then you enhance the subgraph and then deploy again and so on so forth until you're ready and then you can publish i also have kind of a collection of pattern in this presentation but it's also probably better for looking into that later but they are here it's kind of some stuff that it could do but um i would say let's just do it can i go into this terminal list or something no right okay so you i hope you have graph installed latest version so what can we do we can go to um either scan and uh i don't know i could just go for a random nft and you can just copy the contract out actually you can just do graph init dash dash um what is it index events and this dash index events actually it's kind of the trick to make the initialization much much faster and we need to document this a little bit better but like now i'll tell you and if you do this then a wizard starts and then they ask you so okay which protocol do we want to index the graph supports rv theorem near cosmos uranus theorem here so let's go with ethereum then like in which product you want to initialize there is the subgraph studio which is the new one where you can publish your decentralized network so we always go with the cool new stuff and then we will give you this log so let's name it hash masks sub graph all right directory to create in yeah let's let's go with this hashmas subgroup why not then which uh then before we we use the protocol now we kind of take the chain so we go with your maintenance contract address i already got it so it's this one you just go with the address oh [Laughter] um what did i do wrong just try again visual studio net contract address all right yeah the alternative is that you have the api um maybe for the hackers around you uh especially when you start to write your contract you can also just add the path to an api but i think it would take too much time here to do this but actually i want to show you anyways there is a much cooler possibility currently with new tools so now we have the soulbound studio which more or less does the same that i've just tried to do uh in in the command line but let's see maybe this is a little bit more um more reliable so actually how this works so you can go on the sole mounted xy set and then there is this subgraph canvas right and um here uh you have very nice ui so we can give it a name i don't know what's going on here i don't want this so we do we add a contract and now i can paste in the address and what it does in the background kind of it starts to download the api and and also resolves the start block eventually nice and then that's it and i can save this smart contract and probably now in the explorer here nice okay cool so you see i have to i have to contract um and and and you see okay the contract has these events so we i have the approval events i don't really care about the approval actually like i just want the hash marks have funny features so the names of the name change event so i well let's let's change this the ownership is also kind of just internal and then the transfer so you can easily go and say like okay i want to just index those events and here they are and it's all kind of prepared for you and go back to edit sub graph maybe i need to there are box today i'm not lucky with my live coding or maybe maybe it's because of this i don't know it's about yeah i know that they worked on it today and now also this demo doesn't work but what we have is the abi so let's go the other route again oh i cannot con i cannot work with vi on on the english keyboard let's do this any private data that would reveal if i do this or maybe let's do a new folder all right okay graph init index address i will see that coming all right yeah the dependencies will take some time probably in the meantime um so what it does it it now creates the scaffold which is cool so we can go to um the graph.com slash studio or actually if if you want if you want to follow then you have it here in the products you go to the studio oh yeah sign and then you can just create a sub graph you get minute hash masks continue and actually you just need to do graph oauth let's see where we are here so what we have here is now it initialized the subgraph for us already so you have to subgraph yaml which says like um which which source we can also add to start block here which is you can see it the transaction when it was deployed it saves us time thinking and so on so forth you also have like auto generated schema you see similar like all the events are just indexed and you have also the auto generated mappings so you can easily just get started with that stuff and don't don't need to overthink and um yes and i would not really do much now we just kind of say yeah that looks good just deploy i need probably need to do a graph code then first yeah nice deploy nice it's here see it's already sinking and oh did it fail now no and then you can go already in the playground and start to yeah and it already see it's already starts to have data very cool also when you do this here um you see this this query endpoint and this actually comes the next thing how to create the stuff from the graph so in the from the studio you have this graphical endpoint that you can also use in your front end then but you can also go here and start to um inspect and i know for example like the hash masks they can change names so i can say like okay id name new name what happened here and then you can just send this query and then you see okay someone changed the name to this or 11 mark cuban dodge nikola tesla so we already have like you see it took me five minutes and we already have like very valuable data about the contract ready in a in a nice graphql interface um yeah so that's about a developing subgraph so if you want to go like if you just do this you probably don't get the bounty you should then start you know to change the schema change the the mappings and so on but like if you have that first step and you want to extend your subgraph then come to our booth and i'm happy kind of to explain the concepts you can also go to the documentation how to proceed from here further but i want to spend some time also here on this next slide um how to query data from the decentralized network we also don't need to go through the slides but this works like this so you have again the graph and then there is this one product called the graph explorer and here you see all the sub graphs like all the 439 subgraphs which are deployed to decentralized network yet now and you can you can scroll through them and there are very interesting ones so for example um uh yeah you have i know like lido ray main net pull together sushi hop protocol some doodles ethereum blocks you can query if you want and there's this ess 721 marketplace that tracks kind of marketplace uh volumes and stuff but like the one that i like the most actually is the crypto punks so you can search for crypto punks and then you need to be careful always go for the ones with the highest signal that's what correlation is here for i think this one is a scam um but yeah it's a decentralized network everybody can deploy right so how it works again here you have a playground similar to before so we can see like okay which accounts do we have let's send the graphical query you can start to little bit explore it um but actually the the cool like it really gets gets cool if you do if you do this so you can get the query url here and then you go to graphi ql dash online.com and and paste it here but like the next step is also you need to have uh you need to have an api key you can also go again into the studio and then you can create api keys here we can just create one and then you can use it so you can copy that api key there is there are a thousand free api keys initially queries if you do this and then you just need to replace the api key here with your api key sorry should i take the other one ah here okay all right get the api key nice again and now i can say for example like i get this is actually interesting so who wants to know um [Music] actually the sales yeah the yeah for the punks it's interesting the highest sale so i can say like the nft id and then amount order by amount or the direction descending sent and see this it's interesting nobody knows that but actually the highest sale on the crypto punks was that much and it was a wash trading if you go to the larva larval apps or now uvolabs website and you want to see this you don't see that watch trading here but it's true like somebody did it get a flash loan get the highest sale and then uh get the flashlight back so it was interesting so now we had kind of this walkthrough from how you can like what is the graph how you can create the subgraph and how you can query existing subgraphs on the decentralized network yeah i know it was a lot i have packed in 30 30 minutes but um yeah thanks for watching and if you have any more questions we're trying to be at the booth as much as possible and then there is discord and other channels yeah happy hacking [Applause] you
Automatic transcript — names and jargon may be misspelled.