I am developing a locker smart contract where a user can send erc20 tokens and set the time for a locking period. For the withdraw function I want to implement:

  1. the locking time should passed
  2. the address( wallet) lock the token can only withdraw

I am having difficulty to build the logic for withdraw function.

Here is the code

pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract lock  {
    address public owner;
   struct Lock{
       address token;
       uint amount;
       uint endtime;


    constructor() {
     owner= msg.sender;

    mapping(address => Lock) public tokenLock;

    function locker( address _token, uint256 _amount, uint _time) public{
        tokenLock[msg.sender]= Lock(_token, _amount, _time);
        IERC20(_token).transferFrom(msg.sender, address(this),_amount);
    function chkBalance(address _token) public view returns (uint){
        return  IERC20(_token).balanceOf(address(this));

    /** HERE **/
    function claim(address _token) public {
        require(_time >block.timestamp);
    /** END-HERE **/

1 Answer 1


It should be something like this:

function claim() public {
    // Get the Lock for the requester
    Lock storage lock = tokenLock[msg.sender];

    // Check timestamp
    require(lock.endtime >= block.timestamp);

    // Transfer locked tokens
    IERC20(lock.token).transfer(msg.sender, lock.amount);
  • how to implement this logic, *the address( wallet) lock the token can only withdraw
    – learner
    May 13, 2022 at 12:29
  • @learner The logic is implicit in the use of msg.sender as index to store the Lock. An address can only withdraw from its own lock. I haven't done any error check, you shouldn't use this code for production. The error checks are left as exercise.
    – Ismael
    May 13, 2022 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.