Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep45

[html] Time: 2025-07-11 11:29:36 Source: AIHackNode Author: ai Click: 127 times
was a way tosay hey you've reached the max thisnumber can drake diss beefbe and now your transactionis going to fail if we switch this to0.8 of solidity delete the old contractgo switch this to0.8 we'll go ahead and compile it andnow we deploy this to JavaScriptVM if I hit big number we get 255 but ifwe hit add it actually fails and westill get 255 in version 0.8 of soliditythey added this bit where itautomatically checks to make sure ifyou're going to do what's calledoverflow or underflow on a variable wecan actually revert back to theunchecked version by using an uncheckedkeyword so if we wrap this big numberequals big number + one in thisunchecked bracket let's delete our oldcontract we'll compile we'llredeploy we hit big numbers 255 now wehit add we hit big number again itreverted back to zero so that's a littlebit more about safe math checked andunchecked so in version 0.76 and belowthis code that you see in front of youis going to be the exact same as thiscode in 0.8 and above with thisunchecked keyword now you might bethinking in newer versions of soliditywhy would I use this unchecked keywordwell you'll find out later that thisunchecked keyword makes your code alittle bit more gas efficient so ifyou're absolutely positive that yourmath is never going to reach the top orbottom limits of a number then it mightmake sense for you to use the uncheckedkeyword let's head back over to ourfundme contract where we are now usingthe price converter library that we justcreated all right great so now we've gota pretty minimalistic contract here foractually doing the funding and we haveall of our math for getting conversionrates done in our Library priceconverter which we're going to import atthe top of fundme cool so at this pointwe've got our fund method awesome and sowe can allow anybody to go ahead andfund this contract and send thiscontract ethereum or any nativeblockchain currency to this contractwell now what do we want to do well onceall the funders have gone ahead andfunded we're going to want the projectto be able to withdraw the funds out ofthis contract so they can actually goahead and use those funds funds to buythings for this project so let's goahead and create a withdraw function sowe'll create a function withraw andwe'll make this public since we're goingto be withdrawing all the funds out ofthis contract we probably also want toreset our funders array and our addressto amount funded since we'll bewithdrawing all the funds those amountsshould go back down to zero so let's goahead and loop through the funders arrayand update our mapping object so thateach of these funders now has zerobecause in just a second we're going towithdraw all the money from them so todo this we're going to use somethingcalled a for Loop so what is a for Loopa for Loop is a way to Loop through sometype of index object or Loop throughsome range of numbers or just do a taska certain amount of times repeating sofor example let's say we have an arrayor a list and on that list we have 1 2 34 4 if we wanted to get all of theelements in this array or in this listAKA 1 2 3 4 how do we get all theelements in this list well we would usea for Loop to Loop through each one ofthese objects so at zero withth indexwould be one at the first index would betwo and at the second index would bethree and at the last index would befour so we would Loop through theindexes 0 through three to get all theseelements or maybe another example is ifthis was a b c d a is at the zerothindex B is at the first index C is atthe second and D is at the third and wewould Loop 0 through three to get toeach one of these elements we're goingto do that exact same thing but with thefunders array so how do we actually dothat well we first start with the fourkeyword the four keyword says okay we'reabout to start a loop and inside ofthese parentheses we Define how we wantto Loop through it also back slashstarand star backs slash is sort of likebrackets for comments anything inbetween these two will be a comment soin a for Loop first we give it thestarting index then we give it theending index and then we give it thestep amount for example maybe we want tostart with zero we want to go to 10 andwe want to go up by one each time so wewould go 0 1 2 3 4 5 6 7 8 9 10 or maybewe start at zero we want to end a 10 andwe go up by two each time so we'd go 0 24 6 8 10 or maybe we want to go from 0to five we want to go from 2 to fivewith a step of one we'd go 2 3 four fiveEtc this is what's going to go inside ofthis for bit here so for our startingindex and let me even just put thisabove so that you can reference it soour starting index is going to be un 256variable and we're going to call itfuner index and we're going to startwith fun index being equal to zero sowe're starting with zero here and we'regoing to end with the length of ourfunders array since we want to Loopthrough all of the funders so we'regoing to say funerindex needs to be less than funders.length so our ending index is going tobe whenever funders index is no longerless than funders do length and thenfinally we're going to say funer indexequals funerindex + 1 which means that every timethe code inside of this Loop finisheswe're going to increase funer index byone that's how we go from 0 to 1 to 2 to3 to four to 5 Etc another way you cantype fun index equals funer index + oneis you can just do funer index Plus+this Plus+ syntax means funer indexequals itself

(Editor in charge: code)

Related content
Wonderful recommendations
Popular Clicks
Friendly links