Skip to content

Instantly share code, notes, and snippets.

@codebender828
Last active November 15, 2021 05:40
Show Gist options
  • Save codebender828/74dd56229c846b10b4f3c1ae2676be78 to your computer and use it in GitHub Desktop.
Save codebender828/74dd56229c846b10b4f3c1ae2676be78 to your computer and use it in GitHub Desktop.
Solidity Questions
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/*
INSTRUCTIONS FOR THIS TEST
==========================
1. Deploy Store
2. Deposit 1 ETH each from Account 1 (Alice) and Account 2 (Bob) into Store
3. Deploy `Consumer` with address of `Store`
4. Call Consumer._withdraw sending 1 ETH (using Account 3 (Eve)).
QUESTIONS
=========
1. When Consumer._withdraw is called, what is final balances of the wallets A, B, and C?
2. What is the order of functions called?
3. Are there any problems with this code? If so identify them.
*/
contract Store {
mapping(address => uint) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw() public {
uint bal = balances[msg.sender];
require(bal > 0);
(bool sent, ) = msg.sender.call{value: bal}("");
require(sent, "Failed to send Ether");
balances[msg.sender] = 0;
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract Consumer {
Store public etherStore;
constructor(address _etherStoreAddress) {
etherStore = Store(_etherStoreAddress);
}
// Fallback is called when Store sends Ether to this contract.
fallback() external payable {
if (address(etherStore).balance >= 1 ether) {
etherStore.withdraw();
}
}
function _withdraw() external payable {
require(msg.value >= 1 ether);
etherStore.deposit{value: 1 ether}();
etherStore.withdraw();
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment