0

i am developing a locking smart-contract where user can add token address, amount and lock time. i imported erc20 standard from openzeplin but when i transfer erc20 tokens to my smartcontract is saying not a ethereum address( on ropston). i also try on remix its not working.

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).transfer( address(this),_amount);
       }


    
}
1

1 Answer 1

1

When a user must to deposit an amount of tokens in your smart contract, you must to use transferFrom() function. This operation, allow user to give permission to access in his wallet and deposit tokens amount without exceed the allowance. In your case, you must to change your smart contract in this way:

// 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 {    
        tokenLock[msg.sender]= Lock(_token, _amount, _time);
        IERC20(_token).transferFrom(msg.sender, address(this),_amount);
    }

    function getBalanceSmartContract(address _token) external view returns(uint) {
        return IERC20(_token).balanceOf(address(this));
    }
    
}

And before a user call locker() function, he must to approve your smart contract to deposit his tokens into it. For this operation, user'll call approve() function that exists into ERC20 Token smart contract and must to pass it following values:

  • spender: your smart contract address;
  • amount: how many tokens spender can handle.

And only then a user can call your locker() function.

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.