SHARE
TWEET

Untitled

a guest Apr 25th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.4.21;
  2.  
  3. /// The contract has exactly one owner, the creator of the contract.
  4. /// The contract stores funds in two categories: checkings and savings.
  5. /// 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.
  6. /// Only the owner can access the funds.
  7. /// 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.
  8. contract CheckingSavings {
  9.    
  10.     // <contract_variables>
  11.     uint256 checking;
  12.     uint256 savings;
  13.     address owner;
  14.     uint createdAt;
  15.     // </contract_variables>
  16.  
  17.     /// The creator of the contract (msg.sender) is the owner of the contract.
  18.     /// Any funds sent during contract creation are stored in the checking and savings accounts (90%-10%).
  19.     function CheckingSavings() public payable {
  20.         owner = msg.sender;
  21.         createdAt = block.timestamp;
  22.  
  23.         splitMsgValue();
  24.     }
  25.  
  26.     /// Sends $(amount) wei to the sender of the transaction.
  27.     /// Succeeds only if the sender is the owner and the amount does not exceed the current balance of the checking account.
  28.     function withdrawChecking(uint256 amount) public {
  29.         require(msg.sender == owner);
  30.         require( checking >= amount);
  31.         checking -= amount;
  32.         owner.transfer(amount);
  33.     }
  34.  
  35.     /// Sends $(amount) wei to the sender of the transaction.
  36.     /// 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.
  37.     function withdrawSavings(uint256 amount) public {
  38.         require(msg.sender == owner);
  39.         require( savings >= amount);
  40.         require( (block.timestamp + 365 days) >= createdAt);
  41.         savings -= amount;
  42.         owner.transfer(amount);
  43.     }
  44.  
  45.     /// Receives ether from any address.
  46.     /// Stores 10% of the received funds in the checking account, the rest in the savings account.
  47.     function () public payable {
  48.         splitMsgValue();
  49.     }
  50.    
  51.     /// Stores 10% of the received funds in the checking account, the rest in the savings account.
  52.     function splitMsgValue() private {
  53.         uint256 s = msg.value / 10;
  54.         savings += s;
  55.         checking += (msg.value - s);
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top