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
@PatrickAlphaC BASIC SOLIDITY LOOP 📍
- for loop syntax:

✅for (startingIndex; endingIndex; stepAmount);
@PatrickAlphaC - Within the withdraw() function, add a for loop statement

✅for(uint256 funderIndex=0; funderIndex < funders.length; funderIndex++);

-> funders.length = length of the funders array
@PatrickAlphaC ✔️ Upon calling the withdraw() function, the funds have to be transferred and the funders array has to be reset.
@PatrickAlphaC
address funder = funders[funderIndex];
addressToAmount[funder]=0;
funders=new address[](0); // implies that it has 0 objects
@PatrickAlphaC SENDING ETH 💰
1. transfer
2. send
3. call
@PatrickAlphaC USING transfer() 💸

✅payable(msg.sender).transfer(address(this).balance); // "this" here refers to this contract

💡msg.sender is type cast to payable type which is otherwise of address type.
@PatrickAlphaC USING send() 💸

✅bool ret=payable(msg.sender).send(address(this).balance);

- The send() returns a boolean if the transfer is successful. However, if send() fails, we need to revert the state. ⚠️

✅require(ret,"send failed");
@PatrickAlphaC USING call() 🤑

✅(bool ret,)=payable(msg.sender).call{value:address(this).balance}("");

- the call() function returns more than 1 variable but we're only interested in knowing if the call went through or not.
- Revert the state if call fails ⚠️

✅require(ret,"call failed");
@PatrickAlphaC 🚨Note:
✔️call() is the recommended transfer method.
✔️Check out solidity-by-example.org/sending-ether/ for more.
@PatrickAlphaC BASIC SOLIDITY CONSTRUCTOR 💻
- A constructor is a function that gets immediately called in the same transaction that we create the contract.
- use the "constructor" keyword. ✔️
@PatrickAlphaC 🚨Note:
Only the owner must be able to withdraw the funds.
See how to ensure that below 👇🏽
@PatrickAlphaC ✅address public owner;

constructor(){
owner=msg.sender;
}

- This sets the owner to the user who is initiating the transaction.
@PatrickAlphaC - To ensure that only the owner initiates the withdrawal, add the statement below within the withdraw() function:

✅require(msg.sender==owner,"not owner");

OR

💡 Use a modifier :)
@PatrickAlphaC ✅modifier onlyOwner{
require(msg.sender==owner,"not owner");
_;
}

💡 The underscore _ after the first statement within the modifier implies that the rest of the code of the function is executed after the first statement of the modifier only.
@PatrickAlphaC Now, modify the withdraw function as shown 👇🏽

✅function withdraw() public onlyOwner{
....
}
@PatrickAlphaC Now, you can go ahead and try to fund/ withdraw on the testnet 🎉
@PatrickAlphaC That was 30 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 13 (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 7
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
Read 22 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!

:(