What will be the usecase for this code? excuse my markdown trials.

contract ApproveAndCallFallBack { function receiveApproval(address from, uint256 tokens, address token, bytes data) public; } function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) { allowed[msg.sender][spender] = tokens; Approval(msg.sender, spender, tokens); ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data); return true; }


Assume you want to send tokens to a serviceContract to pay for a service. Because the contracts in general do not receive events, even if you transfer the tokens, the serviceContract will never know that you sent the tokens.

The solution is then to approve serviceContract to take the amount required from your token balance (that and not more!) first, and then execute a function in serviceContract to move the approved tokens form your token balance to the balance of serviceContract, and provide you with the service because they are now sure that you paid.

I hope this helps.

  • I see yes. I thought it a attempt to kurb multiple withdrawal attack , decreaseapprove/increaseapprove seems better to me so thats why i asked as ERC20 has approve and transferfrom ,it just seems extra ?
    – Trac3y
    Apr 4 '18 at 16:40
  • Is not extra because the other functions do not do the call to the other contract. That call is the way to let know the other contract that you have approved the funds.
    – Jaime
    Apr 4 '18 at 17:30
  • Oh i understand now thx
    – Trac3y
    Apr 4 '18 at 20:24
  • If this has covered your question please accept the answer.
    – Jaime
    Apr 4 '18 at 22:01
  • if you tell me how i will .You did answer my question
    – Trac3y
    Apr 4 '18 at 22:40

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