SOLIDITY BASICS - 6 (Arrays, Libraries, SafeMath, unchecked Keyword)🛸🪐

Digest 11 of "Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course" by @PatrickAlphaC ⭐🤩

A Thread.🧵

#solidity #coding #blockchain #smartcontracts #web3 🚀
@PatrickAlphaC Check out the previous thread of the series here 👇🏽
@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 👇🏽

✅funders.push(msg.sender);
✅addressToAmount[msg.sender]=msg.value;
@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"

✅function getPriceConverter(uint256 ethAmount) internal returns(uint256){
...
}
@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.👇
Follow @LearnWithSiv for updates ☺️✨

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Sivakama Sundari 👩‍💻🥤

Sivakama Sundari 👩‍💻🥤 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @LearnWithSiv

Jun 8
SOLIDITY BASICS - 7 (Loops, Constructors, Modifiers & Sending ETH) 🛸🪐

Digest 12 of "Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course" by @PatrickAlphaC ⭐🤩

A Thread.🧵

#solidity #coding #blockchain #smartcontracts #web3 🚀
@PatrickAlphaC Check out the previous thread of the series here 👇🏽
@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
Read 24 tweets
Jun 2
SOLIDITY BASICS - 1 🛸🪐

Digest 6 of "Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course" by @PatrickAlphaC ⭐🤩

A Thread.🧵

#solidity #coding #blockchain #smartcontracts #web3 🚀
@PatrickAlphaC If you haven't checked out the previous threads covering the basics of blockchain, please do before getting started on this one! 🧱
@PatrickAlphaC SOLIDITY ✨
- It is a programming language developed by Ethereum developers. 🔗
- Helps build smart contracts that run on Ethereum blockchain.
Read 20 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(