Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
// SPDX-License-Identifier: UNLICENSED

/**
* @title Lock
* @dev A simple time-locked contract that allows the owner to withdraw funds after a specified unlock time.
* The contract is initialized with an unlock time and can receive ETH. Only the owner can withdraw after the unlock time.
*/
pragma solidity ^0.8.28;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
/**
* @dev The timestamp when funds can be withdrawn.
*/
uint public unlockTime;

/**
* @dev The address of the contract owner who can withdraw funds.
*/
address payable public owner;

/**
* @dev Emitted when a withdrawal is made.
* @param amount The amount withdrawn (entire contract balance).
* @param when The timestamp when the withdrawal occurred.
*/
event Withdrawal(uint amount, uint when);

/**
* @dev Initializes the contract with a future unlock time. Sets the deployer as the owner.
* @param _unlockTime The timestamp after which funds can be withdrawn.
* Requirements:
* - _unlockTime must be in the future.
* - Contract can receive ETH at deployment.
*/
constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
Expand All @@ -20,6 +45,13 @@ contract Lock {
owner = payable(msg.sender);
}

/**
* @dev Allows the owner to withdraw all funds after the unlock time.
* Requirements:
* - Current time must be greater than or equal to unlockTime.
* - Caller must be the owner.
* Emits a {Withdrawal} event.
*/
function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
Expand Down