The web3 stack: building fullstack dapps with Camila Ramos Garzon

ETH Seoul 2022 · August 2022 · 22:35

Camila Ramos Garzon, a developer relations engineer at Edge & Node, a core developer of The Graph, walks through the web3 stack for building full-stack decentralized applications. The building blocks include the blockchain, development environment, off-chain storage, indexing, identity and client libraries. Camila shares a personal stack using Polygon, Infura, Hardhat and Foundry, IPFS and Arweave, The Graph, Coinbase Wallet, RainbowKit, ethers and wagmi, then gives a crash course on subgraphs and GraphQL queries, starter repositories and the open-source 30 Days of Web3 course.

Speakers

Topics

From ETH Seoul 2022, hosted by Kryptoplanet.

Transcript

thank you before i get started i just want to thank the organizers and the production crew and the volunteers and everyone who helped put this together it's been a really great experience so far my name is camila ramos i'm a developer relations engineer at edunode which is a core developer of the graph protocol and a little bit about me a quick intro i kind of have a traditional and non-traditional path to kind of crypto and engineering in general my background before i did computer science was in education i've always loved teaching i've always loved education and i always thought i was going to be a regular kind of k-12 teacher and then in high school i got interested in computer science and coding once i kind of realized that

teaching doesn't really pay well in the united states and it's really just not a good path to go down in terms of quality of life so i was like hey look i live in the bay i see everyone has teslas everyone's here as an engineer they do pretty well so i'm going to learn how to code too and then i did i studied computer science in college and then started off my career at paypal where i worked on the checkout experience as part of the engineering team so everywhere where you're checking out and you see the button pay with paypal that's what i worked on for about a year and a half um and then today what we're going to talk about is the web 3 stack and kind of a more practical point of view so when i was getting started building in blockchain and in

crypto it was kind of hard to understand how to make all the protocols how to string them all together how to make them communicate with each other how to actually build something that works so in this talk i'm hoping to outline this in a really practical way and show you guys code and kind of very specifically and explicitly explain how you can build a full stack decentralized application so um a lot of these definitions are from a blog post which are linked in this presentation and i'll tweet out the slides with all of the links so that you can read more about these and actually like be able to check this out yourself so the web3 stack as defined by this blog post that we're talking about is a set of protocols that enables fully decentralized applications um so real quick i'm not going to talk

about these one by one because i think everyone here kind of is passionate and understands why we're here i usually use this slide when i'm talking to newer audiences and maybe some web 2 audiences but i just wanted to add this so we can kind of capture what we're going to talk about in the next few slides and how these protocols enable these points so when we're talking about building decentralized applications and maybe if you're coming from like a traditional engineering world actually maybe i shouldn't stand here because i'm blocking sorry there we go if you're coming from kind of traditional engineering there's certain pieces that and architecture that you're familiar with right we know that we work with apis we have this concept of a front end we have this concept of a back end we

have this concept of a database etc and i found that it's helpful to kind of start by listing out what are the building blocks and the primitives that you need to keep in mind when you're building so blockchain your l1 your development environment file storage off chain data protocols api indexing and querying identity client frameworks and libraries oracles and then kind of in their own bullet point is web3 social protocols and again at the bottom there's linked this blog post that was created by the research team at edge and node and that was authored by nader who's sitting right there where it goes kind of more into detail about all of these things and then this image was really helpful to me when i first started building because it kind of visually lays out the

whole stack and then it allows you to kind of check this out and define for yourself what is an ideal stack based on my use case so i'll leave this up for a second and we're going to kind of go through this and talk about what my stack looks like and then go through some screenshots of some code for how to actually implement something like this okay cool i also have these uh so web3 social i wanted to highlight live pier and lens they're part of you know this last bullet point oops here about web three social protocols that kind of weren't captured in any of the other bullet points but that i wanted to highlight so live pier is a decentralized video infrastructure that allows you to

integrate live streaming into your adapts and then lens is a decentralized non-custodial social graph for building composable social applications and in this slide there are two resources linked like a tutorial to get started with both of these which again when i tweet these slides out you'll be able to actually click on these links and access those so talking about kind of what a stack could look like this isn't the stack for everyone you know a lot of this is up to personal preference it's also kind of where you are in your timeline of learning and you'll notice that in one of these i have an arrow to kind of indicate that so let's talk about this so when i build on ethereum i use polygon as my side chain and um for my rpc provider which is actually the the way for you to write to the

blockchain it kind of provides that node that access point to the blockchain i use infira but the other ones out there are like alchemy anchor um there's a bunch more i use infura for my development environment i started out using hard hat that's kind of what i used the whole like nine months or something up until this point and then recently i started learning foundry and trying to use foundry so that's kind of what i mean when i say this is really relevant to where you are in your journey you might have started off with hard hat because that was what was really popular a year ago and kind of now foundry is picking up speed and picking up adoption and after you've worked with hard hat you'll be able to see like okay this is a better experience for xyz but for other cases you might have a better experience

with other tools so it just kind of depends where you are and what you're working on for off-chain storage i use ipfs and web3 storage and recently i've started using rwev another example of kind of just depends where you are in your learning i started off with ipfs and web through storage and then recently for an app i was building there was something that was a lot easier to do with our weave so i kind of just tried it for the first time and realized that that was a better choice for this specific app that i was building but they both do similar things it's a way for you to store data off chain such that you you know storing things on chain is expensive so there's not everything has to be on chain but still you want to be able to store these things so you can use something like

ipfs slash web3 storage and rweave for indexing i use the graph and that's where i do developer relations and we'll kind of get into that more deeply about what that means for identity i've been using coinbase wallet as my development wallet coinbase wallet is i'd say a relatively new wallet but they actually have a lot of cool features for developers and they're really good at listening to developer feedback so i've been working pretty closely with the team to give them my feedback and it's been pretty cool to see them implement some of this stuff and then i use rainbow kit for that piece that kind of allows the user to connect their wallet and we'll talk about that more later and then finally for client i use ethers and wag me so now how many here have heard of the graph

by a show of hands okay cool how many people have actually used the graph okay so not that many so this is great so i'm going to kind of give you a crash course 101 for how to build with a graph what it actually means so the graph is an indexing inquiry protocol that allows developers to efficiently read data from the blockchain so you know as we're writing blockchain applications a bunch of stuff gets stored on chain and you as a developer you might want to access this data for your own application and right now there's really no efficient way to make queries against the blockchain for things that are more sophisticated than asking single questions for example with something like etherscan you can go in there and ask who's the owner of this specific id or you can say what's the uh what's the

balance of the specific wallet you can ask these kinds of simple questions for but for more complex things like you know give me all of the wallets that have you know 10 ether or more and own this nft you can't really do that right now without kind of writing your own indexer and going through this whole process so the graph makes it really easy to do that and at the time of writing these slides this was the current block count of ethereum 14 million i think now it's past 15 million and in a block you can fit an average of 70 simple transactions so we're looking at at least 15 million times 70 pieces of data that you as a developer might be interested in so it's a whole breadth of data that you as a developer have access to and the graph is the door for you to actually access

that and build with it so the data you want to query for is somewhere in the blockchain right but due to the nature and the structure of a blockchain there really is no built-in way to identify query for this data and then like i said it's a querying an index protocol that allows you to read data from the blockchain and the cool thing is that if you come from a traditional software background you're probably already familiar with graphql and the graph uses the graphql query api to be able to call this query from your frontend application so for me as a developer when i was first starting it was a really cool way and easy way to get started with web3 because it was something that was already super familiar to me and instead of reading from a database like you do in you know web 2

you read from a blockchain so some quick bullet points about the graph it's chain agnostic meaning that it's not built specifically for one chain there's support for many chains right now on our hosted service and then on the decentralized network all that support will be coming over there now instead of reading from a database like i said you're reading directly from one of these chains and then you as a developer you write these resolvers which are just functions that apply logic to transform the raw data from the blockchain into these objects that you define in your schema so we'll talk about this in a more practical sense in a second like what that actually means and then just a quick note on indexing so indexing refers to the process of turning unordered data into an order that will maximize the efficiency of a

query made against that data so a real life example is if you are in a library and you're looking for a book i don't know about world war ii you don't just walk around the whole library looking book by book row by row right there's a system in place these books are indexed in such a way that when you walk in you go okay this is where the history books are and then on this specific aisle it's where the world war ii books are so that's what the graph does with blockchain data it indexes it so that you don't have so you make very efficient queries against this type of data there we go so these things that you build with the graph are called sub graphs anyone can make one and once they are published and deployed they are open apis that anyone can have access to so

if i build a sub graph for my project all of you guys can see it and all of you guys can call it in your application if you wanted to and there's already a bunch of subgraphs deployed so like live beer radical uniswap a bunch of big projects already have a subgraph deployed so if you were interested in the data that for example uniswap is indexing and you could use it in your application just by calling it in your front end and this is just a cool little visual to kind of help you understand where the graph sits in the stack now so to define a subgraph like i said crash course we'll talk about this a little bit more kind of in a real world use case shortly after this but in order to define a subgraph you as a

developer you define a few things so the first is the data source or the data sources so this means what contract address you're interested in indexing so for example if i'm building a dap i have to write a smart contract for it therefore i'm interested in indexing this data so i'd pass in my address you could pass in multiple addresses the second thing that you define is the data that should be indexed this is the actual data that you're going to be interested in querying for later so if for example what i'm going to show you later we're building an application for event creation and management like something like eventbrite or something like luma people are able to create events like you know eat seoul and then users like you guys are able to rsvp to them me as a developer i would maybe be interested

in knowing you know who is rsvp to my event who created my who created different events so the data that i would want to index is that specific thing so who created events what's the name of the event what's the max capacity of the event etc and then similar information for who's rsvp the third thing is events you want to listen for so the graph works based on events solidity events that are defined in your smart contract so for example i might create a custom event called new rsvp created that will be emitted every time someone creates an rsvp in my application and this is kind of the hook or the trigger for the graph to know okay it's time to re-index because something new has happened and this is the mechanism for you to ensure that you

always have fresh data and that your data isn't stale because if for example you make a query in your subgraph to say you know give me the list of people who rsvp to this event and there's say 10 people but in the next minute two more people rsvp'd how are you going to ensure that you have those two people and how are you going to ensure that you have the most up-to-date data is through these events so when someone rsvp's it emits this event and then the graph knows okay we need to go ahead and re-index and get the freshest data so that's what that is for and then finally or you as a developer you write these resolvers that execute when these events are emitted so when this event gets emitted new rsvp created something has to happen some logic has to happen for

this data to be indexed and then for this new information to be stored and those are the resolvers that are written by you a developer and then this is just a screenshot of what it looks like to make a query in the playground so it looks kind of blurry but this is a lens sub subgraph so here's someone's making a query for profiles the total number of followers their profile id their handle etc so it just looks like regular graphql and now we're going to kind of walk through an app that was built and kind of go through all of the pieces that we talked about in the in the tech stack and how they actually communicate with each other so first i'm going to show some screenshots i thought i was going to be able to kind of live code and live show you um but i yeah that was impossible so

i just put up screenshots and then we'll talk about it so this is what the homepage looks like discover what's happening in the metaverse the events that are shown in the front are kind of the events that are you know in your area or that you're able to rsvp for this is the flow for creating a new event so if you were like oh i want to host a birthday party or something you could create a new event you set the name the time the capacity a refundable deposit event link and description and then you create it and then you have your own user dashboard once you connect your wallet where you can see both the events you have created and then the events you've rsvp'd for so it's kind of like now so when i say events that's what i mean we're talking about this specific app

okay so now i want to bring up this kind of architecture diagram and i don't know if it's easy to view but again i'll post these slides and you can see but for me this was really really helpful to start building dabs because it kind of lays out how everything actually communicates with each other and don't worry if it's not quite clear from this picture we'll kind of walk through it together in a second but on the bottom uh left for you guys you have your contract and the contract that you wrote specifically for this that handles things like rsvps new event creation etc and then you'll see at the very top above it you have your sub graph so the subgraph like i said it gets triggered by events that get emitted from our contract and then the subgraph

also um collects data from our off chain storage because although some of the data is stored on our contract some of it is not stored on the contract right for example something like the event name and description don't need to be stored on chain it would be super expensive to do so so stuff like that is stored off chain then we have our front end application our client app and the blue right there and you'll see that it reads data from our sub graph and then and then it also sends queries to our subgraph so you ask for a query you say hey give me the i don't know the top five events in my area and then the subgraph returns those that you can then display in your ui uh you'll notice going from my front end up we have rainbow kids wag me which is

the way for users to connect their wallet and finally kind of below that we have inferior that's kind of the rpc provider that's connecting the contract to wag me slash rainbow kit to our front end so remember infuria is the rpc provider that allows you to actually make transactions it's the entry point to the blockchain so that you don't have to run your own node so that's kind of where the role that that's playing okay so again i thought i would be able to live code so i just put screenshots and i tried to enlarge them as much as i can but i'll kind of walk through these real quick can you guys still see me if i stand up here okay yeah um okay so this is what it looks like to send data from our client application to web3 storage via an xjs api route so i think pretty much

at least for me my standard for building client applications is next js and they have a really cool thing that they have these built-in api routes so when you as a developer are writing and you want to say all right the data that i want to store in ipfs aka off-chain data this is exactly how you would send it so you have a body that you define that's maybe gathered by user input right and then you just do you know your classic kind of next.js api thing where you say okay fetch this route the method is post and i'm sending in my body super easy and then here's just kind of some error handling in case it doesn't go well so the next thing is how do you call the contract from your client so in your client application like aka your next js

app the thing that your users are looking at that you see like the buttons and the events how do you actually call the contract so using wagmi those are react hooks for ethereum and it makes it really easy by using this connect contract method and then this is literally how you call a function so the function from my contract is called create new event so after creating my contract all i do is i literally call the function create new event and then i pass in the parameters do some error handling and then done so now i've called a function on my smart contract from my client okay and then here is the actual api route that's sending the data to ipfs so in this slide we're sending it from you know

createevent.js which is kind of like a form that you saw remember the form so that's gathering everything in the body and then sending it to the api route and then here is like okay this is how you actually store it and like persist it in ipfs this is just some code that is from their docs it's nothing really special you kind of just copy and paste it from their docs but here you'll see we're making sure that the request method is post and then these are all literally um stuff from their from their docs so i'm passing in my body and then the rest i got from their docs so now that's done now writing your subgraph to index and query events so this is just a like a small part of it there's more to it but you'll see here we have something that

handles a new rsvp so these are resolvers these are the resolvers i was talking about so this handles our new rsvp and then the second function handles the confirming of an attendee so if you remember when we were creating an event we asked for an event deposit so you can set it to like point i don't know 005 matic or something like that and the idea of this is that if people don't show up then you as the event organizer get to keep the money as kind of an incentive to make sure people actually are showing up to the events that they are as vp for so the confirm attendee function is to is that mechanism to say okay you actually got here i'm going to confirm your presence so that you can get refunded so this is part of the subgraph that handles that i

won't walk through the code but this these are the resolvers it's only like maybe 10 or 15 lines for each one okay cool and then configuring the graph in your apollo client file super easy seven lines you just are defining the url for your subgraph which for us it's the graph slash subgraph slash web three rsvp and then how you actually make a query how you actually fetch the this data is by just using like i said a regular graphql query where you can write it inside of your file just like you would with regular graphql you would save it maybe into a variable called data and then display that in your ui maybe you would map over it you know if there's many events you can map over this data

go to events and then show a little box for each that's what we did and then so inferior how to configure infura in your app.js file you literally this is just straight from their docs there's nothing kind of like unique about this where we just set the inferior id which you get from your project dashboard um you configure your chains for this specific project we use polygon mumbai we wanted to use a test net and then down here we just defined the app name and the chains which we defined earlier and then again configuring rainbow kin and wagome this is straight from their documentation nothing unique about this so we just said we created the wagmi client and then down in our app we wrapped our app with apollo provider rainbow kit provider and wag me client which is from their documentation

and then actually having the access to the connect wallet button from rainbow kit is super easy you just import connect button from rainbow kit in any page of your application so either in your index.js and then any other page is part of your app and then how you use it is then you literally just use it as if it was a component built in super easy okay cool so that was kind of my maybe i'll go back i see someone taking me here oh you wanted this one okay um that was kind of my like practical crash course of how these actually communicate with each other and now i have two quick starts so this is a typescript front end starter repo which has rainbow kit wag me tailwind css and an xjs app already up and running so all

you have to do is fork this clone this npm install and then you have something running this isn't typescript and then there's this other one which is in javascript that has a little bit more to it so it has rainbow kit wag me tailwind next it also has the graph and web3 storage already built in so you can look at the code and then just edit it based on what you're doing so obviously i have for this one i have a lens sub graph just so you can see how you make a query but now you know how to set it up you can just change the url write your own query and then your app will be up and running so this is for javascript and then a last thing so for anyone who's still kind of like i'm not maybe i don't feel quite ready to build a full

stacked app on my own women build web 3 which is a dow focused on high talent female engineering uh female and non-binary engineers in web 3 created this open source course called 30 days of web 3. um it just launched last week and there's over 3 000 developers taking this now you can just go to 30 days of web3.xyz and here's kind of what people are saying about it the t and you don't really need to have a coding experience the goal of this course is not to teach you how to code like we're not going to teach you how to map over an array or we're not going to teach you how to write smart contracts really the goal of this course is to give you an understanding of the architecture of web3 so we give you the code we walk you through everything and then by the end

of it you'll have deployed a full stack decentralized application and that's it thank you