diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2017-12-12 05:19:13 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2017-12-13 07:05:29 +0800 |
commit | 6f2cb661638f86bae7a000f69cca1a6ba32a7087 (patch) | |
tree | 808fdead32d97db2e6ee32d5b4f2ad5f4524869c /packages/contracts | |
parent | 8b29f6f18de0af62cb1737a3b6d4cc188b764b97 (diff) | |
download | dexon-sol-tools-6f2cb661638f86bae7a000f69cca1a6ba32a7087.tar.gz dexon-sol-tools-6f2cb661638f86bae7a000f69cca1a6ba32a7087.tar.zst dexon-sol-tools-6f2cb661638f86bae7a000f69cca1a6ba32a7087.zip |
Refactor unlimited allowance logic out of ERC20Token
Diffstat (limited to 'packages/contracts')
-rw-r--r-- | packages/contracts/contracts/test/DummyToken_v2.sol | 2 | ||||
-rw-r--r-- | packages/contracts/contracts/test/Mintable_v2.sol | 4 | ||||
-rw-r--r-- | packages/contracts/contracts/tokens/ERC20Token.sol | 16 | ||||
-rw-r--r-- | packages/contracts/contracts/tokens/EtherToken_v2.sol | 6 | ||||
-rw-r--r-- | packages/contracts/contracts/tokens/UnlimitedAllowanceToken_v2.sol | 46 | ||||
-rw-r--r-- | packages/contracts/test/ts/unlimited_allowance_token.ts (renamed from packages/contracts/test/ts/unlimitedAllowanceToken.ts) | 0 | ||||
-rw-r--r-- | packages/contracts/test/ts/unlimited_allowance_token_v2.ts (renamed from packages/contracts/test/ts/erc20Token.ts) | 2 | ||||
-rw-r--r-- | packages/contracts/test/ts/zrx_token.ts (renamed from packages/contracts/test/ts/zrxToken.ts) | 0 |
8 files changed, 56 insertions, 20 deletions
diff --git a/packages/contracts/contracts/test/DummyToken_v2.sol b/packages/contracts/contracts/test/DummyToken_v2.sol index 24c604369..e3392bf64 100644 --- a/packages/contracts/contracts/test/DummyToken_v2.sol +++ b/packages/contracts/contracts/test/DummyToken_v2.sol @@ -8,7 +8,7 @@ contract DummyToken_v2 is Mintable_v2, Ownable_v2 { string public symbol; uint public decimals; - function DummyToken( + function DummyToken_v2( string _name, string _symbol, uint _decimals, diff --git a/packages/contracts/contracts/test/Mintable_v2.sol b/packages/contracts/contracts/test/Mintable_v2.sol index 95e2fcfe3..a2605791f 100644 --- a/packages/contracts/contracts/test/Mintable_v2.sol +++ b/packages/contracts/contracts/test/Mintable_v2.sol @@ -1,13 +1,13 @@ pragma solidity 0.4.18; -import "./../tokens/ERC20Token.sol"; +import "./../tokens/UnlimitedAllowanceToken_v2.sol"; import "./../lib/SafeMath_v2.sol"; /* * Mintable * Base contract that creates a mintable UnlimitedAllowanceToken */ -contract Mintable_v2 is ERC20Token, SafeMath_v2 { +contract Mintable_v2 is UnlimitedAllowanceToken_v2, SafeMath_v2 { function mint(uint _value) public { diff --git a/packages/contracts/contracts/tokens/ERC20Token.sol b/packages/contracts/contracts/tokens/ERC20Token.sol index 4596331c1..5ec81032f 100644 --- a/packages/contracts/contracts/tokens/ERC20Token.sol +++ b/packages/contracts/contracts/tokens/ERC20Token.sol @@ -1,11 +1,9 @@ -pragma solidity ^0.4.18; +pragma solidity 0.4.18; import "./Token_v2.sol"; contract ERC20Token is Token_v2 { - uint constant MAX_UINT = 2**256 - 1; - function transfer(address _to, uint _value) public returns (bool) @@ -17,22 +15,14 @@ contract ERC20Token is Token_v2 { return true; } - /// @dev ERC20 transferFrom, modified such that an allowance of MAX_UINT represents an unlimited allowance. See https://github.com/ethereum/EIPs/issues/717 - /// @param _from Address to transfer from. - /// @param _to Address to transfer to. - /// @param _value Amount to transfer. - /// @return Success of transfer. function transferFrom(address _from, address _to, uint _value) public returns (bool) { - uint allowance = allowed[_from][msg.sender]; - require(balances[_from] >= _value && allowance >= _value && balances[_to] + _value >= balances[_to]); + require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value >= balances[_to]); balances[_to] += _value; balances[_from] -= _value; - if (allowance < MAX_UINT) { - allowed[_from][msg.sender] -= _value; - } + allowed[_from][msg.sender] -= _value; Transfer(_from, _to, _value); return true; } diff --git a/packages/contracts/contracts/tokens/EtherToken_v2.sol b/packages/contracts/contracts/tokens/EtherToken_v2.sol index d56d54432..a501013e4 100644 --- a/packages/contracts/contracts/tokens/EtherToken_v2.sol +++ b/packages/contracts/contracts/tokens/EtherToken_v2.sol @@ -18,10 +18,10 @@ pragma solidity 0.4.18; -import "./ERC20Token.sol"; +import "./UnlimitedAllowanceToken_v2.sol"; import "./../lib/SafeMath_v2.sol"; -contract EtherToken_v2 is ERC20Token, SafeMath_v2 { +contract EtherToken_v2 is UnlimitedAllowanceToken_v2, SafeMath_v2 { string constant public name = "Ether Token"; string constant public symbol = "WETH"; @@ -46,7 +46,7 @@ contract EtherToken_v2 is ERC20Token, SafeMath_v2 { } /// @dev Sells tokens in exchange for Ether, exchanging them 1:1. - /// @param amount Number of tokens to sell. + /// @param _value Number of tokens to sell. function withdraw(uint _value) public { diff --git a/packages/contracts/contracts/tokens/UnlimitedAllowanceToken_v2.sol b/packages/contracts/contracts/tokens/UnlimitedAllowanceToken_v2.sol new file mode 100644 index 000000000..2da54fee2 --- /dev/null +++ b/packages/contracts/contracts/tokens/UnlimitedAllowanceToken_v2.sol @@ -0,0 +1,46 @@ +/* + + Copyright 2017 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.18; + +import "./ERC20Token.sol"; + +contract UnlimitedAllowanceToken_v2 is ERC20Token { + + uint constant MAX_UINT = 2**256 - 1; + + /// @dev ERC20 transferFrom, modified such that an allowance of MAX_UINT represents an unlimited allowance. See https://github.com/ethereum/EIPs/issues/717 + /// @param _from Address to transfer from. + /// @param _to Address to transfer to. + /// @param _value Amount to transfer. + /// @return Success of transfer. + function transferFrom(address _from, address _to, uint _value) + public + returns (bool) + { + uint allowance = allowed[_from][msg.sender]; + require(balances[_from] >= _value && allowance >= _value && balances[_to] + _value >= balances[_to]); + balances[_to] += _value; + balances[_from] -= _value; + if (allowance < MAX_UINT) { + allowed[_from][msg.sender] -= _value; + } + Transfer(_from, _to, _value); + return true; + } +} diff --git a/packages/contracts/test/ts/unlimitedAllowanceToken.ts b/packages/contracts/test/ts/unlimited_allowance_token.ts index 3b4d2db77..3b4d2db77 100644 --- a/packages/contracts/test/ts/unlimitedAllowanceToken.ts +++ b/packages/contracts/test/ts/unlimited_allowance_token.ts diff --git a/packages/contracts/test/ts/erc20Token.ts b/packages/contracts/test/ts/unlimited_allowance_token_v2.ts index 1edcfcbbd..73e12f00c 100644 --- a/packages/contracts/test/ts/erc20Token.ts +++ b/packages/contracts/test/ts/unlimited_allowance_token_v2.ts @@ -14,7 +14,7 @@ const web3: Web3 = (global as any).web3; chaiSetup.configure(); const expect = chai.expect; -contract('ERC20Token', (accounts: string[]) => { +contract('UnlimitedAllowanceTokenV2', (accounts: string[]) => { const config = { networkId: constants.TESTRPC_NETWORK_ID, }; diff --git a/packages/contracts/test/ts/zrxToken.ts b/packages/contracts/test/ts/zrx_token.ts index 1c9655402..1c9655402 100644 --- a/packages/contracts/test/ts/zrxToken.ts +++ b/packages/contracts/test/ts/zrx_token.ts |