0
0
Blockchain / Solidityprogramming~30 mins

Timelock pattern in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Timelock Pattern in Smart Contracts
📖 Scenario: You are building a simple smart contract on Ethereum that holds funds and only allows withdrawal after a certain time has passed. This is called a timelock pattern, which helps secure funds by delaying access.
🎯 Goal: Create a Solidity contract that stores an amount and a release time. The contract should only allow withdrawal of funds after the release time has passed.
📋 What You'll Learn
Create a releaseTime variable to store the unlock timestamp
Create a depositor address variable to store who deposited the funds
Write a constructor that sets releaseTime and depositor
Write a withdraw function that allows withdrawal only if the current time is after releaseTime
Use block.timestamp to check the current time
💡 Why This Matters
🌍 Real World
Timelock contracts are used to secure funds by delaying access, useful in decentralized finance (DeFi) and governance to prevent immediate fund withdrawal.
💼 Career
Understanding timelock patterns is important for blockchain developers working on secure smart contracts and DeFi applications.
Progress0 / 4 steps
1
Create the contract and state variables
Create a Solidity contract named Timelock with two public state variables: uint256 releaseTime and address depositor.
Blockchain / Solidity
Need a hint?

Use uint256 public releaseTime; and address public depositor; inside the contract.

2
Add the constructor to set release time and depositor
Add a constructor to Timelock that takes uint256 _releaseTime as input and sets releaseTime = _releaseTime and depositor = msg.sender.
Blockchain / Solidity
Need a hint?

The constructor sets releaseTime from the input and depositor to the sender.

3
Add the withdraw function with timelock check
Add a public function withdraw that allows the depositor to withdraw funds only if block.timestamp >= releaseTime. Use require to enforce this and transfer the contract balance to depositor.
Blockchain / Solidity
Need a hint?

Use require to check sender and time, then transfer balance.

4
Add a receive function and test withdrawal
Add a receive external payable function to accept funds. Then add a withdraw call after the release time to test the contract. Print "Withdrawal successful" after withdrawal.
Blockchain / Solidity
Need a hint?

Add receive() external payable {} to accept funds.