[ETH Seoul 2024] Querying Web3 Data With The Graph by Pranav Maheshwari

ETH Seoul 2024 · 11 Apr 2024 · 19:05

Pranav Maheshwari, Solutions Engineer at The Graph, explains why reading data from a blockchain is hard and how subgraphs act as middleware between smart contracts and a front end. Two live demos follow: querying an existing CryptoPunks subgraph through the Graph Network with an API key, and building, deploying and publishing a new ERC-20 subgraph for a Base token. The session also outlines the hackathon bounties.

Speakers

Topics

From ETH Seoul 2024, hosted by Kryptoplanet.

Transcript

hello everybody really be happy to be presenting for the third time in each Soul uh this time we'll be going over two demos first of all would be querying the graph Network and secondly we'll be deploying a subgraph on the graph network uh but first things first the most important part uh we have $2,500 bounties for everybody hacking up for grabs if you're building a new subgraph which will be talking about in this presentation in some time you have uh the first prize at $800 uh second at 400 and third at 300 there are uh if you're create using an existing subgraph that's already created on the network then also we have really amazing bounties so definitely check them out before uh like you know you start hacking all right what were the

problems which led to creation of the graph Network and you know people deploying subgraphs uh the biggest issue is that getting information from the blockchain is really really tough it it is like kind of impossible to get the information from these Ling list structure to your particular front end uh if you want to actually get information let's say of an nft which is uh transferred 10 times it would take this big of a code to get that information to your front end right and enters the graph the graph is the middleware between the blockchain and the front end so that you can basically deploy a nft subgraph and get the information from the blockchain to your front end and at the end can just write simple graph qu queries to query that

particular data very simple very easy right you can consider this as a blockchain in which this particular uh is a block right it has information about maker it has about Uma it has about Unis swap but you need only information related to Unis swap so the graph converts this data which has so much information about so many things you don't need into these particular database packets which you can directly extract uh transform query and load right so that's the reason why we have the graph and for creating your own uh data machine you have to create something like a subgraph a subgraph is basically a smart contract extracted so that you can query information right a subgraph consists of a smart contract schema design and mapping structures

that's all about it once you have deployed a subgraph you can use the graphql API to basically make queries using subgraphs there are really really important amazing front ends one of them Unis swap which actually has deployed a Unis swap subgraph to get information like this if you have traded on Unis swap or have been on Unis Swap this dashboard is created using something like a Unis swap subgraph so if you are deploying a smart contract you should always have a subgraph for your front-end app if you are actually creating a dashboard or aggregation of so many uh dashboard you should uh qu very existing subgraph for your needs so first of all first thing uh there are two types of bounties right the first one is uh use of existing subgraph

that's the first Bounty that we have right uh so I will be giving you a short demo as to how you can actually uh query an existing subgraph right uh from the graph net of course this is the graph Explorer you go on the arbitrum uh one network basically select a particular subgraph of your choice and you can start quing the network right it's very simple just find out the subgraph you want on the graph Network and you can start quing it the way you want it to all right uh so you will be basically have to set up a API key so that you can query the network let's do it live IRL uh so let's see this is the question right I have to figure out the top 10 sales of crypto punks uh in the history right the top 10 sales that have ever happened the top 10 transfers that have

ever ever happened so what I'll do basically is basically first of all go to the graph Network this is very small okay the cph network first of all I go over here right and I go on crypto punks I want to find out about the transfers of Crypt Punk so I go on Crypt punks yeah this one this is the subgraph I actually want to query to find out the top 10 transfers that have happened in in uh the crypto punks ever in history I go on playgrounds and I basically start searching over here about transfers yeah here okay I don't want this I want transfers these are the transfers I

found out the top the transfers the top 10 first 10 right nothing else just delete this the the first and transfers by ID and that should be it right I should be able to query and find out the top 10 transfers in crypto punks that have ever happened and this is real like if you want to check the top 10 transfer that happened let's just go on ether scan and find it out which particular transfer was this in crypto Punk etherscan.io and uh go on the address and you see oh here this was the crypto Punk that was transferred like you know back in those days it was transferred for zero

ethereum which is very interesting so that's what I'm saying right and 36 ethereum gas fees oh anyways this is all about it this is how you can basically make a subgraph to actually create amazing dashboards I showed you a particular query that you can run on your front end so that you can get the information you need you can use existing subgraph you can create your own subgraph based on what you want if you're creating a dashboard simply I would suggest you to use an existing already created subgraph and this is an example of that so the way you query the graph Network remember the bounties are only for querying the graph Network it's not for actually uh uh using the playground or the subgraph studio so the way you

actually do that is basically go on the subgraph studio uh connect your wallet metamask sign the transaction here is your API key right I have a few you get 100K free queries per month after that it's charged so you I created a API key I can just basically go here over here and copy my API key this is my API key go back to the subgraph that I wanted to query the gra.com Explorer in the crypto punks

right this one query oh it's not showing up yeah this is the query uh I can just copy this if you try to Deep dive query copy this particular query paste in the information in let's say this place then I should be able to make the query

happen right on I paste in the API key from here yeah this is where I have to write the API key then I should be able to make the query that I want this is the query and uh here's I query yeah I should be able to get the information I need but remember you have to you can check the subgraph on the playground test and see

what you want to query but use the query button that's given over here click on that get the API key from the subgraph studio and then start querying the graph Network that's the first kind of bounty that we are offering right the second Bounty that we uh so you can basically create really amazing uh dashboards if you have seen this crypto fee. info dashboard pretty widely used in the crypto world it was created uh using the graph Network so you can create really amazing dashboards that are used by the whole world using the graph subgraphs so that's about it uh there there are other examples like dap looker that you can look which have used existing subgraph to create their dashboards but uh there

is a second one which is how to create a new subgraph if you're writing your own smart contract in the hackathon then you would want to create your own subgraph as well uh so let's just deploy a subgraph and see how that looks like so to just make it more fun I'll set a because it's very easy I'll set a timer of 5 minutes time more of 5 minutes to see if I can deploy the subgraph in 5 minutes and yeah I started this uh that particular thing now let's deploy a erc20 subgraph and let's do dgen D is a very famous token nowadays so let's go to base scan uh here find the Deen token yeah this is the Smart contract this is

from where I will be creating the subgraph right so so the first thing first I do is go on the graph the graph Studios over here connect my wallet sign the transaction and start creating a subgraph let's Name It dgen We want all the information related to dgen the smart contract so we will start creating this subgraph this is the d subgraph uh that I've have created in

the studio you need to first of all install the graph protocol CLI uh globally so that you can use it so that's the first thing you you got to do and the second thing is after that I go on this here okay are you able to see this now maybe yes I can expand it a little bit okay I guess now you can see it uh let's name okay mkd so so first of all you need to make sure that I have graph installed so I can just check over here that I have the graphs uh repository installed now I

will actually deploy a subgraph live in front of you guys which is the dean token subgraph let's go to graph in it ethereum subgraph Studio the slug has to be deun and then we choose base chain where is base okay base chain smart contract address is this one we copy the smart contract address paste it here get the API key oh the start block we could not find you know let's paste the smart contract uh the start block from here this was the

smart contract it was created in this particular transaction at this part particular block number so I paste that contract get the entities and I should be able to create a subgraph let's see how that looks like it's very easy to create a subgraph that's why I have set up timeline of 5 minutes by then we should be able to create a dent token subgraph and start querying information related to G Deen like transfers the top 10 holders of De tokens and stuff like that let's see how we doing on the

time oh we have only one minute okay maybe maybe let's let's extend to S minutes all right uh DJ I might make a world record right now but let's see okay here I have created the smart contract let me make this bigger for you guys see I have created the smart contract here you can see the toml file which has information related to my smart contract right all the transfers

the delegation the ownership transfers the posible the meable stuff like that uh and now I can just uh you know you can see over here the entities that are created as well as the mapping files that are required we can talk about that later but this is the whole of the dent token subgraph that you that is required for you to actually query right so now we go to authenticate this particular uh subgraphs go terminal yeah graph authenticate then uh graph code gen and

deploy and then we publish the subgraph on the graph studio version one all right so the subgraph is published on the subgraph studio as you can see over here right 82% already there and now I can start querying the Deen token information that I could not so this is amazing right and how much time did it take we okay all right let's forget the challenge but we were close 4 minutes 5 minutes something like that right so that's how much time it takes takes to

build a subgraph when you have a smart contract and you have deployed the ABI Keys uh very very easy now I can just query this particular subgraph uh for example I want to check out all the transfers that have happened the first and transfers or something like that I can just find it out order by descending so the highest amount of transfers in Deen token that have happened I can just find it out over here right these are the number of transfers and this is the value that was transferred so really amazing just just have a smart contract and you should be able to deploy a subgraph based on uh you know your needs uh the last step and the most important one is basically querying the subgraph studio is not enough to be eligible for a bounty you have to publish this

subgraph on the graph Network so I have to publish this on arbitrum okay publish the subgraph on arbitrum yeah just ignore the thing that's there and I'll try to publish the subgraph if you want you can take some arbitrum E from me to basically deploy your subgraph on the graph Network it's on you uh confirm okay confirm and my subgraph should be deployed on the arbitrum network yeah

it's deployed let's check it out DJ yeah this is my ens address zk. if you like my uh demo then you should always donate no I'm just kidding so you can actually just query this subgraph on the subgraph on the graph Network and that's about it uh that's all that's required for you guys to basically deploy a subgraph uh just to give a recap of the presentation you can use an existing subgraph for your needs or if you are deploying your own smart contract you should have a subgraph uh and you know this is the way you can actually use it so that's about it for

the presentation uh if you have this is our hacker kit you should always scan it to get more information of uh uh basically everything related to subgraphs uh we also have a telegram group created dedicatedly to help you guys hacking in this particular hackathon uh if you have any questions I'll be around all the 3 days you can ask me we can deploy a subgraph together again if you're writing a smart contract you should always have a subgraph so if you have any questions you can just reach out to me and we will deploy the subgraph together that's about it uh if you have any further questions you can also uh check check check out my Twitter or Telegram and message me there for any further information thank [Music] you