Skip to content

Instantly share code, notes, and snippets.

@alecchampaign
Last active December 3, 2017 11:32
Show Gist options
  • Save alecchampaign/4c1df27a208157dc80e48ad8eb799435 to your computer and use it in GitHub Desktop.
Save alecchampaign/4c1df27a208157dc80e48ad8eb799435 to your computer and use it in GitHub Desktop.
Simple contract to store Ether.
pragma solidity ^0.4.18;
/** Simple contract to store Ether */
contract Vault
{
address owner;
uint balance;
// Modifier for authenticating owner
modifier ownerOnly(address _owner) {
require(msg.sender == _owner);
_;
}
// Constructor
function Vault() public payable {
owner = msg.sender;
balance = msg.value;
}
/** Withdraw ether from the contract */
function withdraw(uint amount) public ownerOnly(owner) {
require(amount <= balance);
balance -= amount;
owner.transfer(amount);
}
/** Query the balance of the contract */
function getBalance() constant public returns(uint) {
return(balance);
}
/** Query the owner of the contract */
function getOwner() public constant returns (address) {
return(owner);
}
/** Kill the contract and send the remaining ether to the owner */
function closeVault() public payable ownerOnly(owner) {
selfdestruct(owner);
}
/** Increase the balance of the smart contract */
function deposit() public payable{
balance += msg.value;
}
/** Send ether from the contract to an external account or contract */
function sendPayment(uint amount, address receiver) public payable ownerOnly(owner) {
require(amount <= balance);
balance -= amount;
receiver.transfer(amount);
}
/** Transfer ownership of the contract to an external account */
function changeOwner(address newOwner) public ownerOnly(owner) {
require(newOwner != address(0));
owner = newOwner;
}
// fallback function
function () public payable {
// Rejects normal attempts to send ether from unknown sources
revert();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment