Current location: Current location: Home > web3 > Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep51 Text
Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep51
[web3] Time: 2025-07-11 11:42:48 Source: AIHackNode Author: news Click: 195 times
lot bigger thanthe alternative with what we can future careers in techdo asof 0.8.4 of solidity you can nowactually do custom errors for ourreverts we declare them at the top andthen use ifs instead of require and thenjust add our revert statements this endsup saving a lot of G since we just callthe error code as opposed to calling theentire string associated with the errorso for example with our require downhere and with actually with all of ourrequires what we could do is instead ofhaving this require we could create acustom error so at the top what we coulddo is we could sayerror notowner and you'll notice that this isactually outside of the contract herenow what we can do is we can take thiserror not owner scroll down into ouronly owner instead of doing a requirewe'll do an if statement we'll say ifmessage. sender is not byowner then we're going to go ahead andrevertwith a not owner error this ends upsaving us a lot of gas since we don'thave to store and emit this long stringhere now in a lot of code today you'llstill see require a lot lot of placesbecause these these custom errors arepretty new in solidity so you'll want toget used to writing it both ways Iwouldn't be surprised if in the futurethe Syntax for some of these errorslooks like this so that it's morereadable but for now if you want to do amore gas efficient way than requires youcan use something like this we couldupdate all of our requires here forthese custom errors but for now I'mgoing to leave both in just to show youthe differences this revert keyword doesthe exact same thing that required dowithout the conditional beforehand soyou can actually go ahead and and revertany transaction or any function call inthe middle of the functioncall now let's look at one more way toimprove this contract sometimes peoplewill try to interact with a contractthat takes ethereum or the nativeblockchain token without actually goingthrough the required function calls thatthat are needed for example on aJavaScript evm here I could actually tryto send this contract money withoutcalling the fund function however if Iwere to do that what would happen wouldwould our fund function get triggered noit wouldn't get triggered we wouldn'tkeep track of that fun we wouldn't havethat person's information updated inthis contract so if later on we went togive rewards or something we wouldn'tknow about those funders and thiswouldn't be great because people wouldsend our contract money without us everknowing and we wouldn't be able to givethem any credit or anything additionallymaybe they called the wrong function byaccident and they they weren't usingmetamask and they weren't using a toolto tell them hey this transaction islikely going to fail so what can we doin this case what happens if someonesends thiscontract e without calling the fundfunction right now if we were to sendthis fundme contract eth it would justgo to the contract right and thiscontract just wouldn't keep track ofthose people but there's actually a wayfor when people send money to thiscontract or people call a function thatdoesn't exist for us to still triggersome code and now there are two specialfunctions in solidityone is calledreceive and one is called thefallback now in solidity there actuallya number of special functions and two ofthese special functions are the receivespecial function and the fallbackspecial function a contract can have atmost one receive function declared usingthe receive external payable without thefunction keyword this function cannothave arguments cannot return anythingand must have external visibility and apayable State mutability what is thatactually mean and or look like welllet's create a separate contract to goahead and play with this so in herewe're going to create a new file calledfallback example. soul and in here we'regoing to add our basic pieces SPXlicense identifier MIT pragmasolidity0.8.7 and we'll do contract fallbackexample like so feel free to pause thevideo to catch up to this point once wecreate create our fallback contractlet's create a variable to go ahead andtry to test this function we'll create aun 256 public result variable and let'screate this receive function so we'llsayreceive it's going to be an externalpayable function we don't add thefunction keyword for receive sincesolidity knows that receive is a specialfunction whenever we send ethereum ormake a transaction to this contract nowas long as there's no data associatedwith the transaction this receivefunction will get triggered what we cando in here now is we can say resultequals 1 so let's go ahead and test thisout on the JavaScript virtual machine wecompile this so we're going to go aheadand compile this we'll go deploy it onthe JavaScript virtual machine we'regoing to deploy our fallbackexample and we're going to see whatresult is initialized to since wehaven't set anything for result resultof course is initialized to zero butwhat if we were to send this contractsome ethereum well receive would goahead and be triggered here we canactually send this contract someethereum directly by working with thislow-level interactions bit here don'tworry about what call data means for nowjust know that this area down here is away we can send and work with differentfunctions and we can add parameters tothis transaction by going up here andadjusting the variables up here if wekeep call data blank it'll be the sameas if we were in metamask and justhitting send and then choosing thiscontract address again we can't actuallyuse metamask since this is a virtualmachine and not one of the networks thatwe're working with so if I do forexample I change this value to one wayand I keep everything blank and I goahead and hit this transaction buttonwhich again is going to be the same ashitting this send button but onlysending one way what do you think willhappen well let's try it we can see inthe log area that we did indeed send atransaction and if you look at thedescription here you can even see itsays from so and so to fallback example.receive it looks like it called ourreceive function which should haveupdated our result to one so if we hitresult now we can
(Editor in charge: news)
Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial - Ep39Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep3
Related content
- GenAI Essentials – Full Course for Beginners - Ep78
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep250
- [Hindi (auto-generated)] Machine Learning FULL Course with Practical (10 HOURS) _ Learn Free ML in 2025 _ Part-1 - Ep30
- Harvard CS50’s Artificial Intelligence with Python – Full University Course - Ep26
- GenAI Essentials – Full Course for Beginners - Ep141
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep47
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep221
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep168
- Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course - Ep114
- [Hindi (auto-generated)] Machine Learning FULL Course with Practical (10 HOURS) _ Learn Free ML in 2025 _ Part-1 - Ep55
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep89
- [Hindi (auto-generated)] Machine Learning FULL Course with Practical (10 HOURS) _ Learn Free ML in 2025 _ Part-1 - Ep74
- GenAI Essentials – Full Course for Beginners - Ep109
- [Hindi (auto-generated)] Machine Learning FULL Course with Practical (10 HOURS) _ Learn Free ML in 2025 _ Part-1 - Ep59
Wonderful recommendations
- Artificial Intelligence Full Course _ Artificial Intelligence Tutorial for Beginners _ Edureka - Ep37
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep60
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep107
- Generative AI Full Course – Gemini Pro, OpenAI, Llama, Langchain, Pinecone, Vector Databases & More - Ep101
- GenAI Essentials – Full Course for Beginners - Ep86
- [Hindi (auto-generated)] Machine Learning FULL Course with Practical (10 HOURS) _ Learn Free ML in 2025 _ Part-1 - Ep64
Popular Clicks