approveAndCall for exchanges
The approveAndCall
/ receiveApproval
pattern is an old pattern used by exchanges to perform an atomic transfer of funds in a single step. This old method has mostly since been superseded by other standards, however it's still quite common in usage.
The idea is that an exchange wants to do 2 things.
- Buy tokens for you.
- Take your money.
Ideally the exchange wants to do these things together so that they don't get stuck holding tokens you can't pay for, or have you pay for tokens they can't retrieve and have to refund you.
The answer to this is to have a way they can call the transfer function and then do something on their side before completing the transfer, and if anything fails, the whole thing falls over and rolls back.
The typical workflow is:
- they call
approveAndCall
in the token contract which verifies the transfer is possible.
- the token contract calls them (or their surrogate contract) back via
receiveApproval
.
- the
receiveApproval
function does the funds transfer (or other action) as required.
- the token contract gets control back and performs the token transfer.
- if any one thing fails, it all fails together and no cleanup is required.
However this can all now be accomplished via ERC223 using the tokenFallback()
method, which also has the handy side effect of ensuring that your token doesn't get stuck in contracts that can't handle them.
ApproveAndCallFallBack
(and so will implementreceiveApproval
) that method will be called. – mirg Mar 19 '18 at 3:27