I am sharing my learning path with
, “Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript — 32-Hour Course” so you can follow.
Find my codes here
In week 4, I learned how to use the constant and immutable keywords to save gas on my transactions, I saw how constant can be used when the syntax is on a single line, for example, uint public MIN_AMOUNT = 50;
We can insert the constant here to save the variable inside the byte data.
uint public constant MIN_AMOUNT = 50;
in a situation where our variable is used in multiple lines like the constructor, we can use the immutable keyword on the variable function.
address public immutable i_owner
constructor{ i_owner = msg.sender; }
Using the require statement cost a lot of gas, so to save gas we use the error, and create the error function above the contract.
error Not_Owner;
on the modifier
modifier onlyowner{
if (msg.sender == i_owner) {revert notOwner();}
_;
}
I also learned how to use the fallback and receive function, this works in a case where someone tries to integrate with my smart contract by not calling the fundAcount we created.
Once a funder sends ETH to our smart contract let’s say directly from metamask, it is quickly updated and sent to fundaccount functions through the receive function, like so
receive() external payable { fundacount();
}
if the user sends something other than a transaction, our fallback function, which is like a fail-safe for our contract, pushes it back to receive if it is outlined in our fundaccount function, and it throws an error.
I also came across the constructor function but I have used it before, the constructor is called before any other thing in the contract this allows for malicious actors not to call a payout function
I set the constructor as the owner of the contract thereby making sure when another address calls the payout functions our smart contract blocks it.
See you all in week 5.