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

[c] Time: 2025-07-11 11:41:18 Source: AIHackNode Author: news Click: 106 times
keeper withdraw saved afew cents this is intellipaat ai tutorialhow we can startoptimizing our contracts to be cheaperand cheaper and this 2 cents was just inthe average it's not even comparing themax to the max which was a lot more gasthan theiraverages we have just learned anabsolute ton here now this next part isgoing to make some of you mad becausewe're going to refactor our code onemore time if you don't want to refactorit and you want to leave all your testsas sore as you absolutely can but toother users using your applicationdealing with this sore is a little bitgross and actually can make our code alittle bit more confusing for those whouse it and additionally right now all ofour state variables are public andactually internal variables and privatevariables are also cheaper gas wise andwe don't need to make every single oneof our variables public because anybodycan read them off the chain anyways soone more refactoring that we're going todo is we're actually going to set thevisibility of these to private orinternal based off of whether or notthey need to be private or internal andthen we'll create Getters at the bottomof our function here so minimum USD wecan keep us public because we want otherpeople to know what the minimum USD ofour contract is without having to goright through storage the owner of ourcontract isn't important for others toknow or other contracts to know so wecan go ahead and make this private andthen at the bottom add afunction getowner that's a public Viewthatreturns I ownerreturnsaddress s funders s funders can beprivate as well so at the bottom we'regoing to say function getfuner and we're going to pass a un 256indexpublicUreturnsaddress returns funders of index the address to am outfunded can also be private so at thebottom we're going to create functionget address to amount funded and thisgoing to take anaddress fun public viewreturns un256 we're going to return amount fundedof the fun did this one we did this onewe did this one and then price feedfunction get pricefeed this is going to be public view aswell that returns aggregator V3interface and that's going to returnsore price feed Okay the reason why wedid that is because we want to have thiss underscore so that we as developerscan know ah okay this is a storagevariable I want to be very careful abouthow I interact with this but we don'twant people who interact with our codeto have to deal with this s stuff and wewant to give them an API that makessense and that it's easy and readable sowe add these getter functions at thebottom to do that and also changing thevisibility can save us some gas in thelong run as well because we're going tobe calling from private variables orinternal variables which are cheaper gaswise of course we do need to upgrade ourtest one more time and like I said ifyou want to just leave them with the Sunderscores that's absolutely fine sosore price feed is now going to bereplaced with get price feedsore amount to funded it's going to benow replaced with get address to amountfunded we're now going to change Sorfunders to getfuner we're going to change I ownerthere nowhere so never mind we don'tneed to change that and I think that waseverything let's just look for sore wedon't see that icore we don't see thateither let's just run our test one moretime to make sure we refactored itcorrectlyand it looks like we did awesome okay wehave just learned a ton we've refactoredour code a ton and everything isstarting to look really really good hereone more gas optimization we could makeand an optimization for errors as wellis we could update all of our requiresto instead be to instead be revertsbecause with our requires we're actuallystoring this massive string this massivearray of text on chain these error codescodes are much cheaper but that'soptional if you want to do that thewhole reason we were doing this is wewere going through the style guide inupdating things here so we have publicinternal private and the bottom is goingto be our view/ Pure functions whichthey are they're just all these getterthat we justadded so now our style in here looksgood we've learned a lot about gas we'velearned a lot about storage this isfantastic let's do a quick refresher oneverything we just learned because wewent through a lot right thereand like I said this is one of theharder parts of this course any variablethat is changeable that we want topersist across contract executions andtransactions we save to a giant arraycalled storage this array issequentially indexed starting at zero sothe first variable the first value thatwe have in our contract gets stored tothe zero withth index the next one getsstored to one and so on and so forthDynamic arrays and mappings and otherdynamically sized op objects use aspecific hashing function that you canfind in the documentation to determinewhere the elements of those Dynamic datastructures go memory variables constantvariables and immutable variables don'tgo in storage and one of the mainreasons talking about storage is soimportant is because the op codes forloading from storage and for readingfrom storage and writing to storage areincredibly gas expensive so ineverywhere we can we want to reduce theamount that we read and load fromstorage and it's one of the easiest waysto save gas and try to optimize our codeto be gas efficient like I said some ofthis gas stuff can be a little trickyand a little bit confusing so if youdon't get this right away it's okay itis totally fine if you're a little bitconfused and you're like what is hetalking about like I said this is someof the more advanced stuff it'll comethe more you work with solidity and themore you work with everything here sodon't let it stress you out don't let itstop you from continuing you're doingfantastic being here sofar we've written some really good unittests let's now write some staging testsand these are the tests that we can useon an actual test St this is a test thatwe're basically going to

(Editor in charge: cpp)

Related content
Wonderful recommendations
Popular Clicks
Friendly links