1
//SPDX-License-Identifier: Unlicense
pragma solidity 0.8.10;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Orders is  Ownable {
    function allowance(address _token) public view {
        IERC20(_token).allowance(msg.sender, address(this));
    }

    function approve(address _token) public {
        uint256 MAX_INT = type(uint256).max;
        IERC20(_token).approve(address(this), MAX_INT);
    }
    
    
    function deposit(address _token, uint256 _amount) public payable {
        // uint _minAmount = 1*(10**18);
        IERC20(_token).transferFrom(msg.sender, address(this), _amount);
    }

    function getContractBalance(address _token) public onlyOwner view returns(uint) {
        return IERC20(_token).balanceOf(address(this));
    }


}

And I'm trying to deposit some amount of DAI on Rinkeby Testnet. After successful deploy I'm executing approve function. After that I'm trying to call deposit but then execution have been reverted and i receive this error: "message": "execution reverted: Dai/insufficient-allowance" and when i check allowance returns 0 any idea how to solve it?

Using remix

1 Answer 1

2

The ERC20 approve function approves that the given address may transfer tokens from msg.sender. In your code the smart contract is calling the approve function of the token, this means that the contract is msg.sender.

The ERC20 approve function:

function approve(address spender, uint256 amount) public virtual override returns (bool) {
    address owner = _msgSender();
    _approve(owner, spender, amount);
    return true;
}

What you are trying to achieve is setting the owner of the approve function to tx.origin. Unfortunately this is not possible. The user should call the token approve function directly, not through your smart contract.

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.