0

i am developing a locker smartcontract where user can lock their erc20 token. i have successfully develop the adding(lock) functionality but having difficulty developing the withdraw function. for with draw function i want to implement-

  • user can only able to withdraw after the locking period & *the wallet add the token can only be able to withdraw.

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));
   }

   

   >  function claim(address _token) public { 
>          require(block.timestamp >time);
> 
>        }

}
3
  • Could you explain more explicitly what is not working please ?
    – Franfran
    Commented May 9, 2022 at 17:34
  • look at the claim function. i want to implement claim function as such- user can only able to withdraw after the locking period & *the wallet add the token can only be able to withdraw.
    – learner
    Commented May 9, 2022 at 18:40
  • Gotcha. You could load the struct in the memory using: Lock memory myLock = tokenLock[msg.sender]; And access those variables from here.
    – Franfran
    Commented May 9, 2022 at 19:12

1 Answer 1

0

I tried to solve your issue and implementing my logic in your claim function. I put also some notes for understand you your errors. You can see this smart contract code:

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

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{
        // NOTE: Add 'block.timestamp' plus time for manage lock time  
        tokenLock[msg.sender] = Lock(_token, _amount, block.timestamp + _time);
        IERC20(_token).transferFrom(msg.sender, address(this),_amount);
    }
    
    function chkBalance(address _token) public view returns (uint){
        return IERC20(_token).balanceOf(address(this));
    }

    function claim(address _token) public { 
        // NOTE: Retrieve the end time from Lock struct
        uint time = tokenLock[msg.sender].endtime;
        require(block.timestamp > time, "You must to attend your locktime!");
        address _to = msg.sender;
        uint amountTokenInSmartContract = IERC20(_token).balanceOf(address(this));
        // NOTE: Use 'transfer' method for send '_to' address the amount of token locked inside smart contract  
        IERC20(_token).transfer(_to, amountTokenInSmartContract);
    }

}
1
  • kerry99 your way of explaining is awesome.
    – learner
    Commented May 10, 2022 at 12:15

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.