The example of how to create a token (https://www.ethereum.org/token) includes contract tokenRecipient
. Why is it there?
2 Answers
I'ts just an example contract where you send tokens to. It represents a contract which you could sent tokens to.
You can see it where it says:
/* Allow another contract to spend some tokens in your behalf */
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
returns (bool success) {
allowance[msg.sender][_spender] = _value;
tokenRecipient spender = tokenRecipient(_spender);
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
-
Can someone give me a code example of how to implement approveAndCall()– tjakkoCommented Jun 8, 2016 at 9:07
-
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Commented Jun 8, 2016 at 9:26
Good question. I know I'm late, but still posting the answer as I was also wondering about it. I agree with @arodriguezdonaire. But ethereum explains it clearly in the documentation.
You'll notice that there some more functions in your basic token contract, like approve, sendFrom and others. These functions are there for your token to interact with other contracts: if you want, say, sell tokens to a decentralized exchange, just sending them to an address will not be enough as the exchange will not be aware of the new tokens or who sent them, because contracts aren't able to subscribe to Events only to function calls. So for contracts, you should first approve an amount of tokens they can move from your account and then ping them to let them know they should do their thing - or do the two actions in one, with approveAndCall.
If you want to see the sample implementation of toeknRecipient, have a look at "shareholdersassociation".
-
The wording from the docs here is misleading: contracts do not "subscribe" to functions in the same way that clients subscribe to Events. The point is that if you want to another contract to react to your contract somehow, you need to explicitly send a message to it.– jordanpgCommented Dec 22, 2017 at 20:49
-