Created
July 20, 2018 15:58
-
-
Save noot/29b776d842d4e1e9a593eee424500a09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.8; | |
/* ERC223 additions to ERC20 */ | |
import "./interface/ERC223.sol"; | |
import "./interface/ERC223Receiver.sol"; | |
import "./StandardToken.sol"; | |
contract Standard223Token is ERC223, StandardToken { | |
//function that is called when a user or another contract wants to transfer funds | |
function transfer(address _to, uint _value, bytes _data) returns (bool success) { | |
//filtering if the target is a contract with bytecode inside it | |
if (!super.transfer(_to, _value)) throw; // do a normal token transfer | |
if (isContract(_to)) return contractFallback(msg.sender, _to, _value, _data); | |
return true; | |
} | |
function transferFrom(address _from, address _to, uint _value, bytes _data) returns (bool success) { | |
if (!super.transferFrom(_from, _to, _value)) revert(); // do a normal token transfer | |
if (isContract(_to)) return contractFallback(_from, _to, _value, _data); | |
return true; | |
} | |
function transfer(address _to, uint _value) returns (bool success) { | |
return transfer(_to, _value, new bytes(0)); | |
} | |
function transferFrom(address _from, address _to, uint _value) returns (bool success) { | |
return transferFrom(_from, _to, _value, new bytes(0)); | |
} | |
//function that is called when transaction target is a contract | |
function contractFallback(address _origin, address _to, uint _value, bytes _data) private returns (bool success) { | |
ERC223Receiver reciever = ERC223Receiver(_to); | |
return reciever.tokenFallback(msg.sender, _origin, _value, _data); | |
} | |
//assemble the given address bytecode. If bytecode exists then the _addr is a contract. | |
function isContract(address _addr) private returns (bool is_contract) { | |
// retrieve the size of the code on target address, this needs assembly | |
uint length; | |
assembly { length := extcodesize(_addr) } | |
return length > 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment