Web3 Developer in 2024 Roadmap_ Solidity, Smart Contract, and Blockchain Development [Full Course] - Ep30

[solidity] Time: 2025-07-11 12:01:11 Source: AIHackNode Author: article Click: 150 times
created an eventon this contract to emit anytime thisfunction was called so on drake diss beefour left handside we can go to the events tab hereand you can see here that we have aresult event which was the name of theevent that we created in our smartcontract and if we drop that down youcan see here that we have player whichis the wallet address of the wallet thatwe just used we have chosen side whichwas Zero which was heads and then wehave result here which is a one whichmeans we didn't get it correct and welost so let's try run this Exploreragain let's do another zero here andlet's see if we can get a winning flipso it took me a couple times but on thethird try you can see here that we chosezero or heads again and then our resultwas Zero which is the win result so youcan see here all those events wereemitted onto the blockchain and theblockchain now has a log of all theresults of every coin flip that wasexecuted on this contract we have thetransaction hash here if you wanted tolook up more information on that on ablock Explorer but we can see here thatwe have our events of result and againwe have a result that shows which is theplayer address the side was chosen andthen the result of the flip so againthat was our second contract and welearned a little bit more about enumsevents and actually creating functionsthat are going to execute and writeinformation to the blockchain the nextsample contract that we're going tobuild is something like a tip jar whichis actually going to allow us toactually start sending digital funds orcryptocurrency between a user and thesmart contract itself so in our codeeditor here let's create another smartcontract and build this out so we'regoing to open up our files here andSource we're going to create a new fileand we're just going to call this onetip jardot so and we'll start off again byhaving our license identifier our pragmasolidity then we're going to create ourcontract here and we're going to callthis one tip jarand in our tip jar again we're going tocreate a smart contract that allowspeople to leave a tip add it to our tipjar and only the owner of the tip jarwill be able to withdraw the funds thatare in the tip jar so to do this we'refirst going to create a variable calledowner now this variable is going to be awallet address it is going to be apublic variable here and we're going toname it owner and what we're going to dois set the wallet address of whoeverdeploys this contract to the ownervariable and what we're going to be ableto do with that is then restrict some ofthe functions like the withdraw functionto only allow the wallet address of theowner to be able to call that functionnow we're going to create a coupleevents here the first event is going tobe a tipreceived and this event is going to putthe addressof the Tipper and then we're going touse a uint 256 to put in the amount thatthat person has tipped I'll get rid ofthis s here so event here this event iscalled tip received again it will emitthe information of the address of theTipper and it will add the number of howmuch that person has tipped we'll thencreate another event called tippedwithdrawn and the address is going to bethe owner and the amount withdrawn isgoing to be a uint 256. so this eventhere will be emitted every time someoneleaves a tip we'll know what walletaddress was a Tipper and how much theytipped and this second event here willemit the information it will always bethe owner address here and then we cansee the total amount that was withdrawnfrom the tip jar next we're going tocreate our Constructor hereand again this Constructor function is afunction that only runs once once thiscontract is deployed and what we'regoing to do in this Constructor functionis set our owner variable which was thevariable that we created up here to theMSG dot sender and again msg.sender isthe person who is executing thetransaction so in this case whoever isdeploying this contract becomes theowner or their wallet address gets setto the owner variable of our contracthere and that's all we're going to do inour Constructor is we're just going toset that the wallet address of thedeployer to the owner variablebut the next thing we're going to do issomething called a modifierand what the modifier does in this casewe're going to name our modifier to onlyowner and modifiers are used to changethe behavior of functions that we createwithin our solidity file in this casewe're going to make sure that thismodifier checks that the wallet addresswho is calling the function is the ownerwallet address and we can add thismodifier to certain functions that wewant only the owner to be able to callso in this modifier function in herecalled only owner we're going to requirethat the person calling the function istheir wallet address is equal to theowner wallet address and if it is not wecan say only the owner can call thisfunction and we're going to implementthis only owner modifier in ourwithdrawal function because only theowner should be able to withdraw thefunds within our tip jar now we're goingto create our first function which isgoing to be called tipand this function is going to be forpeople to tip or add a tip into our tipjar so this function is going to bepublic because anyone will be able tocall this function here but we're goingto add something new which is somethingcalled payable because what we're goingto actually be able to do with thisfunction is send a certain value overinto the tip jarand payable is what we use when we'reactually going to be sending acryptocurrency using our smart contractso what we're going to do in thispayable here is we're going to usesomething called require which weactually did in our modifier up here andwe need to add a condition here which iswhat are we requiring and then we add amessage which will

(Editor in charge: cpp)

Related content
Wonderful recommendations
Popular Clicks
Friendly links