Indexing Smart Contract Data with The Graph - ETHGlobal Singapore 2024
ETHGlobal Singapore · September 2024 · 21:11
Pranav Maheshwari explains why blockchain data is hard to query and how The Graph acts as a decentralized middleware between contracts and front ends. The workshop covers the three parts of a subgraph (manifest, schema and mapping), then shows how to query an existing subgraph through Graph Explorer and Subgraph Studio using a CryptoPunks sales example. It finishes with a live walkthrough that deploys an ERC-20 contract and indexes it with a new subgraph.
Speakers
Topics
From ETHGlobal Singapore, hosted by ETHGlobal.
Transcript
[Music] all right uh so I'll be talking about the graph the graph is one of the most important Dev tooling and infrastructure in web 3 and we'll be going around how to query a existing subgraph for your data needs and how to create a subgraph okay so let's do it really quickly I'll always want you guys to have your mobile and camera out because I have a lot of QRS to show you guys this is the first QR that you need to scan if you are hacking on the graph here we have all the information as well as the repository is important for the graph work we have a lot of amazing bounties uh so yeah I mean I hope that
motivates you guys so first of all what is the graph the basic problem while we were creating the graph what we understood is that blockchains are amazing they're a mutable databases but they are really hard to query it's impossible to get the data from the blockchain to the front end without just you know ramping up the RPC just banging the RPC and getting the data and it's very expensive hence uh the front ends are always struggling with data it's very tough for front ends to get information just imagine you want the crypto Punk of 2021 the RPC will have to Traverse through 3 years find out which Crypt punk are you looking for find out in
that block and give you the data that's required that's actually an implementable it's like you made a query today and you'll get the results in 3 days hence we uh you know it's it's it's not amazing that's why we created something like the graph the graph is a middleware between your blockchain and your front end to get the data you basically you can consider the graph as uh getting the data for you guys storing it in a middleware layer of course in the decentralized manner and then you can query the data as for what you need and how you need right uh the reason why we wanted to create the network a protocol a decentralized network for that is because there are sometimes centralized indexers which you can use
of course to get the data but then why are you using a blockchain right blockchain is for storing data in a decentralized fashion and hence no middleware should be allowed to manipulate the data hence you need something like the graph which is decentralized which is by the graph Network to make sure the sanctity of the data remains intact and your front end gets the most important data in the real time fastest way possible okay now let's understand what is subgraphs uh subgraphs are basically you telling our infrastructure the graph node to tell uh to index whatever data you need let's say you are a crypto punks holder right and you want to index all the people who are cryptopunks holder so you will be basically telling
graph node that hey Guy this is my smart contract these this is the data that I'm looking for can you please give this to me and that is basically a smart contract program uh subgraphs are basic basically a program written in typescript to basically tell everyone that this is the data that's required can you get this for me so you can consider subgraphs as the programming language or the programming standard that you create tell the graph node that is the data I need and you know just get it for you guys you can see a live footage of how uh a subgraph basically works right you can consider all these balls as unisab data Uma protocol data a data you know all the SS of data mixed
in one particular blockchain and you can consider these black uh these wooden things as a subgraph they diversify everything and get you what you need directly so that you can query it so what is a part of the subgraph what is uh basically inside the subgraph right so in a subgraph there are basically three files the first file is the yl file this is basically the introduction of your subgraph in a yl file you mention the smart contract address the start block and the handlers in which will be handling the data the logic file is the mapping file in which you you get the data from the events right and you convert the data with the uh with the mapping file so basically you can
consider the schema file is the file which you have to use for your own data needs so basically let's say you are again we go back to the cryptopunks example and you're looking for the top 10 sales so I will be creating a schema which is the sales right so say sales ID of the sales the time stamp in which that sales was done the amount of uh eth that was required to buy that crypto that crypto Punk that will be a part of a schema right so I will first of all create that schema right and I will be getting the data via events of the sales contract in the Crypt Punk smart contract so in the mapping file what I'll be doing is basically writing the logic to make sure that that sales that
is a event that gets converted to the schema here okay so these are all the three files the yl file will be constructed pre-constructed when we do graph in it I'll show you in sometime the schema file is the data that you're looking forward from that particular smart contract and the mapping file is basically you can considered as the logic to convert the event data from the smart contract in the schema I hope if that is clear let's first of all query subgraphs there are it's not always required to create your own subgraph if you need your own data if you are creating a smart contract and writing your own dab from start then you need to create your subgraph if you are quering an existing dap for your data needs then
you can just query the existing subgraph let's say you want to get Unis swap data for to understand the different you know uh prices of Unis swap usdt e pair on different chains Unis swap is deployed on five to six chains you want to have that data how do you get that data there are existing Unis swap subg graphs on all these chains you can query these smart contracts and get the data for yourself you don't have to basically basically create a new smart contract right and for that the most important part for quering an existing subgraph for your dashboards in this particular hackathon we have two prizes first prize and second priz 3,5 500 uh 3750 which is the first price and then
$1,250 if you become a runner up right so how do you query a subgraph let's look at this this is the legendary graph Explorer which also has uncx Alex yeah okay cool uh and how do you get your data first of all you look at the protocol that you're looking for right and let's say I want to query bits World I'll just query it like this let's just see that let's say I want to have uh uh uncx data for example how how do I get that I just uh you know go to uncx search who whatever I'm looking for I get it here and I can just query it right here okay so let's say I'm looking
for a data I just go to subgraphs again I search a get the best subgraphs from a misari subgraphs are the best subgraphs that have been created uh you can just go to query get this query and get things going here you can see there is something called as the API right this API you will have to extract from the subgraph studio I'll go to the studio and I will be able to create my own API it's pretty pretty standard there is no mental math or like you know crazy amount of work that need that needs to be required to do that so it's it's pretty simple just first of all set up your API key once you have identified
the protocol that you are looking to query the data from uh get that particular subgraph from the Explorer go to the subgraph studio get the API key access the data and you know win $3,750 simple right are we good with time okay I think we good the second thing uh this is how you query the data Let's do an example to query Crypt pun's data I want to have the top 10 sales the first 10 crypto punks that were ever sold let's try to find that out so here I go to subgraphs I here come on crypto punks uh let's do this one the first 10
sales right remember so here I go to the first 10 sales that were done of Crypt punks there is a sales entity here you see it sales of first 10 let's close this part accounts let's just look at the top 10 sales of Crypt punks top 10 so here it gives you all of them but let's say we just need order by uh no order by oh let's see the amount of like you know the amount in which they were sold amount it gives us all the crypto punks that were sold in ascending order
right this is the amount of crypto punks being sold like what value these crypto punks were being sold at you know the cheapest one to the most expensive one and I can actually Rectify this on the Explorer I go to Explorer ether scan you know put this ID this ID from here you see this this is the nft that was bought which is a crypt Punk let's given that this is not very visible and other few transactions were also done in this this is a red Crypt Punk somebody did
some deun stuff with this so maybe we query another Crypt Punk see this is the Crypt Punk that we are getting and talking about swapped in $69,000 uh uh so yeah this is uh all you can query so if you're looking to create a crypt Punk dashboard all you need is this particular subgraph and this particular API uh and you will be able to create the best Crypt Punk information dashboards this is all about querying subgraphs now let's move to creating a subgraph right if you are creating your own dap if you're
deploying your own smart contract you definitely need to uh create your own subgraph again two prices $3,750 and $1,250 the architecture of how a Dap looks like when you create a subgraph of your own is something like this first of all you have the blockchain which can be ethereum or any Layer Two that the graph is deployed on we are on 39 chains uh secondly you will have the smart contract that you are writing and to get the data from the smart contract to the front end there is a middleware called as the subgraph that you will be building okay so let's build a subgraph very very quickly so I will be deploying
a dummy erc20 SM uh smart contract very quickly this is a smart contract right uh erc20 minable mergeable right uh let's just deploy it okay I have some seipo that should be fine hopefully let's deploy estimated gas will fail oo demo gods are not with me today okay so what we'll we'll do is we'll do it on arbit from Main net I'll be spending my own money for this okay thank you thank you guys let's deploy oh
again reject okay maybe we create a new one new contract uh let's just go to this let's create a new uh new create new template make it erc20 minable posible yes okay uh my token like you can see over here I created that my token it has all the things that are required let's deploy this on arbitrum very very quickly compile it
compiles now let's deploy for that I need to disconnect metamask and connect again just because web 3ux connect account connect confirm see I deployed now I need to add the address and deploy the smart contract on arbitrum oh wait I need to inject me metamask hopefully now it works disconnect connect again okay Works
hopefully now let's deploy the contract on arbitrum Main net see I deployed the smart uh smart contract first of all on arbitrum you can see this this is the Smart contract that's been deployed and I should be able to view it as well uh let's see let's go to the smart contract that's been deployed RB scan search so I deployed a smart contract this is
the Smart contract that you can see that's been deployed okay now we are going to Index this particular smart contract for that what I need to do is go to my Explorer and uh basically CD CD desktop oh no downloads downloads Craft demo mqd SG here I'll do first of all uh what you need to do is have the graph Library installed right like you can see I've already installed the graph library but it's really easy to install graph library in your local place with npm once you've installed graph Library I'll
do graph in it on ethereum subgraph studio and name it uh erc20 uh new and I will be on arbitrum one this is the Smart contract address is this one this is the Smart contract address I give it the smart contract address it's not able to fetch the ABI so I'll have to get the ABI from normally it fetches the ABI if you have verified the smart uh verified the smart contract but if it doesn't for some reason because ether scan apis are down I can get those abis for you guys so basically get the Abi
uh and store it somewhere so I create a new ABI folder code oh oh let's create use a new terminal okay CD desktop uh c m KD new ABI CD new ABI code I store the ABI in this Abi Abi do Json store this here copy the file of
the ABI copy path and uh put it to our terminal which is this one I can just be like no no no okay this is the path see it got the smart contract that needs to be created and we'll have the smart contract created in just one second right so basically I put in the smart contract address and the start block and it will create the subgraph for me that's how the graph tooling is really amazing now let's deploy the subgraph on the subgraph studio this is the subgraph studio let me just connect my metamask
create the subgraph name it erc20 new create the subgraph see I created the subgraph I already did the global graph protocol installation right I already did graph in it now I need to do graph o so let's do graph o uh okay CD I get into the subgraph I do graph o now I need need to do code gen to make sure all the code is being compiled and now I will just show you how the subgraph looks you see this this is the subgraph I showed you it will
have three important files first is the schema file second is the yl file and the third is the logic file which is the mapping file here you can see so the schema uh so the yl file has the smart contract address and the start block as well as our events of the erc20 uh smart contract then there is this uh schema file because we want to get approve uh information of our erc20 approvals uh ownership transfer posible minable transfer all these information we need so we created the schema for that and the third thing is of course the mapping file this is the mapping file uh which
has converted the event data into our schema data these are the three things that are required once this is done now I will deploy my smart contract on the I'll I'll just deploy my smart contract on the Explorer which one was it yeah this one I'll just deploy the smart contract subgraph I mean and it should work so I have deployed my smart contract right I have now deployed my subra gra and it will give me a API which is this particular API which I can use to query my front end okay all these three things are done now you can happily create your decentralized
application and of course win $3,750 right my subgraph is created now I can play with it as you can see right now it has uh let's see if I approval approval let's go to approval right now the approval has nothing now if I make a particular transaction with approval here let's do an approval uh let's do an approval uh wait approval this is the address that's I'm looking to approve approval and the value is let's say 100 and to the
transaction Max approval so this data needs to be indexed by a subgraph as well because we have triggered an event right so when I see this this is the Smart contract that I deployed it should have the data see so basically you can have a smart contract deployed a subg grab deployed and query it with your front end if that's clear that's all about it you guys can uh you know definitely subscribe to the newsletter and thank you so much if you have more questions we are on the booth we have a lot of good swag and a lot of good bounty please thank you [Music]
Related talks
- The Graph 🛠 Indexing your smart contract with The Graph - Pranav Maheshwari
- Decentralizing querying in web3 through The Graph Protocol with Pranav Maheshwari
- Best use of Goldsky - Superhack 2024
- The Graph 🛠️ Quickstart to Subgraph Development & Querying - Marcus Rein
- 2023] Full Stack Web3 Development with The Graph by Pranav Maheshwari
- Defining Your Web3 Stack: Building Fullstack Dapps
- The Graph 🛠 Building decentralized, serverless applications - Ørjan Auestad
- Dune Analytics 🛠 Open Data for Open Finance
- Воркшоп «Блокчейн-аналитика в реальном времени» | Алексей Студнев
