0

I have an interface to Dai from my contract and want to allow a user to approve the contract to transfer their Dai. Dai uses DSToken code which has an approve function that relies on msg.sender:

function approve(address guy, uint wad) public returns (bool) {
    _approvals[msg.sender][guy] = wad;

    Approval(msg.sender, guy, wad);

    return true;
}

I have an interface my contract inherits which calls the interface contract like

contract DaiInterface {

    function approve(address guy, uint wad) public returns (bool);
}



contract DaiTransferrer is Ownable {

    DaiInterface daiContract;

    function approveDai(address guy, uint wad) public {
        daiContract.approve(guy, wad);
    }
}

So the problem is that msg.sender changes by the time it gets to the token contract so I think the interface code gets approved rather than main contract which needs to transfer the Dai. What's the best way to solve this?

1
  • the msg.sender at the Dai contract is the address of your main contract DaiInterface. The question is whether the approved address is your contract address or an EOA.
    – Jaime
    Nov 4, 2018 at 7:34

2 Answers 2

1

Not possible.

I think you misunderstand the approve method. A contract cannot approve itself to spend other people's funds under any circumstance.

You need to coordinate it on the front end so two transactions are sent by the user in a choreographed fashion.

  1. User => DAI (token contract), approve the destination contract (yours) to withdraw an amount.
  2. User => contract, transferFrom DAI (token contract), exercise the permission granted in step 1.

Hope it helps.

0

I don't think what you are trying to do is possible.

Imagine that you could create a function which approves an amount of a token to be spent by another address. If I made a malicious contract, and got you to call that function, I could steal all your tokens this way.

Same with the transfer function.

This is why these functions use msg.sender, precisely to make sure that only the exact person who should be able to call that function (within a particular user context) does so in an explicit way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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