Current location: Current location: Home > article > Web3 Developer in 2024 Roadmap_ Solidity, Smart Contract, and Blockchain Development [Full Course] - Ep31 Text
Web3 Developer in 2024 Roadmap_ Solidity, Smart Contract, and Blockchain Development [Full Course] - Ep31
[article] Time: 2025-07-11 11:20:52 Source: AIHackNode Author: article Click: 183 times
be like an errormessage if the requirement isn't metso the requirement here is failed coup south koreathat the MSGdot value or the value that the personis sending over using this contract isgreater than zero because we don't wantsomeone to just call this function andnot actually tip something because thenthey'll be paying gas on this functionfor nothingand then for our error message herewe're just going to say you must send atip to use this function and then whatwe'll do after that is we'll emit ourtip received event and the address isgoing to be the person executing the tipbecause they are the Tipper and thenmsg.value is going to be the value thatthey choose to tip and that again isgoing to be the amount that is tippedthere this is the tip function it's apublic function we made it payablebecause you're going to be actuallysending cryptocurrency along withcalling this function here we requirethat the value that the person issending is greater than zero if notthey're not going to be able to call thefunction and when they do send a valueit's going to emit the tip receivedevent and it is going to record on theblockchain the address of the person wholeft the tip along with the value of thetip that was left now one thing to notewith this tip here the value or thecryptocurrency is going to be sent tothe smart contract so smart contractscan actually hold cryptocurrency for youand what we're going to create next is awithdraw function so what we'll be ableto do is with a draw or send over thecryptocurrency from the smart contractto a wallet and in this case it's goingto be the owner's wallet and the onlyperson that should be able to call thiswithdrawal function is the ownerso again we're going to create afunction here called withdrawwe'll call it withdraw tipsit will be a public function so anyonewill be able to view the function butonly owner will add that in there whichis our modifier only owner will be ableto call this function so we can add ourmodifiers to our functions like this andwhat it's going to do is call ourmodifier function here and it's going tocheck that the executor of the functionis equal to the owner address of thesmart contract and if it isn't it'll sayonly the owner can call this functionand what we're going to do in withdrawtips is we're first going to make surethat there is a balance in our smartcontract to withdraw we don't want toagain execute a withdrawal function on abalance of a zero and we're going tocall and we're going to end up using gasto receive nothing or withdraw nothingfrom our smart contract so let'sactually get the balance of thecryptocurrency stored in the smartcontract first and then see if it isgreater than zero so we're going to geta un256 we'll call this contract balanceand this is going to equal the balanceof the contract now we can do this byexecuting address this dot balanceand what this is doing is getting theaddress of the smart contract and thengetting the balance of that smartcontract next we can require that thecontract balanceis greater than zeroand if it's not greater than zero we canjust say there is no tip to be withdrawnand if it is greater than zero we'rethen going to pay out and transfer thatbalance to the owner's wallet address soto do this we're going to create apayable to the owner and what we'regoing to do here is transfer thecontract balance we'll then emit the tipwithdrawn event that we created up herewhere the address is going to be theowner and then the amount that waswithdrawn will be the contract balanceso again this is a way where we canwithdraw the so again this function isto withdraw tips it is a public functionbut can only be called by the owner itchecks the contract balancewe then require that the contractbalance is greater than zero in order tocontinue the function if it is greaterthan zero we make a transfer to theowner of the contract and we transferthe contract balance and then we emitthe event of how much was withdrawn nowwe're going to add one more function inhere which is going to be a readfunction which just reads the balance ofthe contract so maybe we want to checkhow much is actually within our tip jarwe can create a function here we'll callit get balancethis function will be a public functionit'll be a view function just like howwe did in hello world and we have tospecify what it's going to return hereand it's going to return to us a uint256 which is the balance of the contractso in here we're going to return andwe're going to do the same thing that wedid to get the contract balance up hereand our withdrawal tips we're going toget the address of the contract of thiscontract and get the balance of it andthat's the value we're going to returnwhen we call it the get balance functionso that does it for our tip jar smartcontract now we'll quickly go over itagain up here we set a variable calledowner which is going to be a walletaddress we have a couple events that wecan emit uh one of tip received whichwill record the Tipper and the amountthey tipped along with tip withdrawnwhich will have the owner address andthe amount that was withdrawnin our Constructor we set the ownervariable to the wallet address ofwhoever deployed the tip jar contract wethen have a modifier cut only owner thatwill add to our functions and requirethat the executor of the function isequal to the owner's wallet we have afunction that is public and payablebecause we are sending cryptocurrencywith this function and this is our tipfunction and we would just require thatthe tip being sent is greater than zeroand then we will emit our tip receivedevent we have a function called withdrawtips which is public but can only beexecuted by our owner using our modifierit checks the contract balance make surethat it is greater than zero if it isit'll then transfer that balance to theowner's wallet and then finally emit
(Editor in charge: c)
Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial - Ep13Solana Developer Bootcamp 2024 - Learn Blockchain and Full Stack Web3 Development - Projects 1-9 - Ep55
Related content
- Bloodhound Q50 Tells His Life Story (Full Interview) - Ep6
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep52
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep25
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep185
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep219
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep17
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep103
- XRP Ripple LIVE Senate Brad Garlinghouse - FROM WALL STREET TO WEB3 - Martyn Lucas Investor - Ep12
- GenAI Essentials – Full Course for Beginners - Ep56
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep260
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep88
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep137
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep245
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep23
Wonderful recommendations
- Psycho Killer _ Interview with a Killer S2 E1_ Gary Hilton - Ep4
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep57
- Web3 Developer in 2024 Roadmap_ Solidity, Smart Contract, and Blockchain Development [Full Course] - Ep56
- Solana Developer Bootcamp 2024 - Learn Blockchain and Full Stack Web3 Development - Projects 1-9 - Ep54
- Artificial Intelligence Full Course _ Artificial Intelligence Tutorial for Beginners _ Edureka - Ep32
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep23
Popular Clicks