2023] Full Stack Web3 Development with The Graph by Pranav Maheshwari

ETH Seoul 2023 · June 2023 · 19:45

Pranav Maheshwari of The Graph explains what blockchain indexing is and why it matters, comparing The Graph to Google's index of the web. A live demo then builds a first subgraph from scratch: deploying an ERC-20 contract on Sepolia, running the Graph CLI, reviewing the manifest, schema and mapping files, deploying to Graph Studio and querying a freshly minted transfer. The session closes with hackathon prizes for new and existing subgraphs and an invitation to ask for help during the weekend.

Speakers

Topics

From ETH Seoul 2023, hosted by Kryptoplanet.

Transcript

foreign [Music] first of all really uh you know thankful to be in Korea uh exploring the neighbor maps and being lost all the time anyways a big shout out to Krishna who is also from the graph Hannah has given me this laptop for present I'll be talking about a little bit about how the graph or what the graph does and at the end like you know if you follow this particular documentation or my particular live demo you should be able to code your own first subgraph it's very easy we'll be doing it in 30 minutes so if you are a developer or somebody who wants to develop a particular sub graph in 30 or

like you know just 5-10 minutes just get your laptops and let's do something great uh what does the graph do it's a pretty like you know heavy term indexing it indexes blockchain data but what is indexing right indexing is uh something what Google does to where to right basically you have all the web data that exists on the internet right for example if I want to search each Soul right uh there are so many things in Seoul that are happening there is this Fried Chicken then there is this Soju then there is you know so many things but when you search eats all why does eat Soul come all the time and the reason behind that is that the graph has indexed uh the Google has indexed

everything according to what's required so whenever you query Google it gives you the right to web to data and that is the reason why web is so much accessible if we did not have Google and the indexing power of Google you could not actually access the web it's similar to like you know if you have a 10 000 page book and you want to find a particular page which has your particular context and the index page gives you that so the graph is like the index page of web3 the Google is like the index page of web2 so the graph indexes blockchain data just like the Google indexes web to data on the internet there is so many things it's not open source that's why for Google it's a little tough to index web to data but blockchains are open source

right all the data that exists is live on the blockchain anybody who needs data just can run a particular graph node and get all the information so that's why in web3 indexing blockchain or indexing information becomes even more interesting and that's why we have the graph so basically why do you need indexing so if you go on top with this particular GIF you can check that the things you have is the blog data and at the end what you get is what you want basically what I mean is that in a particular block there are so many data that exist for example you just want the uni swap data right you want to know

that usdc usdt swap was the rate for that or how many people are doing a usdc usdt swap in one particular second right you just want to get that particular information but in a particular Block in the blockchain there must be information that you want or that there must be information you don't want in every third block on ethereum uni swap puts a particular transaction right and you want that transaction for your uni swap data but you cannot like you know you cannot just discard all the rest of the data a particular block has so many information but you just want your information what the graph does is separate the information and just gives you the uni swap information that you

require right you can do it in other ways too you can basically run a iterator on every block in the blockchain get your information put it on you but that would take a long time so if I go to info.uniswap page today with this particular method I will access it today and I will get this information in net next Eid Soul which is in June 2024. how do you make it quick that is via the graph the graph is a middleware in between your smart contract and your front end so that you can index your information which is your smart contract event information and put it on your UI if I take an example if I go to for example this thing called info

dot uh you uni swap right this most of you who have traded or have you know understood web3 must have gone to this particular website all the data that comes on this particular dashboard of uni swap is powered by the graph uni swap has smart contracts uni swap has a front-end but how does this particular data populate into the unisa front end that is via the graph and if you don't believe me which you should you can also check it here I just can type and I will be able to get the you know graph data just expanding on this header if you check this is okay

not expand this further if you see this particular API this is the graph uni swap API so what the uni swap team did to index their smart contract needs is that they created a subgraph for uni swap smart contracts so that they could portray their information on the front end and you can either you know use the already created sub graphs for your own needs let's say you want to create something like crypto fee info which is a great website crypto fee info right these people don't have most of their contracts right they don't have their own sub graphs they use let's say

uni swap sub graphs for this particular information and this is such an important important information they have not created their own uni subgraphs but they have used the already created uni swap sub graphs for this particular information but if you are building a Dap and you have your own smart contracts then you can build your own subgraphs and this is what we are going to do today so let's stop talking and let's start coding okay uh if for big for somebody who does not know the graph they would have to install graph CLI so if you have a laptop and you have the urge to code let's just please follow me this is let me just go to the grab.com

studio is this visible guys I hope it is uh you then go to create a subgraph no wait let me just go to create a subgraph and name it Krishna who's right here and I'll be deploying it on sepolia because nowadays even goldily is expensive create a subgraph so our sub graph is created no I'm just kidding this is just the dashboard to create a subgraph right and uh first of all what I'll do is install the graph CLI which exists over here so this is the information you need

right for the graph tooling this is not the start of our Workshop this is just the information you need the libraries and the packages you need before you start using the graph once you have this the most important thing is let's create a smart contract first I go on remix I am on remix I hope everybody has used this if you have not then you should use it in this particular hackathon and I will create a new workspace and let's let's deploy erc20s you know token do an Ico get a million dollars and you know just get out anyways so I have created an erc20 smart

contract you know this is a simple basic vanilla erc20 smart contract which is mintable and what I do of this particular smart contract is I will deploy it on sepolia I have the smart contract very very simple let's just deploy it oh there's an error fine it deploys let's just deploy the smart contract up till here I don't think there is anything new that you guys like you know might be seeing uh no I have to shake it to metamask Let's deploy the smart contract confirm the transaction

and so I have my erc20 token smart contract deployed Hi Sam not good very good very good sorry so I have this smart contract right which is the erc20 token smart contract and I have named it my token right this is a smart contract and I have it is already verified on a etherscan if you create your own custom Smart contract you will have to verify yourself so this is a smart contract which is the erc20 token right and right now it does not have any transactions

what I'm gonna do now is the graph part as of now we have deployed the erc20 token right that's all that we have done and we have installed the graph CLI packages now I just go here close these things no no not this one yeah I'm in the terminal you might not be able to see it can I increase are you is it visible this is the place where magic happens right I okay I'll create a m e Soul I'll create a folder for myself

go inside that particular folder right now I will just do a command and my sub graph will be created just buy a generative you know tool that is the graph CLI so I do graph in it dash dash index events Dash Dash from contract uh I go to the place where my smart contract is not this one as the other one yeah here is the Smart contract address I

take that smart contract address and I just paste it over here and I'll have a sub graph which will be indexing all the activities that I do on that particular smart contract Let's do ethereum let's do it on the graph Studio let's name it Krishna uh I'll deploy it on sepolia yeah and this is the Smart contract it does is being able to fetch the start block the start block is not this one man okay the start block will have to find

out is this one foreign the dependencies so that my erc20 sub graph is created what I've done is deployed a smart contract right and now I want to track all the activities that I do with that erc20 smart contract let's say I mint a token I transfer the token all these things will be tracked via the subgraph and that's we'll see right now how did it happen no okay our smart contract is here our subgraph is also here

okay so we have our sub graph deployed over here let me just go a little detail to explain you what does this contain so this one has first of all your smart contract address secondly it has the start block and third it has all the events that are in your smart contract which includes approve transfer all these things right when you create a smart contract you always Define events in it these are the events which are listed over here right and these are the event listeners that exist so this is the yml file which introduces your subgraph to the graph

node that hey this is my smart contract address these are the entities you have to index which is to it secondly what you do is Define the schema right you define what you want to see if you're deploying a uni swap sub graph you would Define let's say a swap right and in that swap you will Define the token name the amount all these things when I'm deploying the erc20 token uh there is uh there are like you know a few events that happen inside let's say approve ownership transfer transfer all these entities are predefined but they are aspiration rated code if you want your own entities you will have to you know just change this particular entry schema according to your needs the third thing and the most important one is the

mapping file this has all the code so basically what graph node does is that it sees the event it gives you all the raw data this mapping file converts that raw data into schema file let's do it again I know I'm a little uh you know fast so basically what we need to do is that graph node indexes all the event data and gives you the raw data but you don't need the raw data you need this schema data right so the mapping file converts the Raw event data into the schema data so basically all the event logic all the data and logic resides inside this particular mapping file and that's all that you need to

code to create a subgraph we have everything we have a smart contract deployed we have our subgraph created now let's deploy it and I will I'm going to show you how easy is it to do it uh this is my studio first of all hopefully you have you know installed the dependencies and I have done the init the next thing is I do the graph auth this is just something that's required to you know just Deploy on your place um okay graph or done then I will do a code gen and a build and again I'm not you know I'm just copying things from the

dashboard and I'll deploy the subgraph Point 0.1 version okay we have our subgraph live over here this is the erc20 uh you know token and this is our subgraph which is indexing all the data right now what data do we have nothing right we have not done oh we have something what okay let's take a transfer we never never did a transfer let's let's take a transfer

transfer um transfers first 10 . see I am legit right we don't have any transfers now what we will do is mint a token let's mint a token mint I'm gonna mint it on myself right because it's an Ico remember the foundation gets 100 allocation I'm just kidding mint to this address let's do 100

transfer confirm and you can see once the confirmation happens I will get some information in my transfer etherscan is still indexing the data but the graph has already not index the data it happens guys it happens we'll we'll blame it on the internet Erica see

[Applause] so you know demos should be fun right they should not be oh guys this is my project uh so this is this is how you you know uh create a subgraph and index all the information from your smart contract and good news for you guys is that I'm in Seoul so you can run about subgras do some hackathon stuff and at the end my treat is the Soju that's all I want in Korea of course so if you have learned something from this particular demo do tweet about it this is you know how you scan let's create some noise that you deployed your first subgraph let everybody know that subgraphs are awesome the graph is awesome and

soul is awesome Soju is awesome Fried Chicken is awesome Erica who has created this crypto Soul things is awesome mix are out yeah uh why do we need sub graphs because there is about 15 million blocks that exist on ethereum and if you scan every particular block and get every particular transaction in that particular blockchain you will be paying more money to Google then it's required to buy the whole Google right that's why you need something like the graph so that it can index transaction data and give you all the results you either create your own subgraph if you are deploying a smart contract or you can use already existing subgraph

for amazing dashboards we are sponsoring this hackathon for all the amazing hackers so if you're creating a new subgraph we have 1800 uh which is awesome or if you are using an existing one we have fifteen hundred dollars as you know grand prizes and me and Krishna as we were saying will be here all two days so if you are writing a smart contract and deploying smart subgraphs then reach out to us we'll be easily be able to help you out as I told you it's a very easy step deploy a smart contract do graph in it have the index data create a schema use that schema for your front end that's it thank you

Related talks