I'm trying to wrap my head around the approveAndCall() function, because I want to allow users to pay for functions of my main contract with the associated tokens.
I'm trying to understand the example of this article. In the "approveAndCall()" section, the following functions are shown:
function approveAndCall(address _recipient,
uint256 _value,
bytes _extraData) {
approve(_recipient, _value);
TokenRecipient(_recipient).receiveApproval(msg.sender,
_value,
address(this),
_extraData);
}
and
function receiveApproval(address _sender,
uint256 _value,
TokenContract _tokenContract,
bytes _extraData) {
require(_tokenContract == tokenContract);
require(tokenContract.transferFrom(_sender, address(this), 1));
uint256 payloadSize;
uint256 payload;
assembly {
payloadSize := mload(_extraData)
payload := mload(add(_extraData, 0x20))
}
payload = payload >> 8*(32 - payloadSize);
info[sender] = payload;
}
What does the assembly part in the second function do?
The example function stores data, so all the actual function called via the token contract does is info[sender] = payload;. Do I need a payload if I replace it with my function that only has "simple" arguments? (No bytes, just uints, addresses, etc.) Do they need to be converted to bytes?
(I might ask a different question for more details about approveAndCall() and recieveApproval, for now I'd just want to understand that payload part.)
receiveApproval? I can't write instructions directly, because I want to be able to call different functions. Do I have to write as manyapproveAndCallandreceiveApprovalas I have functions? – Teleporting Goat Apr 10 '18 at 15:47ifs I can call my function directly, right? Likeif (function_code == 1) { function1(arg1, arg2); } else if (function_code == 2) { function2(arg3); }– Teleporting Goat Apr 11 '18 at 09:31recipientthe service contract address? If not, where do I put the contract address? – Teleporting Goat Apr 12 '18 at 09:28msg.sender, can I still use it or do I have to replace it? (I assume that in thereceiveApproval()function,msg.senderbecomes the token contact, am I correct?) – Teleporting Goat Apr 12 '18 at 09:38recipientis the service contract address. Note that this will create a balance entry in the token contract for the service contract. So your contract service should have a function to transfer the tokens if needed.Yes again, the msg.sender is now the token contract, however, the original sender is in receive approval as _sender
– Jaime Apr 12 '18 at 12:44