my goal is to create a smart contract which user can deposit erc20 and withdraw erc20 token using.i tried to implement this functionality using token transfer and transferfrom functions tried to send tokens to contract address .but its showing some error. i tried all the possible solution available on internet but no luck .please help me out

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.10;

interface IERC20 

    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);

    event Approval(address indexed owner, address indexed spender, uint256 value);


contract TestFunding

    mapping(address => uint) public users;

    address public owner;

    uint public minimumDeposit;

    uint public totalDeposit;

    uint public noOfUsers;

        owner = msg.sender;
    function depositToken(address _token,uint _amount) public {
         IERC20(_token).approve(address(this), _amount);
       function depositToken2(address _token,uint _amount) public {

        IERC20(_token).transfer(address(this), _amount);
    function getUserBalance() public view returns(uint)
     return users[msg.sender];   
    function getCurrentBalance(address _token) public view returns(uint)
     return IERC20(_token).balanceOf(address(this)) ; 
    function getTokenBalance(address _token,address _account) public view returns(uint)
     return IERC20(_token).balanceOf(_account) ; 
    function withdrawToken(address _token,uint _amount) public
    IERC20(_token).approve(msg.sender, _amount);

enter image description here enter image description here

when i tried to transfer using depositToken function error showing transact to TestFunding.depositToken errored: VM error: revert.

revert The transaction has been reverted to the initial state. Reason provided by the contract: "ERC20: transfer amount exceeds allowance". Debug the transaction to get more information.

when i tried to transfer using depositToken function error showing transact to TestFunding.depositToken2 errored: VM error: revert.

revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

  • Please add the errors in text form and not in a messy image
    – egjlmn1
    Nov 22 '21 at 15:23
  • Hi HarishManthurthi! Welcome to Ethereum Stackexchange! You have to learn how to use transferFrom ethereum.stackexchange.com/questions/46457/…. You have to be aware that a contract cannot call approve on behalf of their users, the users have to call approve before executing the contract. The famous two steps approve+transferFrom.
    – Ismael
    Nov 24 '21 at 5:34

The ERC20 approve() transaction has to be mined first. And only after that has been confirmed by the network can the transferFrom() be used in a subsequent block.

Also, in the depositToken(), the way you have used approve(), it will be the smart contract that will approving itself.

The correct way is: The user has to send the approval transaction to the network. So at the user end, this instruction has to go to the network first:

_tokenContract.approve(address(_yourContractAddress), _amountToApprove, {from:_user});

  • thanks for replay, can you please edit my code and send back, i tried your code its showing me error expected primary expression . Nov 22 '21 at 15:31
  • IERC20(_token).approve(address(this), _amount, {from:address(msg.sender)}); Nov 22 '21 at 15:31
  • please help me. Nov 22 '21 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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