Skip to content

Instantly share code, notes, and snippets.

@Sagleft
Created July 5, 2024 16:22
Show Gist options
  • Save Sagleft/9e145a851100e59beaa8588e76aaa2ae to your computer and use it in GitHub Desktop.
Save Sagleft/9e145a851100e59beaa8588e76aaa2ae to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TokenLocker {
address public owner;
struct Lock {
address tokenAddress;
uint256 unlockTimestamp; // seconds
uint256 amount;
}
mapping(address => Lock) public locks;
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
constructor() {
owner = msg.sender;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "New owner is the zero address");
owner = newOwner;
}
function lockTokens(address _recipient, address _tokenAddress, uint256 _unlockTimestamp) external payable onlyOwner {
require(_recipient != address(0), "Invalid recipient address");
require(_tokenAddress != address(0), "Invalid token address");
require(msg.value > 0, "Amount must be greater than zero");
// Check if a lock already exists for this recipient and token
require(locks[_recipient].unlockTimestamp == 0, "Lock already exists for this recipient");
// Lock the tokens
locks[_recipient] = Lock(_tokenAddress, _unlockTimestamp, msg.value);
}
function unlockTokens(address _recipient) external payable {
Lock storage userLock = locks[_recipient];
require(userLock.unlockTimestamp != 0, "No lock found for this recipient");
require(userLock.unlockTimestamp <= block.timestamp, "Lock not yet expired");
uint256 amountToTransfer = userLock.amount;
// Clear the lock
delete locks[_recipient];
// Transfer unlocked tokens to the recipient
IERC20 token = IERC20(userLock.tokenAddress);
require(token.transfer(_recipient, _amount), "Token transfer failed");
}
function getBalance() external view returns (uint256) {
return address(this).balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment