Skip to content

Instantly share code, notes, and snippets.

@anselm
Created February 12, 2021 20:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anselm/33015bffe4989a6ebe20a68dbb17c499 to your computer and use it in GitHub Desktop.
Save anselm/33015bffe4989a6ebe20a68dbb17c499 to your computer and use it in GitHub Desktop.
Time Lock Self Sovereign 401k Future Money Smart Contract for ETH Denver Hackathon
// See the Figma https://www.figma.com/file/pzq5EhzdxXvhpBoFlB8MDd/Money-Savings-App?node-id=5%3A3
// And see the app https://glitch.com/edit/#!/web3-time-lock
pragma solidity >=0.4.22 <0.7.0;
contract FutureBank {
address private owner;
mapping (address => uint256) private balances;
mapping (address => uint256) private locktime;
modifier onlyOwner() {
require (msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function deposit(uint256 lockdays) public payable {
require(lockdays < 365);
uint256 balance = balances[msg.sender];
balances[msg.sender] = balance + msg.value;
if(balance == 0) {
locktime[msg.sender] += ( now + (lockdays * 1 days) );
} else {
locktime[msg.sender] += (lockdays * 1 days);
}
}
function depositSeconds(uint256 lockseconds) public payable {
require(lockseconds < 24*60*60);
uint256 balance = balances[msg.sender];
balances[msg.sender] = balance + msg.value;
if(balance == 0) {
locktime[msg.sender] += ( now + lockseconds );
} else {
locktime[msg.sender] += ( lockseconds );
}
}
function balance() public view returns(uint){
return balances[msg.sender];
}
function delay() public view returns(uint){
return locktime[msg.sender];
}
function withdraw() external {
require(balances[msg.sender] > 0,"No funds in account");
require(block.timestamp > locktime[msg.sender],"Too soon to withdraw");
uint256 amount = balances[msg.sender];
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value:amount}("");
require(success, "Transfer failed.");
}
function withdrawAmount(uint256 amount) external {
require(balances[msg.sender] >= amount,"Insufficient balance");
require(block.timestamp > locktime[msg.sender],"Too soon to withdraw");
balances[msg.sender] -= amount;
//require(msg.sender.send(amount));
(bool success, ) = msg.sender.call{value:amount}("");
require(success, "Transfer failed.");
}
event Sent(address from, address to, uint amount);
function transfer(address receiver, uint amount) public {
require(amount <= balances[msg.sender], "Insufficient balance");
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
// Fallback function - Called if other functions don't match call or
// sent ether without data
// Typically, called when invalid data is sent
// Added so ether sent to this contract is reverted if the contract fails
// otherwise, the sender's money is transferred to contract
function () public {
revert();
}
//contract IsPayable {
// function () payable {}
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment