|
| 1 | + |
| 2 | + |
| 3 | +### API |
| 4 | + |
| 5 | +ERC223 requires contract to implement the `ERC223Receiver` interface in order to receive tokens. If a user tries to send ERC223 tokens to a non-receiver contract the function will throw in the same way that it would if you sent ether to a contract without the called function being `payable`. |
| 6 | + |
| 7 | +An example of the high-level API for a receiver contract is: |
| 8 | + |
| 9 | +```solidity |
| 10 | +contract ExampleReceiver is StandardReceiver { |
| 11 | + function foo() tokenPayable { |
| 12 | + LogTokenPayable(tkn.addr, tkn.sender, tkn.value); |
| 13 | + } |
| 14 | +
|
| 15 | + function () tokenPayable { |
| 16 | + LogTokenPayable(tkn.addr, tkn.sender, tkn.value); |
| 17 | + } |
| 18 | +
|
| 19 | + event LogTokenPayable(address token, address sender, uint value); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Where functions that have the `tokenPayable` can only be called via a token fallback and inside the functions you have access to the `tkn` struct that tries to mimic the `msg` struct used for ether calls. |
| 24 | + |
| 25 | +The function `foo()` will be called when a user transfers ERC223 tokens to the receiver address. |
| 26 | + |
| 27 | +```solidity |
| 28 | + // 0xc2985578 is the identifier for function foo. Sending it in the data parameter of a tx will result in the function being called. |
| 29 | +
|
| 30 | + erc223.transfer(receiverAddress, 10, 0xc2985578) |
| 31 | +``` |
| 32 | + |
| 33 | +What happens under the hood is that the ERC223 token will detect it is sending tokens to a contract address, and after setting the correct balances it will call the `tokenReceived` function on the receiver with the specified data. `StandardReceiver` will set the correct values for the `tkn` variables and then perform a `delegatecall` to itself with the specified data, this will result in the call to the desired function in the contract. |
| 34 | + |
| 35 | +The current `tkn` values are: |
| 36 | + |
| 37 | +- `tkn.sender` the original `msg.sender` to the token contract, the address originating the token transfer. |
| 38 | + - For user originated transfers sender will be equal to `tx.origin` |
| 39 | + - For contract originated transfers, `tx.origin` will be the user that made the transaction to that contract. |
| 40 | + |
| 41 | +- `tkn.origin` the origin address from whose balance the tokens are sent |
| 42 | + - For `transfer()`, it will be the same as `tkn.sender` |
| 43 | + - For `transferFrom()`, it will be the address that created the allowance in the token contract |
| 44 | + |
| 45 | +- `tkn.value` the amount of tokens sent |
| 46 | +- `tkn.data` arbitrary data sent with the token transfer. Simulates ether `tx.data`. |
| 47 | +- `tkn.sig` the first 4 bytes of `tx.data` that determine what function is called. |
0 commit comments