New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Intro to ERC-20 tokens - 0xPoland S01E02

ETH WarsawThu, Oct 7, 2021, 12:00 AM

👨‍🏫 Piotr Szlachciak, VP Of Engineering at Ethworks, will give an intro to ERC-20 tokens: "Tokens have taken Ethereum by storm. They democratized creating cryptocurrencies to such an extent that if you wanna trade a pair of tokens on Uniswap a new token is created specifically for that. But what are those tokens? See you at the meetup where I'll explain everything." 0xPoland is an initiative to build an active community of blockchain developers. If you want to improve your coding skills, make sure to join our monthly meetups, workshops and a hackathon in May 2020! ➡️ Sign up for our events here: https://www.meetup.com/0xpoland ➡️ Follow us on Twitter: https://twitter.com/0xpoland

Transcript

so this is the second episode of xerox poland we're gonna have a wonderful evening today learning ethereum and uh we're gonna give it just one minute more for everyone to gather and we're gonna start and in the meantime i'm gonna tell you a little bit about what's coming so what's coming uh well first of all uh today is the second meet up january the 12th today no surprise here and just uh in the following weekend we're gonna have a workshop so for everyone who would like to learn more about coding ethereum building smart contracts they are invited but keep in mind there are only 10 places so please subscribe on our meetup website and when you do uh please make sure that you will fill in the form there is only 10 places there is only 10 people who can attend so make sure um that you're going to do all the formalities to get in and starting from february we're going to have a meet up once every month and as i was already saying last time we're going to have more even more interesting events coming starting in march we're gonna have uh workshops for students who would be interesting to join in youtube in space and it's gonna be a series of events probably six or eight around one hour one hour and a half each so there's gonna be an opportunity to learn really really a lot and finally uh for our season five finale we're gonna have a weekend workshop just a week before a hackathon so there's gonna be a hackathon two days of hacking there's gonna be awards and there's gonna be tons of time to learn and hack and do wonderful things [Music] and uh for today we're gonna have two presentations first coming from people excellent and uh yeah i see the slide is not fully updated so don't look at the titles um gonna talk about your c 20 erc20 tokens so we're gonna learn more about uh that's gonna be a beginner presentation so we're gonna learn all about basic of tokens uh and why it's so important and how it works under hood and for a second presentation i'm going to do it myself and it's going to be about it's going to be more advanced this time it's going to be ethereum under the hood about arteries and data structures and why it's important to understand them to fully comprehend the whole ethereum and blockchain and smart contract space so uh i would like to make a shout out to our media partners to crypto dev dot tv and resumes bitcoin who who are supporting us in our mission to get as many blockchain and especially ethereum developers um to the space get as many new people uh building the future of economy together and we are at works and together with the maker we are organizing that meetup so without further ado if you don't follow us yet then please follow us on twitter on xerox poland you can check out uh all the materials from our presentations are available and zero exponent of death so please feel to um feel free to go there and check that out and um yeah for now uh without further ado i'm giving away mike to uh to piyot to talk about percy 12 ufc 20 tokens are you ready yes i am can you hear me well marek loud and clear okay okay fantastic so hello everyone uh thanks again mark for uh the warm welcome uh today we'll be taking a look at how erc20 tokens work on ethereum and this is a beginner friendly talk so if you are a beginner you can feel right at home because we're going to cover the erc20 from the basics explaining why and how it works and i have a little bonus at the end for the curious so let's start with a simple question what is money anyway and as the richest man in the world said money is an entry in a database and this is how we gonna think about money in this talk so okay here it is a database and some entries so we've got money right not exactly not exactly it turns out that you need uh one more thing for it to uh function properly as money right because we can have this nice database table we can know that alice has some money bob has some money charlie has some money but this is kind of static nothing changes in this table so we need to introduce a way to make changes and this is going to be achieved using some way to transfer money so here we have a way for uh to transfer money and in this example alice is transferring money to bob and she's transferring one dollar and you can see that this is the balance deducted from her account and also the same dollar is given to bob now a few things uh that it's nice to notice here first of all even though this dollar is transferred from alice to bob there is no way to really distinguish the dollars between each other right you can see that uh of course bob's balance updates but after the update there is no way to know which one of those uh 200 was the one uh given to bob from alice and similarly it's not really possible to know which one of those 105 was deducted from alice's balance so this is what we call fungibility so no way to tell the different units of money apart okay so so we've got the basics with basic properties first we need to know uh which uh person has what balance second we need to be able to transfer the money and third the money has to be indistinguishable from each other because otherwise we run into problems okay so unsurprisingly you'll find that blockchains like ethereum have this as their transaction format you have a from and this is analogous to of course to the transfer function you have a from which is an address so blockchains like ethereum don't use uh names or identities or whatever for for knowing the senders and recipients basically everybody on the blockchain has a pair of cryptographic keys and this is a fancy way of saying that they know a secret number that they don't tell anyone and there is another number which is their address which is public and this identifies them on the blockchain so from is some address two is some address right uh we've got value so uh how much uh is transferred and then data and this is very interesting but we'll talk about it later okay so here is an example of a blockchain transaction you can see that from is some address and this address we know uh belongs to alice and we know it because it's an example and the second address belongs to bob the value that's transferred is one ether basically this works just like this example but with cryptocurrency on the blockchain right now the curious among you have probably noticed the field called data right because the original design of money didn't specify anything like data and here is uh where ethereum innovates over what blockchains did previously basically the data field is a way to talk to smart contracts okay fancy word here is a smart contract uh it's a relatively simple basically this smart contract has this field called value and this function called increment it works sort of like classes okay where if you call the increment function the value gets updated so you can imagine this transaction right the previous transaction was from alice to bob and it sent some money this transaction is from alice but the recipient is the counter contract okay so the money transfers is actually zero in this case but the data is calling the increment function this one and this changes what is actually stored on the blockchain so it turns out that this little script is actually a database sort of right you've got the data and you've got a way to modify it it sort of like a database no sql included but hey we got to live with what we have but wait a second so if a smart contract is basically a database this means that we can get a smart contract to represent money right so okay okay let's uh look at this this is a way to do money but as a smart contract a program running on the blockchain so here we have a contract a token and instead of just having one single value stored inside it has a mapping and a mapping you can think about a mapping as a database table that has a key and some value right and then you can query the value by the key so the key in this case is an address is uh who owns the coins and then the value is how many coins do they own so this is a mapping from an address to an unsigned integer an unsigned integer is a fancy way of saying that it's not less than zero so every address can have a balance and this balance will either be zero or something positive but as we discussed previously just knowing this mapping is not enough we need to have a way of interacting with it to actually do something useful so we have a function of transfer and similarly to what we had in the example with the uh abstract database here the function transfer has three parameters from two and value and it updates the balances right so the balance of the sender which is the from right is deducted the the value is deducted from this balance and then the recipient's balance is increased by the values so basically the same thing that happened here right alice's balance gets decreased and bob's balance gets increased by the value but now it's a programmable smart contract on the blockchain lots of fancy words you can see that inside it's simple code now i've got a note here that says no restrictions and thanks node for reminding me that this code is really really insecure because it allows anyone to call a transfer and say that someone transferred money to someone else but then i will be able to call this function right and say that actually you know what marek right has transferred all of his money to me and now i have all of that money without marek knowing about it or saying that he wanted to do this so this is of obviously insecure and we can add a slight modification so this is token v2 right v2 because we have significantly improved it and you can see that uh the from field has been removed from the transfer okay and the account the address that the value is being deducted from is actually the sender of the transaction so this is something specific to ethereum but when you send a transaction like this right when you call a smart contract with some data calling some function the from field from this transaction right and this transaction is signed cryptographically so this means that only alice can send a transaction from alice this from value is then exposed to the smart contract as message sender so this is how we ensure that only the appropriate person only the person that called the transfer can transfer the money out so if i were to call the transfer function saying that i want to transfer money to me well i'll be transferring money from my own account not marx and if marek actually wants to transfer money to me then he himself needs to call the transfer function but once again the note is kind enough to remind me that actually i'm missing something here and what i'm missing well i'm missing a check that i have enough money so let's see how this can be achieved token v3 oh wow we have achieved peak agile version 3 in under like 10 minutes but basically now it's more secure because we've added this one additional line the require is a keyword in solidity which is the programming language that we were using all along which basically uh cancels the execution of the transaction makes it like never happened if the condition doesn't hold right and what was what is the condition here well in order to spend from my account in order to send someone some value i first need to have enough value in my balance so we check that balance of message sender is greater or equal to value now this is all great this is our own nice implementation of a token on ethereum but if everyone were to roll their own token we would have problems so this is where erc 20 comes into place instead of everybody creating their own custom weird token implementations that have different function names different field names different behavior we have a standard right erc stands for ethereum request for comments 20 is just the number 20 because there are many different ercs and these ercs define standards that people in the ethereum ecosystem can use when developing their own applications and drc20 is a standard that defines how a token should look and behave so that someone willing to interact with that token doesn't have to learn and implement everything all over again if they have something that works with one erc20 it will work with a different erc20 because they are compatible so here is what the standard actually entails we have a name so each token has a name right i have this example here one of the known well-known tokens in the space is tether and the name for tether is tether usd now we have also a symbol and the symbol is usdt we also have decimals and this gets tricky right the decimal value for tether is six what does that mean that means that uh one tether one usdt is actually represented as a million units okay so in the smart contract where you have the balances stored then if someone has just one usdt the balance will actually say a million so this is a funny because i can be a millionaire just by owning a single dollar the other thing that uh we have uh from this kind of descriptive part of the standard we have the total supply so each token has to actually declare how many tokens in existence there are okay straightforward each token declares that now we have a section of the standard that talks about what values can be read uh from the smart contract so we can read the balance of someone someone has an address and we can query their balance so if i had one tether calling the balance of function will return that i indeed have this one tether and of course because we have the decimal set to six the function will return a million okay we also have allowance and let's skip this part for just a second because the next slide is going to deal with this and we need to talk about the rest we have the transfer function the transfer function is also standardized it takes the input address sorry it takes the destination address and the value and it uses the message sender as the input address and you can see that this function returns a boolean so the standard defines that if an operation succeeds this function has to return true and if the operation doesn't succeed the function has to return false or revert okay so we also have transfer from and approve and this is connected to allowance with which we'll get to in just a minute and we have some events which also get their own slides so let's move on to approval and the picture in the background isn't a coincidence approval is basically the mechanism that happens every time you do online shopping so imagine i wanted to buy something on amazon right i go to amazon i type in a nice book okay i browse the books i click to purchase the book and what happens well amazon calls my bank and says hey we need the money from the guy and the bank sends the money to amazon well the same thing can happen with smart contracts that represents tokens basically i can approve and exchange a shop or any other smart contract to be able to take balance from my account instead of me having to send the money to them they can send money from me to themselves if i have approved it previously how this looks well here is a simple example let's say that alice wants to trade some of hair uh tether for some of her uh e for for new eath right so she will pay tether and get it well what she does is she calls the tether smart contract with the approve function right and this is part of the standard the approve function and what she does is she specifies that she approves the exchange to be able to spend as much as a thousand usdt from her account later the alice can trade on the exchange and she can say hey exchange i want to trade 200 usdt 4.2 eth and the exchange will then take the money from alice's account using another function defined in the standard which is transfer from okay so the exchange cost transfer from specifies that it wants to take money from alice's account the money should go to exchanges account and uh the money is equal to 200 usdt and because the allowance at the beginning was a thousand usdt after this operation the allowance is only 800 usdt now this is all great and you can also use uh the allowance function which is the other function that allows you to read data alongside balance off so you can use the allowance to see how much allowance a spender has for a given owner so in this case we could query how much money has alice approved the exchange to spend however the approve function is actually a bit tricky to use because okay consider this alice wants to approve bob to spend a thousand dollars later on alice wants to approve bob to spend a different amount because she changed her mind either increasing or decreasing let's say that alice actually wanted bob to be able to spend more so she decides to approve bob for not a thousand but a thousand and two hundred usdt then bob calls transfer from to get the money to himself he gets 500 usdt and his allowance is decreased meaning that it's now 700. however if the order of events were slightly different right and this might happen because the transactions can be included in a different order on the blockchain well first alice approves bob then bob takes the money out then alice approves bob for the higher amount but because the approve function sets the value then bob's allowance is actually now a hundred and sorry uh 1200 instead of a thousand even though he spent the money and this is why i included the scream cat emoji because this is a very undesirable situation so this is why the erc20 standard has been extended by the two special functions increase and decrease allowance which when called do not set the allowance to an absolute value but rather increase or decrease it by a specific amount and in this case if the second transaction was not approved but rather increase allowance by 200 then the transaction ordering wouldn't matter so this is why those two functions are needed and now after we've discussed allowance it's time for events everybody loves events especially xerox poland which you're attending right now and the events on the blockchain are special because events on the blockchain are a way for the um transaction to signal to anybody watching the blockchain what happened inside and basically events uh are something that's emitted during execution and then included in a transaction receipt so it's like what is a transaction receipt basically whenever a transaction gets mined on the blockchain and this is also fancy lingo for saying that the blockchain now just includes this transaction uh this transaction has a receipt so you can know which block that transaction got included into the hash of the transaction whether the transaction succeeded or failed and you also get the events so if i am a client uh an application right that reads the blockchain in order to show whether or not some money has been transferred then i can rely on those events to see whether or not the events were emitted and how much money was actually transferred and this is great because it improves ux and also enables us to see what's going on behind the hood okay i have a bonus for you right so you've been all through the erc20 standard now you know everything about the how the erc20 tokens work now let's spice things up with erc 721 right if your head isn't spinning i don't know what to do to make it spinning because this is a radical change in how the tokens are constructed as i said before tokens that represent money and need to be fungible right so the um the units are indistinguishable from each other well erc 721 tokens are distinguishable from each other each of the cats that you can see in the picture is actually an erc 721 token and each one is unique so you can tell them apart from each other how is this achieved in the code and this is the best part this is the cherry on top at its core an erc20 is a mapping from an address to a uint right an unsigned integer and in this way each owner knows their tokens but what an erc 721 is at the core is a mapping from uint to the address so that each token knows their owner and this enables the tokens to be non-fungible non-fungible meaning that the units of the token can be distinguished from each other very fun stuff i encourage you to read more about each of the topics explore it a little bit see what tokens exist in the ecosystem and most of all keep learning about ethereum my name has been published blackjack uh you can hit me up on twitter at p where i occasionally post some nice stuff thank you very much uh i'm giving it back now to marek and yeah thank you very much once again hello everyone back i think we have one question to build the presentation let's try to answer that um opening right now oh yeah here it comes from um at the bottom of the screen as a smart contract perma programmer you are responsible for writing matter like balance transfer or approve or most methods and classes are at the absolute level in solidity by default now thank you yaku for this excellent question and no and no actually is the answer so when you're programming smart contracts you don't re-implement all of those methods unless you're doing something very custom but also none of those things are actually built into solidity what you want to do is you want to install a library containing battle tested audited smart contracts for example open zeppelins smite contracts and then use them to create your own tokens and opens up a lens library includes a reference implementation for erc20 with all of the custom stuff that you might potentially want which is great because you're not reinventing the wheel and also your code is then secure at the end of the day

Automatic transcript — names and jargon may be misspelled.