HW06: Checking/Savings
Problem statement
Implement a smart contract for personal finance management.
- The contract should have exactly one owner, the creator of the contract.
- The contract should store funds in two categories: checkings and savings.
- Every time some ether is transferred to the contract, 10% of the amount is stored in the savings account, while the rest is stored in the checking account.
- Only the owner can access the funds.
- The owner can access funds in the checking account at any time, while ether stored in the savings account can only be withdrawn after at least one year has passed since the creation of the contract.
Example scenario
Alice creates a contract with 10 ether initial balance. The initial balance of her checking account is 9 ether, while that of the savings account is 1 ether. Every month, Alice transfers her salary to the contract. Anytime Alice needs money, she withdraws from her checking account. After 1 year, she can freely withdraw from her savings account too.
Contract interface
constructor: function CheckingSavings() payable
- The creator of the contract (
msg.sender
) is the owner of the contract.
- Any funds sent during contract creation are stored in the checking and savings accounts (90%-10%).
withdraw: function withdrawChecking(uint256 amount)
- Sends
amount
wei to the sender of the transaction.
- Succeeds only if the sender is the owner and the amount does not exceed the current balance of the checking account.
- Note: do not allow reentrancy attacks!
withdraw: function withdrawSavings(uint256 amount)
- Sends
amount
wei to the sender of the transaction.
- Succeeds only if the sender is the owner, the amount does not exceed the current balance of the savings account, and at least 365 days have passed since the creation of the contract.
- Note: do not allow reentrancy attacks!
fallback: function () payable
- Receives ether from any address.
- Stores 10% of the received funds in the checking account, the rest in the savings account.
Contract skeleton
pragma solidity ^0.4.21;
contract CheckingSavings {
// <contract_variables>
// </contract_variables>
function CheckingSavings() public payable {
// TODO
}
function withdrawChecking(uint256 amount) public {
// TODO
}
function withdrawSavings(uint256 amount) public {
// TODO
}
function () public payable {
// TODO
}
}