From f53ec8613ed9864286fb33507574d3458bc30823 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 12 Oct 2016 20:03:23 -0300 Subject: [PATCH 1/3] new token base --- contracts/BaseToken.sol | 54 ++++++++++++++++++++++++++++++ contracts/ERC20.sol | 4 +++ contracts/Token.sol | 73 ----------------------------------------- 3 files changed, 58 insertions(+), 73 deletions(-) create mode 100644 contracts/BaseToken.sol delete mode 100644 contracts/Token.sol diff --git a/contracts/BaseToken.sol b/contracts/BaseToken.sol new file mode 100644 index 000000000..4fbea044f --- /dev/null +++ b/contracts/BaseToken.sol @@ -0,0 +1,54 @@ +pragma solidity ^0.4.0; + +// Everything throws instead of returning false on failure. +import './ERC20.sol'; + +/** + * ERC 20 token + * + * https://github.com/ethereum/EIPs/issues/20 + * Based on original code by FirstBlood: + * https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol + */ +contract BaseToken is ERC20 { + + function transfer(address _to, uint256 _value) returns (bool success) { + if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) { + balances[msg.sender] -= _value; + balances[_to] += _value; + Transfer(msg.sender, _to, _value); + return true; + } else { return false; } + } + + function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { + if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) { + balances[_to] += _value; + balances[_from] -= _value; + allowed[_from][msg.sender] -= _value; + Transfer(_from, _to, _value); + return true; + } else { return false; } + } + + function balanceOf(address _owner) constant returns (uint256 balance) { + return balances[_owner]; + } + + function approve(address _spender, uint256 _value) returns (bool success) { + allowed[msg.sender][_spender] = _value; + Approval(msg.sender, _spender, _value); + return true; + } + + function allowance(address _owner, address _spender) constant returns (uint256 remaining) { + return allowed[_owner][_spender]; + } + + mapping(address => uint256) balances; + + mapping (address => mapping (address => uint256)) allowed; + + uint256 public totalSupply; + +} diff --git a/contracts/ERC20.sol b/contracts/ERC20.sol index 2ed37f73e..157bd9077 100644 --- a/contracts/ERC20.sol +++ b/contracts/ERC20.sol @@ -1,4 +1,8 @@ pragma solidity ^0.4.0; + + +// see https://github.com/ethereum/EIPs/issues/20 + contract ERC20 { function totalSupply() constant returns (uint); function balanceOf(address who) constant returns (uint); diff --git a/contracts/Token.sol b/contracts/Token.sol deleted file mode 100644 index 5e22ec2eb..000000000 --- a/contracts/Token.sol +++ /dev/null @@ -1,73 +0,0 @@ -pragma solidity ^0.4.0; -// Source: https://github.com/nexusdev/erc20 -// Flat file implementation of `dappsys/token/base.sol::DSTokenBase` - -// Everything throws instead of returning false on failure. -import './ERC20.sol'; - -contract Token is ERC20 { - - mapping( address => uint ) _balances; - mapping( address => mapping( address => uint ) ) _approvals; - uint _supply; - - function Token( 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); - } -} From f8c486ea1b729d979697c8490665f23ab25caddd Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 12 Oct 2016 20:07:15 -0300 Subject: [PATCH 2/3] style changes to base token --- contracts/BaseToken.sol | 80 +++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/contracts/BaseToken.sol b/contracts/BaseToken.sol index 4fbea044f..c3fdc8134 100644 --- a/contracts/BaseToken.sol +++ b/contracts/BaseToken.sol @@ -7,48 +7,50 @@ import './ERC20.sol'; * ERC 20 token * * https://github.com/ethereum/EIPs/issues/20 - * Based on original code by FirstBlood: + * Based on code by FirstBlood: * https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol */ contract BaseToken is ERC20 { - function transfer(address _to, uint256 _value) returns (bool success) { - if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) { - balances[msg.sender] -= _value; - balances[_to] += _value; - Transfer(msg.sender, _to, _value); - return true; - } else { return false; } - } - - function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { - if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) { - balances[_to] += _value; - balances[_from] -= _value; - allowed[_from][msg.sender] -= _value; - Transfer(_from, _to, _value); - return true; - } else { return false; } - } - - function balanceOf(address _owner) constant returns (uint256 balance) { - return balances[_owner]; - } - - function approve(address _spender, uint256 _value) returns (bool success) { - allowed[msg.sender][_spender] = _value; - Approval(msg.sender, _spender, _value); - return true; - } - - function allowance(address _owner, address _spender) constant returns (uint256 remaining) { - return allowed[_owner][_spender]; - } - - mapping(address => uint256) balances; - - mapping (address => mapping (address => uint256)) allowed; - - uint256 public totalSupply; + mapping(address => uint256) balances; + mapping (address => mapping (address => uint256)) allowed; + uint256 public totalSupply; + + + function transfer(address _to, uint256 _value) returns (bool success) { + if (balances[msg.sender] >= _value && + balances[_to] + _value > balances[_to]) { + balances[msg.sender] -= _value; + balances[_to] += _value; + Transfer(msg.sender, _to, _value); + return true; + } else { return false; } + } + + function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { + if (balances[_from] >= _value && + allowed[_from][msg.sender] >= _value && + balances[_to] + _value > balances[_to]) { + balances[_to] += _value; + balances[_from] -= _value; + allowed[_from][msg.sender] -= _value; + Transfer(_from, _to, _value); + return true; + } else { return false; } + } + + function balanceOf(address _owner) constant returns (uint256 balance) { + return balances[_owner]; + } + + function approve(address _spender, uint256 _value) returns (bool success) { + allowed[msg.sender][_spender] = _value; + Approval(msg.sender, _spender, _value); + return true; + } + + function allowance(address _owner, address _spender) constant returns (uint256 remaining) { + return allowed[_owner][_spender]; + } } From 0a0f8c58a277a5ea1002bb2fe282d3afe7da93c4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 13 Oct 2016 15:33:25 -0300 Subject: [PATCH 3/3] StandardToken first version --- contracts/SafeMath.sol | 28 +++++++++++++ .../{BaseToken.sol => StandardToken.sol} | 41 ++++++++++--------- 2 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 contracts/SafeMath.sol rename contracts/{BaseToken.sol => StandardToken.sol} (58%) diff --git a/contracts/SafeMath.sol b/contracts/SafeMath.sol new file mode 100644 index 000000000..2d4bebf77 --- /dev/null +++ b/contracts/SafeMath.sol @@ -0,0 +1,28 @@ +pragma solidity ^0.4.0; + +/** + * 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 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 assert(bool assertion) internal { + if (!assertion) throw; + } +} + diff --git a/contracts/BaseToken.sol b/contracts/StandardToken.sol similarity index 58% rename from contracts/BaseToken.sol rename to contracts/StandardToken.sol index c3fdc8134..3d3c3e1d0 100644 --- a/contracts/BaseToken.sol +++ b/contracts/StandardToken.sol @@ -1,42 +1,43 @@ pragma solidity ^0.4.0; -// Everything throws instead of returning false on failure. import './ERC20.sol'; +import './SafeMath.sol'; /** - * ERC 20 token + * ERC20 token * * https://github.com/ethereum/EIPs/issues/20 * Based on code by FirstBlood: * https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol */ -contract BaseToken is ERC20 { +contract StandardToken is ERC20, SafeMath { mapping(address => uint256) balances; mapping (address => mapping (address => uint256)) allowed; uint256 public totalSupply; - function transfer(address _to, uint256 _value) returns (bool success) { - if (balances[msg.sender] >= _value && - balances[_to] + _value > balances[_to]) { - balances[msg.sender] -= _value; - balances[_to] += _value; - Transfer(msg.sender, _to, _value); - return true; - } else { return false; } + if (balances[msg.sender] < _value) { + throw; + } + balances[msg.sender] = safeSub(balances[msg.sender], _value); + balances[_to] = safeAdd(balances[_to], _value); + Transfer(msg.sender, _to, _value); + return true; } function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { - if (balances[_from] >= _value && - allowed[_from][msg.sender] >= _value && - balances[_to] + _value > balances[_to]) { - balances[_to] += _value; - balances[_from] -= _value; - allowed[_from][msg.sender] -= _value; - Transfer(_from, _to, _value); - return true; - } else { return false; } + var _allowance = allowed[_from][msg.sender]; + if (balances[_from] < _value || + _allowance < _value)) { + 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 (uint256 balance) {