Full stack dApp development on Polygon with Rahat Chowdhury
ETH Seoul 2022 · August 2022 · 22:59
Rahat Chowdhury, a developer relations engineer at Polygon, shows how to build full-stack decentralized applications on Polygon, the EVM-compatible proof-of-stake chain, and how its low fees change what is practical, such as deploying a contract for a fraction of a penny. They survey tools including Hardhat, Foundry, wagmi, RainbowKit and Slither, stress security audits, and explain reading data with The Graph. The talk ends on meta transactions and relayers, as used by Lens Protocol, so users avoid gas fees and onboard more easily.
Speakers
Topics
From ETH Seoul 2022, hosted by Kryptoplanet.
Transcript
awesome thank you all for coming out today uh really excited to be talking to you about polygon uh today we're gonna be talking about full stack dap development on polygon and specifically how you can kind of use the overall web3 ecosystem as different building blocks for building out like true truly full stack like robust applications um so a little bit about me emerhat i'm a developer relations engineer at polygon previously been a front-end engineer for a while was this literally solitary freelance engineer and overall been an indie hacker slash web 3 enthusiast i've tried to build several failed startups i can tell you everything i did wrong i cannot tell you how to succeed because i've never done it myself
but now i can get you a little bit closer to there and tell you all the wrong things i did these are some places you can find me always happy to chat happy to go over any kind of like ideas that you have if you want to bounce any ideas off or have any technical issues always open to chat about that so one thing that i kind of noticed as i was going through this specific presentation is that the title of this presentation might not be correct and i'm not going to give you the title of the presentation just yet we'll go through that later but just to keep that in mind um although we're talking about full stack dab development on polygon uh there is like a bigger kind of underlying message here um that we're going to be building towards so a little bit about polygon what is it
so polygon is the leading platform for ethereum scaling and infrastructure development so we provide a whole bunch of different solutions for you to build on top of l2 solutions a lot of things on the zk front that are coming up that i'll be talking a little bit about as well as folks may already be aware of like our proof of stake blockchain which is going to be one of the main focuses of our talk today so calling those out again a little bit right here proof of state blockchain again that's what we're going to be uh talking about today uh one other solution that i really wanted to highlight today though we're not going to go like fully deep into is our zk evm uh which we hope to release later um in this quarter on like our test net so
hopefully you'll be able to start building uh you know some test applications on that platform very very soon one of my favorite things about the zke evm is the fact that everything is evm compatible the solidity that you may already know that you're already building will work all you have to do is just deploy onto this avm um we did that just to make it easier for folks to like start you know coming into the zk realm to make it you know compatible for dev shops who are already building in solidity who want to leverage the power of zk proofs so i will be talking a little bit more about that at polygon connect so if you're going to be there i really hope that uh we have more of a conversation on that there um
before i move on there um you know i just also wanted to call out like we've got an entire suite of different uh solutions for scaling uh we don't believe in just like a one-size-fits-all solution there are different solutions for different use cases um so that's kind of what we're aiming for we're trying to build use cases that make sense for you for your specific um you know uh project for your specific depth um so what we're gonna what we're aiming to do is build that entire suite so you can pick and choose what is actually useful for you to build out for your users so building on the proof of stake chain a few things to keep in mind as you're building on on top of it it is evm compatible meaning you can leverage all of the tools that are currently available to
you in the ethereum ecosystem if you've already been developing on ethereum you already know how to develop on polygon there's no difference in different things that you have to learn on like the technical level all you have to do is switch your rpc endpoint of where you're deploying everything else just works one-to-one a couple of differences uh the token which is thematic token all fees and interactions are paid on the blockchain with thematic token and you know rather than eighth this is how you're kind of like paying those fees it's accessible very uh very much like super low fees compared to ethereum and sustainable the pos chain is carbon neutral and polygon as a whole is striving to go towards being carbon negative
so here's a little bit about the overall architecture of the proof of stake chain at the bottom with those nice little purple blocks you have thematic sidechain this is where you will be doing most of your interactions as a developer this is where your users will be doing most of their interactions where their wallet is going to be connected to um this is where most of the magic happens right you'll see up top though um that everything is connected to the ethereum mainnet specifically we refer to the ethereum as our root chain in the middle we have a layer called the plasma layer what the plasma layer does that has the specific checkpoint nodes that are going to be looking periodically at the state of the matic side chain and is going to be saving a hashed value of that to a
smart contract that lives on the ethereum main net so a good way to think about this is like maybe you're playing a video game certain parts of the video game you're saving your state so you can come back to it at a later point in time right that's essentially what the plasma checking point is doing it's saving a hash value of whatever the current state of the medic side chain is to ethereum giving you essentially the ability to inherit some security from ethereum it's everything is backed up on ethereum and transforms this from just being just like the magic side chain into what's called a commit chain so one thing that i mentioned during that conversa that whole spiel there was that pos is accessible so i wanted to give you a little bit of an example of
that i have a little bit of code up here um this is a very bare bones smart contract on building your own erc20 token there's nothing fancy going on here i believe i copy and pasted this from the opens upland documentation um so this is just creating a token called gold token in the constructure it just takes an initial supply for however many tokens you want to mint nothing fancy right just mints a bunch of tokens so a couple of lines an import from open zeppelin uh here is me trying to deploy this using a remix so remix is an online ide for quickly building out your own smart contracts once i tried to deploy it in my metamask i see that
the cost for me to actually deploy just what two or three lines of code is 76.42 same thing now except i've switched everything over to being on the polygon main net on polygon main net now this was about we'll say a month month and a half ago it's about point zero zero two nine matic uh to send this out it's like a fraction of a penny so you're going from 76 bucks to a fraction of a penny to deploy just three lines of code right so now imagine i'm actually trying to build something in production an erc20 token that has you know more functions like i want to add things like attacks on transfer i want to add like an allow list maybe this is part of an
ico maybe i have an additional contract for like an ico the more logic the more state variables everything i add the higher the cost of the smart contract right because storage is expensive so if i were to go through this this very very quickly becomes expensive beyond this like 76.42 so that is one of my favorite parts one of the reasons i actually joined the polygon team in the first place uh when i was first learning about smart contract development myself um it was during the height of like the nft craze when everyone was just like minting everything and all these gas wars were going on i wrote a very very sloppy smart contract it was like the first one i
ever wrote and i was trying to be a hot shot and like put it out onto mainnet and when i saw the cost i said never mind so so i wanted to go back to this point here building robust decentralized applications requires a combination of tools in the ecosystem so i want to go over a bunch of the tools in the ecosystem that i like using for building out apps so first we have just a list of like your developer tools smart contract development can be done with a few different things hard hat and truffle are very popular especially if you come from like a javascript background these are javascript-based tools brownie is a good framework if you're
more of like a python person and then foundry relatively new built with rust but it allows you to kind of like stick to your solidity and not really care about anything else i haven't done foundry too much i'm really excited about what that team is doing so hopefully i'll be able to build a few things in that sense some front end tools that i really like using wag me hooks although it's in beta my personal opinion is it's one of the best tooling for front-end developers in the web3 space so definitely check them out uh rainbow kit which gives you an awesome ui for just like connecting not only to you know a metamask wallet but you have to make sure when you're building out your front ends as part of your gaps
that you're being as inclusive as possible to as many different users as you can rainbow kit makes that very very easy because out of the box you're able to connect not only to a metamask wallet you're able to connect to mobile wallets you're able to connect to um you know leisure hardware wallets etc all kind of like done for you very quickly without a lot of configuration so wallet connect is also um you know a good way to do it go about things and is one of the underlying parts of rainbow connect uh rainbow kit as well etherjs is the what wag me hook's kind of like built around so i definitely recommend also checking that out uh one thing i wanted to touch on at
least for a little bit is security we do not talk about security and smart contracts enough and um i mean over the past you know a couple days weeks you've probably seen some of the messes that you know occurred so it's very very very important that you take security seriously when you're building smart contracts the move fast and break things culture in startup world does not always work in web3 when you move fast and you break things and you break people's money people get mad so it's very very important to understand what tools are available to you as a developer on the security side of things some of the things that i like to use are slither and mithril so slither is one of my more
kind of like favorite ones it goes kind of like does an analysis of your code and kind of like flags different potential security vulnerabilities for example maybe you have a reentrancy vulnerability in your smart contract when you're withdrawing funds it'll be able to kind of like let you know hey there is potentially a vulnerability here please look over this code this alone is not going to be enough it'll give you an idea of like where you kind of need to go where you kind of need to look at for your code but it is important to go beyond this a little bit and you know if you're going to be putting something out into production especially something in the d5 world especially something you know that deals with people's money to get your smart
contracts audited by a third party um slither will get you kind of like the base of like what you need gets you start thinking unlike the security side of things but it's very important before you go into production you get things audited so now i'm going to be talking about specific things that you do inside of your applications to make them a little bit more robust first reading data really awesome that i got to go after cami because i don't really have to explain the graph too much so you know everything academy just explained about the graph is really helpful for like reading data no matter what type of application you're building you're going to need to read some sort of data from the blockchain reading data from the blockchain is not the best thing in the world you know
you know you don't want to be sitting there right writing a whole bunch of like getter functions here and there until you get all the data that you need the graph makes it super super easy to like get a lot of that information indexed and sent over to you through a graphql api just like kenny mentioned right before me so we'll move into like a little bit of file storage file storage there's a couple of different options a file coin or ipfs is a really good one as well as rwave i've used filecoin and ipfs specifically their web3 storage and their nft storage projects those are really good have generous free tiers for just like you know putting up like if you if you don't even want to like have um anything to do with like
uploading anything directly from your app you can just like drag and drop upload assets if you need marketing assets available or anything for like static websites all of that can be done really quickly through filecoin using like web3 storage rwev very similar has a lot of really cool toolings in that sense one of my favorite things in the rv ecosystem is bundler bundler allows you to upload and keep like storage on the rv blockchain without the need for our weave native tokens you can even use bundler and use matic to actually pay for storage on the rv blockchain so bundler makes it really easy and accessible to use the rb black blockchain definitely recommend checking
that out for your storage needs one thing i wanted to add on top of the storage side of things is a really cool protocol i've been like looking into called lit protocol so they provide decentralized access control for web3 for apps private data on the open web so let's say you have some you know you know some assets that you have put on like uh file coin or on our weave and you want to make sure that not everybody has access to it lit protocol can actually help you with encrypting it making sure only specific people maybe certain allowed users and their addresses can access to it maybe certain people in specific like nft groups maybe you're trying to make
like an nft gated blog or something like that lib protocol is really good for use cases like this and providing that level of access control so now we kind of go into different like off-chain data protocols uh two of my favorites in this area are ceramic and threaddb ceramic is doing a lot of really cool stuff in this sense where you know you can have like these different data streams and profiles because like not everything needs to be saved on chain not everything should you know be saved on chain but it should be saved in some type of decentralized manner so i definitely recommend taking a look here i really like ceramic because they're also doing things in the sense of like improving on top of like different ways of doing sign
in with ethereum identity as well so check out ceramic for that type of thing now one thing i want to stress is this is not a complete list there are like tons and tons of protocols out there that are going to help you make it easier to build you know actual robust decentralized applications so definitely check out all of the new things that are kind of like always popping up it's hard to keep track of things because i feel like every day on twitter you see this new protocol with this new thing that you can do to add to your app but you've got to like kind of like vet them to see like what's useful for you what's useful specifically in the application that you're building now what i want to mention here is like web3
portals again they're your building blocks it's kind of like how you use them together to build that gets you to that eventual final state of like a full stacked centralized application but what about the cherry on top how can we tie everything together we can use polygon to supercharge your decentralized applications ux and i'm going to go into what i mean about that but i mentioned all these different you know protocols all these different building blocks and tools you can use them all together you can use them all on top of your adapts that are built on polygon i want to show you this tweet from a while back from my friend denison he is working at tally and uh this this tweet always like it was funny
to me at first but it resonated with me a lot um so he says that the biggest ux fail of ethereum is gas i absolutely know why it's necessary please fam i get it i know but regular people absolutely do not get it they do not care and they do not want to care and they quite frankly hate it so now we're in a bit of a conundrum right how do we get regular people who don't care about why you need gas why you need this level decentralization to start using your applications income something called gasless meta transactions so i have a little bit of a diagram here i'm sorry i'm not the best person at
these diagrams uh but i wanted to get first kind of like take you through the flow of what a gasless meta transaction would look like first thing would be doing something and i'm going to break these down so there's going to be like further slides on each of these points but the first thing you do with a gas less minute transaction is you sign a transaction using your wallet using eip712 with structured sine data and we're going to break that down a little bit further in a second you take that sine data you send it to a relayer something like opens up in defender or by economy that relayer wraps the request into a transaction and sends it to that contract into the contract right but what happened before
this relayer received that transaction you as a developer might have loaded up that relayer with somatic right to cover some potential gas fees that your users might might incur that relayer is going to take the inputs from your signed data as well as some loaded up matic and send that transaction to the contract to be executed on the user's behalf now let's say this is you're minting an nft you have a actual free minting nft experience with someone who doesn't have to pay anything to mint someone who doesn't have to pay any guesstimate you now have the ability to onboard someone who is completely new to web3 without
them having to go to an exchange buy somatic buy some tokens put it make sure it's you know it's not a good user experience as much as you know we like to pretend it is so let's break this down a little bit further signing eip 712 this builds off of the concept of don't trust but verify we verify that the user has access to their wallet because they're the ones signing specifically this crypt account this cryptographic signature you design some data structure on your front end could be a json object or whatever with specific inputs that you know whatever end function is being called needs if you're using something like ethers the implementation is this like signer
dot sign type data this is in the ethers documentation you can take a look at how to build out this signature using that functionality and from a javascript front end this creates the signature and makes it ready to be sent to some http endpoint for a relayer so we're going back to web too you're just sending a request to some endpoint using the relayer like i said we're making an http request to that relayer relayers can be loaded with funding for gas again opens up their defender economy ethereum gas station network all great options here and we just make these requests for to actually
we sign the data make the request it executes the contract on our the transaction on our behalf this is implemented by uh lens protocol and i think this is one of the best examples of like how to use a full robust decentralized protocol without paying any gas because if you use any of the um any of the projects that are built on top of lens protocol like lenster et cetera a lot of them are using lenses graphql api and the graphql api that they built uses a relayer i believe it's on by economy i'm not 100 sure so that you don't have to pay any gas anytime you like a post anytime you comment any time you you know make a post or whatever because
all of these things change some state on the blockchain all of these things all of these interactions would use some level of gas but now you don't need to do that as a user you don't need to care about that because we have the relayers acting on your behalf in the background this improves your ux by a very very large amount people don't need to care especially folks who are not native to web3 because if we're going to be going for mass adoption we need to be building our decentralized applications in a way that is inviting to people who don't understand how this kind of works and you don't have to do this for your entire application if you think about like how
certain software and everything works in the web 2 world you know you have like your 30 30-day free trials or something like that it doesn't exist in web3 you don't have a free trial you just have gas fees that you have to pay now all of a sudden you don't have gas fees you have to pay um you can you know front run and like pay for at least your users at the beginning to like get used to how your applications work they have a way to actually try your applications in a real production environment without having to spend any of their own money this is economically viable on polygon as a chain because of our incredibly low gas fees that i mentioned you know kind of at the beginning we're a lot more of an accessible uh blockchain versus you
know imagine doing this on ethereum mainnet especially when there's like a huge nft gas war going on right so what should the title of this talk actually be title of this talk should actually be building better user experiences in web3 on polygon that's what we really care about we really care about making sure your users are able to benefit from your services use your applications in a way that benefits you and benefits them this entire ecosystem is built on folks like you the builders and the users who are using your applications so we're aiming to make sure that the experience is as good as possible and hopefully what you can take away from this is one way of doing that for your applications
so again i'm a hot developer relation engineer at polygon a few places you can find me up here again i really enjoyed talking to you all today and thank you
