0

I am trying to build a contract that allows user to supply/deposit token to the aavae protocol but i need to make my contract get approval of the token amount to be spent by the contract. I tried using the code bellow which seems fine to me as a beginner but the allowance

//SPDX-License-Identifier:MIT
pragma solidity >=0.8.0 <0.9.0;


interface IWrappedEther{

    function deposit() external payable;
    function transfer(address dst, uint256 wad) external returns (bool); 

    function approve(address guy, uint256 wad) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);

     function balanceOf(address _address) external view returns(uint256); 
}

contract DeFi{
    IWrappedEther wrappedEthereum;
    constructor(address _wrappedEthereumContract){
        wrappedEthereum = IWrappedEther(_wrappedEthereumContract);

    }

    bool public isApproved;

    function getWrappedEther()public payable{
        wrappedEthereum.deposit{value: msg.value}();
        wrappedEthereum.transfer(msg.sender, msg.value);

    }

    function approveToken(address _spender, uint256 _amount) external  {
        require(wrappedEthereum.approve(_spender, _amount), "Token Spend is not approved");
        
    }
     function checkAllowance(address _spender)public view returns(uint256){
       return wrappedEthereum.allowance(msg.sender, _spender);
    }

    function checkBalance()public view returns(uint256){
        return wrappedEthereum.balanceOf(msg.sender);
    }

    
}

1 Answer 1

0

It won't work like this. The wallet needs to be the msg.sender while calling the approve function. But when you call the function via another contract, that contract becomes the msg.sender.

In this contract DeFi, you provided:

function approveToken(address _spender, uint256 _amount) external  {
        require(wrappedEthereum.approve(_spender, _amount), "Token Spend is not approved");
        
    } 

When you'll call this function, you are expecting the spender to get approved for the tokens your wallet has, but what will happen is the spender will get approved for the tokens this contract DeFi has, because the contract DeFi became msg.sender

The only way is to approve the contract by directly calling the token (WETH) contract. This means the user will have to call the approve function.

2
  • The solution you provided is a little ambiguous to me as I'm new to smart contract development. If you could provide an example I would be glad Commented Mar 18 at 18:43
  • Sorry if it wasn't clear. There's no solution. What I said was, the user needs to directly call the approve function in the token contract. This means you won't define any function related to approval in the DeFI contract. The token holder will directly call the approve function to the WETH contract. Commented Mar 19 at 5:42

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.