What Is Polygon zkEVM? The ULTIMATE Developers Guide!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll talk about everything you need to know to create your own full stack applications using the polygon ZK evm we'll do this by first following a transaction and seeing what's going on under the hood by seeing the user's perspective of submitting a transaction how that gets executed all the way to how it gets included as part of a batch sent across to ethereum and proven using zero knowledge proofs once we cover off how all of this works behind the scenes we'll then dive into a coding section where we create and deploy a smart contract to the polygon ZK evm all the way from setting up your wallet getting funds on the layer 2 to shipping your smart contract to a deployed State and then we'll create a decentralized application built on top of the polygon ZK evm that allows your users to connect to the chain as well as call some functions on the smart contract that we just deployed before we Dive Right In though if you don't care about anything I'm about to say check out the polygon ZM quick start if you're already familiar with creating smart contracts you don't want to learn about any of the details just Chuck in the RPC URL and the chain ID everything else you don't need to be concerned with as ZK evm is designed to have the exact same experience from a developer standpoint and a user standpoint as ethereum so if you are familiar with ethereum you don't care about anything else go ahead and jump into the documentation and grab what you need okay so just to give you some context before we dive in this is a presentation deck that I've given a couple of times in real life at hackathons and conferen and events so I've refined it over multiple feedback sessions in multiple countries and it's one of my most proud pieces of work that I've done so far at polygon so I hope you enjoy it it's been through a couple of iteration Cycles with feedback and based on the questions that I receive I try to kind of incorporate the answers throughout the presentation as well so depending on the context of where you're actually watching this leave any questions that you have in the comments below or in the Discord relevant to whatever you're participating in but without further Ado let's get started so the presentation is called building full stack apps on polygon ZK evm so the reason polygon ZK evm needs to even exist today is because ethereum and all other blockchains face What's called the blockchain trama and the blockchain trama is an issue that all blockchains face where they need to kind of choose their strong points between three aspects of blockchains so the three kind of points of that triangle of the blockchain tri first scalability decentralization and security so decentralization security and scalability the three aspects of the blockchain trima when we're talking about ethereum it has a very strong sense of decentralization and a strong sense of security that we love and it's been kind of through battle tested years and years of development to become this decentralized and secure blockchain however today ethereum can only really handle anywhere from 15 to 60 transactions per second which even today is not enough to support the adoption of blockchain and web3 Technology it isn't capable of handling High transaction games or applications or even everyday operations that you would want to use the blockchain for things like sending your friends ethereum or minting an nft from your favorite collection there's a lot of demand for both developers and users alike to use these unique capabilities that ethereum unlocks like demand for decentralized storage stage storage that is available in a decentralized manner that can't kind of be controlled by a central entity and decentralized compute to have your code running on this machine that is capable of executing your code in a decentralized and permissionless manner so we love ethereum for its strong sense of security and decentralization but it really lacks in that third point of the trilemma which is scalability and what this leads to as this kind of diagram over here suggests is applications are either being pushed away from ethereum because the capabilities are not enough to handle what they want from the applications or from the games or ethereum or blockchain technology as a whole is just not being built on by let's say web 2 companies because they see this and say well I don't want my users to be scared to use my application or be pushed away or be limited to even use my app based on gas fees so today even to send someone some ethereum you're talking about a massive gas fee of anywhere from1 to a few hundred depending on what transaction you're actually performing and obviously this is not realistic for everyday operations like social applications or games where you expect your users to pay huge gas fees to every time they want to write something to the blockchain so we love ethereum but even today it's not capable of handling the amount of transactions that users and developers desire from this network work let alone what we hope to see in the future where we reach a point of mass adoption of the web 3 ecosystem and blockchain Technology imagine how congested this network is going to be when we reach that point so polygon Z evm was built to address the scalability issues that ethereum faces in that trilemma while maintaining the strong sense of decentralization and the strong sense of security that ethereum has by utilizing that as the base layer for all of the transactions to kind of live on and we'll talk about what that looks like further down in this deck here so essentially polygon zkm was built to maintain the decentralization and security that ethereum offers while significantly boosting the scalability by orders of magnitude so we've talked about what polygon ZK evm actually is but maybe we should actually cover what it stands for so ZK evm stands for zeron knowledge ethereum virtual machine and that doesn't really mean anything but let's break it down so ZK means it utilizes zero knowledge proofs somewhere in the system and the evm means ethereum virtual machine which means it's evm compatible and this is an important part because evm compatibility means you can access all of the existing ethereum Community all of the development process is the same you just need to change what blockchain you're deploying to essentially just change one line of code the ZK part refers to what's happening under the hood that allows polygon ZK evm to say that it inherits the security and decentralization of ethereum is because it posts all of the information back and uses these zero knowledge proofs which is just cryptographically proving the computational Integrity of the batches of transactions which is just kind of fancy words for saying is what got posted actually correct using zero knowledge proofs if you've ever read anything about ZK evm you might have seen a diagram that looks something like this where a bunch of transactions go through Layer Two and all of those transactions get batched together submitted as one transaction to what's called layer one and we're going to break down this diagram because while it does give a good summary of what's happening it leaves out a lot of the details of what gets sent how this process happens under the hood I'm going to explore the process of a transaction being submitted to layer 2 all the way to it being proven on layer one and this process really happens in five steps so we're going to cover all five of these in a kind of logical order here so the first one is submitting transactions where users submit their transactions from a wallet or a script or whatever it might be via Json RPC second is accepting those transactions and executing them based on a couple of quick checks that we're going to run to make sure these are actually valid transactions then we move into the three steps that kind of happen behind the scenes where we're talking about inheriting the security and inheriting the decentralization of ethereum by posting all of that information back to ethereum so under the hood behind the scenes we're going to take all of the executed transactions batch them all together send them across to ethereum we're then going to do this kind of two-step process called sequencing of those transactions and then aggregating those sequenced batches of transactions and we'll talk about what sequencing and aggregating means as we go through these slides okay so step one of five in this kind of process is actually submitting the transactions and if you've ever submitted a transaction to ethereum or even polygons proof of stake chain before you're probably using a wallet like metamask wallet or coinbase wallet or you're building out your own applications like ethers JS web.js web 3js rather web 3 Pi whatever you're familiar with that allows you to submit transactions via what's called Json RPC to the blockchain essentially is all you're doing is you're saying hey this is what I want to execute here's the signature and approval to actually execute that from my wallet this is the exact same for polygon ZK AVM this is an important thing again I want to repeat this all of the development process is the exact same you can use all of the exact same tools you can use all of the existing wallets everything from both a developer experience perspective and a user experience perspective is identical to what you would expect in ethereum there are two key benefits to what the polygon Z evm offers over what ethereum offers though so in this example screenshot here I'm sending my friend 0.00001 ethereum and clearly I'm not feeling very generous over here but as you can see the gas fee associated with this is Tiny right it's less than a cent in this example of sending ethereum to my friend here so compare this to ethereum where you'd pay any anywhere from 10 to hundreds of dollars depending on how congested the network is for this exact same transaction to go through so the fees are substantially lower on polygon ZK evm compared to what you would experience on ethereum the other benefit to this is that the speeds these transactions go through is almost instantly right so this whole process from clicking this confirm button going to the network all of the stuff that we're going to talk about happening under the hood sending the information about that transaction whether it got accepted whether it got rejected what the result is back to the user is all going to happen in around 3 to 5 Seconds right it's going to be very quickly and this is an expectation that the zkm has that this is going to go through and come back very quickly for a very small fee so we're starting quite simple here where hopefully most of you are familiar with this kind of screen where you execute a transaction it either gets confirmed or rejected and you continue using the application what's happening on this right hand side of the screen though is kind of high level right it's going to this black box of polygon zkm and coming back so let's take a closer look by inspecting what's actually going on under that black box here so that was step one was submitting transactions so it's already very familiar hopefully to what you've been building if you've ever used an evm compatible chain before such as ethereum what happens next is Step number two which is executing these transactions so when you actually click that confirm button or when you run the script or however you're sending these transactions via Json RPC to the network it's going to go into a pending transaction pool so up the top on the right hand side here you can see in this big purple circle all of these transactions are sitting in this purple pending transactions pool the sequencer which is a essentially you can imagine it as a piece of software running looks at this pending transaction pool picks those transactions up and decides hey am I going to execute this or am I going to get rid of these transactions it does this decision based on some key inspections that it makes about each transaction so it says things like hey can you actually afford this transaction is this transaction valid based on if you're calling a smart contract does that smart contract exist Does the function you're trying to call exist is this a duplicate transaction that's going to conflict with another transaction that you've submitted all of the these kind of Sanity checks it's going to run and make a decision to say yep that looks good I'm going to execute it or no that does not look good I'm going to discard it and you'll get a rejection message back into your application or in your transaction assuming your transaction gets executed and it was valid this is going to go through a process called broadcasting where the sequencer broadcasts the information of the executed transactions to all of the nodes on the ZK evm Network and these nodes are the ones providing the information back to the application to say hey here's the new state of whatever information you require so maybe you Meed an NF here's the information about the nft you minted here is the in this case information about what happened when I sent my friend some ethereum so this is just covering step one and two which are the user facing aspects of the polygon Z evm which hopefully as you can imagine are if not identical very similar to what's happening in any other evm chain such as ethereum right it goes into the pool getss picked up gets executed gets distributed to the nodes and you read that information back in your application so that's step one and two is submitting the transactions and executing the transactions so that's two steps out of the five that we're going to talk about so we've covered submitting transactions and we' covered executing transactions so submitting was just sending them executing them is doing something with them and giving the information back what we're going to talk about next is what's happening under the hood that enables the security and the decentralization aspects of the polygon ZK evm before we do that however I want to make an important note at this point in time so everything we've talked about so far is what the user sees and what most users are going to care about when it comes to interacting with the polygon ZK evm everything that comes next is what's happening behind the scenes and usually in almost all cases except one specific transaction that a user wants to make the user doesn't need to necessarily care about what's happening next now the one point where they do need to care about what's happening next is when they want to withdraw funds from the ZK evm back to ethereum okay and what that means is in order to pay for these transactions on the layer 2 which is ZK evm the user would have needed to bridge funds across from ethereum to the layer 2 which takes right now around 10 to 15 minutes for that process on the main net beta environment right now I'm not sure what it's like when you're watching this video it depends when you're watching it but right now I believe it's around 15 minutes to go from ethereum to polygon ZK evm and then you can just live on the ZK evm having all of the benefits of the fast transaction speeds and the low fees however in order to withdraw funds back to ethereum let's say you want to make a transaction on ethereum you want your funds back across on the layer one what that means is the transactions that occurred on layer 2 need to go through this behind thes scenes process where we send all of the information back to ethereum the ethereum chain receives all of those transaction data information and proves it using zero knowledge proofs at that point once it's been proven to be correct and what happened on the L2 has been verified by L1 on ethereum at that point the user can withdraw the funds out of the L2 and back onto L1 we're going to talk about why that is the case and what process this goes through to enable that withdrawal back on L1 one of the massive benefits to ZK rollups such as the polygon zkm is that they have a significantly faster withdrawal time than optimistic Roll-Ups do so optimistic rollups typically have a 7-Day or a 14-day withdrawal period where they open up an opportunity to say his fraud proofs or fault proofs that prove transactions were incorrect that sort of secure the rollup in this manner and they allow those to be posted for around 7 days so users are blocked from withdrawing funds for anywhere from 7 days or 14 days or some number of days that the protocol has set ZK evm on ZK rollups it depends however long you take to generate the proof and post and verify that proof on ethereum and as I'm recording this today it takes around an hour for this process to happen so polygon zik amm is going to generate the proof post the proof to ethereum ethereum is going to verify the proof and at that point around an hour later at this point in time we're going to be able to withdraw our funds so this is a substantially less amount of time than compared to existing Solutions like optimistic rollups okay so what we just saw was the sequencer picks up the transactions from the pool and executes them or discards them the sequencer is also responsible for an important part of what happens behind the scenes and that leads us into step three which is called batching transactions so the sequence is going to take all of the transactions that it just executed and actually batch them up together send them to layer one so in this screen here the sequencer is going to send across batches of transactions to what we're calling layer one and if you're a little confused about what layer one means it's essentially ethereum in this context so we're actually sending batches of transactions to a Smart contract called polygon ZK EV M on ethereum so one aspect of the ZK evm is literally a smart contract called polygon ZK evm deployed on ethereum and what happens here in this step three of batching transactions is the sequence that says hey I've just done executing all of these transactions let's send them all up batch them together send them across to ethereum and it does this by sending it to a Smart contract called polygon DK ebm the way that the sequencer does this is by sending it in this format in the middle here called a batch data so this is a struct which is just a way of structuring data on ethereum or solidity which is just a a smart contract language for writing evm smart contracts what it does is it says hey I've got all of these transactions I need to somehow stick them into this batch data struct here and send them across to L1 so that's how it's doing it it's taking the transactions somehow putting them in this format which is what we're going to talk about next and L1 or the smart contract deployed to L1 receives them in this format here but how does the sequencer take the transactions which have a bunch of fields right it has where the transaction came from who the transaction's going to what smart contract that transaction calls what function it calls all of the information from the transaction somehow needs to be transformed into this batch data field here the way that it does that is a two-step process so for now you can just kind of ignore the bottom three fields and we're going to focus on this circled field here called transactions so you'll notice it's a type of bites which is called transactions so somehow we need to take all of that information right where the transaction came from who it's going to what smart contract is calling and somehow get it into bytes the way that we do that is this two-step process here the first step is serialization and I have in Brackets here rlp which stands for recursive length prefix serialization which is really just a fancy way of saying take one data format and transform it into another data format in this case we're transforming it into bytes so we take all of the information that we talked about where the transaction came from who it's going to etc etc transform it using the serialization process into bytes and then the second step is called concat concatenation It's A Hard word concatenation and that's literally just a fancy word for sticking them together right so it just this literally means join all of the transactions that were serialized together so we're going to first transform them into bites and then stick all the bites together and that's actually what we're sending in this transactions field here okay so to break this down a little bit we first here have the sequencer who just executed the transactions remember it it made those checks said yep that looks good I'm going to execute that and then broadcast it so the sequencer has all of the transactions that it executed and it says to itself hey I I need to somehow get all of these transactions into this bytes field called transactions here so again this is a two-step process this was serialization and concatenation so first it's going to take these transactions send them to the serialization process here serializes them into bytes so you can see here the executed transaction one goes into bytes one executed transaction two gets serialized into bytes 2 executed transaction 3 gets serialized into bytes 3 and ET Etc this can happen for as many transactions as it needs to and can fit into this transactions field here so it gets serialized into bites and now we have a bunch of bytes representing the transactions and the Second Step if you remember was concatenation which literally again just means stick them all together so it takes all of these btes it's four bytes one bytes two bytes three bytes four and sticks them all together so we have one big long bite of all the transactions going into this transactions field so I know it's kind of scary when you look at it at first but when you break it down it's actually not that scary right it's it's kind of simple what's actually happening it just says here's the transactions let me get them in a format that I can understand and then I'm just going to stick them all together so that we can fit lots of transactions in this one field what's actually happening now is we're going to do this as many times as we need to to cram as many batches as we can and send over to L1 this is because it costs us gas fees to call the smart contract on ethereum right the polygon ZM smart contract it actually costs us gas fees to call that transaction so we want to get as many transactions as we can into that one transaction so this process that we just went through is going to happen as many times as we can to cram as many transactions as we can and send as many batches as we can in one transaction so that we get kind of that maximized efficiency when we make that call that costs us money so we're going to repeat the process that we just saw in the previous slide to make multiple batches each having their own transactions field so this brings us to step four which is sequencing the batches of transactions so we just batched everything together right and we need to send them to ethereum so this is literally all we're doing we're calling a function on the polygon ZK evm smart contract stored on ethereum and that function is called sequence batches so as you can see in the bottom right here it's it's probably kind of small don't worry we're going to zoom in in the next slide so if you can't see exactly what's going on we'll zoom in in exactly one slide's time so what's happening here is on the right side you can see the transaction is coming from the sequencer which is the party that we just saw do all of this batching and the sequencer says Hey polygon zkm smart contract which is the Smart contract stored on ethereum I I want you to know about all of these transactions that I just executed and I've batched them all up together and tried to serialize them together so that you can understand them and I've crammed as many as I can so that I made this efficient for myself and and kind of cost optimized so it it literally just calls a function called sequence batches and sends over all the batches so again it's it's kind of intimidating at first but when you look at what's actually happening it's just sending all the transactions in a format that is is is optimized or or cost efficient right I've cran them all together into this function so that you know about all the transactions and you can store them on ethereum and this is important because we want all of the transaction data to be stored on ethereum because that's how we can say hey ethereum knows about all of the transactions and what's going to happen next is e is actually going to verify that these transactions are correct so if we zoom in on what's Happening Here you can see in this case if I move my head over here you can see the number of batches that got submitted is actually 52 so that's what I'm pointing at here in this particular example transaction we're literally just sending 52 batches of transactions so if we go back a couple slides here we did this process 52 times right so I'll move my head over here we did this process to create 52 batch data structs now important distinction here is that's not 52 transactions right because if you remember we crammed as many transactions as we could into this transactions field so we've got many transactions into that field and then we've also created many batches of transactions so each batch contains multiple transactions if we go back to this slide in this particular example transaction that I've pulled this sequence batches call had 52 batches 52 batches of transactions so it took 52 batches containing multiple transactions each and sent them across to ethereum now What's Happening Here is each of those batches if you can see here that is literally transactions serialized and stuck together so that is underlined it's multiple transactions stuck together and we repeated that 52 times to create multiple batches each containing multiple transactions sent them across to ethereum so it looks kind of scary I will admit in this slide with all the the numbers and letters here but again all that's happening is we said hey here's what I saw occur on the L2 I've tried to optimize them and stick them all together in a format that you understand on ethereum sent them across and now you know about all of the transactions that I knew about on L2 so this process has taken away so much load off of ethereum because rather than users sending each individual transaction to ethereum it's occurring on layer 2 and 10 or hundreds or thousands or however many transactions that the L2 took care of just sticks them all together in one transaction and says here you go ethereum here's all of the transactions that occurred so this can scale ethereum by orders of magnitude by taking all the heavy lifting away from ethereum all the execution away from ethereum and just saying here's heaps of transactions that I know about send them across to ethereum and what we're going to do next is ethereum is going to say okay you sent me all this but how do I know it's correct and that's what we're going to talk about next once all of these transactions hit the L1 smart contract from that sequence batches function we reached the second phase out of three phases of finality that the transactions go through in this DK evm so right now we're in a virtual State and a virtual State just means hey I've actually received all of the transactions that you sent to me and now what I'm going to go do is actually try and prove that these transactions are correct so if we go to the the next slide here after we submitted the transactions and executed them and went through that broadcasting process to distribute all of that information to the ZK evm nodes we were in what's called a trusted State and the way that I like to think of trusted state is if you trust what the L2 or polygon ZK evm in this case if you trust the information coming back from that such as the transaction data or transaction history or anything that really occurred on the L2 if you trust that environment then you can kind of rely on that trusted state right so that's kind of how I m the the first phase is hey do I trust what's being given to me on L2 then I'm in The Trusted state right so the trusted State really just means the transactions were submitted and transactions were executed and we completed the two steps out of the five steps that we're going to describe throughout this presentation so what we just saw after the transactions were executed was the transactions were batched up together and then sequenced which really just means send those batches over to the ethereum smart contract stored on etherium so the polygon zkm smart contract stored on ethereum and that is where we are now so as you can see we're in a virtual State and the way that I think of a virtual state is we've received all of these batches of transactions but that doesn't mean they're necessarily mathematically proven to be correct which is what we're about to do we're about to use zero knowledge proofs to actually use maths to prove the computational Integrity of the batches that got sent and stored on that L1 smart contract so then once we prove those transactions are valid we're going to reach that Consolidated state so the first two steps submission and execution reaches the trusted State the SEC the third and fourth steps reached the virtual state which was batching transactions together and sequencing those batches on the L1 smart contract the fifth step that we're going to talk about is aggregation of those sequence batches as you can see on the right hand side once we aggregate those batches which we'll see what that means in a second we're going to reach the Consolidated State and if you remember what we were talking about earlier where we were talking about withdrawals from L2 to L1 a Consolidated state is what's required for us to actually enable those withdrawals so what we're going to do next is we're going to take the batches that got submitted and we're going to prove them using zero knowledge proofs and reach this point so that brings us to aggregation which is Step number five of this full process here so we're going to introduce introduce this new kind of party here which is aggregator now the aggregator role is to take the batches of transactions that were available on the ethereum smart contract and it's going to try to prove prove that those transactions are valid and post that proof back to the ethereum smart contract and the ethereum smart contract is going to verify whether that proof is valid or not and then we can reach the Consolidated State and the role of the aggregator is to take these batches of transactions it says Hey polygon ZK evm smart contract on ethereum L1 I see you've got these batches for me I'm going to take them as input and I'm going to generate a proof for you that I'm going to send back for you to verify so the aggregator takes the batches sends them to what's a ZK what's called a ZK Pro and as the name suggests the ZK Prov is going to generate zero knowledge proof so ZK proofs that the aggregator is going to receive in return for those batches and I'm going to kind of oversimplify this right hand side of the diagram as I don't really want to go too deep into how these actual zero knowledge proofs work it's probably a topic for another time but the the ZK Prov essentially generates one kind of zero knowledge proof called a ZK stock which stands for zero knowledge scalable transparent argument of knowledge and this is kind of a bigger style of zero knowledge proof and it's going to do that multiple times and then it's going to reach the snark Builder which is a smaller kind of zero knowledge proof and this stands for ZK succinct non-interactive argument of knowledge so that is what's going to be received by the aggregator and all you really need to know here is it's going to generate multiple big zero knowledge proofs and then it's going to generate a final proof and that proof actually proves the other proof okay so it's a proof of a proof is essentially what the aggregator receives and that zero knowledge proof is small because we want to save as much gas cost as we can when we're sending that zero knowledge proof to the ethereum smart contract because again we're going to pay the gas fee to send this information to be verified so if you didn't really understand what was going on in that slide it's okay I didn't understand it at first either the aggregator is sending the polygon ZK evm smart contract a zero knowledge proof otherwise known as a validity proof so I've been saying whether these batches are valid quite a bit in in the kind of previous sections and the reason for that is because and the validity proof just proves whether or not the batches of transactions that got submitted in the previous step in the in the sequencing of batches of transactions whether those sequence batches are actually valid the way that it does this again I've taken some code from the actual smart contract in the middle here just so we can see how this actually works the aggregator is calling a function called verify batches and within that function it literally provides the zero knowledge proof that it just received from the ZK Prov and sends it as part of the arguments to this function so verify batches trusted aggregator again the aggregator is the one calling this function currently it's what's called The Trusted aggregator and that is being sent over to the polygon ZK evm so again here's an example transaction up top we have the aggregator calling the ZK evm smart contract again this time it's calling the verify batches function and actually providing the proof here to say hey here's the proof of the batches I want you to go ahead and verify that and this slide is actually very important even though it's very small this is probably one of my favorite slides in the deck is because this is code taken from the smart contract deployed to ethereum and this is what I love about it is because here's why we can say we inherit the decentralization and the security of ethereum because ethereum the smart contract on ethereum is the one calling this verify proof function so ethereum's environment takes the proof calls verify proof which calls this rollup verifier smart contract which basically is just responsible for saying hey is this proof legit or not and says if the proof is valid from this verification process then I'm going to reach that Consolidated state if it's not then we're going to revert which ins solidity just means kind of get out of that and and stop running this function so that means if it's not verified as valid then we're going to it out of the call and the state of the chain is not going to accept that proof and the batches that got submitted are not proven yet so we're still in that virtual State because they are not actually verified to be able to say hey this ethereum agrees this proof that we just received from the aggregator something's fishy about it something went wrong some of the transactions within it are not correct maybe someone's trying to do something malicious something someone's trying to attack this system you know all kinds of things could be prevented by running this function on ethereum and that's what I love about this slide so here I'm just highlighting and it's it's not that relevant for this particular deck but here I'm just highlighting why we're using the SN instead of a different kind of zero knowledge proof so the snar is more succinct and which just really just means it's smaller than other kinds of zero knowledge proofs and it costs around 350,000 I believe it's actually less now gas to run the function that actually verifies the proof that got sent across by the aggregator okay so now we've covered how the ZK evm Works we're going to talk about how you can actually build your full stack applications on the ZK evm and this part's actually quite simple hopefully if I've got the point across here and and done a good presentation you might have remembered this process of deploying to the ZK evm is designed to be exactly the same as ethereum or any other evm chain and that means if you've ever deployed to ethereum or if you've ever deployed to any evm chain such as polygon POS then you have all of the skills necessary as both a user and a developer to build the same applications using the same tools same processes that you're familiar with onto the ZK evm you just need to change one thing which is where you're actually pointing to deploy those smart contracts to the first process is to actually Bridge funds from layer one to Layer Two so the bridging process is essentially saying I want my eth from ethereum to be sent across to the polygon ZK evm and this is a simple process it's even more simple if you're working in a testing environment or a test net environment so if you are in a test environment you can go to this website faucet. polygon.net the ZK evm tab here paste your wallet address in and we'll send you some funds to test with on the ZK evm for free so that's probably the easiest way if you're testing if you are not in a testing environment and you're on the main net environment you can go to wallet. polygon.com tools from the polygon documentation so once you're here you can select a native bridge and sign in with your wallet and that is going to take you to the page where you'll be able to bridge your funds from ethereum sorry from ethereum to the polygon ZK evm main net environment so this is only relevant if you're deploying to polygon ZK evm main net if you're on a test net you can jump into the faucet which is available at faucet. polygon.net just by pasting your wallet address into that faucet and I also love this slide because it shows how simple it is to actually just swap your development environment from ethereum to the polygon kevm just by changing the URL that you're pointing to so if you've ever deployed your smart contract with hard hat or Foundry whatever you use to deploy your smart contracts just change the RPC that you're deploying to from ethereum or from whatever chain to the polygon ZK evm RPC which again is available on the zkm documentation or as I showed at the very start of the video as well so that's really all you need to do what I'm going to show you next is creating full stack applications using a framework that I've actually built out called evm kit so evm kit allows you to create full stack applications here is the website evm kit.com here's a quick little demo for what capabilities and features it has built in it you can connect your wallets with this nice pretty UI you can interact with smart contracts on any evm chain including the polygon ZK evm where you can read and write information with loading States and era States everything that you need with built-in PCS built-in wallet connection user authentication decentralized storage and all of the pieces and parts of the puzzle that you need for the smart contract development side which is a hod hat environment so we're going to deploy a smart contract from within a hod hat environment and then we're going to connect to the polygon evm and we're going to connect to our smart contract and start reading and writing information from our front end environment here and this is built with nextjs it's powered by Third web for both the smart contract development environment and the frontend development environment and it uses Shad CN UI library for these kind of pretty designs on the front end so I'm just going to follow the evm kit documentation I would recommend that you do the same or you can just watch me do it as well so what we're going to do is actually create the project I'm going to go ahead and copy the mpm command to initiate this project here and open up my terminal I'm going to maximize this so you can see here give me one second to make it a bit smaller there we go so I'm going to jump into a terminal I'm on Windows using Powershell you shouldn't have any issues whatever terminal you use if you're on Mac whatever I would suggest you install node.js npm Yan and git if you face any issues check your node version check you have all of the stuff installed you shouldn't face really any issues with the CLI though so just go ahead and copy the install command paste that into a folder and replace this my evm kit app with a name of your project I'm just going to say demo evm Kit app here and that's actually going to clone a template repository from GitHub and essentially make a copy on your machine so once this goes through as you can see the evm kit pops up and the next suggest step is to change directories into that folder I just go ahead and clear it up so the next step is to get a third web API key so third web allows you to utilize their free kind of RPC services so we're going to go ahead and get an API key which is free at least for a specific amount of usage and then we're going to plug that API key into our application while we do that in the background what I'm going to do is open this up in my text editor by type in code Dot and and what I'm going to do is open up a terminal in vs code and I'm going to click this split icon over here just on the very right hand side hopefully you can see that going to split it and then from the left hand side if we open up the folders section here you can see it's kind of split up into two sections so there's application and there's contracts we're going to change one terminal into application which is our front end environment and we're going to change another terminal on the right hand side here into the contracts directory so CD contracts I'm just going to zoom out once here so I can stop leaning over here and then both terminals we're going to install the dependencies so you can use npm install you can use Yan you can use pnpm whatever you prefer I'm just going to use yarn because that's what I'm most familiar with so I'm just going to type yarn in here and I'm going to type yarn into the contract section as well so while that's happening in the background I'm going to head over to third web so this is just at third web.com what we're going to do is Click get started here and we'll go into this top right corner I'll just put this into frame for you and what we're going to do is we're going to connect our wallet and connect with whatever you want I'm going to use metamask just because I have some existing test funds available in that wallet I'm going to sign in go ahead and sign the message from my wallet as well and we're going to go and head over to the settings section and you can see here we're going to create an API key so click create a API key just give it a name let's say demo evm Kit app and I'm just going to go ahead and select everything with unrestricted access you can pick and choose which uh Services you want really you don't need to concern yourself that much just go ahead and create one for testing purposes get yourself up and running so I'm just going to say unrestricted access take everything off click next unrestricted access again and create it so this is going to present you with your client ID and new secret key I'm just going to leak mine here because I don't really care if you use these Keys maybe I'll blur it out in post production you however should keep them safe especially if you are going to use the paid services that third web offers you should definitely keep these safe but what we're going to do is I'm going to copy this one and I'm going to go into the application section and in the application section you'll find aemv do example file what I'm going to do is I'm just going to copy and paste this rename the copy and pasted file to EnV so so this is kind of our secrets file you'll see it gets gray out so you don't Commit This to GitHub or any kind of leaks into wherever you're storing your code you'll see up the top here there's a field for a public THD web key and a THD web secret key so let's go ahead and go to grab our client ID for the public key and the secret key is this key here I'll paste that into the theb secret key field we don't really need to worry about any of the rest for now that is going to take us to the next step so we'll go to the getting started section and that's really it so if we open up a terminals again here let's just open that back up you might see this is running something on the left hand side in the application I'm just going to go ahead and kill that process because I don't really care about the third web generate in this particular scenario so I'm just going to kill it and I'm going to clear both of these and what we're going to focus on next is actually the smart contract development section and deploy our smart contracts to to the polygon ZK evm so what I'll do is open up this contract section here you'll see now we have the yarn lock file which if you see this or if you see a package lock. Json file that means your dependencies have successfully installed within the contracts folder there's another contracts folder called grea sorry where a file called greer. soul is available you can see here is just a very basic smart contract which has a Constructor a read function and a right function with some emission of events as well as this internal state so all we're really doing is storing a value called greeting in a variable called greeting has a Constructor where we can provide the initial value of that greeting it has a read function to actually read the value of the greeting and a set greeting function to actually write or update the value of the greeting which is going to require us to initiate a transaction and pay some gas fees and it's going to emit this event called greeting changed which is defined on line 25 down the bottom here so you can change this to whatever solidity smart contract logic you want I'm happy with the grea smart contract I have what I'm going to do now is show you how to deploy this to the polygon Z evm Network so what I'm going to do is if you just keep your terminals open from the contracts directory here so you should be within contracts if you're not just say CD contracts don't do that too many times so you might get lost but just make sure you're in the contracts directory before you run this command so what we're going to do is if you open up package Json you don't have to do this I'm just going to show you what's going on you'll see there's a script called deploy so what this does is it's actually just going to take the smart contracts and it's going to open up a UI via third web dashboard for us to actually deploy these smart contracts so what we're going to do is just going to run Yan deploy from again within that contracts directory here this is going to run the underlying third web deploy command you might be prompted to update your THB package just go ahead and accept that and you should see the thir web logo come up it detects that you're running a hard hat setup here what it's going to do then is you'll see it's compiling if you really want to look at what's going on under the hood you'll see the artifacts and the cache folders pop up within the artifacts folder you can see okay here's actually the greer. Json and if we scroll within this file you can see the ABI get output you can see the bite code you can see the deployed bite code what we really care about though is this URL here so you can see the process was successful the upload was successful so that means our contract is ready to deploy via this URL here so let's go ahead and open this up and this is going to take us to the deployment page so you can select any evm chain you want here including ethereum ethereum test Nets polygon POS chain polygon proof of stake chain test net like the Mumbai chain really any evm chain is available here and what we're going to do is just Define a initial value for our greeding so these are fields that get dynamically created kind of based on your Constructor so whatever arguments you need to provide to your Constructor will be available as options within this field here what we're going to do next is down here we're going to select the polygon evm search that up and you can see there's polygon ZK evm there's pogon ZK ebm test net there's also the polygon main net and there's also Mumbai if you want to deploy to the proof of stake chains as well as any of the cdk chains should also be available here if you can't find it you can add a custom Network where you can provide your own RPC your own chain ID all of the information required so what I'm going to do is I'm going to go to the polygon a evm test net I'm going to select that which will prompt me to First add the network to my wallet and then swap over to that added Network since I've already got it it's just one step to swap over if you don't have any funds you'll be able to see in your wallet up the top right here if you don't have any funds on this network that's where the faucet comes in or if you're on a main net environment that's where this bridge comes in here so if you're in a test net environment all you'll need to do is log in with Discord to make sure you're not a bot farming the faucet and paste your wallet address in which you can copy from this copy address icon here receive free ethereum test net funds on the ZK evm to test with just make sure you've got this tab selected of polygon ZK evm here so since I've already got some funds on the test Network all I need to do is you can add it to your dashboard if you want to visit it later I'm just going to skip that step move my head over a little bit so you can see I'm going to click this big deploy button here so go ahead and click deploy now this is going to prompt a transaction in your wallet here contract deployment you can see very very small gas fees which we love to see one of the massive benefits of the ZK evm test net go ahead and click confirm second major benefit is W that transaction went through very quickly matter of seconds to actually process that transaction and receive the updated information of the chain comes back in matter of seconds so now we've just deployed a smart contract successfully onto the ZK evm testnet so that's the smart contract development side of the the full stack development setup what we're going to do next is we're going to jump into the application and the application side of the evm kit is built using nextjs and the third web react SDK among a variety of other tools like Shad Cen UI which is amazing UI Library I think you're going to love it if you haven't seen it already so what we're going to do now is we're going to take this smart contract that we just deployed and we're going to plug it into our front end to allow users to connect their wallet swap on over to the ZK evm and then actually re read and write information from this smart contract here so let's jump into the application folder now head back on over to the code window here what I'm going to do since we're done with the contracts folder here I'm just going to close that side of the terminal leave the application terminal open and what we're going to do now is let's actually just open that up into our code editor here so we'll get rid of all of the stuff that we're not using anymore and within here you can see a couple of different things you should first see this uh where is it Yar lock file or package lock or pmpm lock whatever you're using you should see this if you don't see one of those files that means you need to install the dependencies first please make sure you have done that otherwise it's not going to work so you should also see the EMV please keep this secret unlike I'm doing here I don't care about these secret keys because I don't have any funds that I want to keep safe or I'm not using any of the paid features at least from this account on the third web- so please keep these safe you should never ever leak any of your values please do not follow what I'm doing if I haven't bothered to blur it out I'm lazy but I don't care if you use these Keys there's nothing being lost for me but you should never ever let anyone see these or or use any of the keys okay so please be careful with this file what we're going to do now is inside of the application folder there's two kind of configuration files in the const folder so one is called chains and chains all ows you to Define two chains that your application is going to work on the chains full chains file sorry allows you to find a development chain which is just set to Mumbai and also allows you to find a production chain which is again just set to Mumbai the way that this works is it exports this variable here that says is this a development environment if it is then we're going to use a development chain if it's not I.E it's one of your deployment URLs we're going to use this production chain value which in this case they just both set to the same thing just for Simplicity but what we're going to do is we're going to change this from polygon Mumbai to the polygon ZK evm test net so go ahead and import that from the atth webdev chains package I'm just going to get rid of both of these and set our development chain to the test net also going to set our production chain to the polygon Z AVM test net so now we've declared to our application to say hey this is actually the chain that I want to use for both development environment and a production environment the second configuration file is this contracts file and this actually works pretty similar so it first Imports that is this a development environment variable from the previous file that we just looked at and it then allows you to Define two smart contract addresses one that says hey here's a smart contract address on the test net that I'm using and the other one says hey here's the smart contract address that I'm using on the production chain so for us what we're going to do is since we're on the polygon zik evm testnet for both our in development environment and production environment let's go ahead and copy the address of the smart contract which is available at this button here then what we're going to do is we're going to replace that value with the smart contract address and just for good measure if we want to deploy our application let's just paste that into this value here as well and you can repeat this process for as many smart contracts as you want and repeat this kind of process for as many smart contracts as you want as well I think most cases you're just going to use one or two development environments sorry one for development and one for production if that's not the case you can completely not use this file if you want to it's really up to you how customized you want this setup so the way that that works is in the thir web provider in this app file here which wraps our whole application it's going to set the active chain to the chain that we import from that chains file there was a lot of chains but essentially it says hey is this a development environment read this chain or hey this is a production environment read this chain and operate on that okay so hopefully that makes sense then in the index file we have a couple of different components that we're just showing off you can pretty much safely go ahead and delete all of this stuff so let's go ahead and delete this delete the tabs I just don't want any of the demo let's go ahead and delete this state and delete any of this kind of extra stuff that I don't care about so I'm just going to go ahead and delete this and now we just have this evm kit section let's maybe change that to say hello world and delete this and now we have a very simple homepage that we can test on our Local Host so from the terminal just go ahead and run yanev or npm runev whatever package manager you're using open up your browser at localhost 3000 what you should see is your application kind of available for you to preview what's actually happening in your code so while that's happening in the background you can see this is just loading up using nextjs seems like it's ready now give it a couple seconds here just to load up for the first time and here we go okay so we have the header component and then we have our homepage which just says hello world so to make it even simpler let's go ahead and first allow our users to connect their wallet and then we're secondly going to allow users to read and write information from our smart contract so the easiest way to do that is use the use contract Hook from the thrb react SDK so here's a documentation you can read through it if you want but it's pretty similar if you've ever used wag me it's also very similar what we're going to do is at the top of the file we're going to use that use contract hook deconstruct the contract value out of the Ed contract hook import that on line one here and then we're going to say we need to provide the contract address okay and if you remember we just pasted that in to a value in contracts. TS here so I have a contract value of variable here called grea down the bottom it says hey am I in a development environment then I'm going to use the environment sorry I'm going to use the value for the dev smart contract if I'm in a production environment I'm going to use the value for the production smart contract address here what we're going to do is we're going to import that value so we're going to say grea and you can see the first option here is for me to import that on line three I'm importing grea from that and you can see here is the value that it's importing okay so now we can interact with this smart contract that we have just kind of connected to Via this hook so what we're going to do first is we're going to say read the smart contract greet function and then second we're going to actually call the set greeting function using what's called a web3 button component the web 3 button component so what we're going to do here is we're going to use again this is very similar to wag me if you've ever used wag me before what we're going to do is we're going to say use contract read in top of the file here here so we're going to import that from the react package we're going to say hey cons data so we're going to deconstruct the data value out of what gets returned from this hook we're going to say is equal to use contract read and we're going to first provide that contract here and you'll see we need the function name as the second one now I'm going to show you since we already know ahead of time we know that the function is called greet but what if we typed a typo in here it doesn't actually know what's kind of going wrong here right we can legitimately call this in typescript has no idea it's not complaining and that is because we haven't run What's called the generate command so if you care about type safety if you care about typescript this is for you so what we're going to do is we're going to open up the terminal in the application just going to split it so we can keep running the environment here from the application uh directory here you can see there is a script called generate and generate is essentially going to read all of the files within your project and say hey is there any smart contract addresses that I can fetch the abis for so that you can use things like use contract read safely with kind of typescript compliance so what we're going to do if you care about typescript if you don't you can probably skip the next minute what we're going to do is run this generate command so we're going to run Yan generate from the application folder here and this is going to run the underlying third web generate command which is kind of going to go and say hey we found an ABI for one of the smart contracts so first says hey is there any smart contract addresses in here looks like there is there's C I'm going to go ahead and get that AI for you and I'm going to kind of make the ABI available for your typescript compiler so now if we I'm going to hit control shift p and search up restart sorry control shift p whoops control shift p sorry and search up restart TS server click this and then once this little spinning icon down the bottom here you can't see it on this screen if I drag it up see there's a spinning icon here that is kind of loading up and saying hey some stuff's going on what's going on we're going to load it up what we should see is this use contract read starts to say hey wait a minute something's not right here you you don't have a one two3 function on your smart contract's give it a couple more seconds here while that loads up I want to show you where this gets stored so it's pretty cool it actually stores the ABI inside the node modules folder in the the webd SL generated abis and in here you'll be able to see okay hey my ox2 E7 which actually doesn't look like the right one and what we care about is Ox CED so copy that and it doesn't seem to actually be there which is not what I wanted okay if this kind of bugs out like it is for me you can go into the third web Json folder and you can actually manually replace this so you can add any contracts in here set the chain ID so what I'm going to do is this should ideally for you just pull this stuff automatically I'm just going to replace this with my smart contract address and then I'm going to change the chain ID so the chain ID should be the chain ID of the ZK evm test net which you can actually get from the infrastructure tab here and you go to RPC you'll be able to see what RPC the network you're connecting to is using so in this case I'm going to search up polygon ZK evm polygon ZK evm test net here and you'll see okay the chain ID is 1442 so let's go ahead and set that here set that here let's reun that command Yan generate on the right hand side here so Yan generate the sort of bugged out in the visual but I'm just rerunning the previous command it says Hey here are the new abis from that file now if we again run that restart typescript server command it'll initialize down the bottom here to say initializing these features give it a couple seconds here what we should see now there we go is it says hey wait a minute you don't have that function on your smart contract you only have greet or set greeting so you can't call this so This prevents you from writing typos or running into any bugs where you just made a silly little mistake and typescripts there to kind of help you save the day it was a little bit of work in the end to actually get that to work but if you do care about having a typescript be helpful in these little read and write operations it is sometimes worthwhile to actually go through that process so instead of greet One 123 we can now see oh you can call either greet or set greeting on your smart Contra so that's what's happening with that generate command let's go ahead and hit the Greet function that's that's what I want to read and really the benefit to this was essentially now you don't need to copy and paste abis maybe you have one person working on the smart contract maybe you have some other people you need to make new configuration files to store the abis you need to make sure you have the Avi up to date you can just hit that Yan generate command it's going to go ahead and fetch the latest AI of your smart contract pull it into a place where you don't need to care about it and you have type safety on read and write operations I think it's pretty cool it was a little janky to get completely set up I will admit but hopefully it's worthwhile for you typescript lvas out there so now what we have is we have the data field which we can go ahead and rename using this colon syntax to say hey let's rename this data field to be greeting so now we have the actual value read from the smart contract in a field called greeting we can also pull in fields like is loading or is loading error the actual one that I want is is loading and maybe we can pull in error as well so this brings in the error message of if something went horribly wrong you can console log this or or inspect what's going on or or display an error to the user I'm just going to go ahead and use the loading field and the error field for the application here so I'm going to scroll down and beneath our heading here I'm going to add a P tag that displays the loading State first of all so if it is loading I'm going to say display a loading tag but once the field loads I'm going to say let's actually display the greeting so greeting in a P tag and if we go back to our Local Host should be able to see uh where's my Local Host onone here it is okay so we should be able to see very small here but it is first loading when we refresh loading loading loading loading until it comes back and then we display the actual value of the variable on the smart contract so that's pretty cool what we're going to do next is we're actually going to update the value on the smart contract as well we're going to do that using this UI component called a web3 button now the web3 button is very cool because it first connects your users to their wallet it secondly swaps them over to the network that you've defined and finally it actually allows users to click the button call the function on the smart contract has a nice loading animation while we wait for that transaction to go through and then update any of the read values that we have automatically so whoops that's the wrong button what we're going to do next is we're going to beneath this P tag we're going to add a web3 button so just type out web3 button go ahead and select this import it from the react package up the top here make sure that's there and then we first need to grab the contract address so there's two fields we need here first is contract address and the second is action and then we can close that off put the text here to say whatever you want let's just say set greeting in this case here we're going to use the value of grea so let's just paste the contract address is grea again that's going to use the development contract address in the development environment and the production contract address in the production environment on the correct chain within the action here we can access the contract and we can simply just call contract. call and we're going to type the set greeting what have we got here set greeting sure why the type script is not popping up call the set greeing function and then we need to provide in an array here the new argument that we want so let's just say new value you can alternatively use this use contract right value if you want to type safety so you can say mutate or mutate async if you prefer working async ly and deconstruct that out of the used contract right hook again you'll need to import that on line one then you should get the type safety if you pass the contract and then if you hit yeah there we go so you can get greet or set greeting in this case let's use the mutate async and instead of this way just so I can show you the type safe way as well what we're going to do is we're going to call this mutate async function which again we can rename to be set greeting save that so now we have a function called set greeting which calls the set greeting function on the smart contract hopefully that's self-explanatory within the action we're going to call this function and say set greeting and here we need to provide a object and arguments are set to an array of strings so if you provide something that doesn't work here it's going to tell you off with the typescript setup if you provide a valid valid value such as hello world this is a new value set that in here what we're going to do is actually go back to our application and preview this kind of setup now so if we click this first it's going to pop up with this really beautiful customizable connect wallet component go ahead and connect our wallet it's going to swap us if we're on the wrong Network here for example if I swap to ethereum main net it's then going to say switch Network and confirm that in your wallet swap us over to the network and then show the actual button so now we can actually call the function this is going to load up and be disabled while while it is loading up go ahead and confirm this spins until that transaction goes through should be a couple seconds here should be like 3 two one here we go couple seconds there we go it's gone through and the transaction as you can see here has automatically updated based on that function being called so this is pretty handy the values automatically update the button is disabled while it's loading the user is easily onboarded to whatever chain you're using and it get some pretty nice uh designed UI components here as well so that brings us to the conclusion of full stack development on the polygon ZK evm if you're interested in using the evm kit if you like the way it looks recommend you read through all of the other capabilities that come kind of out of the box my favorite one is the Shad CN UI capabilities with his CLI that he's built he recently joined the Vel team amazing amazing Library would definitely recommend you use this for your front end development if you're interested all of the other features such as decentralized storage with ipfs user authentication with the sign in with ethereum standard really taking third web's capabilities to its limits here in this evm kit and a lot of cool stuff that you can do and kind of get up and running very quickly hopefully if you like it you can join the Discord as well to ask any questions that you might have and again it is all completely open source so if you want to contribute as well feel free to do so I also have another video on my Channel about evm Kit as well I believe it'll be called how I'm building web 3 apps in 2023 or in 2024 whenever you're watching the video hopefully I've remembered to update the title the video goes into a bit more detail than I did here but hopefully that was a good introduction to creating full stock applications again the massive benefit to a evm compatible zero knowledge rollup is that you get all of the familiarity with both Dev tools and user tools out of the box as it is evm compatible so it's a massive benefit to be evm compatible use all of the familiar tools all of the familiar wallets to actually create these applications and really all you're getting is a much more scalable chain that you can build on so that's it for this video remember to leave a like if you enjoyed it subscribe to the channel if you want to see more web3 content very soon and if you did I'll see you in the next one
Info
Channel: Jarrod Watts
Views: 3,138
Rating: undefined out of 5
Keywords: polygon zkevm, polygon zkevm devleopment, polygon zkevm smart contracts, how does polygon zkevm work, polygon zkevm explained, polygon zkevm guide, polygon zkevm hardhat, polygon zkevm web3, polygon zkevm tutorial, polygon zkevm get started, polygon zkevm for developers, polygon zk-evm, web3 polygon zkevm
Id: pkvgS26VR8E
Channel Id: undefined
Length: 71min 13sec (4273 seconds)
Published: Mon Oct 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.