Skip to content

Instantly share code, notes, and snippets.

@YashKarthik
Created February 6, 2022 08:30
Show Gist options
  • Save YashKarthik/baae772dbb19ac20682e39e65470a8c7 to your computer and use it in GitHub Desktop.
Save YashKarthik/baae772dbb19ac20682e39e65470a8c7 to your computer and use it in GitHub Desktop.
Piggy bank that locks in funds for the given time and allows withdrawal by owner.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract Piggy {
address payable owner;
uint public releaseTimestamp;
constructor() payable {
owner = payable(msg.sender);
}
event Locked(uint _releaseTimestamp, uint _lockAmt);
function lockIn(uint _lockPeriod) public payable {
releaseTimestamp = block.timestamp + (_lockPeriod * 60);
emit Locked(releaseTimestamp, msg.value);
}
event Withdraw(uint _releaseTimestamp, uint _actualReleaseTimestamp, uint _amount);
function breakPiggy() public {
require(msg.sender == owner, "msg.sender not owner");
require(block.timestamp >= releaseTimestamp, "Lock in period not complete");
uint value = address(this).balance;
emit Withdraw(releaseTimestamp, block.timestamp, value);
selfdestruct(payable(msg.sender));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment