Skip to content

Instantly share code, notes, and snippets.

@hayeah
Last active September 24, 2017 05:16
Show Gist options
  • Save hayeah/bd9b1c0c1d96d4db8b0f25eba914f0a8 to your computer and use it in GitHub Desktop.
Save hayeah/bd9b1c0c1d96d4db8b0f25eba914f0a8 to your computer and use it in GitHub Desktop.
contract HODL {
uint256 amount;
uint releaseAt;
uint createdAt;
address owner;
modifier canRelease() {
// 当前时间超过了 HODL 期限即可取款
// 或者 7 天内后悔了
require(now > releaseAt || (now - createdAt < 7 days));
_;
}
modifier isOwner() {
require(msg.sender == owner);
_;
}
function HODL(uint256 n) public payable {
require(msg.value > 0);
require(n > 30);
// 别让用户手抖输入太多钱或者太大的值
require(msg.value < 100 ether);
require(n < 365 * 5);
releaseAt = now + n * 1 days;
createdAt = now;
owner = msg.sender;
amount = msg.value;
}
function getBalance() public returns(uint256) {
return this.balance;
}
function withdraw() public isOwner canRelease {
require(this.balance > 0);
// 原路退款
owner.send(amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment