Workshop: Introduction to smart contract programming - Christian Reitwiessner - at GSC
Devcon·Tue, Oct 9, 2018, 12:00 AM
Speaker
Workshop: Introduction to smart contract programming - Christian Reitwiessner. We will start with an introductory talk and then write smart contracts, interact with them and in general have fun with blockchains! Please try to install the Metamask browser plugin (http://metamask.io/) before the workshop and perhaps try to get some Rinkeby testnet ether: https://www.rinkeby.io/#faucet Many thanks again to the Game Science Center (GSC) who hosted this event in Berlin.
Transcript
[Music] mr. Shermer giving us microphones okay shall we start is everyone set let's wait until the police is gone okay thanks everyone for coming to this Meetup lots of people here I see that's really nice I hope I mean that tables look really packed so I hope there's not the same same amount of people waiting outside yeah so that's this is the first time we are in the game science center here and looks like a really nice venue and I want to thank the game Science Center for hosting the meetup here and Leah wants to say some words about what this place actually is smart contract watch gamecenter I wish you fun while coding and learning solidity so the workshop will end route about 9:30 and afterwards we will open up the bar an exhibition for you so feel free to have a beer and mingle at the bar and also if you are planning events like networking livestream and eSports events then you can talk to me afterwards at the bar and we can make plans so have fun yeah and also ii thank you goes to live peer who are again providing the AV setup here and they are also live streaming this in a decentralized way to the internets so that's really cool okay my name is crystal right is not by the way and let's start getting to know something about how to ride smart contracts on aquarium so did a lot test that oh no i didn't [Music] okay as I already said please try to install the meter mask browser plugin this is so that's not the best way to access a blockchain because it's fully centralized but it's a very very convenient way to try out aetherium and to have everything running just in the browser without the need to install anything apart from the plug-in of course so there is a version for firefox and chrome i'm not sure about safari if you have the option use chrome because last time firefox had some issues some some pictures okay so yeah this is about blockchain right and blockchains that's this weird world where you have people buying tons of GPUs and having solve them fully useless problems and people actually rent airplanes so that they can buy GPUs faster than some other people who do the same thing and there's also this weird thing where you trick people into giving you money and promised to change the world and then in the end you do nothing just run away with the money set an issue on my side and then there are these companies who used to do iced tea and then they've changed their name and introduced blockchain into their name and the stock price increases I think threefold over ten or so something like that yeah and in the end you think you're perhaps this blockchain thing is something for me so you asked some experts and if they are real experts then in 95 percent of all cases will turn out that you actually don't need a blockchain so but there are of course legitimate use cases for block trades otherwise I wouldn't be here and and in addition to that this this thing that you're wasting electricity and mining is actually not essential there is something that is called proof of stake which can hopefully replace proof of work very soon and most token sales are scams but they're so a token sale is actually an interesting way to fund a product if it's done right so you shouldn't be allowed to just walk away with the money and you can build a token sale such that this is this is technically not possible and there of course some use some good use cases for block chains in general and let me just show you two example use cases of block chains worth using a block chain actually makes this makes a lot of sense and then we will start with writing some very simple smart contracts and one of these use cases is the swarm system it's a decentralized file system so basically a way to store your data in the cloud where the cloud just doesn't mean Amazon's compute form but hard disks donated by users of the system and it uses a BitTorrent like system to distribute the data and but the you can't use BitTorrent right away for distributing data because it has so you can't use it right away for a general file source system because it has some flaws and these are unpopular files are deleted after some time so only popular content stays available and what can I do okay just speak louder I can try that I mean do we actually need do we actually need speakers for this room perhaps you can use the microphone just for the streaming people in the back do you need speakers [Music] sure yeah for now it's fine okay good so in Victorian there's actually no advantage for you to to upload files [Music] try again please there's an echo in the room okay let's just try to continue so yes so impotent people send each other files and usually only want to have a file you have no incentive or you have no gain in sending other people some files it's part of the protocol so people do it and that's why it works but yeah for general file storage system again why should you send someone some other persons backups or their own backups and the blockchain can solve these two problems it can pay people for storing files so it works by you giving someone money for storing your files and that person basically promises to not delete them and make them available and this promise is done in a monetary way so they send some money to a locked account and if that person cannot provide the files when you need them that the money in that locked account is destroyed or you might even get a share in that or something like that so that person is incentivized and also paid for keeping your data available and then even if your data is available when they just send it in one bit at a time then that doesn't help much because you need your terabytes of backup at some point and you're also willing to pay for having faster download so people also pay each other for providing elements and that can be solved by a blockchain and in general coming up with these rules who pays whom in which situations and whose deposits get destroyed and so on this is called crypto economics or in more general mathematical branch is called mechanism design and then another thing that can be nice is off with block chains is removing the problem of you having to trust so-called certificate authorities when you browse the web and so this works whenever you access a website that is encrypted encryption solves two issues here the first issue is that you don't want your data to be publicly visible and the second issue you want to be sure that you're actually connected to the correct website this mostly works because you enter the correct name but if someone is has a tap on your internet connection then they can pretend to be that person and still intercept your data or even modify it and this is currently solved by certain organizations I think there are more than hundred hundreds of them who kind of yeah signed certificates that claim that the person you're currently communicating with is the person behind a certain name and this is of course yeah really badly flawed because I mean there are 100 of these organizations and any of these organization can give out such a certificate and most of them are yeah they are distributed all around the world in various questionable regimes and so on so yeah this doesn't look nice and another problem is that the main ownership itself is not part of the system so you can sell such a CA can issue a certificate and they are obliged to check that you're the legitimate owner of that domain but this is not part of the protocol so if they don't do it I mean someone might notice at some point but they can just skip checking that and also the way so they yeah the thoroughness in which this check is done on yeah dramatically depends on the specific certificate authority now yeah at any one everyone can easily create fake certificates on a technical level and a blockchain based system and yeah for example the etherium name system can go the whole way so there you can build a system where the allocation of names and the issuing of certificates is part of the same protocol and cannot be faked on a technical level so you can yeah the the the certificate that is issued will be stored on the blockchain together with the same record that states that you own that that name in the name system and of course this the same system can be applied to something that is not about DNS but about anything that is kind of ownership of virtual things and for example land registries is in a certain way it stores ownership of a virtual thing I mean it's it's not yeah I mean what's the thing in the sense that an artificial distribution of land made by humans so it's kind of virtual and it is already I mean it's already a word sure thing that is stored in digital databases in tunnels and things like that and of course here in Germany this is probably not such a big issue but there are certainly countries where land registries are prone to corruption and a system where yeah this is technically impossible might help there and in general when so coming back to this flowchart diagram of earlier when our blockchain is useful and in my opinion block trains are useful the application you're you're you want to add a blockchain to is already digitized so it doesn't help with yeah I don't know Postal Services where you have to confirm that you received a package or something like that because this is a purely physical thing but language mysteries which are already digital can be used here where the enforcement of ownership is of course separate thing and then yeah participants have to have conflicting goals if you don't have conflicting good the goals you can use a regular database because nobody has an incentive to to commit fraud and yeah even if you have conflicting goals at least if all participants have a specific third party who they trust then there's also no sense in using a blockchain because you can use the database controlled by that third party so these three things I think yes application is digitized participants have conflicting goals and there's no trusted third party and then the fourth important thing is you have to be aware that using a blockchain is probably much more costly than not using a blockchain so these three things on top have to be so important for you that you can cope with the additional costs that come using the blockchain okay then let's get to the little bit more technical part what is the theorem yeah in general if theorem is a database and the interesting thing about the theorem is that it is a database that is not running on a single computer but it is running on thousands of computers all around the globe and because of that it's yeah you can say it's decentralized it's also a transparent database meaning that everyone can take a look at the database everyone can take a look at modifications of the database it is a manipulation resistant which means that if the database does not allow a certain modification then it is very very hard to do such a modification it is also authenticated which means that any modification to the database any message sentence system is signed by so it's digitally signed by the sender of that message and it's publicly accessible means everyone who wants can use not only read the database but also modify the database this is also a very so very big simplification and also everything I will say in the following is probably a very big simplification so if you have questions please ask I mean perhaps not now but later so what is a smart contract smart contracts are neither smart nor are their contracts they are just programs running on a database nothing more nothing less and okay a little bit more and this little bit more that this database is a blockchain but that's it you can also think of them as stored procedures in the blockchain based database if you know what sort procedures are okay that's it for the introductory talk there are so let's let's try to program some smart contracts there are instructions behind the link there on the top this is the most annoying part getting this link to you and huh I could do that so who has not yet installed the meter mask browser plugin and so I hope the the set up of the tables here help and I would ask everyone to help each other because that way I think it works best we have some people who walk around it and help out but we have to build on you helping each other so who so who has installed the plugin already okay that's almost a compliment good I think everyone will get along good now the next peak is getting every one of you tested ether so we won't do these yeah we won't program smart contracts on the actual aetherium main network because that would be too expensive but instead we will do it on a test net where it's yeah that is meant for testing and where the currency ether has no value but still every one of you needs to get some of these of this test net ether and we will do it on a network called rim QP that's one of the test nuts and I think yeah so I think we will start taking a look at that and then now I need my second hand so I'm not sure what we do now do you have something to hang that [Music] okay no worries so these are the instructions here [Music] who has set up during the the meter mask plugin so this is this little fox here and make sure that it is set to rinkeby test that not to main it who is at that stage so at least one person from each table I think good now if you ever plan to use this account also on the mainland I would strongly advise you to create separate accounts for each of the networks and you can also name your account just put rinkeby there just to make sure so that you not accidentally confuse these 40,000 US dollars for real US dollars and cents or even worse the other way around and you can switch the account using that I can here and you can create a new account here okay I have another oh yes someone wanted me to put that on the meter page [Music] I think I'm not signed in here nope can someone of you do that please put the link into the meter page Moyes can you put this link into the meter page for someone else just said okay thanks so the next step is for you to get ranked you be tested ether is everyone comfortable with using one of the suggested method methods so I let's just close this annoying thing yeah we somehow forgot to set up a communication channel for this meter sorry about that he's just doing it that's fine [Music] so [Music] so who does neither use Twitter nor github okay so because these are the two ways to get tested either and so it works why this faucet okay sorry no it's not Twitter or github you can either use one of these three methods or put a comment with your rinkeby test net account number at the bottom of the of the gist with the instructions does that work for everyone and then I will send you some of mine so oh we can also use another way that's perhaps most [Applause] yeah does that work with caster cones just paste your address here I will put this link into the gist okay that's an unfortunately rather long process in the beginning this is also a rather annoying process in reality with the actual if you're a network but once were past that it only gets better so yeah if you can't use Twitter or any of the other methods to get tested ether please request it by pasting Edna's address into that thing just that work for you can you edit it so I posted that as a comment below the instructions site so does it work for anyone yes so did you already pay stuff because as soon as you paste stuff there okay wonderful so now we can see how that all works and so as this is as I said a collaborative workshop people who already have enough tests that ether can try to send some to the other addresses here then mark them as already sent so I will now now this is a bit annoying I'm sorry I think I'll switch my video setup to mirrored is that fine that would change the resolution I think okay I just copied the address and this is my meter mask plug-in now I have some test net ether I will send some to the address I just copied I think one ether should be enough I click Next down now I see all the information again yes sorry I can't increase the size here the neck lick submit and now it created the transaction and sent it why are this centralized gateway but still to be decentralized if your network and I can click here to get more details on a so-called blockchain Explorer which is also a centralized thing but still better than nothing and it says it's pending now this means the transaction has to wait for a so-called minor to include the transaction in the next block miners are people who are yeah randomly selected by a certain process to determine the order of transaction and determining the order of transaction is important because there could be conflicting transactions in the sense that I want to send one ether to person a or I want to send all if I have in my account to person a and all if they have to in my account to person B and those two transactions cannot be valid at the same point so someone has to determine which of the transaction is the one that goes in first and that one will be the one that will is well it in the end the transaction has a or I can increase the font size here so the transaction has a from field which is the sender and a to field which is the recipient a value the amount of ether that is sent and a gas limit we'll come to that later yeah now it says success here which means the transaction was included in a block this is the number of the block blocks are numbered consecutively starting from from zero and when I click on the block now I get information on the block and it has a timestamp of 51 seconds ago it includes 18 transactions of 17 transactions apart from the one I included it has a hash hashes are rather important in blockchains but I think we will not be able to cover them in as a parent hash which is which is kind of linked kind of a link to the previous block and this is the address of the miner the miner is awarded by yeah zero ether in the test net and on the real net the miner gets an actual reward for working on yeah participating in this random selection which is a computation intensive process yes so I think that's yeah uncles are a complicated process which basically allowed to reduce the block time from 10 minutes as it is in Bitcoin to 15 seconds as it is in etherium and if you reduce the block time then it's much more likely for this random selection process to kind of select to minus at the same time and then they would produce two parallel blocks and because of that they would be conflicting in the same way as two parallel transactions from the same person will be conflicting and yeah this random selection process this mining in the end will find a way to select one of these two parallel blocks as the canonical block but this takes a while so you want to reduce or yeah increase the speed of the the blockchain network to converge on one of the alternatives and uncles is a way to do that because yeah complicated I mean it's not too complicated but perhaps a little bit too complicated for this workshop so the person who requests at the test Anita did you get it who was it good now yeah let's so perhaps also take a look let's take a look at this dashboard that you see when you go to rinkeby dot IO this views the current network it has the current best block so the the block hide the block time which is a diagram of how long it takes from one block to the next this is a random process which happens roughly every 15 seconds so that's the reason for the for the variation here block propagation is a diagram that shows how the newly created blocks are distributed inside the network yeah okay then I will send some more so this is done so you probably need 0.001 ether for this workshop so would be fine if someone could help me distribute ether this is mark doesn't mean that someone is currently sending yeah that was faster than I expected okay only six addresses that doesn't sound correct who has tested ether now Wow nice so I assume everyone is fine now Oh another address okay so is queenie probably okay so someone who still does not have testes and now I think you can handle that in your your tables locally okay now if there is only part of the fun of course we can now send around tests and if there which is already nice if it would be really that we could use it to pay for whatever pizza but of course what we all came here for are smart contracts and because there's so great support for it we will still do a rather boring thing that smart contracts can be used for and that is tokens so this is almost the same as ether but still there are big differences because you can create tokens with yeah you can when you create a new car token you determine what the mechanic will be whether it will be a token that is destroyed randomly or where new tokens are created at certain points in time whether it's some kind of thing that goes into the direction of a universal basic income or whether it's just ownership tokens or virtual kitties or whatever okay where are the instructions again so let's take a look now at this example contract I put here this simple contract this for now it's just source code but we will create that as a smart contract on the blockchain and for that we use the IDE remix which is a dreaming study theorem dorg let me reload that so that's a development environment for our solidity but it also so where it's really strong is that it has an integral integrated execution environment so it can create a virtual block chain that only runs in your browser where you can call functions and test the behavior of the smart contracts you right then you can modify some part and run it again and you can also interface with the real blocks and deploy stuff either on the test net or on the real network we only have to wait for it to load I was sorry I didn't mean to meant to use Firefox anyway although any questions for now I know you all really badly want to write smart contracts but we have to wait for the website to load an empty one I guess I mean I I created the compiler so there was something to test but I think yeah I didn't start with contracts it just started with with with simple expressions yeah that is weird does it load for you okay then perhaps start huh refresh yeah just did so perhaps start by pasting the simple contract source code that you have here into the source code part there is a rather complicated smart contract there by default you can just erase that and replace it by this simple one yeah I can explain some aspects just on the source code so this first line just tells the compiler or it tells the compiler which version you want to have or which it should expect so that they aren't surprised by any breaking changes and then you have this contract keyword and that's the name for your other contract you want to create so one one tiny complication we have is that there's no there are no two different words for a contract a yeah a specific contract that is deployed on the blockchain and the source code correspond to that contract both it's called contract and the source code contract there's only one but you can have multiple copies of them multiple copies of the same source code on the on the blockchain in programming terms that would be distinct the distinction between class an instance but both is called contractive for us so and so it has a variable here called stored number this number is permanently stored on the blockchain and if you so un means it's an unsigned integer of 256 bits that's the default and public means it will the compiler would would generate a function for you to read the value that is stored there and then you have a function called name which is public which means it's accessible from outside it is pure which means it's not allowed to modify the blockchain State and it returns a string and what it returns is my github username and then you have another function which yeah where now the interesting part starts it's called sort number it takes a pair of type ants an integer again named X it's publicly available so which means everyone can call it and what it does is it modifies the value of the number that is stored in this slot here so you can write and you can use that function to store an arbitrary number inside the smart contract here and because there's no access control here everyone can call that everyone can modify the number okay let's see if it oh yeah it's there so let's try to run that you can run that on rinkeby but yeah okay I think you now have to click start to compile or you take auto compile and it auto compiles whenever you change something now if you click on details here you can get some details about the contract the truth we should dive into them at this point perhaps not and now the interesting part is when you go to the run tab here because there you can select a contract in your source code and when you click the red create button it will create a transaction that will create the contract this selection at the top is very important because that determines where the smart contract is created if you select JavaScript VM it will create it in a virtual blockchain just in your browser if you select injected web 3 then it will created on the rinkeby test net and let's do this just for now so if you click create here then the meter mask plug-in will pop up and will ask you to confirm it it now it now has a different icon here because it creates a contract instead of just sending either and we can now click Submit and we will again get a pending transaction damn it sorry so this is the glitch I was talking about if you use Firefox then the browser will not properly work with contract calls so you have to go through here again click on the pending transaction and then you will see this in the blockchain Explorer I will show you how it looks in chrome yeah okay so yeah so the thing is this smart contract will now be created at a certain address in e theorem so everything has addresses we already talked about addresses because you got an address associated with a public/private key pair and contracts also have an address and when you want to interact with a contract you have to know its address because that's where you send your transaction to ok meter mask already says it's confirmed if the scan does not say it's confirmed yet perhaps reload will help nope yeah confirm so and now it says contra created at this address and here you can see the yeah the the contract byte code so the yeah if um uses a certain virtual machine called the virtual machine which has a separate byte code that is different from all other virtual machines and this is its hexadecimal representation okay now it says contract created we copy this address or actually let's just go there I mean you y'all did that in parallel I guess so you have different addresses there of course but that doesn't matter now in this box on Explorer we have a different view because what would the address we're now looking at is a contract address which has a tab called code where we can look at the code this doesn't help us very much so that's the hexadecimal view and that's the bytecode view which is similar to some assembly code this is really annoying to look at and because of that so but we actually know the source code behind the small contract there and but ether scan doesn't know this so there the centralized component here doesn't know it only our browser knows it but there's a way to tell it what the source code behind this contract is and this is always very useful to use and you use this you get to that using this verify and publish link here in the code tab okay am I too fast I might you slow okay okay so where are you excuse me which what uh sorry yes so I used injected web yes so every one of you has the source code here you copied it from the from the instructions can I get sir can I see some hands okay good now you select injected web here which should be the default anyway then you click the red create button meta mask pop-up you confirm so I'll do it again so click create here then me Tomas pops up I click Submit ok now ok the step that was probably too fast is how to get to the to the other website here so I just sign the transaction and the plug and disappeared again so I I open it up again and then there's at the words that the top entry is an entry that has these three dots or perhaps is already confirmed but in any case you click on this icon here which will lead you to the ether scan website and it's it's the website for the transaction and the transaction should say contract created with a green mark ok question [Music] yes the find similar contractor I just repeat your question [Music] yes so [Music] I didn't verify it yet so the question was whether so the scan has a functionality where it can find contracts with the same source code automatically or see me automatically and the question was why is this not working in this case now and the reason is I did not upload my source code yet to ether skin and is that that's what I think is the reason okay now you click on the contract address and you see a transaction so there the creation transaction of the contract you see who created it so as I said earlier every transaction every message in aetherium always has a sender it's always on Takei 'td and you know yeah even the contract creation has a kind of origin okay now you click on the code tab and then I'm very fine publish so did I still lose someone okay verify and publish let's click on that okay now we get to a very complicated website which will hopefully get simpler by a compiler feature we hopefully will be able to release soon so what you do is the address should already be correct you paste your source code into the bottom part here so we all paste or the same source code unless somebody modified the string here perhaps perhaps next time okay we paste the thing here we so because a single source file can contain multiple contracts we have to specify the name here oh my named account but it's just a stupid example don't read too much into it and then you have to select the compiler this is most probably we've 0 for 23 plus something so the compilers with nightly should not be used for such things only the one that start yeah they have committed Eric Lee check again by going back to remix sorry and and the settings or here at the top there should be the compiler version in the address bar [Music] okay and oh yeah optimization is the second setting we need to check so we still have to go to settings yeah an enable optimization is probably turned off I think it would also appear in the address probably optimize false so if optimizes false here then you have to switch optimization from yes to no so these huh that's just whether the compiler should do some optimizations so to make the code faster and cheaper without check it changing its semantics and so what we're doing here is we're so ether scan is checking whether the source code we provide here matches the byte code stored on the blockchain and it is important that it's so it is impossible to fake that so it's if so if I could just change the name of this function here or even worse if I could change this X to 2 then this function would do something else than I thought and by yeah due to some reasons it's impossible to provide the incorrect it's impossible to provide a different source code here but it's not possible to recover the source code from the byte code so the byte code loses information because it's much more compact but it still contains a hash of the source code and what they do is they just recompile the source code we provide here with the settings and if the same if it results in the same byte code then they say that is the source code okay and the rest here can be ignored for now and yeah it's a centralized service so it uses other centralized services [Music] yeah that didn't work [Music] let's try again did it work for somewhere else did you change today of any other settings let's just try again [Music] ah okay now it worked so now just this is a confirmation page now and when we go back hmm I just retried I don't know [Music] you go to the you click on the code tab and then it's letting us hear it so now if I reload this website it should show me the source code so let's see yep so when I click on code now we have the actual source code here it is verified exact match everything is fine and now so one thing before I get the question is now we have a read code read contract tab where we can interact with the smart contract for example it calls the name function for us and displays the string that it returns these are very yeah yeah this is not a very nice interface to interface with the smart contract we can do the same thing from remix which I will show you next but there were some questions [Music] so everyone will Korea so the bytecode so if you don't change the source code you use the same compiler version and the same settings then everyone will get exactly the same bytecode so basically yeah I think I can't switch back okay so this should be exactly the same for every one of us but what is different is the sender address the person that signed the transaction to create the contract and the the address at which the new contract is created is determined from the sender address and because of that everyone will get a different smart contract in the sense that a smart contract at a different address but it will be identical identical copies of the same contract sorry I don't understand what you mean by contract ID yes that's the address that's the the address at which the instance is located okay so each of us will never different yes that is the same for everyone so the byte code is the same for everyone it's just the byte string okay so you can you can take a look if you click the link yeah all right that that's the sign that's the I mean adopter so yeah yeah it's compressed information you can only go you can prove that you went the way and that way you can go back but you can't if you don't know the source code there's usually no way to go back because it involves the hash functions and okay so now let's do some interesting stuff okay people who used chrome for them that will be automatic and everyone else or at least for Firefox we view that manually what we do next is we copy the address of the smart contract yep it's in the URL of remix [Music] we copy the smart contract address we go back to remix we click on the run tab and people using Chrome they will have something here and due to a glitch in the connect between Firefox and the meter mask plug-in we have to do that manually and what we have to do is we have to paste the address here into the input box next to the at address button and then we click Add address and that basically tells remix hey at this specific address on the blockchain there is a contract of the type that was just selected here and by that remix provides us with an interface to to interact with the smart contract it creates one button for every public function that we can call so this add address button is always a little bit confusing create create transaction actually create in smart contract and at address just tells remix to display an interface to you that allows you to interact with a pre-existing smart contract [Music] so the create button does not run method it's it just creates the smart contract so any question for this step - was it again too fast yeah what do you mean why are we using this address button or so we're using that address button to get this interface here below if you use Chrome then you already have it because it will be created automatically yes there can be multiple of these here and depending on what you selected here in the top yeah exactly [Music] so the question was about the contract address and what it is it has nothing to do with the block it is just so an address of an account you create that is not a smart contract let us derive from the public key of your public private key pair and for smart contracts the address is derived from yeah basically from the from the account that created the smart contract so the account that created the smart contract and the nonce that is incremented with these transaction and at that address the data is stored hundreds are so blocks are a dimension of time and addresses are kind of a dimension of space so they are unrelated to each other so blocks are just there to group transactions and order transactions so they determine in which order changes are applied to the blockchain and addresses determine where data is stored where changes can be applied so of course a smart country is created in a certain a certain block so it starts existing from a certain block but yet so the bytecode of the smart conduct logic is stored in the block now so the transaction that created the smart contract which kind of includes so the the bytecode of the smart contract is part of the transaction that creates the smart contract and that transaction is part of the block but you can't really say that the the final smart contract code is stored in the block that it is sort in the blockchain add an address unrelated to blocks ok and now so we have this interface here we can click buttons blue buttons are buttons which do not cause a change in the blockchain they just read from the blockchain because of that they are free of charge and are executed instantly so you don't need to confirm the transaction with me Tomas and if I click name it says Chris F and stored number is probably zero yes but as soon as I click a red button and I can give an argument for the function store number here let's see a 17 and we click store number then we'll execute this function here with the argument of 17 which means that is still 70 in the store number here I click the button metre mask will pop up ask me for confirmation I confirm now we have to wait for the transaction to be included [Music] key on the left here it says transaction was included and now when I click store number again it reevaluate the function should say 17 so let's check yep sorry [Music] okay said more less clear how to interact with my smart contracts now using this interface okay here just out of curiosity there are these buttons here details and debug if you click debug then you will get to the second big feature of remix which is a debugger you can run derp than any transactions where they are so yeah over the transaction so it's not it's not a debugger that executes and you can stop it at some point it's more like an execution inspector so you can rerun and so there's there's actually a slider which represents time there you can run back and forth yeah but let's not get too deep into that okay now let's go to the next stage and create a token so there's the second contract that was linked now we'll do it in a different way and this time I will create the smart contract and you will use it so so the question was so yeah what I didn't tell much early because we use it on the test that is when you want some execution to be performed where you want to create a smart contract when you when you want to cause any permanent change in the blockchain you have to pay people for doing that so that the network doesn't run on its own you have to pay for things to be done and this is paid in a unit called Gas Gas more or less measures the amount of computation required and then there's the second factor called the gas price this means how much you're willing to pay per unit of gas and this is something you set when you create the transaction it's not - Brizzy millimeter mask I think it kind of determines that on its own but in other interfaces you sometimes have a slider where you can select to either pay more or pay less if you pay more then your transaction will be included earlier if you pay less it's cheaper but it will take longer for your transaction to be included it might even happen that your transaction is never included that's kind of how it support how it works is it answer your question okay so yeah let me create the smart contract I will verify the source code and then I will share the address with you and we will interact with it [Music] yeah I just wrote that smart contract in the afternoon didn't test it yet let's hope it works no it looks like me Tomas doesn't work so let's do it again in Firefox it looks like chrome isn't better [Music] so that's the creation of the weird token contract we can already take a look at it so the weird token has a name it's called weird token it has a symbol which is a short version of its name it has decimals which we can ignore and the most important thing is this mapping called balances and this kind of holds the for every possible address it holds the amount of token that is assigned to the address so this is kind of the balance sheet of this token system and it's a total it also has a total supply which is the the amount the the overall amount of tokens so the sum of all tokens the sum of all balances we have a function called balance off the Texan address and can be used to read the balance at that address and we have a function called transfer which takes a to address and a value and yeah what does the transfer function do it checks whether the person that wants to send to send tokens has enough balance so has at least a certain value I already said that all transactions have a sender and you can access this Center using MSG dot sender here that's a special value of solidity which can be used to access the sender of a transaction or more specifically the caller of a function so transfer so you can perhaps see the transfer that has no authentication part so it doesn't ask whether the person so whether someone is allowed to transfer tokens and the reason is because every transaction is signed it is already authorized by the signer so when you send a transaction you always have to authorize so by accessing MST that sender this already means that the sender has authorized a transaction so the sender has authorized the transfer of the tokens by invoking that function and this is something I really like about the theorem because you don't have to think about yeah who can do that and do I have to ask for a password or something this is all abstracted away in the in the cryptography below in the small contract level you just ask for who does this who wants to do something and if they want to do something then of course they yeah you can check if they are allowed to do that but you don't have to check if they are actually that person so transfer works by the person invoking the function asking to transfer a certain amount to a certain address so we check that the person has at least that balance and if that is the case then we reduce that person balances person's balance by that value and we add the same value to the recipient and we emit a func emitter yeah we emit an event to tell our environment that something happened now I'll get back to the two functions below in a second let's just check whether the transaction succeeded okay now the address so I'll add that to the hack MD and to the other one as someone was faster well okay l said it here so what you do now is you use this at address function to tell remix where the token is [Music] okay I will remove that here and [Music] okay now we have this nice token contract please use please paste the address here and use at address make sure that you selected we're token and then yeah make sure that you have this interface here please do not use your own instance of the weird token because you want to use all of us want to use the same token of course so now you probably ask yourself how do I get one of these shiny new weird tokens right now let's take a look at the two functions below here and this create token functions already sounds like it would create tokens but it's marked internal which means it cannot be called from outside what it does is increases the balance of the sender by one token and also the total amount total number of token in existence but it has said it cannot be called from outside but it is called it is called from here inside the request token mathematically function and that is a public function so that's good but it starts with two required statements and if the if the if the argument inside the require statement is false then this will cause the transaction to be reverted so it will it will stop executing and revert any effect it might have had so you have to think of a way to make this condition true so multiple of seventeen has to be 0 modulo 17 and number used at multiple of 17 has to be false and you invoke this request token mathematically function by of course clicking this red button here and I fear that if you just click the red button now the first person who does that will get a token can someone explain why so that the thing is we don't do or yondu we do input checking here if you just leave this input box empty it will use 0 as well you right yeah so if you leave this empty and just press the red button here it will use 0 for multiple of 17 and what will happen inside the small contract here when multiple of 17 is 0 yeah I mean this module is a bit complicated perhaps four people are not yes that it checks whether multiple of seventeen is a multiple of seventeen so a and a integer multiple of seventeen so zero times seventeen one times seventeen 2 times 17 three times seventeen and so on and zero seems to be zero times 17 so this check passes and the second check is just so everything is false by default so this reference is the mapping here and this is just a means to say that no two numbers can be used twice so if you use multiple of seventeen once then this is set to true and if someone does use the same number later it will stop here because that will no longer be true question so the question was if two people use the same number at the same time but one gives a higher gas price that person is more likely to get the token it depends so it depends on the miners I think they currently sort transactions by gas costs but I'm not so it might also be so this is a test net they are things might also be a little bit different but you're probably right yes so the person that uses a higher gas cost is more likely to get the token I think but also I mean it really depends on timing I mean if the miner is not aware of the second transaction yet but the but is aware of the first transaction already then and it's yeah it's a so it's a decentralized network so you cannot predict anything about timing that's that's the whole thing that blockchain solved so and because if this is a token and it implements the so called ERC 20 standard blockchain explorers have special support for that and we can use that support by telling if the scan that this is actually an E or C 20 token I will do that now I think I just have to reload so if the scan will notice that this is an E or C 20 token as soon as this event is fired oh yeah it already did so it automatically recognized that it's a or C 20 token oh we click here now we will get more information about the tokens yeah we already have 12 addresses that have tokens and we can get a list of all token holders one token holder is already quite wealthy and let's see I mean I'm wondering whether we can see which numbers were already used and of course we can I know it's not probably go visible yeah we can take a look at the I know we can take a look we have to look at the transactions and there we can see which numbers are used I think yeah we have to click on the dragnet that's not you convenient put yet here one CB in hex that is a number oh yeah that's a good idea the question was can we make the token wisdom limit I'm asking I think so yes you just have to copy the contract address the token contract address then click on meter mask and then you switch to the token tab here so there's a cent tab and the token tab and you click on add token you enter the token contract address so you paste it all red audio matically loads the token symbol then we click Add and it will say that I have 0 tokens yeah that's a different token so let me get some of these tokens too [Music] I don't think so okay how do I secretly come up with a multiple of 17 before you realize [Music] come on [Music] so we have 20 56 bits right so [Music] so if you have larger numbers here then you put them in quotes larger than the precision of a JavaScript 256 bits 256 bits again please this is 32 bytes in storage you can choose to use a smaller one by putting the number of bits after the UN so you into 80 for example oops but at there's no big advantage in using smaller numbers because the theorem virtual machine has the default data type for the universe machine is 256 bit integers everything smaller has to add additional operations to make it work for the smaller size it is a it might be an advantage in storage sometimes so if you want to store many small numbers it might be an advantage but you have to try yes [Music] you can't use them for scuff subscript what do you mean by that what do you mean by that for array index I don't know you can't that I wouldn't also see a reason for that okay so let's see if I got some yeah nice testement works okay any questions [Music] yep the question is so in the weird token we change the balances of the sender and receiver and then the we omit an event and the question was what is the event for the event is because it makes it easier for outside viewers to see what happens if they want to see so it whence are stored at a certain place which is much easy accessible if we wouldn't have events everyone would have to rerun the execution and basically look inside the execution and see what happens and with events you can just take a look at the blockchain itself and ya notice that something happened does it make it more transparent now it's more like easier searchable yep [Music] so why does ether scan recognize the concept of total oh yeah so so the question is how does me Tomas see the concepts how does it know that this contract represents our token I mean you just told it is a token you'd set new token at that address right so there yeah but I mean so the the minimum requirements for a token is this transfer function that will be called okay I mean why we added this token to purchase yes yes so why we added the token to meet a mask is that because now we can actually transfer tokens directly at least I thought okay yeah that's weird okay it looks like me Tomas doesn't support that yeah so many etherium wallets support also transferring tokens automatically it looks like meter mask only supports displaying that displaying them but yeah okay whatever but going back to your question so the minimum requirement for a token contract is this transfer function and the balance of function and perhaps the name but yeah the balance of function require allows you to clear your balance and the transfer function allows you to transfer tokens and then we also have this event here this transfer event which is also useful too so that you don't have to search the full blockchain all the time that's all we need and if that's there in the contract then it is a token I mean so having said that of course the fact that we have a function called transfer doesn't automatically mean that the transfer function also transfers tokens we can we can put whatever you want here inside the code so we can say if it's a Sunday then all transfer all transfer functions will send token to a special address and if it's a Monday half of them are burned and on all other days we have correct transfers we can we can we can emulate the the the existing traditional banking system and say transfers and Sunday are not allowed nice [Music] so the question was [Music] so the question vendor the question was whether the transfer event executes sati logic no I mean it's just it's just it stores this static is supply in a special data store data structure that is easily accessible from outside but does not execute any code okay now our next task try to get more than one token in a single transaction so for now we just click this red button and we have to find a new multiple of seventeen and we get one token in return okay is there a way to click a red button and get more than one token in return I'll give you a hint it will require a little bit more than copy pasting using this I mean yet so more than one of this these tokens so from this contract now the contract stays the same when yours exactly the same contract we what oh it's not an exploit I mean so request token mathematically publishes one token for every conceivable multiple of seventeen so so the exact ask is is there a way to click a red button here in remix so to execute a state changing function on some smart contract which will then award you with more than one term more than one of these weird tokens in return yeah it's very good so we have to write our own smart contract that calls request token mathematically on the token contract and of course multiple times and yes the difficult task is coming up with smart with with code that kind of finds multiples of seventeen in a different way than in the way all others in the room do it so let's let's try to find so I will show you how a very simple contract that does that looks like so I click on this plus this plus button on the top left and now okay what is a bit annoying okay actually yeah I don't like the red totaling the plus button it's much easier to just add another smart contract below the the the weird token so just add something below here and so the the most important thing is that this new smart contour gets to know the address of the wheel token smart contract because it needs to interact with it and do that by what was the other thing was it this one you how here so you if you still have it here you can just click here on this button to copy the address into the clipboard and then you paste it here okay we have an error here yeah okay we can't use that address Jung can I file an issue did you see the error message here Kim can you encode it in the checksum with correct checksum why not okay so yeah addresses in theory I'm have some checksum which is which uses upper and lower case of hexadecimal letters and actually it yeah if you click on compile here they just actually should show how the correct checksum looks like oh yeah here is okay I will paste that somewhere oh sorry about that yeah but it's also not checks and oh here it is okay so this is the I just would check some because it has an uppercase F in here and the lowercase e I just can't copy it because the browser does weird things okay [Applause] okay now we need to tell the compiler that we actually think that this is really a weird token at this address so but so this is the most complicated part okay I'll put that in the heck MD [Music] okay now what we do is function get token public and now we call we're token dot request total mathematically and the most important thing now so if we only do it like that then of course I know this model of 17 is already taken but more importantly we're talking will generate a token but the token will be assigned to this smart contract so it will not belong to our account but it will belong to the account of the fountain contract okay because the fountain contract called request token mathematically and we're token assigns tokens to whoever called request token mathematically so what we need to do is as a second step send the token back one step further to the caller of the fountain so we're token dot transfer msg dot sender and one token so okay one one other complication that arises here msg dot sender in this context at this point here and msg dot sender at at this point here so the person that gets sorry at this point here these are two different values they always represent the caller of the current function and if we call two functions are for each other than this of course changes inside the fountain this is us calling cat token but inside we're talking it is fountain calling we're talking so was everyone able to copy that contract did you get this weird this thing where so if you have any red marks here on the left it means there's a compiler error so something's wrong if you do not have any marks and you have a fountain in green here then it should be fine okay let's see what happens when I create that contract so make sure that you select the correct contract here that you want to create [Music] theoria need randoms so best randomness is produced by the outside I [Music] so I just created this fountain contract and now I click on the get token button and I get this message here this is very bad UX because it just means that something failed so it means one of these require statements here failed yeah so and yeah the reason why it probably failed is because my number was already taken but I want to know the failure before I have to pay for it yeah okay let's let's send it a good let's send it anyway and then we can oh we can check in the debugger and see where it figured but the debugger won't catch the source code correctly right because I've modified it the contract here ether scan also has a debugger can check there no but yeah yeah and we already have a pull request pending that displays a nicely formatted error right I mean we have this it's perhaps not in gas estimation but yeah that will improve soon by the way so whenever you see tooling lacking here and you can program there are tons of grant programs where you can just apply with an idea get some money and improve the ecosystem so the thing there are at least three different grant programs that are happy to give you money to work on improving this ecosystem so there's this I think it's called if price there's the ethion Community Fund and there's also the etherium Foundation Development grant program yeah okay oh that's nice so if a scam now tells me it this transaction failed and the only reason so I want to know why it failed let's see what I can get some information there yeah so if this can has a debugger assets you can click on tools and utilities here and click on remix debugger this will use more or less the same debugger we already saw i think it's an older version right Ian yeah I think that's quite tricky to get yeah they haven't even implemented returned data copy returned it sighs they don't know revert they really have to update their version okay so I think we have to close the workshop in 15 minutes and go to the fun stuff that involves liquids right has anyone succeeded in getting more than one token how many and what did you use [Music] so 163 transactions or you also see whether it was actually failed in the transaction list here this is weird 800 well they were spamming that Network what oh no someone is spamming in it but this is all the same address now it's the same transaction hash okay that's yeah yeah so there's an event for creating a single token inside the wave token smart contract which gets an entry for every single creation and then the final transfer of 100 is a single thing okay nice nice so shall I show you an example how to get multiple tokens okay how do you get banned in this text question just provide it from outside yep rinkeby I mean it doesn't have the source code so it's so that's just so yeah I mean what we could do is just copy their ten times okay so first of all we just multiply that by a second number we provide when we create the transaction oh the interface is really slow the question was where the state variables are by default public or private they are private by default but that of course does not mean that they are secret so everyone can read everything that is on the blockchain you and just select a random number here then this will still be multiples of 17 and someone can only outrun me by taking a look at the pending transactions or taking a look at the screen here so let's do that okay create the contract so what's the question the question is whether the require only concerns the current function or whether it's cascading I'm not sure what you mean so it's it's executed every single time it's just a normal statement ah right so if it require fails everything will think so so if the yeah if if one of these has already been taken then I will receive nothing so and due to two reasons actually so the first reason is that because a failing require here reverts the whole thing but even if it wouldn't I wouldn't be able to send ten tokens to me here ok let's see if that worked yep so try to copy that so if you're greedy or perhaps replace this 10 by 100 that wasn't the best idea because I think it will not work and the reason is I already said earlier that you have to pay for everything and the reason for that is if you don't then you can easily spam the blockchain you can just request a very expensive operation to be performed and that would take a long time and time is the important thing here because there is an upper limit on how much can be done inside a single block inside a single transaction and if you want to do more so yeah there's just enough about you can't do more than a certain thing inside a transaction it this upper bound is kind of flexible it changes over time but it's a hard upper bound so if you change this to 100 or I mean at least for 1000 they will certainly fail and so yeah of course the solution to do that is to break it up into multiple transactions or one solution so okay this was confirmed let's see how many tokens I have now Gary I'd worked 13 good so I hope you learned some thing about smart contracts here yeah question a filler honey is this solid colored man and his smallest nificant oasis this tournament was get talking permeated opinion the question is where do I set the total supply I it so it starts with zero and it increases for every token that is created it starts with zero so total supply is not a fixed number it changes every time a token is created is your token you can do whatever you want so currently it's unlimited you can generate an unbounded number of tokens with this smart contract so as I said I mean token does not does not necessarily mean that it's money so token is just something that can be transferred and yeah this certainly is not money because it's very easy to get your tokens they're more questions okay [Music] it defies much you should know that about consumer goods iPhone only attacking wine film is not completely that's containers oil has a theaters get such good humor that I'm a computer okay any more questions yeah how you have dividing into the lighter smaller transaction like you said didn't fight for like two one thousand let's assume I need for ten thousand is there some pattern that one can use and then have it like a split it into the operation it's going to read in the later stages so the question is if you have a longer operation how do you split it up into multiple transactions so that it fit into fits into the blocks I mean in this case it's it's trivial because I mean it already you can already split it up but just choosing a number and then running it multiple times but if it's it's if the result of the first part is important for the second part then it's more tricky I think there's no generic pattern but you just have to save the state okay and then continue on from there [Music] so if some of the steps fail you just start over from the last successful one yeah yep of which of the number used for clapping the balances is about so you can't so for a mapping so mappings are yeah infinite in size kind of they allow so you can't really get a full list of everything that there are ways but it's difficult but if you know you can look at certain points in the mapping and you can use to just use these two buttons for that so you're certain certainly cannot iterate over all the values inside a smart contract but there are ways to iterate over it from outside in a certain way but the problem is so that the keys you put inside the mapping they're hashed and what you certainly will never get is the actual values of the keys it's a hash map yes [Music] so solidity has a very low-level language and it has to be extremely gas efficient because otherwise you can't do most of the operations on the blockchain and because of that mappings are the yeah simplest thing you can think of that resembles a key value store if you also stole the keys together with the values it's already much more expensive that's why it's not done but you can build your own user-defined data type on top of mapping that also stores the keys it's deciding with me it's a sha-256 and so it's a sharp three hash with 32 bytes so there's practically no collision there yeah it's not it's not like a hash map where you cope with collisions it's a hash map that use a large hash value where you hope that there will never be a collision so if you have a collision in in the balances mapping then the whole blockchain is screwed because it uses the same hash function and if that has any collision then its origin later [Music] if gas estimation failed then this usually means that one of the requires failed so he did something that was not allowed so for example your Renan one of your numbers was already taken [Music] so my salsa basic modes and if I'm coming like seeing peanuts the skidder and lights income ambulances kid in Santander in the light mode kid in Santa [Music] which okay no I'm stream [Music] so I think we'll end the official part now I'm not sure what the agreements with when you are actually so I think we can still stay but yeah I guess let's end the livestream you
Automatic transcript — names and jargon may be misspelled.