0

I have a smart contract that uses WETH for trading. I am testing on my local and I deploy a WETH smart contract, the source code: source code

I can successfully access the WETH contract from my contract like this:

function testMint() payable public {
    WETH.approve(address(this), msg.value);
}

I am calling testMint via Externally Owned Account (EOA), and I also want to access WETH.approve via the same caller. In above scenario, msg.sender of testMint is not the msg.sender of WETH.approve therefore approve function provides allowance "from contract to contract" not "from EOA to contract". Is there any way to call WETH.approve function with the EAO caller. I would appreciate with any help.

1
  • That is not possible. Only the EOA can call approve on its behalf.
    – Ismael
    Commented Feb 13 at 2:55

2 Answers 2

0

There can be a way, for example using tx.origin in the WETH contract. But it is not a good practice to do.

Also, approve is always a separate transaction. So you have to approve your funds first, only then can you execute testMint() function.

0

I found a way using modifier,

modifier addApprove(uint _amount) {
     WETH.approve(address(this), amount);
     _;        
}

function testMint()  public addApprove(100) {

}

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.