Web3 Frontend Development, Part 2
Urbe Campus ETHRome Edition 2024 · October 2024 · 2:40:36
Orbulo continues the Urbe Campus ETHRome edition lessons by introducing Ethereum token standards, why standards keep smart contracts interoperable, and how to use Scaffold-ETH 2 to solve Speedrun Ethereum challenges. The long hands-on session teaches blockchain basics while getting participants used to the tooling they will need at the hackathon.
Speakers
Topics
From Urbe Campus ETHRome Edition 2024, hosted by Urbe.eth.
Transcript
[Music] [Applause] [Music] we are going to use scaffold this in order to solve um the speedrun ethereum challenges we will go into them and so basically have some really nice
challenges that teach you both how some basics of the of the blockchain and the web tree work and at the same time you can get used to using scaffold this and so on but before starting I want to ask you did you by any chance with fabri to do any like um ERC standards like rc20 and stuff like that and it talken standards no I don't think so no so before talking about the ah also other two things we will have a hard stop around 320 320 25ish because I have a call I cannot
postpone it and then from 5:00 p.m. to 6:00 p.m. lemon will talk you will will talk a little bit about agons and how they work and stuff so that you can get ready for for tomorrow so we will stop at 5 basically so before starting talking about the speedrun ethereum challenges uh I need to talk you I need to talk about the the standard so in the ethereum community there are different standard standards and basically a standard is um a way that we can um Define something that can be for example
reused by many developers or also in the case of ethereum also in uh in guidelines or communication but we'll focus on the uh on the technical implementations and um why do we need standards because of course if in a in um in a blockchain environment if everybody was to develop their own stuff it would get basically a mess so everybody would have their own smart contracts working in their way and it's actually like this but thankfully there are some standards that are pretty used and so we're going to focus on two of them uh which are called token standards
like yeah it's actually like an RFC but in ethereum are called ERC which stands for ethereum request for comments basically so it's like an RFC but for ethereum and in this case we are going to focus on two uh token standards so um which are the rc20 and the erc721 and basically these standards are just interfaces so from a from a code point of view it's like basically defining how this smart contract would work what are their um their their um properties their functions the return value of the functions and so on and so forth and the
the most important one and the one that it's actively I think the most deployed smart contract on any chain on any evm chain is the erc20 token standard which basically defines um fible tokens fible meaning um when something is fible it means that one item like two items of the same category basically are interchangeable so it's you can think as something fible like dollars or Euro for example if you have 1 EUR and 1 EUR they are completely interchangeable so the erc20 token standard is used to represent uh any type of fungible asset
whether the whether it could represent money whether it could represent for example as you can see over here uh reputation points as we said fiat currency like us the skills of a character in a game and so on and so forth so why this uh standard is so important it's because as you may imagine even if it seems fun to make it represent skills of a character and so on and so forth it's mainly used to represent currencies on chain so this is the standard that it's used to basically Define all the coins that you may have heard of um and like Pap or whatever any kind of meme coin as well they are all basically esc20
tokens and as I as I said before erc20 is just a standard then how you define the functions of how you define the body of the of of the function of course it's up to you but of course it's always better to keep pretty much everything um as it's meant to be so the the erc20 standard which I will show you from a contract point of view and I will send it to Discord so that you can check Along on your computer because I think that you will see it better uh right here I send you the ga page of the Year
C20 contract and just want to show you what this contract does so we're going to read this contract together and to understand basically how it works and why it's basically used as a as a curreny in this case so we have some private variables such as the balances which basically it's a mapping you you you saw mappings with Fabrizio and basically this mapping tells the smart contract for each account how much token how many tokens does this account hold basically
then we have the allowances that we will talk about later when we will see the function but it's a mapping of a mapping but I said we're going to see it later then we have the total Supply so basically the total Supply is the amount of uh supply of these tokens that it's available so it can be 10,000 1 billion whatever you want and also it can it can vary as well so it it it does not need to be fixed so as you can see in this contract over here well this is an abstract contract but uh for example you can create an esc20 token that uh you can
create uh also like you do not need to Define it at at the start but you can increment the total Supply when you want to create this token and this operation is called minting so it's a mint operation and usually in smart contracts in erc20 uh token where you mint something so when you create new coins you also have the barn functionality which means to basically destroy so it's decreas in the supply so the mint is going to increase it and the barn is going to um decrease it but this smart contract here does not have the the minting and the burning functionalities also something which is
really cool about your question um so cont doin for Bitcoin sorry doin ah yeah Dogecoin it's an no well Dogecoin has its own chain I think but you get it you get the point like if you add for example I don't know Sheba for it's it's anc20 and um the name and the symbol is actually what we're talking now so the name it's the name of the of the ec20 token that you're creating and the symbol is the is the tier so if we go to any I don't know this one which is pretty funny which is called key key cat this is not Financial advice by the way so
just to be so for example this one is called it's called keyboard cut and key cut it's so this is the name and this is the symbol and well this is an image of a cat typing on a on a on a keyboard so so this is the name and this is the symbol and the symbol is the one used pretty much to show it as a ticker like you USD it's the tier for for for dollars key Cut Is the is the symbol of this token so getting back to to the contract it's pretty easy like when you create in this case a new rc20 you pass a name and a symbol and this is what we were seeing before this function these functions are read function to get the name and the
symbol and whatever and then we have the concept of decimals so I think that it I I think I saw a message that fabrio sent you which should be this one here decimals in solidity so decimals are pretty important when working on solidity and also they are really important when dealing with your c20s because uh as you may know in solidity there's no like floating point and for example 5.05 does not exist in solidity but it's it's actually 505 divided by this amount so this is why we have decimals and
decimals are really important when you build frontends because whenever you read or send amounts to the to the to the blockchain inside a transaction you're always going to send them at the correct decimals so in this case this function this um year C20 has 18 decimals which is the St which is the St standard but actually there are some uh esc20 tokens that have six decimals or nine decimals so whenever you need to uh manage these um these tokens in your front end or when you need to call the the functions you always need to First retrieve the amount
of decimals that that given token has and this is really important because if you do not manage the decimals you may end up for example dividing uh a token that has six decimals with one that has 18 and the result will not be the one that you are expecting because the it's not the same number actually when you send it to the blockchain so example one token with three decimal is one with three zeros basically it's one with three zero yeah and so at the same time if you have 18 it's one with 18 zeros basically and so even if one is one actually when you
are going to send it or when you're going to deal with them you need to send one with 18 zeros and one with three zeros and not uh just one because one means basically zero like all the zeros and then one so it's basically the smallest amount that you could send so well this is another read function for the total Supply this is a function that allows you to retrieve the balance of a given address and we are going to use these functions later uh while using scaffold it and then we have the transfer function the transfer function allows the the the Coler of the function the message sender which in in this case it's not the same message
sender that you saw before because in what you saw with Fabrizio you use this message do sender while instead in oops what is it in the 20 we are going to they are using this function which comes from another contract which is called context but we will not go into that but basically this message sender is used to um always get the the one that is submitting the transaction because there are some cases where the message sender could be modified and so on so it's just a security feature so you do not need to really worry about it in our case when
it's an user interacting with it's always the it's like putting message do sender basically and the transfer function is going to call this helper function which is the underscore transfer and the underscore transfer function it's pretty easy so it just checks that the from and the two addresses so who's sending and who's receiving are not the empty like the void address and yeah I I will go back to it later and then it's going to basically if yeah so now I I I am going to explain it so basically so the transfer cannot be done
to like the the transfer cannot be done from or via the uh address zero but when before I I was talking about minting and burning actually when you mint um when you mint an erc20 it's like the center of the transaction of Min like no say it like this is wrong so when you mint anc20 it's like it comes from the address zero and when you burn anc20 you send it to the address zero and I let me okay so we have the burn and mean so
we're going to see it now so this is the mint and this is the burn okay if we go into to the update function which is the one that it's used both in the transfer in the burn and in the mint what this function does so is if the from is the address zero which I just told you when the from is the address zero we are minting what we are doing is we are increasing the total Supply so we are creating new tokens basically so if we had zero and we're going to Mint 10 tokens we are going to have a total supply of 10 in the case it's not the from in in case it's not the address zero so we are not mean thing what we are doing is that we are retrieving the balance of who's sending the token we are checking that with with
this check that who sending the token as enough balance of course so in the case my I try to send uh 10 tokens but I have five it's going to revert with this erc20 insufficient balance error and then it's going to basically decrease my balance okay so balance of from in this case I'm the one sending gets the by the value that I'm trying to send after my balance has been decreased and this is really important from a security point of view we sadly we do not have a lot of time to talk about uh security of smart contract but you always need to decrease this before updating the other and I will tell you
in 10 seconds but um after I decrease my balance there's another check that says okay if if I'm sending to the address zero I'm burning the the token so I just decrease basically the total Supply so if this if I'm transferring these tokens which is not from using the transfer function but using the burn function if I'm transferring these tokens to the other zero I just decrease the total Supply so if I burn 10 tokens that I just minted basically the total Supply goes back to zero and my balance has been decreased instead if I'm sending it to an address which is is not the address zero his balance is going to be increased why I
was telling you that this always needs to happen before this is because uh there's an a security attack that can be done on Smart contracts which is called reentrancy attack which is basically when you call multiple times these kind of functions and in the case this would would would have been placed after this one whenever I would enter this function basically I could increase infinitely my balance so that's why it need before I need to decrease mine and then I need to update the order and whatever and this emits an event yeah the unchecked so the the unchecked is it's basically um in this case it's
used to per to optimize a little bit the gas cuz when you per perform these kind of mathematical operations basically by using the unchecked um you are able to like spend less gas performing these operations but it's just there are some solidity developers that are really concerned about like even saving 10 units of gas so but in reality the the the unchecked it's used when you need to perform like some bitwise operations so it's used in more complex environments but is it's really Advanced solidity and most of the time it's never used like in this case
it's mainly used to save some gas on these operations basically so it's unchecked because as it says here I there's no possibility for overflow and so I do not need to check for some operational security features that solidity has because as it says over here like the the value it's always lesser or equal than the total Supply and the value and the fromont balance it's always in this range so there's no need to have like underflow or overflow checks so that's why it's unchecked basically and uh yeah those are the mean and the B and then we come to what I think it's one of the most important
functions and also one of the most overlooked one ones which is the approv so the approve the approv function which is this one of course it checks always that the owner and the spender in this case must not be the address zero the approv function increases the allowances mapping of the owner and the spender of the amount of value what does it mean when I approve a spender uh of a given amount basically I allow that address or that smart contract to move funds on my behalf so basically by approving moving a smart contract that given smart contract can
take away your C20 tokens from me and move them okay so that's the goal of the approve so I say I want to approve lemon in this case of 10 usdc spending later demon can pull these tokens from me and use them for whatever so yeah what said uh no it's lion needs to be a smart contract for sure so it cannot be done from EAS but it's it's just a matter of allowing smart contracts to pull tokens from you
because now I'm I'm going to talk about why this is important and what's the difference between ec2s and eth so we I skipped a function before I don't know if you know this but we have the transfer function which is the one that I'm the one sending which is the message sender as you can see over here but then we have also the transfer from function where you can define an address from the one you are basically transferring this token so this is the function which is um which is pulling these tokens and sending it elsewhere so in in in in the case of Lon if Lon was a smart contract
after after I give approve to Lion lemon this the the smart contract can then call the transfer from and use my tokens and transfer them elsewhere maybe he want to transfer to himself or to another smart contract or do whatever and why this is different from and why this is important and why this is different and why I'm talking about this because uh you in the you have done this payments over here okay so this yeah this one has the receive which was this one I think that I saw yeah whatever let's use this
one so the main difference between sending uh year c20s and sending if is that when you interact with a with a smart contract when you send e you can send e alongside the call of the function okay so you can basically call uh a given function uh which is marked as payable and you can send it in the same transaction and getting it with the message do value I don't know if you have it I I remember I saw it somewhere but don't know if you have done this stuff but basically as you have the message do sender you also have the message. value the message. value as the
amount of eth that was send inside that given transaction but this cannot be done with r20s okay you cannot send your c20s to a smart like you cannot call um a smart contract function and send year c20s alongside with it okay so how do we fix this thing so how can I send esc20 to as smart Contra maybe I want to buy I want to participate in an in an auction for example I want to buy this nft which is something that we will see later I I want to buy this nft and I need to pay with with the usdc token which is a stable coin which one one usdc it's $1 I
need to pay 1,000 usdc to buy this nft I need to call this function but I cannot send the tokens and that's where the approved function comes in so basically when using your c20s the flow is that that I approve the contract that I'm about to call to take 1,000 usdc from me and then I'm going to call the function I don't know uh make bid whatever bid or buy let's call it buy nft I'm going to call the buy nft function and the buy nft function is going to call the transfer from and it's going to pull the zc20 tokens from me so that's why we have the approval and the
transfer from yeah I an because because the the erc20 token it's a really old token it's it's a really old standard that was it's like ERC and then the number it's it's it's an incremental number so you can imagine if erc1 was the first ERC that proposed uh how all the future ERC would come the erc20 it's just 20 earc is after and between the rc20 and the RC 721 there's a lot of time that has passed in fact this is not the case with RC uh SE 721 and it's just a matter of how old
the rc20 Zen is and the fact that since it's has been used in so many ways and it has become so the standard basically to do coins uh on chain of course uh we we are stuck with this problem there's a an a standard that they proposed in reality which I don't remember if it's here but it's a standard that they tried to do which is the ERC 777 this one the problem with this one is that as you can read in this huge warning as is really susceptible to a lot of different attacks so basically
they try to extend the rc20 functionality and use the hooks which is the functionality that the ERC 71 has which is basically allows any smart contract to react when it receives ERC 721 tokens they try to put it inside rc7 inside this RC but it went like nuts so they told that's it could create a lot of problems and as you can see this one requires a double call instead this one uses the hook by uh it it it didn't go well if you if you want my opinion so we are basically
stuck with this double transaction forever it could be done yeah it it could be done and we're going to see how it's done on the erc721 so uh I wanted to tell you this stuff about the the approve and the transfer because it's something that you will find a lot like whenever like either in the agodon OR also if you want to keep on working on in the in the web tree this flow of here20 so the approve and transfer from it's used everywhere and any front end whenever the user needs to spend its C c20s it always needs to uh let the user approve the spending first something that you may ask is that the easiest solution for this is that
okay I just approve the smart contract to spend any amount of tokens so that I do do not need every time to I no sorry because I I didn't tell you how the transfer uh from work so after the transfer from has been called my allowance so the it's not mine but Limon allowance um in in my behalf basically decreases of the amount that is going to transfer so why don't we have a whiteboard so if I I approve liony to spend 10 tokens lemon transfers five of my tokens the allowance of Li is five in this case because he just transferred five so
every time decreases of course it cannot go below zero and he can only spend maximum the amount so what does that mean is that after lmon I spent 10 of my usdc I need to give him the I need to give him the approved back okay because he basically exhausted the amount of tokens that he can transfer and you may ask okay why don't basically I approve lemon to spend the total supply of the token who cares so lemon can every time basically transfer my tokens without me needing to approving and so on and so forth and that is something that a lot of developers have done in the past and
actually as of today it's one of the ways that people scam others on the web tree because basically by allowing by um basically people developing front ends um propose these transactions to the users telling them okay just give me the approv of all the tokens possible so in the case in the future you're going to buy some or you're going to get some uh I will be able to get them away from you so it's actually used mainly for scamming this kind of functionality so it's always to always um let the user approve the amount of tokens that he needs that's for security reason so if I
need to spend 1,000 usdc there's no need to let them approve a million okay even if I have 1 million for security reason always always approve the amount that the user needs to transfer even if that means every time performing two transactions so that was the erc20 basically so it's a really simple smart contract even if it may seem long it's just mainly comments and uh the second token that I wanted to talk about it's the ERC 721 which came in 2017 2018 and basically in as the year C20 token is a token that defines fible
assets the RC 721 it's a token that describes non fible assets so assets assets that are not interchangeable between each other so this case you can see some examples you could have collectible items you could have uh numbered seats for concerts or sport events uh you could have uh anything that you can identify as unique basically so I don't know uh unique cards and and stuff like that so the ERC 71 is actually of course more complex than than the erc20 and here there are some popular nfts that maybe you heard of you of the for example the B Apes
that have gone really wild some years ago about I don't know costing millions and crypto kitties that was one of the first actually it's not an rc21 it's not an ERC 721 but has some functionalities and this is really really old this is like 2017 and it was really nice these little kitties but whatever there some Nostalgia sorry and um so the s721 as you can see it's way way way more complex than the rc20 this is why this is because it needs to describe non-fungible assets so there's a concept of uniqueness and
that's why we have uh the concept of token ID so each uh nft each token of um uh each ERC 71 token has its own ID so it's Unique basically and uh you still have the name the symbol you have the owners the balances the approvals the operator approvals which is pretty much the same thing so everything right here is pretty much the same you have the Constructor with the name and the symbol and uh this comes from another ERC don't worry about it it's basically to check that this smart contract actually
supports uh is actually an actually supports the ERC 721 and the ERC 71 meta data that we are going to talk about it also has the balance off but as you can see it has a concept of owner so each token ID has its own owner so that's why I was telling you about for example the uh tickets to a concert as when you go to the or when you go to the to the stadium you have your name written on the on the token here you are basically the the name written on the on the ticket here basically instead you are the owner of that given ticket in this case you have the name you have the
symbol and then we have the first uh main difference between the erc20 and the RC71 erc721 which is the token Yuri so associated with any ERC 721 token you can associate metadata so I'm going to show you uh metadata standard so basically there's a there's a standard for metadata which comes from open Zepplin which is an uh nft Marketplace and so send it sent you on on on Discord basically the token URI which
is let me close the 20 so basically the token U it's a function that gets a token ID as input and then returns an URL this is going to return um a resource a unique resource identifier for that given token ID what does it mean that we can for example put an URL for that given token ID and that URL must return a Json containing different information this is I pretty big standard but you can have for example the image of the of the token you can
have the name the description the background color uh the animation URL so it's it's a standard that allows other clients and other websites to show your uh tokens basically so you can enrich your token with as a I said animations name and also attributes attributes are basically uh traits that are connected to to your to your token and these are used mainly in um um in nfts of course for example if we get a random nft I don't know this one I don't know what is this stuff but
whatever hopefully it has some trades of course I only I took the one that has one TR right so let me get this ones so in in the case of nfts as you can see all this data over here so this image the description the trades and so on and so on and so forth It's taken directly from the metadata standard so you can with with this uh erc721 metadata you can create more complex tokens and also you can add any kind of information that you want because you have this standard but you can also extend the standard to add everything that you need so in in the
case of a um of a sports ticket okay inside your metadata you could put I don't know the number of the row in the in the stadium uh the number of your seat in that given row uh the entrance and so on and so forth so you can enrich basically your token which in reality on chain it's just an ID so everything else that that you see over here in in like do not look at the price of course but the image and everything actually does not does not live on chain so it's outside basically to yeah I think that we can get it
hopefully from if we do some if we can get it somewhere otherwise we can do Bas change yeah 100% yeah this is really this is really important what he just said so basically this token you can be let me get this one as you can see does it look decentralized to you it's not you would put yeah yeah yeah you can you can use some um yeah you can
use epfs you can use its swarm which are basically uh decentralized storages where the data is immutable so but U which they are really interesting but we do not have time to talk about them and they are actually actively used by a lot of nfts or erc721 projects um but but in some of them are just basically using their own servers and their own apis in this case it's the bed ape cab and blah blah blah and this is the this is basically the the Json and as you can see the image of this Json in this case it's stored on epfs
but these are the attributes which is background M1 blue if we go here back to the to the rates as you can see background M1 blue the far it's M1 Brown I don't know whatever that means but so let's talk a little bit about epfs since we mentioned so epfs um it's a really cool open source project um which epfs stands for interplanetary file system and um going to show you the oh my God epfs just
died whatever so basically epfs it's a network of nodes that uh store uh data actually they do not store um full like when you upload something on epfs Via a node your data gets basically chunked and then uh gets shared among a lot of nodes so it's also redundant so that that it's not lost but the coolest thing about epfs is that this value that you see here is it's actually an ash and it's it's an ash of the content that you just uploaded what does that mean is that if I
upload um a image of myself for example on epfs I would get an ash if someone else tries to upload my same image he will get the same Ash okay because the image is the same what does that mean is that if I try to change the I I cannot change the image because that Ash is going to always go it's going to always Target the same content the same image in the case of for example a Json if I upload a Json on epfs and uh actually there was this one which I think this is a yeah this is a website but I'm want to talk you a
little bit about this as well um if I upload a Json on epfs and then I store immutably that given Ash on an ERC 721 smart contract and there's no way for me to change it then um since that image is always going to leave on epfs because I cannot remove it and it cannot be change then I have immutability also for my metadata in this case what they decided to do it's a 50/50 where they basically uploaded an image on epfs but in reality the API it's managed by their own website so it's not fully centralized and what it's cool is that if you go
on uh if you use epfs doio /fs and then you put the ash you get the image so this is served pretty much for everyone everywhere it's always available and this is really cool and what I showed you here instead was this is something that we built at an aaton sadly it goes too fast but um it's an ash that actually points to a website in reality it does not point to a website but it points to an HTML page this is something that we did with lemon in um in Brussels because since you can basically upload anything on epfs you can also upload HTML pages okay so what
we did is that we loaded an HTML page on epfs and when the browser loads that given uh HTML page inside the code of the of the uh HTML page there's a redirect that redirects you to another website was not this one it was this one so it loads and then it redirects and what is what what is the advantes the advantage is that when yesterday I yesterday I I talked about something which is um which is over here I just said this stuff here I said the the
term dap that stands for decentralized application one time used to refer to Unstoppable application an Unstoppable app something which is served in a decentralized manner is in this case an um a website that it's available on epfs okay so if you upload the HTML the Cs the CSS and the JavaScript on on epfs that website is never going to change and can never going to be brought down basically so that's the point of uploading websites this only works for um something that can be compiled of course to HTML JavaScript and CSS it does not work for example with nextjs
but with a basic react project if you actively build it and get the HTML CSS and JavaScript and you upload it on epfs it's going to be there forever it's nobody's going to basically put it down and to give you an an example there's do you know tornado cach okay so basically tornado cach it's um I need to find what's the what's the Gateway for tornado cash hopefully it's this one so tornado cash was an app was actually is it's only banned in the in the USA but uh it was an application
that was used which the the the goal of the application was basically was to break the link between uh transfers of tokens whether they are eth or whether they are erc20 so since the blockchain everything is public every everything is transparent if I want to send tokens to you or I want to send my tokens or my eth to another address uh you can always see what I'm doing where I'm moving my tokens and so on what they did with tornado cach basically is that you could send these tokens and then later you could get back these tokens from another address without needing to share where you are sending them and from who you are
getting them so you can imagine that that that like tornado cach was used to basically wash a lot of money okay and it was mainly used by uh it was used by a lot of people that believed in privacy of course and so on but of course um it was mainly used by um North Korean hackers and like Taiwanese hackers as well to basically wash uh acket money from from Max and stuff like that so there was a huge lawsuit actually the the guy that developed tornado cash even if they did not have any power on the
protocol itself was arrested and that it's really sad actually because he was arrested for basically coding but whatever he's still in trial and um so basically the the the cool thing about tornado cash is that even if the website it's um it's locked in the in the US since everything about tornado cach is decentralized so the protocol is just some smart contracts on the blockchain and since they uploaded the website on epfs you can still actively actively
access yeah because it's active on on uh on epfs I need to I I don't remember what is the I think that it should be like okay make a full yeah that was what we thought a lot of like uh like more like six or seven years ago that was basically the goal of everything like we always had in mind this idea of having the the back end to be uh the blockchain and then the front end being something that you uploaded on epfs and it's going to stay there basically forever nobody's going to change it and so on and so forth but the
fact is that uh mainly for from the front end point of view a lot of stuff has has changed and in order to basically have websites to be uh more usable by people uh to have more advanced features and even usability as well because the fact is that epfs seems cool and nice you can upload your website but it's actually really slow it's painfully slow like the the the more the content is Big the the slower it gets to be from from being retrieved and so also you cannot use uh for example some given apis that you could
use for from from next you could you cannot use caching so the web three experience was really poor and uh the whole Community decided basically to abandon this idea of uploading webs on epfs even if it's really cool because it allows you to to do this kind of stuff and creating these Unstoppable websites but today that has kind of lightened a little bit but mainly due to the fact of as I said slowness and uh difficulty from people to use this stuff and so on so let's get back to the FC 71 and um yeah so
getting back we saw the the the toi we saw the epfs which again it's really cool but sadly they're not getting any money so I don't know if it's going to leave soon then we have uh the the approv function so it works it can work in the same way of the erc20 it has the transfer from it has also a safe transfer from function which um Cod this function over here that I'm going to show you which I don't think is actually here but it's in another contract so
basically the second main difference so the first one is the concept of the the third difference the first difference is that erc20 is have token IDs the second one is that erc20 is have a token Yuri Associated to a given ID and the third difference is that you can Implement in your smart contract this interface which is called the ERC 7721 receiver if you implement this function inside your smart contract which is called the onrc 721 receives you can actually uh react to uh whenever an erc721 token is sent to you how how
does this work when if we go back to the Token on RC71 received if you call the this safe transfer from function so whenever someone calls the safe transfer from function as you can see this is going to call this utils library and it's going to call this method with that under the hood is going to call the on c71 received on your smart contct cont this is really cool because I can send an ERC 721 or an nft let's call it nft it's it's shorter so I can send an nft to a given smart contract and if that smart contract
implements this function it can do some stuff you can also not only send the token but what it's really cool is that as you can see when you transfer nfts you can also transfer data as well this is really nice because in this case and something that is done for example if I want to like let's imagine we want to develop an an nft Marketplace smart contract okay the best way to do this is implementing this function over over here and basically letting the user just send their nfts to me alongside these bytes called Data data
which is unencoded uh it can be an unencoded struct an unencoded string whatever you want mostly it's an it's an encoded struct containing for example the uh amount of tokens that I expect to sell this nft for or uh uh when I want the the auction to end and so on and so forth so you can send also data as well and me the nft marketplace when I receive this and ft I can extract this data and perform any kind of operation that I want so it's actually cheaper for the user and it's also simpler for us from a smart from a smart contact developer point of view
because we just need to basically react to this kind of uh of hook and what it's cool at the same time is that if someone tries to send us any nft without data we can just basically revert the function and nothing happens the token does not get get transferred to us and we can basically just stop so that was the the the ook that they tried to implement also for ER c20s that went really bad in this case we can also for example bypass the the uh approval by using the zook so then there there are not really
other changes not so much about the yeah those are the main three we also have the same concept of minting and barning but instead the the the meaning in years in nft smart contracts basically increment the token ID so the token ID goes from one to two to three and so on and also increases the total Supply while instead the burning is setting the owner of a given token to the address zero basically so when you Barn an nft you you you set the the owner of that uh nft to the address zero and when you mint
instead you create a new one and you give it the next token ID basically yeah so that was pretty much it about erc20 and erc721 do you have any questions [Music] or uh I didn't understand of of year c20s yeah yeah let me get back the let me get back the implementation because I would like to a whiteboard online maybe try can I yeah but can I like type stuff as well like
amazing so thank you I didn't know that I use it but I didn't know that that I could type so um the approval flow is basically I don't know if I can zoom or if I can make it bigger but so in the case of wow this is going to be painful without a mouse but so in the case of of eth it's it's actually uh a push transfer so wow this is really bad but whatever so when you send e you are actually pushing e towards the smart contract
what does that mean as I said before when you uh call a function you can send e alongside that given call okay so if I call for example a transfer function let's call it three TR if I call the transfer function on a smart contract I can also send e with it the problem that we have with um with uh es c20s instead so let's let's say just e 20 is that since we do not have that hook that I I just showed you I cannot perform a push because if I if I try to send erc20 is to a Smart contract
that the smart contract is not going to react I cannot there's no way for me to send tokens alongside the transfer function or or the transfer or whatever function I'm trying to call so that's the main difference between e and uh erc20 tokens so what they came up with since and this can only be done with eth doesn't use no no no no because ER c20s are something which is built on top of ethereum ethereum when ethereum and all evm chains there are some chains that allow you to do some stuff but uh like most common chains like also the most
used ones only allow their native token in this case uh ethereum to be sent alongside transactions since rc20 came later and since it's not part of the protocol because it's just a token it's just a smart contract build on top you cannot send alongside the transaction so what they came up with is this logic over here whoops so this is still the here C20 my God I'm really bad at drawing Tex what also text function yeah but it's it's faster typing so we have the so what they came up with is
that now instead of doing this let's use let's use this one let's make it I don't know let's make it green and in this case this is me and I'm the and I'm the owner okay I I'm the owner of the tokens then let's make another one let's make it red and this is the um the Spender and then let's make this thing over here maybe and no let's keep it this way so
the owner it's me and then we have the spender so the the spender is the one that no let's not call it spender let's call it smart contract so my goal is to send tokens to the smart contract okay because I need to as I said purchase an an nft let's say that this is a nft Marketplace smart contract okay inside this function inside this smart contract there's a function that allows me to buy an nft for a given amount of of tokens the flow since we cannot since I cannot directly
this is harder than I thought since I cannot do what's where's the arrow at okay so this I'm going to remove let's see let's this is the by function as I was saying before I cannot transfer the um I cannot send the ER c20s alongside the uh by function call Okay so what they came up with is instead of the flow of money being from owner to like me actually pushing the tokens so that's why it's called push instead of me actually pushing the tokens what happens is that the first
step is that I approve example one 1,000 WDC spending then when I call the buy function so first call the approve function yeah you always before call the approv function when I call the buy what happens is that the smart contract is going to is going to pull tokens from me so that's the main change so this happens inside the the buy call so you need to imagine this as something that happens when you call the by function
but I I can show to you um this is this is done be this is done because there's no other way for it to be done it's the only way that you can basically send this kind of stuff because as I was saying before esc20 being really old and there was no concept of hooks and and and other stuff we are basically stuck with the first idea that came up to the guys that developed the rc20 which is using this this push I can show you real fast and then we will have a break in 5 minutes um I want to show
you the code of this Mar contract why it needs to be so hard to find something show you real fast what I'm trying to tell you contracts contracts Contra contracts oops should be contracts PR builds Marketplace um English auction logic as you can see for example this is
a this a really complex smart contract that handles pay payouts nftd Marketplace and so on and so forth but as you can see inside all all of this logic when I create an auction in this case what happens is that okay it's going to get the token type blah blah blah it's going to create distruct and then what happens is that it's going to call the no let's let's go to the payout though because I want to show you with ec2s so this function basically it's gets called when I uh purchase um an nft from the smart contract okay the yeah it's it's it's the payout
it's the function that uh takes care of splitting the the money between I don't know if there's some royalties if there are some fees whatever and please make it easy for me no it's in another Library I I hate this stuff but currency transfer lib it's actually in a library because people love to make it easy so as you can see it's the it's going to call this transfer currency with with wrapper and it's going to perform some checks if it's native token or not but whatever what you need to to to check is this is this thing here which is the
safe transfer erc20 and what this does is ignore this case of course because this is the easy case when it's the smart contract actually sending tokens but in this case as you can see it's calling the safe transfer from so and the from it's the user that it's paying and the two is the user that it's selling so basically act as a middleware in this case so if we go back to this to this stuff and we add a third a third actor here and this is the um this is the seller let's change this to
buyer let's make this white again please so what happens is that the nft marketplace is going to pull tokens from me and then it's going to send them to the seller basically so these are so transfer from yeah because I've using the approval that I've done before I allow the nft marketplace contract to basically transfer them however it wants then of course it's up to the smart contract to not steal funds from me obviously so that's why smart contracts always need to be verified they need to be open source and so on and so forth but the logic is that I give him the approval
and then I buy and when I buy the nft marketplace smart contract pulls the token from me and it's sends them to the buyer there's no other way where I could have done this stuff and as I said it's really complicated it's ugly it's disgusting if you want and it's it's it's a pain and uh but it's something that we need to leave up with and um I don't know for for how much time so we can start the the break right now and we will get back at around 4 I think 4 something thank you uh we finish with the with the erc20 and the erc721
um standards so the next step that we need to do is to start building using scaffold this and uh starting the speedrun ethereum which uh as I was mentioning at the start of the lesson it's a series of challenges that you need to do and we of course we do not have the time to um do all of them but I will send you the link and we will start seeing from the first one and then we will go forward with them if I can find the campus it's here so it's it's speedrun ethereum.org are also used inside these
challenges and so you you need to know them and how they work before of course uh start challenging the speedrun ethereum also you uh need to connect your wallet on speedrun ethereum because you will need to submit as well these challenges to the um to ble Guild which is basically uh as I said yesterday it's a a sponsor of the agaton but also it's a community of both very skilled and talented uh teachers and developers and so on and so forth and uh you can learn a lot from that community so like by uh doing the speedrun ethereum and by joining for example their Discord and um you will
learn a lot about web three development so we can go to the if you go as well to the to the first challenge so this is a really easy let me zoom in a little bit so that you can read hopefully you're following Along on your PC as well so the goal of the first challenge is to as we saw yesterday to compile and deploy your first SM contact so it's basically what we did yesterday but you can do it alone on your on your PC today and um you need to follow these instructions which are the same that we saw yesterday so it's a different repo
in this case because it comes directly from from the challenges and not from the scaffold this uh repo but the commments are the same that we saw yesterday the yarn chain to create the local chain and then in another window the deployment and the start of the of the website and um in this challenge which is not really a challenge is will be just basically navigating what we already saw yesterday I think it's really cool if you uh if you do it on your own and then submit the challenge so that in the future you can go and do some more stuff so if you can please execute these
comments that you find here and start your first challenge we will see it together but I need you to start this one because otherwise the second one does not make sense and it's more complex this is the Challenger for one this is the re for
[Music] okay do you have mpm or not installed do you have yeah I okay to install yarn you need to execute this common here so npm install d-h Global yarn this one this will install you yarn if you have an npm otherwise you need to install of course no an npm so this one is you add it okay
so have yeah we're going to see it together because this one it's really it's really easy so well you can also do it on your own it's basically you have these checkpoints and it's really easy to follow but yeah it's just to make you set up with pretty much everything and then we will do the uh the challenge one which is actually a little bit harder than than uh than this one but of course this one uses some concept of nfts and stuff so that's why I wanted to talk a little bit about y needs another package manager like mpm it's yeah JavaScript people like to make the same stuff and call it with other names
pmm yeah pmpm bun whatever so since this one is just a showcase we can do it together then we will go to the to the second one so by following by following the commands I will start the chain as we did yesterday from the from the folder and and as we saw yesterday these are all the accounts this is the the
local network currently running so it says also in a second terminal we need to let me open it on vs code then we will execute the commands yeah trust oops not no no no [Music] uh what it's called this stuff challenge in the second terminal it says to run the the yarn deploy command so as we saw yesterday the yarn deploy command is the one that it's
going to generate the types and then well it's going to deploy the smart contract on the local chain that we are running you can see these are the the transactions and uh then it's going to create that typescript file that I showed you yesterday with all the typings and so on that it's really useful when using scaffold this so after deploying we just need to run the yarar command and when it finishes building everything let me close a little bit of stuff which is not useful anymore so when it finishes building as
you can see this looks pretty pretty much the same as yesterday but also a little bit different that's why I was telling you yesterday about the the fact that with uh scaffold it it's not only a way to the bug Mar contact but also a really useful base for uh your first front 10 because you have already all the hooks all the components a lot of functionality which is already made so looks a little bit different but it's pretty much the same uh good old speedrun ethereum so as we did yesterday the first step in this case is the one to get some funds from theet which is really easy just clicking this button
and we will start getting some eth as well get some eth I don't know how much is needed but you can get pretty much as much as you want and um I think that this one is using my address here as it says don't do it like me so first of all disconnect I didn't read the second step so if you're already connected using metamask disconnect and uh in this case if you're not connected um scaffold D is going to provide you with something which is called a Barner wallet uh a burner wallet is basically uh a temporary wallet that
only Lees in this current session so in this case if I open an incon window as you can see this is a different address from the one that I have over here so it's basically a temporary SL disposable address you may say and um they are completely different they live on their own and it's a useful concept to uh start developing and without the need of also needing to handle for the connection and also without needing to have metam maskar all but yeah so we're going to use the burnard wallet as as they say whatever this is to send tokens we do not need it right now so the second checkpoints the second Che
checkpoint sorry is uh going to my nfts page and of course there's no nft right now so we're going to Mint one yeah what checkpoint checkpoint to this minting the minting NFS yeah yeah yeah you can go you can must be with you no no let's let's follow with me because it's really simple this is just a Showcase of what you can do there not really any kind of code to be executed right now just
wanted to show you the first Challenge and then we will go to the Challen like this is the challenge zero in the challenge one we're going to develop some code and you will actually need to to develop some code so now yeah yeah now you can follow along with me if you want to do it on your own it's easy so you can also ignore me and do it on your own I'm just explaining it and then we will go to the to the more complex one so I just want to basically show you how this challenge work which is not really a challenge it's just a Showcase of what you can do so you have this mint button over here where basically you can click it as many times as you want it's going to
decrease your balance of a little bit due to the sending of the transaction and it's going to mean some animals as you can see it's act actually uploading data to epfs which we already mentioned before so this is really cool and uh you can also as since this code it's open source and maybe at the aaton you want to see uh how to interact with epfs using scaffold it you can check this repo that's why I wanted to show you so we Meed these three amazing nfts and let's open another incognito window so we have another another burner wallet the one that I just closed because I'm an idiot so in our incon window we have no
nfts and what it's asking us to do it's basically transferring this um well actually minting from here but also we can transfer directly from the first one to the second one we can just copy the address and then we can just basically send this nft and without even needing to refresh or anything we get the Buffalo nft over here and you can do the same with the with the zebra so scaffold it seems really easy but at the same time it's really powerful so [Music] um by going on the on the debba
contracts which was the which is the default page of um of scaffold it that it's available on any new instance basically of scaffold it we can see all the various functions of uh of the erc721 which is the what we were seeing today before so we have the counter the name of the nft the symbol total Supply and so on and if we try and call the token URI what we get it's an epfs URL containing the metadata of the token in this case we can just put one and get the token U of the first
nft and this is actually the metad data of the uh nft that we minted so it it's it was the bison and which is the famous bison of scaffold it it's actually their their mascot I think and these are the traits uh the name the external URL and the and the image this guy is actually the founder of beetle Guild is a pretty nice guy I don't know will will atin griffit be Rome lemon atin Griff is coming or not okay sadly let's last year it wasn't there no it was just Kevin okay so yeah this is also something that we
saw yesterday so this is basically the the deployment of the of the smart coner but we do not need to do this stuff right now so because I want you to start actually coding something useful so let's jump straight to the to the challenge one and this is something that you would need to start doing on your own and you can ask me questions and if something is not clear we will read it together understand what they're actually trying to do so I sent you the link over here this bot can also shut up for a second and
um so the first challenge is about a decentralized taking Gap so as superpower of ethereum is allowing you the Builder to create a simple set of rules that an adversarial group of players can use to work together blah blah you can create a decentralized application where users can coordinate the group funding effort so the goal is to build a Staker do Soul contract that collects eth from numerous addresses using a payable stake function and keeps track of the balances after some deadline if it has at least some threshold of e it sends to an example external contract which I don't know if youve saw the the con the
smart contract calls but we can do it together and triggers the complete action sending the full balance if not enough e is collected it allows the users to withdraw so basically the smart contract what it's asking is that different different users can basically send uh eth to the smart contract using a stake function using a payable stake function and our smart contract needs to track the balances of each user that it's going to uh deposit after some deadline which if it's written like this it means that it's going to be um a a variable of the
of the smart contract if it has at least some threshold of e so so if we reached an amount of e inside this smart contract it's going to send this amount to an external smart contract and it's going to uh trigger the complete action uh I think that there's a this code it's available over here so we will also see together the um the code that it's already available and then you can start instead if we did not reach an of e inside this smart contract the users can withdraw basically what they Stak before regarding the front end what we are
asked to do is basically display the information um connected inside the smart contract and the goal is to basically start calling this function so the the front end may be a little bit harder but we can work uh you can work on the on the solidity stuff and then we can do uh the front end part together and yeah blah blah blah the final deliverable is going to be something build but whatever so we can get this uh we can get this code we can get the repo so we see what's available for you and where you
can start please execute as well this comment so you start also seeing the the re and the code yourself so as we saw yesterday this is the typical scaffold this package you have the hard that stuff which is where the smart contract is where the smart contracts uh leaves and you are going to develop and then we have the app so at the start we need to focus first on the smart contract here we have the ster smart contract that you need to implement here
you have all the various like questions also the various functions that you need to implement and this is the logic of this example uh external contact so you can start working on solving this stuff and if you have any questions while you do it you can ask me and if you don't think that you can do it we can do it together so just let me know if you think that it's too hard or it's not clear we can do it together
for for of Star Wars
this remember that you also have the checkpoints and useful info on the speedrun ethereum page so you can also like follow it along and understand what
you're doing if you do not want to do it from scratch just to be clear that spe you e
remember also that if you need to test something or the bar your code you can directly use scaffold this so remember the comments the yarn chain the yarn deploy and the YN start so when you start actually adding the functions you can test them directly on scaffold D because yesterday I wasn't there yeah I don't really understand this scaffold so basically scaffold this is a a starter
for uh it's mainly for the buing smart contract but in reality it can be used also as a starter for front ends that interact with with smart contracts so when you use it in a plain way you have this functionality that it's shown over here in this image which is the debug contracts where basically you can uh call your smart contracts you can read the various parameters like you would do in ramix the what it's the fact that it's better than ramix because you can actually edit it with code uh deployment of the smart contract it's easier you just need to run a single command on your terminal and so on so you need to imagine it right
now by following as I said there's the checkpoints and what you need to do except from the actual implementation but there's some checkpoints of what you need to do and they guide you alongside this challenge basically the so that's what it's basically scaffold is some sort of a starter you may say yeah yeah yeah I think that there's a bounty as well for people using SC scaffold it so I don't get just right the Top Line
says ver4 and for me right now makes I did the moment 0.27 if you have uh if you have this you can change it like ignore the negatively impact submission grading you can ignore it if you have another version you can put the your version change that verion no no no you can you can there's no need to not change it CH if it gives you problem like if if
you cannot install the 0.8.4 you can also use the current one that you have without any problem it should work so it's just that they want to keep it for them when you submit this stuff it's better of course to have their same version but when you need when you need the when
when you're doing it on seia not on hard yeah you need the you need actually the sepolia e but you can if you are doing like you are deploying it on sepolia already no the checkpoint and of the challenge one or the challenge zero zero no you need to be at the challenge one we are looking at the challenge one right now that's why I don't know
[Music] for for for for
Contin are you all still at checkpoint one someone okay so maybe we can we can start seeing
it together so we can go a little bit faster so so let's follow the checkpoint one so it's going to tell us that as we said in the start we need to track the individual balances and also to manage the threshold so it's conveniently saying to us how we need to Define in this case the the balances and the current threshold the thres the threshold as we were reading before is the amount of e that we need to receive in order to um be able to call the uh external smart
contract so it of course it does not say how we need to develop this function it just tells us that we can write it and test it basically so the we need to type the the stake function which is going to be uh public and so before writing the stake function is also telling us that we need to uh add the stake event as well which is an event that the stake function needs to emit basically so we're going to define the stake event as the we can call it Staker and amount
so I won't type anything because you will get spoilers from the compiler from the from the gab compiler but the idea is that the stake function what it needs to do it needs to increment the amount of um the amount of e that I've Stak inside the smart contract by updating my balance and then it needs to um emit the um the stake event so I would just say the balances of the message. sender is going to increase by the value which was something that we were seeing before and then we just need to emit the stake with the message do sender and the message.
value so this is uh basically the easiest way to implement this uh stake function and as we read also here at the end we need also to add the receive special function that allows the smart contract to receive it and it's going to call Stak so we just type uh receive payable does this need to be a function no this should be calling stake it should be P this should be external external payable
so basically this is the simplest best and the most functional way to implement the staking in the context of our of our smart contract so we can remove this comment because we did this and also we can remove this comment as well because we did this so to recap real fast we added the mapping we set the threshold to be 1 E we created the the stake event the same the STI function what it does it basically receives the increases the my my balance inside the mapping by the message value and
then emits a stake event I will leave it like okay so so what it's telling us right now is that we need to test it basically so as we did before and as we projects or challenge one we are going to do the same the same YN chain as before so we start the local chain then we're going to deploy using the Yan deploy this should be deploying the ster smart contract yes and then we start our amazing scaffold D so we're going to test our staking
function to see if it works so decentralized taking up blah blah blah we have our burner wallet I'm going to get some some funds from the faet and it tells us to basically do you see the Staker contract the balance of the Staker contract go up when you call Stak so we go into the The Bu contract and we click on Staker here so we have our Staker contract we need to pass uh let's get some money uh ethereum unit converters this is don't know if there's one over here okay I can multiply it
from here so this is really cool so this is what I was saying about the about the decimals here I can click multiply by 10 to the^ of 18 and this gives me 1 so we are going to check the first question do we see the balance of the Seer contract go up when you stake it does it does not yes it has become one and mine has decreased so I paid one plus the transaction fee the so it worked the second question is is my balance correctly tracked so we copy our address we put it inside the balances if I click on read I've
actually deposited one e so it seems that our function is working pretty fine and uh the third question is do you see the events in the stake events tuab so if I go on the stake events I can see that there's one stake event which is mine because I've only sted one one time the the E so I can STI it another time and we should see the balance go to Che and the same as before we can put it here I stake two e and there are two stake events so I think that we passed pretty easily the the first checkpoint so simple function that updates um a mapping and emits an event
this is the receive which is needed to of course receive e otherwise this would not work and yeah I will keep it let's move this this here so everything get close so this is the contract so let's go to the second checkpoint which is a little bit more complic yeah it doesn't work deoy another time what do you mean like did you do the yarn chain and yarn deploy yeah yeah
but after I wrote theer five did you add also the receive function like yeah no yeah you have it like this and it's not it's not working what because stake function and receive here need to put this one public as well can you try and redeploy it you can type y deploy in the in the terminal oh you have a type on
the okay nothing to F I'm just wor about the about the version yeah I said that but it worked before expect zero examp yeah this you need to this you need to leave it you cannot remove the and add the the other bracket it's missing one bracket choose when I go into the bucket I'm
just ah no no you need to go on Staker that's the other contract no worries no worries so you can try it whenever you're ready just let me know and we will go to the to the second checkpoint which is little bit more complicated spe
okay let's go to the to the second checkpoint point so the second checkpoint is talking about thinking about our smart contract like a like a state machine so what does it mean is that when we deploy the smart contract there's the stake period so it's the period where the addresses can actually send eth to our smart contract and then in the case of uh reaching the threshold we have a success State otherwise we go into the withdraw state so that's basically the logic of our smart construct and so it it's telling us to set this deadline variable over
here equal to the block. timestamp plus 30 seconds what does that mean so block. timestamp when it's used uh inside a function uh sorry when it's used outside of a function it's equal to the time stamp of the block uh in which this Mar contract was deployed and plus 30 seconds is plus 30 seconds so basically when when we deploy the smart contract the deadline it's equal to 30 seconds after we deployed the smart contract so if we would deploy it like this users would have only 30 seconds to basically stake um their tokens like they they
their e sorry um inside the inside the contract and let's leave it like this for keeping up with the with the with the checkpoint so as it says of course and as hopefully fabrio told you smart contracts cannot uh even if a lot of people may be telling no you can execute things automatically with smart contract sadly that's not the truth uh there's nothing even remotely closer to Automation in uh inside of the whole blockchain logic of course outside there are some systems that you can implement but nothing is automatic so we need to have an execute function we need to add
an execute function that anyone can call so uh it's going to be a public there's not that's not going to be any check and uh this needs to be called just once um after the deadline has expired so only once when the we are over the deadline so let's create the execute function which is going to be execute it's not going to take any parameter and it's going to be public as well so the execute function needs to as specification needs to be called just once and just only after the deadline
has expired so we need to have the deadline the the current block. timestamp needs to be after the deadline so the when we call this function we need to we need to perform this check so only after the the the the deadline we can go on with the logic of the execute function so let's add this deadline not reached and then is is telling us so if the address of the contract is over the threshold by the deadline we want to call the external smart contract so it's really easy to call a smart contract as
you can see it seems complicated but it's not so what we need to do is so if the address of this in so the it's a way to reference the current smart contract do balance is greater or equal than the threshold we need to copy and paste this stuff so this what it does is going to call the complete function which is available on the other smart contract over here is going to call the complete function and is going to send the whole balance basically so it's a simple check we check if we basically received enough e inside our contract and someone is
calling the execute function and we are after the deadline we can call the example external contract instead if this is not the case we need to go back to the to the logic of if the threshold was not met which is written over here below allow everyone to call a withdraw function to withdraw their balance how can we do this the easiest way to to to do this is to have another variable which we're going which is going to be a public variable and a Boolean one and we can call it open to withdraw let me know yeah open for
withdraw sorry it's called open for withdraw so you do not need to specify a default value because the default value for booleans in solidity is false so you could leave it like this there's nothing wrong in typing this but it's a waste of six characters so um so the easiest way is to have this uh other variable which is called open open for withdraw and so then if we did not match the threshold we need to set the open for withraw to true the problem no well I'm going to ask you a
question is this function executable only once by by definition here when the execute function needs to be called only once after the deadline has expired is this function executable only once can I after I call it the first time can I call it again no no the the the answer is yes I can keep calling it because there's no there's no check that I've already called it basically so if we left the implementation of the function
like this what could happen is someone calls the execute for example and we did not we we do not meet the the threshold and so we set the open for withdra through maybe in the future maybe in the next block someone is going to deposit some eth and then we meet the threshold and someone calls again the execute function but then we are in this bracket of of the if statement and we call the complete so that's why we need to have this function to be executed only once and the simple the the simpler way to do this is by leveraging the fact that we are setting the open for withdraw variable to true and we can use
this on our advantage to say you can call this execute function only if uh the open for withdraw was never set basically because in in the case was already set the contact is not open for withdraw there are other Solutions another one that I that is I think even simpler is just to say public executed and then set executed to true and require not executed so like as you can see you can do a lot of a lot of different stuff this allow this is I think it's a little little bit
better because in the case you would go in the case you never put it open for withdraw it would still be callable so I think it's better to use this executed I don't know if they suggest something else but no yeah so right now we're going to keep this executed one so right now what happens is that if has never been executed and uh we are over the the deadline whatever happens here so either we call the external smart contract or we um or we open for withdraw we always mark it as executed and then we cannot execute it
again so we can delete this we can delete this we can redeploy the projects we can redeploy the contract by calling yarn deploy with the latest changes and we should see whoops we should see on the back contracts and the Staker we should see our execute function and this is also the deadline I will try and send a stake so the stake was was received let me check this time
stamp this should be 30 seconds from after we deployed the smart contract so this is just a website two parts basically time stamps in our time zone this time stamp is the 5 22 and 5 for seconds so should be over right now so if I try and call the execute function it says the deadline has not been reached maybe it's maybe it's not completely unlined okay a few seconds ago maybe it's my clock that does not
work black stamp needs to be bigger than the deadline just let me check one second okay yeah yeah because this one's the yeah let's add let's add this time left function so we can see actually what's the time left so the time left function is a view that Returns the time left before the deadline and is something that we can show on the on the front end so let's make this
function time left it's going to be public view returns as it says over here if blog. time stamp is greater or equal than the deadline we're going to return we're going to return zero because there are zero seconds left otherwise we're going to return the difference between the deadline and the block. time stamp this is the the time left function time left function this could also now let's put
it now I'm really curious why this is not ah because this is H because this is block based I get it okay so we staked as you can see the time left now is zero and hopefully that line not reached is that equal the problem I'm really that stupid sorry guys but there's something
that makes no sense okay as you can see now the time leftt is 20 we St once the time left is 11 seconds let me get some more funds so now it's 2 seconds okay so now zero okay so finally it worked it was there equal because so we call the execute function as you can see the executed was set to true and open for withdraw was false because we reached
the threshold of over 1 E because we deposited uh more than one than one e if we go on the example EXT Al contract which is the one that receives the money as you can see it has Che of um of balance and completed of course is set to true so we can also have we can also take a look at the ster UI if we added everything so as you can see they show you this taking up trigger the example contract blah blah blah and um this is the UI and let's see what
it's telling us tells nothing okay we already did this which is the receive function ah okay let's see let's see the goal sorry so we saw the time left counting down whenever we trigger a transaction using the faet button that's what we did and it worked so if enough if is Tak by by the deadline does your execute function correctly called complete and stake dat yes the last point is if the threshold isn't met by the deadline are you are you able to withdraw your funds so we're still missing the withdraw Point like the the withdraw the withdraw function and um the withdraw
function as the name says allows us to withdraw the withdraw how much we deposited basically and so of course in order to withdraw it needs to be open for withdraw for sure which is going to be set via the execut and then it's going to whatever this is a check that we can also yeah let's put this check as well this checks this check allows the withdraw function to only be called by someone who has actively St some um some tokens some e sorry and
[Music] um and then we need to basically send the eth to the to the user like this so we get the address payable of the recipient which in this case is the user that is trying to withdraw its funds we get the we read the uh amount value from the um from the storage from the mapping we set the um the value to zero before sending any money because otherwise this as we
already were saying before there's the entran attack and you could call this function basically infinitely you always need to remember to set this to zero and then we get back our money really easily so the the last step that we need to test is basically we are going to deploy the smart contract uh we are going to trigger multiple blocks using the the faet button and then we are going to um set the we're going to call the execute function to set the open for withdraw to true and then we're going to call the withdraw function and uh in this case get back uh actually we're going to
stake less than one e so that we can withdraw back yeah yeah if I write recipient in of message the next line yeah this is just because it's cast to yeah yeah yeah would not change no no no it's just it's it's it's still an address yeah so we need to since we added this check which I think it's useful which is to require the someone who calls the withdraw to act to actually have um staked any any eth we are going to stake
less than one eth so that we do not need to uh so so that we can actually call this withdraw function so we are going to call the deploy by going back to the I don't know what is this but let's go to the debug contract so you can see I'm going to stake 0.1 e remember to multiply it so the balance is less than the threshold the threshold is 1 e and we send 0.1 so we are still waiting for the time lapse so we need to trigger uh you need to click on the on
the refresh strange yeah are you clicking on theet button you need to trigger a transaction if you trigger a transaction and then you click over here it's going to refresh so right now the time left is zero the balance of the smart contact is 0.1 so is less than the threshold I'll try to call the execute and as you can see it has been executed and it's open for withdraw we can also check if we try to call execute again it tells us that it's already been executed and then if we try to call the uh withdraw function we should see the balance of the um of the smart contract go to zero
and our balance got back to uh 11 almost 11 so we also checked up the last point of this checkpoint over here this as as I said before we already done in the in the start so basically the cool thing that we did using this receive over here is that if you try and send directly eth to this contract it's you are actually calling the stake function fun what does that mean let's it's easier to show it in action since there's no check on the on
the stake function on when it's called we can call it pretty much everywhere so if I go over here and no uh how can I is wasn't there a way to to send tokens from here I don't remember how to send tokens ah okay so we if we go over here on the bottom we click on foret we set the address of the of the user of the of the contract sorry of the Saker contract if you put
0.1 e and we it send as you can see the balance has changed but what it's really cool is that if we go on the stake events I it um it looks like the the address of the faet actually staked some money because in this case we send them the the tokens using the using the faucet but basically the faucet has sent 0.1 e to the uh to the to the smart contract this has triggered the receive function and the receive function is called the stake
function and so it looks like the faet contract as Stak basically so if you go on theb contracts and you put the address of the faet you can see that he has St 0.1 e so these matches uh so let's see this side quest which are this question is something that I've already answered before so can execute get called more than more than once and is that okay as I said before no because it doesn't make sense with the with the logic that the smart contract needs to have then we have this question can you stake and withdraw freely after the deadline is that okay do you think that is okay to stake after the
deadline I would say no so we need to CH we need to change out the out the stake function work and add the the the the check that the kurrent block. timestamp needs to be before the deadline because otherwise we will not be able to for example if the logic of execution since the execute function has already been called if someone was to stake their e in the contract after uh the execute function has been called uh that would get a real mess because the funds basically would get stuck in
the contract even better we can instead of using the like we can use the block. timestamp but since we are using the executed function we could also um add the check that we never basically called the execute function so even if we stay after the deadline we can still basically um get the money back but let's keep it easier with the with the blog. time Stam so we can only stake before the deadline so this makes sense so then we have what are other implications of anyone being able to
withdraw for someone does the withdraw function need to be no oh I lost it so what are other implications of anyone being able to withdraw for someone so in the way that we that we develop this function actually you cannot withdraw for someone so only the message sender could withdraw for themselves we could Implement a logic for allowing people to withdraw for someone else and basically pay for the gas but I don't think that there's any kind
of um any any kind of implication so here is saying what I was saying before of make sure the funds can get trapped on the on the contract and so that's why we added the basically the stake function like this check on the on the stake function so that people cannot be uh cannot stake after the deadline the execute function can only be executed one and the withdraw function only withdraws the balances of the given user have you seen modifiers with fabrio uh yeah okay so now another thing
that that we can add is to basically create a not completed modifier and this modifier is going to check that the example external contract is not completed and we we going to use it to protect our execute and withdraw function so we this is really really easy we just need to create this new modifier not completed which is actually just going to call the let's make it more readable let's say is completed we are going to call the example contract completed require required to be not completed and so the cool thing about modifier is that
about modifiers is that you can as well call other smart contracts and read data and uh of course you could do it also also like this but this is actually more readable and so we need to use this to protect as they were saying the the execute and the withdraw function so right now we have basically secured our smart contract from pretty much everything so now this wants to deploy the smart contract and so on so the last step is instead I wanted to show you how they are interacting with the with the
smart contract and how they are reading events because because this is something that we didn't see yesterday but we are going to see today so if we go instead we're going to to close the other that stuff so the smart contact is done finishes it it's finished it works what we need to see instead is the the the the the Staker UI page the Staker UI page does this huge component but basically what it does is uses the hooks that we were that we were uh seeing yesterday
so for example the use scaffold read contract to get the threshold parameter the time left value the balances by passing the address of the user and the eing completed to check for the completed um for the completed variable on the example external contract so basically all the this page is basically built only using the scaffold D Hooks and [Music] um let's see how they use these Hooks and how they use them to basically build this type of page so this box over here of course is it's
um it's a conditional box and it's a box that appears only if the staking is completed so this is how you basically add um a condition inside the HTML when you return from um um react component so what it does it's it's retrieving the completed uh property from the example external contract is mapping it it's retrieving data and it's calling it is stay completed this is short end in in typescript to basically rename a variable when you use D structuring this is equal to saying
basically uh cons data not properly equal but it's like saying this but it's without creating a copy it's just changing the name so it's it's really useful so it's using the eing completed to basically add conditionality to the to the to the page it shows this really ugly message and and then it uses the if to price which is this really cool component that you can use in um in your in your app as well that basically changes from E to uh$ doll the same thing goes of course for
all the other various parameters over here for the time left is it's reading it directly from the contact it's basically just three lines and what it's really cool about scuffled this and their hooks is that well we saw uh well I showed you yesterday think a little bit of the of the wagi ones the scaffold D they extend the wagi ones and they actually allow you to pass the watch property as well the watch property is really cool because it basically whenever there's a change on that given variable on the smart contract is going to change also on your code so this is really nice to basically perform some uh really uh cool interactive pages and
it's also nice to show in a in a demo as well that your your front end actually reacts to the uh changes that happen in the in the in the smart contract so the this pag is all this basically data is red as I said and then we have the the the functions these are the three functions that we just developed so the execute the withdraw and the and the stake I'm going to read the deploy the the smart contract and I'm going to make a little [Applause] [Music] change I'm going to change these [Applause]
two let's change it to 2 minutes so that we have a little bit of [Music] time me close this if we refresh so you can see we have 1 minute and 59 seconds left in this case we St zero e the total state is zero over the one threshold if I call the state 0.5 e the total State directly like automatically changes I don't need to do anything if I click stake again it changes so this is the like the power of the of the watch functionality
like the the watch property basically since the example external contract does not get redeployed because it's always the same because we are not changing it this message is always available here but let's ignore it for now I know instead let's let's do a y deploy Das Dash reset so we do not have that message so it makes more sense sorry so as you can see right now there's no more the the banner because I I've used the dash dash reset so basically I've emptied all the state of the blockchain and recreated from zero so if I CLI on stake I just want to reach the total
amount so we have 1 E state so we reach the threshold how are they basically calling the smart contract they are using the the SC the use scaffold right contract hook that gets as a parameter the name of the smart contract that you want to interact with and this is going to give you this right contract as sync fun this right contract as sync uh function this is the function that we need to call um by passing various parameters to interact with the Staker smart contract if we search in the in the file you would see that our functions right now
are pretty easy because we do not pass basically uh any parameter except for the stake one but as you can see if we have uh solidity functions without arguments you just um call the write contract as in function you just pass the name of the function that you want to execute on this SM contract and you are interacting with the blockchain so it's really easy and uh this is actually a dream for someone that like me that used to do all this stuff by hand basically right now you can do an interaction with a smart contract by using one simple line of code and the same goes with the with the withdraw
function instead the the stake 0.5 ether button which is this one calls the stake function and passes as a value 0.5 e of course you need to if you remember what we did yesterday with react and with use state if you add for example here an input field and you store how much uh eth the user wants to wants to send and we will do it right now we can also um we can also make this take 0.5 ether um I I forget in the word in English Dynamic
so there should be a really nice already made component from scaffold it which is called ether input ether input in this case The Ether input wants a value a name a placeholder an onchange and so on and so forth so the first step that we need to do is to manage somewhere the the state of uh how much stake the user wants to wants to put so we're going to call a stake amount as we did yes they use State we're going to call it string and 0.5 in
this case we're going to set the value of The Ether input we're going to link it to the to the stake amount we are going to get uh we're going to set the unchange uh function of the ether input this is the new value that it's coming from the input and we are going to update the stake amount and yeah see whatever you want and we can change this part sether over here with the with the stake amount variable and we can change also this value over here so if we go back to the to the page as you can see right now we
have this input if I type 0.15 it changes also the button over here I can put I don't know 151 I can put put one e and as you can see it's it's Dynamic of course I can also put zero but this is just to Showcase basically so since this is dynamic now the user can input how much he wants in this case I want to St 1.5 e if I click on stake in this case we already reach the deadline because I was too slow but let's redeploy real fast the the contract so we have a fresh one I hate you when you do this if I click
on stake as you can see I'm staking 1.5 eth every time I can change this to 1.25 and so on so basically we linked we are using the uh this ether input field and dynamically changing how much uh stake basically the user is is sending there are some other really cool components that are basically scattered around all the all the scaffold this there's also a should be their
documentation about the components over here so what we just used was this ether input which I don't even think it's it's the correct one because it's the one that switches for uh from E to USD but maybe this one was the was the one to to use so the integer input which is the one that allows us to switch between uh e um between either and way basically and back side also there's the input address which is the one that um it's using we were using before in the in the challenge zero we we saw it in the in
the challenge zero but we also should have it here as well if I type for example as you can see I typed my ens which is my this is basically my ethereum username and what it's really cool is that it already is mapping it to my real address so this is my I can assure you that this one is my address and um and this is also really really really really cool like if you use these components you will impress in my opinion because they are really simple but at the same time they are they they are really powerful so the ones that I would suggest you using are the address input
the integer input and mainly the and also the the balance which was the really cool one that switches between eth and and dollars and um and yeah the ah yeah yeah no no design me personally as a judge or as a well it's not the the design that matters the most like the what matters the most if if it works of course like something that it's pretty and it doesn't work it doesn't matter that it's pretty in my opinion but of course
the Leon should have showed you before I was outside doing doing a call but the we have as judges when we when we will um give votes to your to your projects we will have all different categories and of course um design is one of them but is the it should be and it is the category with the lowest maximum amount of points that you can score in So at the end of the day it's not that important but of course if two projects score the same points everywhere and one is better and more usable than the other of course
it's going to win but you need to match a lot of different uh a lot of different parameters I I will show you later because uh there's the last thing that I wanted to show you which is something really nice as well is the is the events and I'm going to go real fast on on this one it should be stakings right yeah stakings the stakings basically to read events from a smart contract from a smart contract using scaffold it you have two choices the first one is to use the uh you scaffold event history and uh the use scaffold event history as the name says basically
retrieves all the events from the given block that you pass in this case it's zero now this fromont block zero works because we are working on um a local chain do not try to uh set a value like set from block to zero when you will be at diaton or when you will be on a test net because of course this would mean to start reading a lot of blocks and so the most likely the the the page will crash or either the the API call will crash and so on so the best practice instead is to put the time stamp of the
contract deployment over here so when you deploy the smart contract you you will be able to get the blog. timestamp and you just smash it over there and so you will only read the events from the from when the smart contract was deployed and um the other way instead um is using the the watch contract event instead which takes the same parameters of as the as the other one except from the from the front block and instead
it gets this really nice uh function which is called on logs yeah yeah whatever you want don't know why it's this is linter okay and gets this un logs function that basically uh this this hook over here is going to trigger every time an event anytime the stake event is being emitted by the Staker contract so this is useful for doing some real time Real Time stuff real time stuff in your application maybe I don't know uh you are creating this
um you are creating let's get back to the nft marketplace stuff we are creating this nft Marketplace and whenever a user purchases an nft you want to update the page you want to uh update the user profile maybe so what do you do is you you use the you scaffold watch contract event so in real time you can catch that log and update for example the user profile or um I don't know change the amount of item he owns in his inventory I don't know inside of a game whatever you want to do so this is used for realtime stuff while instead the event history it's just the past history so this one does not react to
new events while this one is the one that um actively it's always active and always reads for events and to answer your question before this is something that I would like to show because we are one of the few acons that does this and here you you have these are all the the various parameters how much like what are the questions that I and the other judges will be answering and how much point is going to score and like the ethr Magadan as uh lion said before
we are developers and so it's just a matter of numbers so what comes out from this is who's going to win so there's nothing more nothing less of a of basically a score and it's public you will you have it under under judging this is the judging criteria to total amount is 100 and uh it's really hard to get 100 I think the the guy over there Matias was the highest scorer last year with 74 I think so it's pretty hard to score 100 but yeah as you can see we have the we actually we do not have anything related to design at all so you don't need to worry about the
design you uh I mean it's always hard to IGN when so the the code quality like code quality in the sense of how well is the code written no how beautiful how beautiful it is yeah yeah yeah yeah no uh no sadly not but just for uh timing reasons I would say yeah yeah what we what we check instead is after the agodon what we do is that we look into the code repos and we check for for example if the code uh
if you started developing weeks before the agodon or if you stole code from somewhere maybe from other acon projects and stuff like that you can see there's this note we have we have a 7day period uh to actually change the results of the of the trucks whatever the sponsor do it's just up to them we don't care about the sponsors and the bounties but for us we really take uh great care of the of the truck so we always check for irregularities and stuff and so we check if someone has cheated basically so um even do well there are some ways and some
stuff so for example something that I do not like that I see in aitao is if magically when I don't know there are three hours missing to the to the agaton there's no comment and then P you'll see all the application coming that is some that's a red flag in my opinion because I don't know how you built it when you built it maybe it's something that you've been building for the last days the best way and it should be written somewhere over here because we explained pretty much everything but uh should be one yeah yeah we we we you need to commit like you need to commit by commit you need to prove that you are act you
are actively working on it and that you worked on it on the agaton we allow external like uh projects that started outside of the aaton but um the only thing that it will be judged the only thing that it will be submiss submitted and uh it's going to be what has been built to the aaton so if you're building the I don't know the newest electrical machine I don't care if you did nothing in the aaton I don't care about the idea at the start what's important is what you do at the aaton and also even if we allow them nobody has ever uh come to eam with their already existing idea because uh it's
also hard to um to prove that you did not work on on uh on that kind of stuff before and so since we are really strict um nobody has ever done it not no no build yeah but there are a lot of there are a lot of a lot of aons that allow it but we are kind of against like we are not 100% against it the problem is that it becomes hard for us judges to understand what you actually built during the agodon and if for me it becomes an headache to understand what
you built so it shouldn't be my problem as a judge you need you need to be clear and if you are able to come to the aaton with your already started idea you are able to prove me in any way how like what you build and so on I'm 100% happy to do it for example what I what I do what me and lion do at at taton when we want to work on on new features for some projects that we develop what we do is actually we create a complete new project so under a new name something which is we do it from scratch and it has only the new functionalities so then when the agodon is finished the judges
have confirmed we get the code and we put it back on the original so because we want to keep it fair so that's what we suggest every time so if you want to work on new functionalities just it's better for you to submit a project on new fun functionalities rather than uh pushing like presenting yourself with a repo that's two years old so so yeah that was pretty much it I'm sorry that it was really hard for me to to talk to to basically to start from from JavaScript basically and to come to until scaffold it in just two days this is something that usually takes a little bit more more time but uh if you have any question you will find me as well at
the at the aaton I will be there the whole day of Saturday and uh Sunday as well and I will be at the checkin tomorrow as well but of course during the checkin I will be giving you tickets so you can ask me anything again at the Aon if you have any questions and so you can bother me you need to ask the the cool the cool hair guy uh 2 p.m. like check in from 2 p.m. but actually there's a side event happening tomorrow morning it's in the same same venue separate room so we will not be checking in for the Aron there
but be already so the will be launch want launch there and then after we start the thank you so much guys [Applause] [Music]
Related talks
- Web3 Frontend Development, Part 1
- Desarrolla tu Primera App Web3 Full Stack | Curso Cyfrin Updraft en Español
- Crea Tu Primera Cripto Moneda ERC20 Desde Cero | Cyfrin Updraft
- Scaffold ETH 2 - Extensions Development Guide
- Ethereum Dev Onboarding Session
- Building on Ethereum in the AI Era
- Solidity 101 in Turkish: Updraft Study Session, Part 1
- Ethereum Foundation: Web2 dev to Web3 dev with 🏗scaffold-eth with Austin Griffith
- Solidity 101 in Hindi: Updraft Study Session, Part 1
