Alexander Praetorius, Nina Breznik| ABC of writing Ethereum smart contracts | CrossLink Taipei 2019
Taipei Ethereum Meetup·Fri, Oct 9, 2020, 12:00 AM
Event web site: https://crosslink.taipei ===== 加入 Meetup 以得知下次聚會時間地點: http://www.meetup.com/Taipei-Ethereum-Meetup/ slack 討論:https://ethertw.slack.com 。請先從網站取得邀請信: http://bit.ly/252hNY0 通關密語:decentralized 臉書社團: https://www.facebook.com/groups/443751072484739 歷次講座投影片、程式碼整理: https://github.com/EtherTW/Taipei-Ethereum-Wiki/wiki
Transcript
hello if you are also ready then maybe we can slowly start continued of an openness so my name is Nina and this is Alex and they were going to have a chance to our workshop about what to write civility smart contracts yes so as I said already Nina and Alex but if you want to follow us or message us ever get some questions on the Twitter Keter telegraph this is also our background a little bit in we joined the ecosystem approximately 30 years ago as contractors with the ethereal foundation at the beginning we joined to help build the advent urn for solidity called remix anybody already knows it but because we are also teaching programming and solidity in general we figured out that we need more easy version of solidity editor so we started building another project called clay which is playful editor suitable for beginners so later we will try it out but other than the last month smart called a peer-to-peer search engine for solidity source to go so later or any possibility you maybe want to search for some code examples and when that search engine in the future now it's alpha but you will be able to find some of course you will so if you can copy/paste and combine we're going to start we leave your own contracts and one more additional thing for two years already the research chemicals and it's a meter that is happening every two weeks and it's focused on all learners so everybody who wants to learn programming complete beginner you can come and join this community and Nora is she's from technologies organizing events so after this you can also join this community we're learning so today's workshop we repeat it approximately three parts so because we could immediately start with salinity syntax but maybe it's better to have like some general understanding how things of robbery really work so when we write the smart contract how it is even fit into the whole ecosystem so we will go through this form of basic concepts so that we kind of go through the main so there is like a vocabulary so what is it cereal purchase she what is proc check in general also so these kind of things then we will go through 3dt syntax so basic examples and more complex examples and then we will try out this solidity code into the in the editor so we will also interact with these contracts because contracts then we can deploy them to the etherium and then we can send transactions and retrieve transactions and this is what we will do in the last part so we want this to be like a relaxed things so if you have any questions in between please ask because it's so useful that we don't if somebody's stuck we also have a chat so what you will need for the workshop is the play editor meta mask we will later go through everything so close no worries and the heater chat for questions so guitar chat we have it still it's too reserved because this is the the heater chat so this is always not just any always if you have any questions this is the chat we're also laura is and the wizards of us here exactly so you can always ask for some questions here okay so I think this is an introduction and now we can jump into the basic concepts is it ok like English slower everything good yeah so the basic thing big difference most of you might know this already and some don't like main differences like we do space over any tradition that you sign up with email password and that's not the case here so what you instead use is a wallet because we today do new things in the browser we're using quality box and what it does it basically uses some math to generate two pieces of data so there are like programs that can generate you this so this is like just an example of a form where the math that generates those two pieces of data already made in and when we generated key it basically gives you here one private key and one public key and basically how it works is you publish one of those basically there which is the public key it's just a combination of zeros and ones you give that to everyone in the world you publish it in Internet and it's basically the theory of context also your address the way people can talk to you send you data interact with you otherwise on Facebook you sign up with your account you know how other people can talk to you using that Facebook account but here instead you have these zeros and ones which are your public key everybody knows it because we publish it and that's your account now the thing is with that math that generates those two pieces of data you can use them like a text and you use your first piece of data and you use a little algorithm like basically like a little formula to convert that data with together with that other piece of data in something nobody can read and then you can use then everybody in the world can basically use the second piece of paper that you made public on it to get back from that unreadable piece of text the real text and because you are the only person that is supposed to have the private key that means everybody who can successfully turn that gibberish into because you're in the same way so kind of in this so basically so basically every of is knows that build this giant Network has inside a growing list of data and to date certain rights like all these nodes this supervised the same data and that is what is the blockchain heart and in each of those blocks you have to the stores a list of transactions and that is basically an interaction between so-called contract accounts and user accounts so you created your user because keys the public is your address and the blockchain will store in the blockchain in every moment transactions that are interactions between contract accounts and your user accounts so how does the local chain link loss so there's another they just mentioned public and private key now you if you turn like if you're any text and you use a hashing algorithm and you it turns it into some small piece of data and it's always the same length no matter how big is the data you put in and it's for the same piece of data it's always the same like a fingerprint you cannot create from the fingerprint the original piece of data the same is for my fingerprint you cannot read me but if I'm wrong I can always give the same fingerprint so if you have like a text as it would you get like a hash that's like a small piece of data if you take a different text you get a different piece of data now this sentence says the red fox run for the eyes and this one says the red fox walks it's a small difference but the fingerprint is different but you take the same text twice you do the same movement now how's that used um basically in every block you have transact keys but you also have a hash to the previous floor so you take one block that has transactions and it has a hash and the next vlog will point to the previous match so that way you can prove if somebody sends you a previous block you can use the hashing algorithm like this one and figure out what is the fingerprint of that block and if you do that you can compare to the second block if it says that that's the previous block that's the case that's supposed to be cryptographically prove that this was the previous block and soul in every block it adds another bump introductions and again the transactions are always between accounting user accounts and here's like a little bit high resolution of how this all works so this one here is a list of transactions but also there's a state so transactions is like for example a very simple transaction would be from my user account send some money to another account and then the state changes and says ok we reduce my balance let's say I send five either or something smaller and I increase the balance of the receiver so every time we add a block transactions it also updates the state now let's - so here you see the state that's part of one more and here's against it so this is no state and here's the other state so here you can see the address of the user that we saw a contest 1,024 easy here's another amount but that's a contra account so some code and it has some data that is stored with that account here's another it's also a contract account it has no neither they also have some code and here's another user code now somebody sent a transaction and we had initiative that state in the current block and that such protection in reality there will be more than one transaction from one block to the next but that particular transaction says which is this one over here talks to this account which is a contract it's this one and it says value 10 so it sends 10 ether from that account to this account and if you see here this account that's thousand 24 ether and now it has thousand forty either because it's centimeters but this other 200 to either now it has fact was 212 so it we see visa so in the new block the state is updated now protection their state and it says data there's two pieces of data number and name so this is a list the first item is to the second item is Charlie to me now here's the code that code says it may be can it can be so if the contract storage so contract storage is basically the data that is stored in the contract this is missed year and TX means transaction and data zero so that means the first item from data is the two number two so contract storage - so you always start counting in this is from the zero so this is zero this is item one this is item two so if there is no contract nothing stored in the position - until this perceived as there's no value in this in this case then contract storage again transactions later not resume that's the number two so set this one to transaction data one so we take the second item to the trend actually which is the name Johnny and we put it here where the zero is stored so if you see on the other side there's Charlie so it updated the zero with Charlie so in order to get from one block in the blockchain to the usual in the blockchain the transaction was executed and a state transition happened so the money was sent there was the simple part and the next part means actually the thing that is fall to the EPMD theory of virtual machine execute the code together with the transaction later in order to update the storage part in the next state now that happens for all the accounts and all the transactions that have been between one walk in the next all right so and that's what we're going to program so basically when we write contracts we wanted to create a new contract account an address can have a balance like a private account but the private account was we're accusing our wallet right like actually the first slide with a public and private key but that's usually controlled by a human or it cannot be program but the second type of the contract you can tax transparently on the blockchain they don't have public and private keys and they could not do anything on their own so only when a human or somebody who has a wallet or a public and private key starts a transaction then maybe a contract account like this one the human or the the wallet that has this address started at when that key and talked to the contract so the contract the code will get executed according to the code that it stores in the contract and that's what we will program and it causes updates now this code can also talk to more contracts in those contracts to talk to or contract so if somebody of water to start a transaction it can touch more than one contract but now this is a simple example so here the contract only updates is own storage it doesn't all right oh so here again the blockchain it starts with the first lock you have some transactions to come to the next state in the next month and so on here that's basically the same graphic so from one stage one protection to the next one and it always comes out like this and the state is the part down in and the red arrow is the hashes of each block is linked to the previous one with a cryptographic hash and also attached is a list of protection so it's a little bit of a simplification but generally this is how it works so you have some metadata you have the linking between blocks you have the state there's a Code section which stores the code there is a balance which which is basically the same as the storage but the storage means you can store any data depends on the contract while the balance is a special place to store the ether because they have a separate meaning or in this case a special meaning that they have value now of course every time you execute the transaction you also have to pay for gas because every how they did you do needs to be executed by all the nodes in the network and if there's a lot of note they all have to update the storage and that's work for them so the ether that you send they're supposed to pay all these people to update the database now once the database is updated if you run your own node you have a local copy so when you read from the from the network you can read from your local copy because when somebody writes to the network every no one gets updated which includes your own node so then you can read the data from your own node so nobody else in the network has to do any work and that's why the reading is free all right now let's try to put it all together so we have user accounts so we are using this wallet meta mask because we're working in the browser and it can generate public private keys the private keys is something that you keep secret maybe you can even write it down on paper and put it in a safe the public key is your in the network this one is supposed to be a death it's like in half but it's decentralized app because you don't sign up with a password email address instead you use your private and public key through your wallet which for example and we made a mask and then you can click on the app you can type text it's just like any normal app but the wallet uses your private to surrender when when the app sends a message to the blockchain to the theory of network it signs that when your private key so that the network knows okay that came from you not from somebody else now the app itself can be written in HTML CSS JavaScript for example but you can also use a mobile app or any kind of app and depending on the language usually there is a repertory j/s library that basically helps you to make that when people click buttons to talk to this network any theory network you see the list of blocks that's basically the blockchain in one node there's a key on every node which stores the storage interests of productions and then comes a new block with the new storage in the new transactions because there's always the state-transition yeah that's it in this network you have a smart contract accounts so that's what the code is stored like you saw earlier and that replaces to some degree to traditional server so instead of an app talking to the server's of Facebook or any server all of this can be done by talking to smart contract accounts and those smart perfect accounts our addresses in the blockchain and every if you run an you know you have a so many being yeah now we can basically do what to him yeah I know that this was not like a lot of information but this you don't have to know everything by heart is just to have a feeling of the space what are we gonna do so don't worry if you didn't understand everything I also didn't completely follow in between my god distracted but we were thinking to start with installing a metal mask so does anybody already have meta masks installed on your computer ok so then maybe you can hug your neighbor maybe it doesn't have it yet so let just kind of quickly go through how how to install it so so as Alex mentioned way the mask is our user account and when we are using any kind of decentralized app this is how we can sign transactions so go to the metal mask dot Iowa and if you are on you have to be a thing on Firefox Chrome brave any other supports metal mask and then open our video so as so people get Firefox add-on or Chrome as its installed the slides were also assent of the Telegraph in chat so if somebody wants to go back or you can open that yourself so okay confirm everything and because you probably don't have a better mask account yet just create a new wallet but if you by any chance wants to reuse the same one on another browser you can also do Porter bonnet but for now just create a new bonnet can you just raise your hand who is installing the first is that we know who maybe needs help okay okay so for those who have it already okay because we will be trying out transactions you probably already know this test networks that you can use but some of them take like long time so we learn now that there is a new one called a theorem which is proof of Authority that's why it goes super fast so I recommend you to add a custom RPC to your existing method musk so just click on the extension and then just click this custom RPC so this would be very useful so here sorry and you can just add here a customer PC good and maybe able to switch to the next one because we have to fill out some information about this custom RPC if you have slides open you can you can click here and get things that you can copy from but otherwise you can also just write it manually this will really speed up all the transactions on your remix for play and eternal so first it's a tier you then it's everybody know then let me let me just send you the link this on telegram aha are you on the chat already maybe using this visit amigas chat so the carousel is here it is concetto exactly this means that it cannot occur listen I'm very sorry sweetie so some people are trying to okay now achieve it over the very [Music] does anybody need help Alex click around and help just raise your hand and we can so just label if somebody cannot see this is HTTP a be a so Alva top Network Cologne 21 0 15 / e xt / e VN / RPC and here is a th explorer yeah I think the best would be here this one yes here you can copy parts you see I have a full tax ground [Music] Oh I know this is a little bit annoying part but I promise this will you will be super happy when you interact with contracts because it's fast now you made a video well so we have a mini tutorial already made on the spot that place and also like for those who already did it does that you are not bored you can click and create another account because when we play the smart contract we need more accounts so one time you would be bought one time it would be Alice or you need like account one account two and then when you have it you can also tell you the faucet this is a name for getting free tokens for this test network so this is also a link that I can send you and from here what you need to do here let me set the link which is the first so here you have a link to therefore sent yes and how does the faucets usually work so when you have your account you can click on the account and this will copy the address of the account and then you put this address into the faucet and you are actually asking the the faucet to give you some free eaters so copy paste your address tell them that your another robot or if you are just like a little and then you can request some funds and at the end of the phone game you should have hopefully two accounts and both accounts will have some either so I will also request in my account one any success see sometimes it will be all not request okay so maybe because we are all not requesting from this faucet it takes a little bit and maybe if somebody is even done with all these and you can even go for yourself but it's not necessary you can for easier playing later you can just name your account slick - and this is one for example but you can also leave it as account one an account - this is just like a sugar on top if you have time I don't know why all the Tigers Alice and Bob let's see so if anybody who still didn't manage to set up this customer we see do you all have a theory now in your metal mask if somebody's still stuck on this step please let us help you and Nora so maybe if I mean she speaks Chinese very good somebody needs help with these workshops it's always like some people are super fast some people are more slow so I hope you can be patient so the end result you have a theorem in your meta mask and you have hopefully two accounts and both accounts have some tests either and if you feel only big you even name them with some lips like Alison okay the majority is ready everybody ready there is one little team that still is debating stuff but I think so can we continue yes it's okay most of you probably have metamath already but basically if you got your public and private key pair and it also gave you the chance to backup your keys now for the tests megamouth account doesn't matter but if you ever use it with real fans you should better be merciful because there is no forget password but if you lose the public credit so right so now the thing is that is that is like how to do contract accounts so you have to traditional contracts then they work with lawyers they're expensive and even smart contracts they have code in a little departure so again this we saw earlier you have transactions and transactions are messages so each transaction has a sender who sent the transaction and a signature means you use your private key to take the transaction data and sign it so basically combining your private key piece of data with the transaction so that everybody on the network because your public keys public can scan see oh we can use this data that you sent to the debtor and figure out the real transaction data the way it's written here and that's only possible if it was encrypted or signed with your private key so that's what will you prove that you are the sender the same so then there's a receiver that reduction in this case that's this contract address but it can also be another one so these are external accounts so this account here could of course also send it to this user account you it doesn't have to be a contract now the thing is it cannot be empty so you can make a transaction from your account to know so what that means is if you do it then the theory of network will think that the payload so the payload is just another another word for data in a way so this one what we saw that that's that's called a reload now in this transaction this was data that is meaningful in the context of this contract because it has code but if you don't have a receiver so if the receiver is empty there is no receiver it's just a sender then a theory Network expects that the payload will be cold so you basically are sending code to no address and what that means is there will be a theory that will generate a new address and the code that is stored in the day section will be the initial coat with a new address so what you're doing is you create a new contract account you have a wallet which is your user account and you can send a transaction to the theory of network with no receiver and code in the transaction message and that creates a new account and it's a contract account so basically it looks like these accounts it has a new address it maybe looks more like this this one because this starts with zero ether usually unless you send either it has the code comes from basically from the code that you sent but what the theorem that what does it execute the code so you don't send the code that is the code for that account but you send code which when you execute it will produce the code that you store so that means there is an initial code that is executed only one time when you create the account and it helps you to initialize the contract so when when you execute the code it can already create a little storage maybe you send code let's say you created a rental contract tenant agreement you're the landlord and you want to rent an apartment so maybe you wrote some code to rent out of apartment and you set the code of the contract to the blockchain but you also include the first tenants and the first hands are Alice Bob and Charlie so it stores the code which is the rental agreement but it also already includes three vendors maybe you have another apartment you use the same code again to make another agreement like to rent your apartment but in that new contract you use different names because will be different people rent all right so absolutely to make a little break in five minutes we will have a ten minute break there are some snacks just so then again so Center is a transaction part receiver value which is ether payload which is data and if you don't have a receiver address the payload will be code to create a new contract amount and you oughta see gas limit and gas price so when you set a transaction all the computers in the network they have to add them to their local database and there are this process is called mining and in order to not pay too much you can set a guesstimate and gas price and then it knows approximately that that only has to do with the details which don't go so much into today but basically that would pay for every every transaction maybe just quickly the way this works is there is a specification for the code and every every command or every statement as it is of so called op codes and every opcode has a cost and if you have a lot of code you can break it down into lots of opcodes and then you can basically calculate what will be the gas price but sometimes you have been programming loops so some code is executed multiple times and the amount that this loop executes might depend on the data so let's say you have a list so if you have some additional dates and smart content account has a contract address it has the binary code so it doesn't store actually the source code so we will write a solidity source tonight but we'll store that in zeros and ones it makes it much smaller so that so much space because everybody has to store it will have the value which it is so four minutes acidity in solidity every special syntax to do something with the value or to do something with the address or to do something with the storage so in order to know how to write solidity good it's good to know what are what is the environment so I there are wallets Keys and there are addresses there's storage and there's all these things and this is what people program so the the storage is basically visualized here as this list and as we saw earlier but let's actually replace this zero in Charlie which means you update historically there is a memory so the memory is knowledge exists and every time you update it the storage is still there the memory is another kind of storage and why this transaction is accepted there will be something stored in the memory but when the transaction was successfully executed it will be forgotten so if you don't want to if you don't want to forget it you should join the storage while the transaction is executed the stack is basically the place where the program stores all the commands that will be executed so for example this contract here before it stores something means it calls another contract but when you execute and maybe checks if nothing sport then it pauses calls another contract it does something and then it comes back and it continues here in this next line so that's basically does that helps you keep track of this and then there's also the event so events cannot you cannot read the events inside of the contract but you can create new events so the reason is when sent let's say you send to transfer some in contracts and some tokens or some kind of money to another account then you want to create an event that can be done with event these wallets or apps called apps they can read from the blockchain all the events that happen in one contract so is a little bit like a mobile phone notification or a browser notification the contract only creates them but your phone or your browser gets a notification and then basically you know or something happened or you can run that knows okay when I received this kind of event I should do something and then of course it's actually history and that's basically just the blockchain and all the blocks with all the transactions that happened in the past so this contract maybe has a very long list of transactions because maybe many accounts send transactions in previous books all right so so let's make a break coffee some snacks and then hopefully you still come back in you
Automatic transcript — names and jargon may be misspelled.