Refactor unlimited allowance logic out of ERC20Token

This commit is contained in:
Amir Bandeali
2017-12-11 13:19:13 -08:00
parent 8b29f6f18d
commit 6f2cb66163
8 changed files with 56 additions and 20 deletions

View File

@@ -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,

View File

@@ -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
{

View File

@@ -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;
}

View File

@@ -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
{

View File

@@ -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;
}
}

View File

@@ -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,
};