1

I'm creating a contract which will transfer usdt token from users wallet to contract address

// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";

contract Contract {

    IERC20 public token;
    address public owner;


    constructor(IERC20 _token) {
        token = _token;
    }

    function approve(address spender, uint256 amount) external  {
        token.approve(spender, amount);
    }

    // Other important functions.
    function transfer(address from, address to, uint256 amount) external {
        token.transferFrom(from, to, amount);
    }


}


Im setting up the usdt contract address in constructor while deploying my contract. The approve funtion is working but when i try to call transfer function its showing maximum allovance exceeded. My primary motive is that i need to create a mint function in mycontract which will take fee as usdt?

1 Answer 1

1

The token.approve in this case should not be called from the contract as the msg.sender will be the contract instead of the user. In this case, if you like to have a contract to transferFrom your token from a from address to a to address, then you will need to first.

  1. Call the approve function from the ERC20 token directly from your wallet. This way the msg.sender will be your wallet address and the spender should be your contract address here.
  2. Call the transfer function that you already defined here. If you have successfully executed approve on the contract with the contract specified as the spender, then the execution should be no problem. Just make sure that the amount approved is more than what is going to be inputted in transfer.

Hope this helps~

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.