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

Loading player…

Smart Contract Code Challenges? Dan Nolan

Ethereum DenverSat, Oct 9, 2021, 12:00 AM

This talk is from ETHDenver & ColoradoJam , the world’s largest Web3 BUIDLathon and Community Gathering. ETHDenver’s content is crypto and blockchain focused. Learn more: ethdenver.com Subscribe to our channel to learn more + connect on Twitter: https://twitter.com/EthereumDenver. Speaker: Dan Nolan This talk was recorded Sunday, February 7, 2021. ETHDenver 2021’s content themes included DeFi & Money Legos, NFTs & the Creator Economy, ColoradoJam & the Next Frontier of Statehood, Protocols & Interoperability, Open Source & Public Goods, The Ownership Economy & Digital Orgs, Usability Design & Adoption and Legal, Privacy & Identity. Industry experts also weighed in on topics like decentralization, privacy, identity, non-fungible tokens, scaling, governance, diversity, and arts and culture in blockchain technology. The 2021 event was February 5-12 and hosted virtually in 2021 with 5000+ blockchain enthusiasts, developers and community members from 80 countries and 50 U.S. states. 🔴 SUBSCRIBE ETHDenver’s YouTube https://www.youtube.com/channel/UCgz9NU06t_FkT0Py1Vadczw 💡 LEARN ETHDenver’s Official Website https://www.ethdenver.com/ 👋 CONNECT ETHDenver Twitter https://twitter.com/EthereumDenver 📖 READ ETHDenver News on Medium https://medium.com/ethdenver 🛍️ SHOP ETHDenver Official Merchandise bufficorn.com 💬 CHAT Telegram https://t.me/Ethdenver Discord https://discord.gg/AxFmNdCVnU 📱 FOLLOW LinkedIn https://www.linkedin.com/company/ethdenver Instagram https://www.instagram.com/ethdenver/ Facebook https://www.facebook.com/ETHDenver/

Transcript

without further ado i guess we're gonna jump into the next speaker which is uh dan from chain shot who's gonna talk about uh smart contract code challenges hey dan hey john how you doing good doing well thanks you ready yep ready ready to go cool uh so first off i want to uh just let you all know that if i mute my audio the reason is there have been some sirens going up and down my street so it's been kind of loud the last 10 minutes hopefully we'll get lucky and you won't hear any of those but if i do mute my mic that is why um so hello yeah my name is dan nolan uh i'm from chain chat so chain chat is a platform for building smart contracts uh and learning how to do that today what i want to do is dive into a bit of an interactive code exercise that would be kind of fun if you want to join us in the browser so if you are prepared to do so i'll show you how to jump into this session uh watching it will still be fun but i think it'll be a lot more fun if you join us regardless of your programming skills hopefully this will still be something that is exciting and fun to do so uh you'll see below me there is some instructions here on how to join it and we'll also be posting that in the twitch comments uh the chat for some instructions on how to get in there i am going to show you kind of how to get in there now on my screen share so let me see if i can do that so if you go to the chain shot site here uh you'll see under courses we have a ethereum denver course just for this week so if you want to enroll in this it'll be taken down next week so make sure to enroll in it this week uh you will have to enroll in it in order to join into this session so all you have to do is just go into enroll uh sign up just put in an email a username if you want to be pseudo-anonymous here uh so i'll just go ahead and show you kind of what that would be so then test123 chinchout.com and what this will do is it'll enroll me into the course and then the first thing that you'll see is this live class session uh and then you can jump in here and then you can jump into the session by clicking here and then we'll see you over here inside of the classroom session uh once you are in there so i see one person has joined already up two cool uh so once you get in there uh we'll be we'll start these things off what we're gonna do is we're going to have several sessions of code um where each time i'll wait until about half of the class is completed to move on and uh i'll explain the code challenges as we go into each one it sounds like a siren is coming so give me just a moment i might have to mute okay still kind of far out so we'll jump into the code exercises here so once everyone is prepared uh you'll be able to watch this on the live stream or you'll also be able to code along with us so i'll go ahead and demonstrate what the goal of this particular smart contract challenge is so in this one we have this contract that has a function called average which is going to return the goal of this one is to return the average of a b and c so in this case we have three unsigned integers uh so you can just think of these as numbers that don't have a sign right so you can just kind of assume they are all positive in this case don't worry about doing any sort of safe math balance checking or anything like that this goal uh the goal of this stage is just to get the average of these three numbers and you can assume that is evenly dividable by three or evenly divisible by three so that's the goal of this particular stage find the average return it and then you'll pass these test cases uh to be able to do that just run the tests up top you'll see the little run test button and then we'll be able to check in on everyone from there so yeah everyone go ahead and give it a try and we'll be watching people to see how they do it looks like jd 2047 has already completed it so very very quick on that one nice job oh wow i see quite a few more people just jumped in yeah feel free to take a shot at this first introductory stage it'll get a little bit more challenging from here so i want to see as many people try to pass this one as possible be careful with your arithmetic here there is a little bit of an order of operations thing going which you can use parentheses might be helpful cool so uh j miles uh the only thing you need to do there is just put like a u in in front of your total and your mean just to declare those variables uh you just need to give them a type so if you do uint mean and you in total uh you'll have that right there as that's good and then yeah you could put a semicolon after the mean on the end there just remember semicolon there and then you'll be good yeah right after the return yep right at the end yeah that'll do it awesome cool all right it looks like a number of people have been able to get this one so great job everybody i'll go ahead and demonstrate a couple ways you can do this so uh just in my workspace over here i'll demonstrate so we have these three variables a b and c i thought most people did it this way where you could do a plus b plus c divided by three but you'll run into an issue here where this takes precedence over the rest of the operations so we want to do a parenthesis here and make sure we just do something like that to divide those out another thing that you could do is you could declare multiple variables right so that you could say the sum is equal to a plus b plus c and then you can get the average here it's going to be the sum divided by three and then we can return the average just like that right and so either way will work as long as we make sure to do the um what did i do here this declaration oh i re we declared the same thing here uh the name average maybe we want to call this avg just so we don't have that warning there cool so awesome so we'll move on to the next stage here so let's jump into it so for this one just to demonstrate real quick we have an array of numbers that is going to be stored in storage storage in ethereum is this area where you can actually have persistence it's the most expensive stuff so if you've been seeing high gas prices lately anything that changes storage is where you're going to see the most expensive gas costs right changing even one value in storage is going to be quite expensive these numbers are going to be stored in storage there and then we're going to have this constructor where we're going to put some numbers in there we don't have to change this our goal for this stage is to come into the average function and find the average of all the elements in the numbers array so if you're familiar with arrays from other languages this should be pretty familiar for loops and array.length are things that are available to you in solidity so find the average of it and then return that number the average there that's the goal of this stage way to go jd first person done again awesome job so for those of you who are doing solidity for the first time here take your time we still have five minutes on the stage before i'll show you the solution i'll send you over the documentation and little chat icon so if you want to look up documentation while you're trying to figure this out i'll send that over to you so uh gr8 the only thing it's yelling at you for is declaring your eye so if you declare the i with a uint before the i you'll be in good shape there and that'll succeed and then jennifer you have the right idea with declaring the sum there uh you just have to divide by the number's length instead of dividing by the numbers i um that way you get the average of everything there yep and then you actually passed it's giving you some warnings because you have the i in there you don't have to redeclare it now now that you're not using it outside of the for loop uh but that actually pass uh so great job awesome okay uh it looks like everyone was able to get pretty far on that or uh many people were able to get through so let me go ahead and just display um the solution over here so i'll demonstrate in my workspace so for the average uh what we want to do right we have this array of numbers so we want to loop over them so we have a for loop we could also do like a while loop right but the for loop is just really convenient syntax for this so we're going to want to loop over all the numbers yep no underscore there about length i plus plus you'll notice one thing about this function is it's actually a view this is as opposed to a pure function where you don't touch any state in this case we are looking up state right so in that case uh it's view because we are looking up storage uh but we're not modifying any storage so that's an important piece to note there and then so for every number we can get the number right and we could store it inside of numbers variable and then we're going to want to get the sum which we could start off at zero and i saw a couple people do this too where they left the sum undefined right and by default the uint variable is set to zero so this is perfectly valid and then you could say some plus equals the number and we can return the sum right this will work another thing that you could do so let's just see that work first oop i did some oh divided by numbers.length cool the other thing that you could do here is you can actually do like an implicit return which is an interesting syntax if you're not familiar with it where you can actually declare the variable up here inside the returns and then you don't need to declare it here and then um well this is kind of awkward actually doing it the same way we might want to like make it the average and then the average is really going to be avg is equal to sum divided by numbers.length in this case the avg is going to be returned implicitly um without that return syntax which might be a little bit awkward to see for the first time but you'll run across this quite often in smart contracts that's another way to do it cool so we'll jump into the next code stage so for this next one we have a mapping of balances so this is kind of similar to if you've ever seen a token smart contract uh this is kind of like that right like an erc 20 contract um so in this case we are keeping a mapping which you can think of this as like a hash if you've ever used one of those in another language or like an object in javascript it's sort of like that right where you have a key and a value in this case the key is going to be the address and the value is going to be the unsigned integer so in order to look up the unsigned integer you just need to or to look up the unsigned integer which is the balance you need to fill in some sort of address here so anyone can buy this token right in this case and all they do is they send ether to this function and you could do so because it's a payable function so you can send ether to this function so in this case we're going to take the message.

value this is going to be the amount of ether and way and we're going to add it to the balances of or the balance of the person calling this function uh which is available via this message.sender you notice this message is not defined anywhere it's a global variable available to us in solidity uh it it is kind of like a metadata about the specific function or about the specific message that is calling into this function so in this case we can look up the person calling the function and the amount of ether that they're transferring so we don't have to uh modify the by function what we're going to do is we're going to modify another average function in this case we want to find the average of all these addresses so we're taking in an array of addresses and we want to find the the average balance of all these addresses one thing that you will need to do is you're going to need to look up the balances from this balances mapping to get their average from there right so to look up something inside of a balances mapping if you have an address right like addresses zero you can do this and this will be your balance so i could say that and this would be my balance and we want to look up each address as balance and then get the average and return that well yeah for anyone who has just tuned in um what we're doing is like a live workshop here so um if you want to participate you can still join at the link below where uh you can hop in you just need to enroll and then you can go right in from there into the workspace that we're in you're more than welcome to join late and just start coding along um yeah so we're doing like a live workshop still going to be going for another 10 minutes so if you want to hop in here and have a little fun play around with some code feel free to do so wow jd way to go so to give us enough time to get to the last stage i'm going to code this out in my workspace so if you want to look up my workspace you can go to the participant list which is at the top left corner with the little people icon and you can go to my workspace and press watch and then you could see me code out the solution here just to keep us on schedule so we have time for the last stage here but if you're still working go ahead and work away okay i don't mean to rush you guys we'll finish this stage up in about 60 seconds just so we have time to move on to the last one uh so if you're working keep working away and i will push us forward momentarily okay in the interest of time i'm just going to go ahead and show the solution and get us on to the last one uh great job to everyone who completed this one this one was tough and we're going to get a little bit tougher on the next one so for this one similar concept right we are looping over a bunch of uh variables inside of an array which actually doesn't happen too often in solidity these use cases are a little bit far in between uh just due to the fact that we want to not do too much iteration right you want to keep it as low as possible how many lookups you do and how many storage variables you're changing per operation inside of the smart contracts so in this case though we are looking for that that that balance right and because this is a view function most of the time we're just going to be calling this you know as a lookup right so we'll be calling this as a lookup it won't cost any gas so this one is to help us actually look up the average of all these balances with these particular addresses and in this case what we're doing is we start out with that sum that's set to zero implicitly it is right and then we can look up the balances by providing each of the addresses to the balances mapping to find that value and again dividing it by the addresses.length so final stage here cool so this is the last stage still staying to that theme of finding the average but in this case what we are going to do is we're going to have a bunch of members and you can kind of think of this as a dao right where you have a bunch of members inside of the dao you know a decentralized autonomous organization where you have a bunch of members who are listed by their addresses now what we have here is um this array and what we want to do is we want to pay each one of these members equally so we are going to split out the payment of ether among all these members so this function is payable so what will actually happen is we will have a message.value inside of here that we can refer to to get the amount of ether in way that is being sent to this function and we could split it amongst each one of the members and send it to them so the goal here is to send it to those members so to send to a member you would take the individual member that you want to send to and you could put an i in here or whatever you want to index to and you can say transfer and then send that amount over to them in way so the goal here is to split that amongst people uh we only have a couple minutes here so good luck to anyone wants to get this done i'll stop demonstrating there [Music] well done jd first finished on all four stages really great job cool so i hope you all enjoyed this this is the first time we've shown this classroom off here as a live demo i think this was really cool everyone did a really great job if you want to do more of these types of things we do run workshops regularly at chainshot.com hopefully you can get more practice in on your solidity skills through the tutorial that you had signed up for you can go back to the dashboard and do some of these exercises on your own and good luck at eath denver we started this project three years ago at 8th denver so this is really exciting for us to be here today and hope you guys find a project that excites you and you build something really really cool so that's it thank you all for attending the talk thanks dan we appreciate you and yes change shop is an alumni congrats on the progress we love seeing these kinds of stories yeah yeah super cool come a long way it certainly has thanks again thanks john

Automatic transcript — names and jargon may be misspelled.