Haseeb Rabbani: Solidity – The Coding Infrastructure of Ethereum | ETHDenver
Ethereum Denver·Tue, Oct 9, 2018, 12:00 AM
#BUIDL the future at https://ethdenver.com Hudson Jameson and Haseeb Rabbani This talk was part of the ETH Stage at ETHDenver 2018 on 02.16.2018. ETH Stage is ETHDenver’s Main Stage which features talks and workshops by top leading blockchain impactors, influencers and experts. ETHDenver is the largest Web3 #BUIDLathon in the world (fka hackathon) for Ethereum and other blockchain protocol enthusiasts, designers and developers. The community attending ETHDenver shows up to contribute to the global blockchain ecosystem, whether by #BUIDLing decentralized applications (dApps), contributing to or starting infrastructure level projects, writing documentation/tutorials/white-papers, designing UI/UX, or info-graphics, or to learn, connect, and get more involved. So bring your inner awesomeness and some friends. We’ll provide mentors, resources, and all of the educational content you can consume. Follow ETHDenver on Twitter: https://twitter.com/ethereumdenver Follow ETHDenver on Instagram: https://instagram.com/ethdenver Join the Bufficorn #BUIDL Brigade on Opensea: https://opensea.io/collection/bufficornbuidlbrigade Join the conversation on Discord: https://discord.gg/sporkdao Subscribe to our channel: https://www.youtube.com/ethdenver
Transcript
okay good morning we're just about ready for our second workshop of the day our pre hackathon workshops another reminder for those that didn't hear before those who have just shown up we have two stages here this is the main stage the each stage over here we have the way stage so check your program check your schedule we have multiple workshops on both stages throughout the day if you're thirsty you need some caffeine refreshments are right back there so we just had Hasib Rabbani up here talking about ethers IO we're gonna invite him back up as well and also Hudson Jamison Hudson needs no introduction for those like long time with 13 people he is a moderator of the etherium subreddit he works with the etherion etherium foundation and is working on a variety of projects we're gonna hear from them about solidity and meta masks and a bunch of other cool things that are happening in terms of the the coding infrastructure of etherion come on up everybody good morning oh oh nice I get callbacks sweet alright so what we're gonna do this morning is an intro to solidity talk I'm Hudson Jameson I have a startup called oaken innovations and I also work for the etherium foundation doing various things like the core dev meeting documentation other fun stuff so yeah I've been working with solidity for a while my role today for this talk is I'm gonna be out in the crowd with you guys and if you have a question if you get stuck raise your hand and I'll come over to you and if it's a question a lot of people are having we'll probably just pause and like see what the main problem is with everybody but I'm out there to help you guys so call me over raise your hand if you have a question during this talk I'm now gonna introduce hasib come up on stage and show you guys solidity thanks that's in I'm just gonna get my laptop set up real quick okay awesome hey everybody it's me again but this time I'm back for the second part where I'm going to talk more about smart contracts and solidity so again a little bit about blog geeks real quick we develop the blockchain engineering talent that will build the applications of the future and the way we do this is through our online courses through a curriculum that we developed we also have a bunch of free articles and guides online that you may have come across while you were searching for for blockchain articles and we also do a lot of in-person workshops like this where we demonstrate how to actually make use of these tools so let's get started you so yeah before we get into solidity let's just take a second to appreciate what it's actually doing for us so EVM aperian virtual machine is actually the language of smart contracts so all smart contracts are written in EVM opcodes but they look very much like machine language assembly language so that's why humans can't really code in that right so that's why we use high-level languages and so some of the high-level language is available for the EVM our lll or serpent and solidity so solidity is the most popular one today and the most officially supported and it has a syntax very similar to JavaScript so it's pretty easy to pick up and real quick what are smart contracts so smart contracts you can think of them as logic some code that you deploy to the blockchain right and once you deploy it to the blockchain it has an address and anyone can use that address to reference the contract and interact with a right to send messages back and forth to it right and the structure of the smart contract is very much like a class right so you know there's not a lot it's not some new sort of paradigm you know it just contains fields and methods and we use a compiler to compile solidity down to EVM opcodes which is actually what is run on on the the etherium network itself so some quick examples of smart contracts you know you could program a will that is executed at a given time so you can say hey at this time I want 50% of my wealth to go to this account and 50% to go to another account and you're guaranteed that it will be executed that way you can also use smart contracts as an escrow for making for just engaging in any sort of transaction with anybody right so it can be this place where you temporarily hold your money while you're engaging in a transaction you can use smart contracts to write multi signature wallets which you can kind of think of as like joint bank accounts so you need least two people's approval to to move any money out of the smart okay out of the smart contract you probably heard of initial coin offerings which are just like really big sort of crowdfunding platforms but also smart contracts and you can also use smart contracts to write your own currency in your own coin with its own use cases around alright so those are some use cases of smart contracts and that's why we want to write smart contracts so again I had these slides in my last presentation as well but just to appreciate at what part we're working on write traditional web apps front end back end front end written in HTML CSS JavaScript back-end usually hosted on AWS written in rails django node whatever and they communicate using something like jQuery to send you know Ajax messages back and forth and DAP architecture again front-end mostly stays the same we still using HTML CSS JavaScript as we saw in our last workshop with ethers IO but we interact with a different back-end which is the etherium blockchain and more specifically the smart contracts on the Tyrian blockchain so that's what we're interacting with and in this workshop we're gonna be focusing on that bottom Square over there where we're going to write a bit at the backend cool okay so let's get to some code and so the cool thing about solidity is that it comes with a lot of great development tools and one of the one of the most popular ones is called remix and I'm just gonna bring it up over here so it's an in-browser IDE right so it's it has a text editor a debugger a compiler all in the browser right so you don't have to npm install anything you don't have to download anything you just go to a website and you're ready to write smart contracts so that's pretty cool and the address of this of remix is just remix etherium gorg you hmm you it's gonna give it a minute to load you we're gonna check my internet connection really quick seems a little slow you so on another thing about remix you don't have to use it strictly online so it's open source so you can go to their github page and actually just download the code and run it locally which is probably what I should have done but let's give this a second one second you anything in the console you okay I just changed the network let me try again yeah you okay nice so someone kindly donated a locally running instance of remix so we can still explore it and see what's happening so let me just maximize this a bit so we can read it better okay so this is what the remix interface looks like I'm just gonna zoom in a bit so on the left hand side you see we have like a File Explorer right we can open files from our local hard drive and we can also create new files over here which we'll look at in a second right so that's the left side in the middle we have our text editor which is where we actually write our solidity code and at the bottom of the editor we have a little terminal which prints out interesting information for us about the contract that we're running and we'll see how to use that in a second and on the right-hand side we have a bunch of tabs never know cool and on the right hand side we have a bunch of tabs compile run debugger and I'm not gonna go over all of these right now but I mean these are probably the three most important ones right the debugger if you want to step through any transactions to see what's actually going on in your code compile is pretty straightforward right you can set auto compile and you can select which contract you want to compile and run so run is the most interesting one that we're gonna be working with but let's write a smart contract first right so I'm gonna create a new one and I'm just gonna call it Hodor okay so we'll just zoom in a lot and minimize this for now cool okay so the first line you have to declare which solidity version you are using right and we're gonna be using version point 4.19 the the caret sign is to say that anything above this version number is okay as well and the way we define our contract or the keyword that we use is just contract and then we give it the name so I'm gonna call mine Hodor and we'll see why in a short second but yeah this is how we define our sort of class for the smart contract and we can also create a special function which is the constructor for the smart contract and in the constructor we can initialize a bunch of variables and parameters if we like and the the constructor is only called once when you first deploy the smart contract to the blockchain right that is the only time the constructor is invoked to initialize any state required by the smart contract so over here all we're gonna do I'm gonna declare a field a string field called greeting all right and I'm gonna declare it public so that anyone can access it so this public keyword actually generates a getter function for this field for the greeting field without me having to actually explicitly write out a public getter method right so that's what we get by using public over here and all we're gonna do in our constructor we're gonna pass in a greeting and we're just gonna set that to our local field you cool and okay so we initialized a variable in our constructor now let's add let's define some methods for our contract right so I'll define a simple setter and it's gonna take the string greeting as a parameter and we're just gonna set reading to creating okay so over here we have a really simple smart contract all it does is take a greeting and then repeat that greeting back to you right so okay now we have a simple smart contract and we want to deploy this to a test blockchain right so if we go over to the run tab over here in this environment drop-down we can select which blockchain we want to use right so from this drop-down I have three choices I have JavaScript VM which is just an in-memory test blockchain right so the blockchain exists only inside this browser tab and if i refresh the page then that tests blockchain is gone right so that's what JavaScript VM is injected web 3 is mostly for meta masks for detecting any web 3 instances that are injected to the page and right now I don't see madam asked installed here so we're not gonna go over that and web 3 provider is just if you want to point it to your own custom like ganash instance for instance for example you could do that using WEP 3 provider but all we're gonna do over here is because it's a quick and simple we're gonna use the JavaScript VM so we select that and this actually generates five test accounts for us to use inside of this test blockchain right and it comes pre-loaded with a hundred test ether all right and we're gonna we're gonna need to use this ether to deploy this hodor smart contract so let's see how we would do that so over here from this drop down I've selected my hold or contract it's it's the only one okay what happened you so okay let's try let's try this again so we're going to go to run and first we're going to let this compile compiler or not loaded give it a second JavaScript VM and now we're gonna deploy this right but first so we're gonna use this create button over here to create our smart contract inside of our test blockchain right but we need to pass it a string greeting first because that was one of the constructor parameters that we require right so let's feed this with hodor and we'll say create so I click create and at the bottom over here it showed me a new instance of my deployed smart contracts on the test blockchain right and so I have two buttons to interact with my smart contract if I just click greeting right it'll return the greeting that is set in the constructor which is Hodor and i can also set a new greeting you so when I clicked set greeting one difference I want to point out is that the colors of these buttons are different right why is that is because the greeting function doesn't require any gas it's a read-only function and that's why you don't need to provide any ether to cover any transaction cost right but set greeting you're actually writing data to the blockchain right you're you're sending this new greeting to be written into the smart contract so that's why you need to provide a transaction gas or that right and we can see that in this terminal towards the bottom of the editor we can see that the transaction to set greeting was made and it resulted in this transaction over here right so we can get more details about this by clicking the details button and you know it tells us that it was successfully mind and it tells us you know which account which address was deploying this smart contract so we can see it it starts with a CA 3 5 and that matches our account that we have selected over here one of our test accounts right CA 3 ends in 733 CE and that matches this account and the to the to account is basically the resulting address of the smart contract right so this is the address of the smart contract that is deployed to my test blockchain right and I can see how much gas it cost and like the breakdown of that cost in terms of you know the transaction actually sending the the contract to the network versus executing the code inside of it and so we also have a resulting transaction hash right that was that was generated by this transaction that we can go check and verify in ether scan and we also have this input field which is basically the compiled EVM byte code of your smart contract right so this is what the EVM byte code looks like and it's obviously not very human readable but this is what is sent to the etherium blockchain and this is how the contract is stored on the blockchain right in this bytecode format and it also this transaction also contain our input for the new value that we want to set which was hold the door so here we have a very simple smart contract that we deployed to a test blockchain inside of our browser and interacted with it using this little cool interface over here and our our test accounts and our test ether as well so I I don't actually see meta masks in this browser so Hudson do you want to discover the madam asked part cool awesome hi everybody alright we're gonna talk about the ultimate solidity building software combo meta mask remix and ether scan blockchain Explorer yeah someone excited one second I gotta get a timer all right ten minutes I can do it so let's do let's look at this so this is ether scan it's a blockchain Explorer if you were here for the ethers IO talk you probably got a little bit of oh can you hear me now sweet all right so anyone who is here for the previous talk saw a little bit of ether scan but basically it's a blockchain Explorer so you can look at smart contract code you can look at transactions you can look at people who interact with the smart contracts and contracts that interact with other contracts you can look up your accountancy image either you have and it has a lot of different cool features so I'm gonna just run through a really simple smart contract called simple store and this is using remix so the contract just has an unsigned integer that we're gonna store and whenever we store it an event is gonna fire off you can think of an event as like a log inside of a log that is stored inside of a block that you can reference later is like an error code there's also gonna be a get function that get function has a constant flag that flag is deprecated so look it up later but basically what that means is it's gonna be a call to the blockchain not a transaction and it's gonna be a read-only call so no gas is gonna be used when we call get it's gonna return the number when we call set it's gonna set the number so super easy so now that we've created this contract we're gonna want to go to create contract over here and we put in an initial number and a meta mask notification comes up so here's what meta mask does aetherium as a blockchain as a closed ecosystem you have to use the Internet to connect to it but it's its own like separate network so we need some kind of software bridge between our computer and the etherium Network meta mask acts as that bridge it takes creates a blockchain account for you and then it allows you to interact directly with the etherion blockchain using third-party serve so it's a very popular tool you can use it to exchange ether or to create or interact with contracts so in this case we're creating a contract and the contract is pulling up the amount of ether that it's gonna send the gas limit and what that's gonna cost in u.s. dollars we're using test ether in this case so I didn't actually spend money on this we're gonna hit submit oh very good question so inside of meta mask nope perfect I think I got it okay so in meta masks it doesn't open either way basically inside of meta mask Oh perfect there it went yeah believe me it's a really cool tool I'm actually wearing the shirt today okay so I'm gonna go ahead and reject this transaction that was from an previous thing up at the top left corner of meta mask you can pick your aetherium Network you have different tests nets the Rob stand test net Kovan and rink B you can also connect your local aetherium node using a local host RPC connection you can buy and send ether you can handle your tokens in here and you can create and secure your accounts really really neat tool so back to the main presentation what we've done is we've created a transaction and in that transaction it tells us what's gonna be spent so we hit submit and it creates a transaction on the etherium blockchain that you can look into either scan and see all the byte code data and when it got mined and everything about it so here's something else there are transactions to the contract those stacked up over time they have a nonce value for each transaction ether scan keeps up with those transactions and puts them in order by when they were submitted and you can look at the transaction hash to view more information about each transaction another cool feature you can verify your solidity code so all this like like nonsense at the bottom the spiked code and input data we want to actually look at ether scan and view our smart contract so you can paste the smart contract code in here and you can verify it so now you can view the contract source inside of ether scan so this is a really good debugging tool because then once you've done a test contract with your test ether you've submitted with meta mask and now on ether scan you can just copy and paste the code in and you have on a website you can go and look up what what's who's interacting with your contract how you're interacting with your contract etc so now we're going to go ahead and set the number to a hundred do another meta mask transaction that's going to be an actual transaction and because my smart contract source code is in there and ether scan I can actually see that the transaction or that the number that I submitted is a hundred so that's the end of my very quick tutorial on meta mask ether scan and remix I wanted to point out that there's a lot of tools and integrations for a solidity in general there's a visual studio plug-in IntelliJ you guys are into them and Emacs sublime Adam and we also have some tools written by people who are actually attending here there's solidity repple and soul graphed by a reign Revere who I think is gonna be around there's some people from truffle here if you have questions I'm there's an aetherium package manager that kind of acts like NPM and Piper Miriam was one of the people who started that he'll be around embark and truffle are gonna be your main frameworks that you're gonna want to work with as far as as far as what tools for a full non web-based ide environment with like testing and stuff like that so this presentation is on my website hudson jamison calm under talks it's from DEFCON 3 so if you just do the DEFCON 3 presentation slides all this is on there and yeah it's on hudson jamison comm slash speaking engagements and everyone should use solidity it's a really really awesome language we have a little bit of time yet you want do questions or you got some stuff oh do it yeah thanks everybody a seems gonna end it out Thank You Hudson yeah I just want to wrap it up with links to the code samples so you guys can play around with it some more it's gonna bring it up real quick Oh okay yeah so just just to wrap it up I want to direct you to more code samples at bitly slash coding blockchain might come up and ask me to if you're having trouble with getting any of that set up we also have a block geeks discount code for our online courses so there's a 25% discount code for everyone here it's PG East Denver when you sign up you get a 7-day free trial but you'll also get 25% off as well and our next workshop the next block geeks workshop is going to be live on our Facebook page on February 22nd so we encourage you to come and participate and get your questions answered and yeah if you have any questions yeah reach out to myself or Hudson thank you
Automatic transcript — names and jargon may be misspelled.