balances to store user balancesdeposit to allow users to add fundswithdraw that uses the Checks-Effects-Interactions patternbalances to store user balancesdeposit to allow users to add fundswithdraw that uses the Checks-Effects-Interactions patternbalances that maps address to uint. Then write a public deposit function that increases the sender's balance by msg.value.Use mapping(address => uint) public balances; to store balances.
The deposit function should be payable and add msg.value to balances[msg.sender].
withdraw that takes a uint amount parameter. Inside, first check that balances[msg.sender] is at least amount using require.Use require to check if the sender has enough balance before proceeding.
withdraw function, after the require check, subtract amount from balances[msg.sender]. This is the Effects step.Update the user's balance by subtracting the withdrawal amount before sending funds.
withdraw function, after updating the balance, send amount to msg.sender using payable(msg.sender).transfer(amount). Then add a print statement to output the remaining balance of msg.sender.Use payable(msg.sender).transfer(amount) to send Ether.
Solidity cannot print to console, so use an event to show the remaining balance.
What is the main purpose of the Checks-Effects-Interactions pattern in smart contracts?
Which of the following Solidity code snippets correctly follows the Checks-Effects-Interactions pattern?
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
balances[msg.sender] -= amount;
}What will be the output of the following Solidity function if balances[msg.sender] is 100 and amount is 50?
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
}Assume the call succeeds.
Identify the bug in this Solidity function violating the Checks-Effects-Interactions pattern:
function withdraw(uint amount) public {
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
}You want to write a Solidity function that sends a reward to a user only if they have enough points. Which code snippet correctly applies the Checks-Effects-Interactions pattern?
function rewardUser(address user, uint points) public {
// Option A
require(userPoints[user] >= points);
userPoints[user] -= points;
(bool sent, ) = user.call{value: points * 1 ether}('');
require(sent);
// Option B
(bool sent, ) = user.call{value: points * 1 ether}('');
require(sent);
require(userPoints[user] >= points);
userPoints[user] -= points;
// Option C
userPoints[user] -= points;
require(userPoints[user] >= 0);
(bool sent, ) = user.call{value: points * 1 ether}('');
require(sent);
// Option D
require(userPoints[user] >= points);
(bool sent, ) = user.call{value: points * 1 ether}('');
require(sent);
userPoints[user] -= points;
}Pass the test and this pattern moves unknown