Last active
August 31, 2016 13:11
-
-
Save christoph2806/066ff68d61de5fda59dfca3e0a40490b 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
// | |
// ControlledToken, | |
// | |
// a simple extension to ERC20 Token, giving token holders full control on | |
// receiving tokens. | |
// | |
// Usage: a token holder creates a "Control" contract implementing at least | |
// one function, | |
// | |
// function allow(from, uint value) returns (bool ok) | |
// | |
// The function can | |
// - throw or | |
// - return false => no tokens will be received | |
// - return true => tokens will be received | |
// | |
// The Control contract can / must be registered in the token contract | |
// (these two flavors are possible, just as you like it) | |
// In the optional case, the transfer is processed | |
// if no control contract is registered. | |
// | |
// Reentrant issues: none, because all logic is concentrated in the modifier; | |
// or have I overseen anything? | |
// | |
// Signature of Control contract | |
contract Control { | |
function allow(address from, uint value) returns (bool); | |
} | |
// Straigthforward implementation of ERC20Token: | |
contract ERC20Token { | |
mapping( address => uint ) _balances; | |
mapping( address => mapping( address => uint ) ) _approvals; | |
event Transfer(address indexed from, address indexed to, uint value); | |
event Approval( address indexed owner, address indexed spender, uint value); | |
uint _supply; | |
function ERC20Token( uint initial_balance ) { | |
_balances[msg.sender] = initial_balance; | |
_supply = initial_balance; | |
} | |
function totalSupply() constant returns (uint supply) { | |
return _supply; | |
} | |
function balanceOf( address who ) constant returns (uint value) { | |
return _balances[who]; | |
} | |
function transfer( address to, uint value) returns (bool ok) { | |
if( _balances[msg.sender] < value ) { | |
throw; | |
} | |
if( !safeToAdd(_balances[to], value) ) { | |
throw; | |
} | |
_balances[msg.sender] -= value; | |
_balances[to] += value; | |
Transfer( msg.sender, to, value ); | |
return true; | |
} | |
function transferFrom( address from, address to, uint value) returns (bool ok) { | |
// if you don't have enough balance, throw | |
if( _balances[from] < value ) { | |
throw; | |
} | |
// if you don't have approval, throw | |
if( _approvals[from][msg.sender] < value ) { | |
throw; | |
} | |
if( !safeToAdd(_balances[to], value) ) { | |
throw; | |
} | |
// transfer and return true | |
_approvals[from][msg.sender] -= value; | |
_balances[from] -= value; | |
_balances[to] += value; | |
Transfer( from, to, value ); | |
return true; | |
} | |
function approve(address spender, uint value) returns (bool ok) { | |
_approvals[msg.sender][spender] = value; | |
Approval( msg.sender, spender, value ); | |
return true; | |
} | |
function allowance(address owner, address spender) constant returns (uint _allowance) { | |
return _approvals[owner][spender]; | |
} | |
function safeToAdd(uint a, uint b) internal returns (bool) { | |
return (a + b >= a); | |
} | |
} | |
contract ControlledToken is ERC20Token { | |
mapping( address => address ) _control; | |
modifier isAllowedTransfer(address to, uint value) { | |
Control control = Control(_control[to]); | |
// compulsory register control | |
// if (_control[to] != 0x0 && control.allow(msg.sender, value)) { | |
// or: | |
// optional register control | |
if (_control[to] == 0x0 || control.allow(msg.sender, value)) { | |
_ | |
} | |
} | |
function registerControl( address control ) { | |
_control[msg.sender] = control; | |
} | |
function transfer( address to, uint value) | |
isAllowedTransfer(to, value) returns (bool ok) | |
{ | |
ERC20Token.transfer(to, value); | |
} | |
function transferFrom( address from, address to, uint value) | |
isAllowedTransfer(to, value) | |
returns (bool ok) { | |
ERC20Token.transferFrom(from, to, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment