Created
February 14, 2019 17:49
-
-
Save jp4g/19e80bd29666ef1f08eb4ecab055736f to your computer and use it in GitHub Desktop.
Ethereum Solidity File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
/** | |
* Interface for bank contract | |
* Topics: How to use and implement an interface, syntax | |
**/ | |
interface Bank_Interface { | |
/** | |
* Deposit value into the bank | |
* @param amount added to balance of bank contract | |
**/ | |
function deposit(uint amount) external; | |
/** | |
* Withdraw value from the bank. Contract must have sufficient funds. | |
* @param amount amount subtracted from bank contract. Can not be 0 | |
* @return true if the bank contract has sufficient value, and false otherwise | |
**/ | |
function withdraw(uint amount) external returns (bool); | |
/** | |
* Return the current balance from the bank contract | |
* @return uint value of bank contract | |
**/ | |
function getBalance() external returns (uint); | |
} | |
/** | |
* Bank contract | |
* Topics: constructors, getters and setters, debugging, addresses | |
**/ | |
contract Bank is Bank_Interface { | |
uint private balance; | |
address private owner; | |
constructor (uint amount) public { | |
balance = amount; | |
owner = msg.sender; | |
} | |
function deposit(uint amount) public { | |
balance += amount; | |
} | |
function withdraw(uint amount) public returns (bool) { //this method is used to try debugging | |
if (amount != 0 && balance >= amount) { | |
balance -= amount; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function getBalance() public returns (uint) { | |
return balance; | |
} | |
} | |
/** | |
* Vault verifies that only the owner can access the bank | |
* Topics: understanding how contracts can be implemented like classes, understanding how to control access in solidity | |
**/ | |
contract Vault { | |
address public bank_owner; | |
Bank private bank; | |
constructor () public { | |
bank_owner = msg.sender; | |
bank = new Bank(0); | |
} | |
function validateDeposit(uint amount) public { | |
require(msg.sender == bank_owner); | |
bank.deposit(amount); | |
} | |
function validateWithdraw(uint amount) public returns (bool) { | |
require(msg.sender == bank_owner); | |
return bank.withdraw(amount); | |
} | |
function validateGetBalance() public returns (uint) { | |
require(msg.sender == bank_owner); | |
return bank.getBalance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment