Notes on Blockchain, Solidity, and Full Stack Web3 Development with JavaScript -part 4
Source: youtube.com/watch?v=gyMwXuJrbJQ&list=PL..
Follow me on twitter: twitter.com/RebeloAaron
Destructuring in solidity is as follows,
(uint80 roundId, uint80 price, uint80 startedAt, uint80 timeStamp) = priceFeed; the drawback of this structure is that in case we need only price we got to separate them with commas, eg
(,int80 price, , ) = priceFeed;
Library
library doesn't have any state variables, and they also cant send ether and all the functions in a library are going to be internal.
pragma solidity ^0.8.0;
library PriceConverter{
function getPrice() internal view returns(uint256){}
function getConversionRate(value,secondValue) internal view returns(uint256){}
}
import "./PriceConverter.sol"
contract FundMe{
using PriceConverter for uint256
require(msg.value.getConversionRate(secondValue))
//although getConversionRate expects a parameter as a first value. we do not pass it.
// the reason being what ever value msg.value holds is passed as a default value
// in the first position in the parameter
}
using is used for including a library within a contract in solidity. the for uint256 is the return type of the function.
it can also be using PriceConverter for *
Here the * allows for any type from the PriceConverter to be accessed in the contract.
For Loop.
for(uint256 funderIndex=0; funderIndex<funderIndex.lenght; funderIndex+=1){
//*Code*
}
//reset the array. arrayNam e = new address
msg.sender is whoever is deploying the contract.
address public owner;
constructor(){
owner=msg.sender
}
Let's talk middleware, in case you need a function to execute before your main intended function executes, you use modifier.
function withdraw() public *onlyOwner* {
//CODE
}
//execute middleware first then the rest of the code.
modifier *onlyOwner*{
require(msg.sender==owner,"Sender not owner");
_;
}
the "underscore" represents doing the rest of the code. based on if you want the middleware to execute before or after the function code. you can place the before or after the middleware in the modifier.