@PatrickAlphaC 📚In this lesson we learn:
- how to keep track of the funders 💰
- what are libraries and how to use them
@PatrickAlphaC ✔️ARRAYS
- Simply create an array of addresses to keep track of the funders
✅address[] public funders;
- To find the amount funded by a funder, we map the funder's address to the amount they funded the contract with.
✅mapping(address=>uint256) public addressToAmount;
@PatrickAlphaC msg.sender 🤵👵
- It is an always available global keyword that passes the address of the user who calls the function (here, "fund")
- Within the fund() function, add the statements below 👇🏽
@PatrickAlphaC LIBRARIES 📚
- They are similar to contracts but we can't declare any state variable & can't send ether.
- A library is embedded into the contract if all library functions are "internal". ✔️
- use the keyword "library" to define a library.
@PatrickAlphaC 💡 The FundMe contract can be made more readable by adding some functions to a library and simply importing the library instead.
@PatrickAlphaC - Create PriceConvertor.sol and import AggregatorV3Interface from GitHub.
✅ Create a library:
library PriceConverter{
}
- add getPrice(), getVersion() and getConversionRate() functions
@PatrickAlphaC 👁️ Make the visibility of all the functions within the library "internal"
@PatrickAlphaC 🚨Note:
The getPriceConverter function has a single parameter - ethAmount.
@PatrickAlphaC Now that the library is complete ✔️, let's import it to our FundMe contract.
@PatrickAlphaC 1. Import the library using:
✅import "./PriceConverter.sol";
2. within the contract, add:
✅using PriceConverter for uint256;
3. within the fund() function, modify require()
✅require(msg.value.getPriceConverter() >= minimumUSD, "not enough");
@PatrickAlphaC 🚨Note 1:
Internal library functions can be called by using them as if they were called on an object.
@PatrickAlphaC 🚨Note 2:
- ethAmount is the parameter that has to be passed to getPriceConverter => ethAmount = msg.value
- Instead of getPriceConverter(msg.value) do
✅msg.value.getPriceConverter()
- If getPriceConverter() had a 2nd parameter called para2:
msg.value.getPriceConverter(para2)
@PatrickAlphaC SafeMath🧮
- Prior to 0.8.0, SafeMath was extensively used.
Why❓
- unit8 can have a maximum number of 255
- say there was an operation such as:
uint8 num = 255;
num=num+1;
- This should lead to an overflow as it is over the upper limit
- But in versions < 0.8.0 we get num=0 😶
@PatrickAlphaC - The unsigned integers ran on the concept of being "unchecked".
- If the upper limit of a number is passed, it gets wrapped around from the lowest number.
- SafeMath was used to check 🔎 and prevent wrapping around.
- However versions > 0.8 check for overflow. ✔️
@PatrickAlphaC 🟧unchecked KEYWORD:
- To wrap around in the latest versions, we can use the "unchecked" keyword.
✅unchecked{num=num+1;}
@PatrickAlphaC That was 15 mins🕢 of the course summarized for you. 🙃
@PatrickAlphaC If you're a web3 enthusiast, wanting to explore, learn and grow as a developer, do watch this video and learn along with me. ✨
@PatrickAlphaC Digest 12 (summarizing the next few minutes of the course) coming soon... 😉
If you found this thread informative, retweet, like, and share the tweet below.👇
@PatrickAlphaC 📚In this lesson we see how to withdraw the deposited funds from our contract. 🤑
We cover:
- for loop
- ways to transfer eth
- constructors
- modifiers
@PatrickAlphaC SOLIDITY ✨
- It is a programming language developed by Ethereum developers. 🔗
- Helps build smart contracts that run on Ethereum blockchain.