Skip to content

Instantly share code, notes, and snippets.

@Dexaran
Created March 5, 2017 12:23
Show Gist options
  • Save Dexaran/48756500684b9d227fc1f7571de4d1a5 to your computer and use it in GitHub Desktop.
Save Dexaran/48756500684b9d227fc1f7571de4d1a5 to your computer and use it in GitHub Desktop.
ERC23 tokens
pragma solidity ^0.4.9;
contract Owned {
function owned() { owner = msg.sender; }
address owner;
modifier onlyOwner {
if (msg.sender != owner)
throw;
_;
}
}
/*
* ERC20 interface
* see https://github.com/ethereum/EIPs/issues/20
*/
/*
* Math operations with safety checks
*/
contract SafeMath {
function safeMul(uint a, uint b) internal returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function safeDiv(uint a, uint b) internal returns (uint) {
assert(b > 0);
uint c = a / b;
assert(a == b * c + a % b);
return c;
}
function safeSub(uint a, uint b) internal returns (uint) {
assert(b <= a);
return a - b;
}
function safeAdd(uint a, uint b) internal returns (uint) {
uint c = a + b;
assert(c>=a && c>=b);
return c;
}
function max64(uint64 a, uint64 b) internal constant returns (uint64) {
return a >= b ? a : b;
}
function min64(uint64 a, uint64 b) internal constant returns (uint64) {
return a < b ? a : b;
}
function max256(uint256 a, uint256 b) internal constant returns (uint256) {
return a >= b ? a : b;
}
function min256(uint256 a, uint256 b) internal constant returns (uint256) {
return a < b ? a : b;
}
function assert(bool assertion) internal {
if (!assertion) {
throw;
}
}
}
contract ERC20 {
uint public totalSupply;
function balanceOf(address who) constant returns (uint);
function allowance(address owner, address spender) constant returns (uint);
function transfer(address to, uint value) returns (bool ok);
function transferFrom(address from, address to, uint value) returns (bool ok);
function approve(address spender, uint value) returns (bool ok);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
/*
* Contract that is working with ERC23 tokens
* it will take only specified tokents and prevent accident token transfers from another ERC23 contracts
* like every contract is throwing accident ether transactions
*/
contract contractReciever is Owned{
//supported token contracts are stored here
mapping (address => bool) supportedTokens;
//contract creator can add supported tokens
function addToken(address _token) onlyOwner{
supportedTokens[_token]=true;
}
function fallbackToken(address _from, uint _value){
if(supportedTokens[msg.sender])
{
//on token transfer handler code here
}
//throw any accident transfers of not supported tokens
else{ throw; }
}
}
/*
* ERC23 token by Dexaran
*
* https://github.com/...
*/
contract StandardToken is ERC20, SafeMath {
mapping(address => uint) balances;
mapping (address => mapping (address => uint)) allowed;
function transfer(address _to, uint _value) returns (bool success) {
balances[msg.sender] = safeSub(balances[msg.sender], _value);
balances[_to] = safeAdd(balances[_to], _value);
Transfer(msg.sender, _to, _value);
return true;
}
function transferToContract(address _to, uint _value) returns (bool success) {
balances[msg.sender] = safeSub(balances[msg.sender], _value);
balances[_to] = safeAdd(balances[_to], _value);
contractReciever reciever = contractReciever(_to);
reciever.fallbackToken(msg.sender, _value);
Transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint _value) returns (bool success) {
var _allowance = allowed[_from][msg.sender];
// Check is not needed because safeSub(_allowance, _value) will already throw if this condition is not met
// if (_value > _allowance) throw;
balances[_to] = safeAdd(balances[_to], _value);
balances[_from] = safeSub(balances[_from], _value);
allowed[_from][msg.sender] = safeSub(_allowance, _value);
Transfer(_from, _to, _value);
return true;
}
function balanceOf(address _owner) constant returns (uint balance) {
return balances[_owner];
}
function approve(address _spender, uint _value) returns (bool success) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant returns (uint remaining) {
return allowed[_owner][_spender];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment