1

I was going through fixed supply token 's solidity code from wiki and I have confusion regarding ApproveAndCallFallBack contract's receiveApproval functions, it is as under

// Token owner can approve for spender to transferFrom(...) tokens // from the token owner's account. The spender contract function // receiveApproval(...) is then executed

 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;
}

Understanding so far, this function is allowing spender contract to spend passed count of the token to be spent from msg.sender account, but I do not understand the use of the 3rd and 4th parameter in ApproveAndCallFallBack function.

This is some vague understanding can someone explain what is exactly going on here.

Here is the link source code of contract which contains these functions

https://theethereum.wiki/w/index.php/ERC20_Token_Standard

  • Please ask one question at a time. Mixing two questions into one makes your question much less useful to future visitors. It also makes it harder to answer (because someone has to answer both questions at once). – user19510 Mar 27 '18 at 19:33
1

Spender contract needs to implement the receiveApproval function

function receiveApproval(
    address from, 
    uint256 _amount, 
    address _token, 
    bytes _data) public;
  • The owner of the tokens is in the first parameter from
  • The amount of tokens is in the second parameter _amount
  • The token address is in the third parameter _token
  • The last parameter _data is a custom data defined by the dapp, for example an referer address, or a customer id, or an operation code.

For example we can do this.

function receiveApproval(
    address from, 
    uint256 _amount, 
    address _token, 
    bytes _data) public {

    // Transfer tokens to spender
    ERC20(_token).transferFrom(from, this, _amount);

    // Raise an approved operation event
    // Frontend will show the purchase is complete
    bytes32 operationId = readOperationId(_data);
    emit ApprovedOperation(operationId);
}
  • 1
    Hi Understood your point. But i have one tiny doubt. In your sample code above code to transfer token from owner to spender should be ERC20(_token).transferFrom(from, this,_amount); instead of ERC20(_token).transferFrom(from, _amount);. Am i right? – Thinker Mar 29 '18 at 17:35
  • @Prashank You are correct, it should be fixed now. – Ismael Mar 29 '18 at 17:44
  • Thanxx man, for clearing all the doubts. – Thinker Mar 29 '18 at 17:47
0

Your understanding is correct. You will set a number of tokens and allow someone else to transfer the tokens as he/she wants, that is the approval. Then you will call a function on another contract, that is the call apart. There are only three parameters. The parameter "data" of type "bytes". This last parameter may be required by the contract that the function approveAndCall is calling. That function is receiveApproval and is defined in the interface in the contract that you are talking about. This basically describe that this function exist and is implemented in another contract.

I hope this help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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