Skip to content

Instantly share code, notes, and snippets.

@SerpentChris
Last active December 31, 2017 15:53
Show Gist options
  • Save SerpentChris/a1bbea177c535a9427143f05bdc81b0f to your computer and use it in GitHub Desktop.
Save SerpentChris/a1bbea177c535a9427143f05bdc81b0f to your computer and use it in GitHub Desktop.
A smart contract that helps people HODL
pragma solidity ^0.4.19;
// Copyright (c) 2017 Christian Calderon
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
contract HODL{
struct Deposit {
uint balance;
uint maturity_date;
}
event newDeposit(address indexed withdrawer, uint indexed hodl_time, uint indexed amount);
event Withdrawal(address indexed withdrawer, uint indexed amount);
mapping(address => Deposit[]) deposits;
function hodl(address owner, uint time_to_hodl) public payable {
assert(msg.value > 0);
assert(time_to_hodl > 0);
assert(time_to_hodl <= 31557600); // one year
Deposit memory d = Deposit(msg.value, block.timestamp + time_to_hodl);
deposits[owner].push(d);
newDeposit(owner, time_to_hodl, msg.value);
}
function withdraw() public {
Deposit[] storage d = deposits[msg.sender];
require(d.length > 0);
uint amount_to_send = 0;
for(uint i = 0; i < d.length; i++){
Deposit storage d_i = d[i];
if((d_i.balance > 0) && (d_i.maturity_date < block.timestamp)){
amount_to_send += d_i.balance;
d_i.balance = 0;
d_i.maturity_date = 0;
}
}
require(amount_to_send > 0);
msg.sender.transfer(amount_to_send);
Withdrawal(msg.sender, amount_to_send);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment