Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #933 from 0xProject/refactor/contracts/tokens
Browse files Browse the repository at this point in the history
[contracts] Refactor token implementations
  • Loading branch information
abandeali1 authored Aug 17, 2018
2 parents 8131a87 + e35788e commit ddf8511
Show file tree
Hide file tree
Showing 46 changed files with 4,771 additions and 834 deletions.
2 changes: 2 additions & 0 deletions packages/contracts/compiler.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"DummyERC20Token",
"DummyERC721Receiver",
"DummyERC721Token",
"DummyNoReturnERC20Token",
"ERC20Proxy",
"ERC20Token",
"ERC721Token",
Expand All @@ -32,6 +33,7 @@
"Forwarder",
"IAssetData",
"IAssetProxy",
"InvalidERC721Receiver",
"IValidator",
"IWallet",
"MixinAuthorizable",
Expand Down
12 changes: 8 additions & 4 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
"test:coverage": "SOLIDITY_COVERAGE=true run-s build run_mocha coverage:report:text coverage:report:lcov",
"test:profiler": "SOLIDITY_PROFILER=true run-s build run_mocha profiler:report:html",
"test:trace": "SOLIDITY_REVERT_TRACE=true run-s build run_mocha",
"run_mocha": "mocha --require source-map-support/register --require make-promises-safe 'lib/test/**/*.js' --timeout 100000 --bail --exit",
"run_mocha":
"mocha --require source-map-support/register --require make-promises-safe 'lib/test/**/*.js' --timeout 100000 --bail --exit",
"compile": "sol-compiler --contracts-dir src",
"clean": "shx rm -rf lib generated_contract_wrappers",
"generate_contract_wrappers": "abi-gen --abis ${npm_package_config_abis} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output generated_contract_wrappers --backend ethers",
"lint": "tslint --project . --exclude **/src/generated_contract_wrappers/**/* --exclude **/lib/**/* && yarn lint-contracts",
"generate_contract_wrappers":
"abi-gen --abis ${npm_package_config_abis} --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output generated_contract_wrappers --backend ethers",
"lint":
"tslint --project . --exclude **/src/generated_contract_wrappers/**/* --exclude **/lib/**/* && yarn lint-contracts",
"coverage:report:text": "istanbul report text",
"coverage:report:html": "istanbul report html && open coverage/index.html",
"profiler:report:html": "istanbul report html && open coverage/index.html",
Expand All @@ -33,7 +36,8 @@
"lint-contracts": "solhint src/2.0.0/**/**/**/**/*.sol"
},
"config": {
"abis": "../migrations/artifacts/2.0.0/@(AssetProxyOwner|DummyERC20Token|DummyERC721Receiver|DummyERC721Token|ERC20Proxy|ERC721Proxy|Forwarder|Exchange|ExchangeWrapper|IAssetData|IAssetProxy|MixinAuthorizable|MultiSigWallet|MultiSigWalletWithTimeLock|TestAssetProxyOwner|TestAssetProxyDispatcher|TestConstants|TestExchangeInternals|TestLibBytes|TestLibs|TestSignatureValidator|Validator|Wallet|TokenRegistry|Whitelist|WETH9|ZRXToken).json"
"abis":
"../migrations/artifacts/2.0.0/@(AssetProxyOwner|DummyERC20Token|DummyERC721Receiver|DummyERC721Token|DummyNoReturnERC20Token|ERC20Proxy|ERC721Proxy|Forwarder|Exchange|ExchangeWrapper|IAssetData|IAssetProxy|InvalidERC721Receiver|MixinAuthorizable|MultiSigWallet|MultiSigWalletWithTimeLock|TestAssetProxyOwner|TestAssetProxyDispatcher|TestConstants|TestExchangeInternals|TestLibBytes|TestLibs|TestSignatureValidator|Validator|Wallet|TokenRegistry|Whitelist|WETH9|ZRXToken).json"
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

pragma solidity 0.4.24;

import "../Mintable/Mintable.sol";
import "../../utils/Ownable/Ownable.sol";
import "../../tokens/ERC20Token/MintableERC20Token.sol";


contract DummyERC20Token is
Mintable,
Ownable
Ownable,
MintableERC20Token
{
string public name;
string public symbol;
uint256 public decimals;
uint256 public constant MAX_MINT_AMOUNT = 10000000000000000000000;

constructor (
string _name,
Expand All @@ -41,20 +42,36 @@ contract DummyERC20Token is
name = _name;
symbol = _symbol;
decimals = _decimals;
totalSupply = _totalSupply;
_totalSupply = _totalSupply;
balances[msg.sender] = _totalSupply;
}

/// @dev Sets the balance of target address
/// @param _target Address or which balance will be updated
/// @param _value New balance of target address
function setBalance(address _target, uint256 _value)
public
external
onlyOwner
{
uint256 currBalance = balanceOf(_target);
uint256 currBalance = balances[_target];
if (_value < currBalance) {
totalSupply = safeSub(totalSupply, safeSub(currBalance, _value));
_totalSupply = safeSub(_totalSupply, safeSub(currBalance, _value));
} else {
totalSupply = safeAdd(totalSupply, safeSub(_value, currBalance));
_totalSupply = safeAdd(_totalSupply, safeSub(_value, currBalance));
}
balances[_target] = _value;
}

/// @dev Mints new tokens for sender
/// @param _value Amount of tokens to mint
function mint(uint256 _value)
external
{
require(
_value <= MAX_MINT_AMOUNT,
"VALUE_TOO_LARGE"
);

_mint(msg.sender, _value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2018 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.24;

import "./DummyERC20Token.sol";


// solhint-disable no-empty-blocks
contract DummyNoReturnERC20Token is
DummyERC20Token
{

constructor (
string _name,
string _symbol,
uint256 _decimals,
uint256 _totalSupply
)
public
DummyERC20Token(
_name,
_symbol,
_decimals,
_totalSupply
)
{}

/// @dev send `value` token to `to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
function transfer(address _to, uint256 _value)
external
returns (bool)
{
require(
balances[msg.sender] >= _value,
"ERC20_INSUFFICIENT_BALANCE"
);
require(
balances[_to] + _value >= balances[_to],
"UINT256_OVERFLOW"
);

balances[msg.sender] -= _value;
balances[_to] += _value;

emit Transfer(
msg.sender,
_to,
_value
);

// HACK: This contract will not compile if we remove `returns (bool)`, so we manually return no data
assembly {
return(0, 0)
}
}

/// @dev send `value` token to `to` from `from` on the condition it is approved by `from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
function transferFrom(
address _from,
address _to,
uint256 _value
)
external
returns (bool)
{
require(
balances[_from] >= _value,
"ERC20_INSUFFICIENT_BALANCE"
);
require(
allowed[_from][msg.sender] >= _value,
"ERC20_INSUFFICIENT_ALLOWANCE"
);
require(
balances[_to] + _value >= balances[_to],
"UINT256_OVERFLOW"
);

balances[_to] += _value;
balances[_from] -= _value;
allowed[_from][msg.sender] -= _value;

emit Transfer(
_from,
_to,
_value
);

// HACK: This contract will not compile if we remove `returns (bool)`, so we manually return no data
assembly {
return(0, 0)
}
}
}

Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Smart Contract Solutions, Inc.
Copyright 2018 ZeroEx Intl.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
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
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
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.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

pragma solidity 0.4.24;
Expand All @@ -32,33 +25,44 @@ contract DummyERC721Receiver is
IERC721Receiver
{

// Function selector for ERC721Receiver.onERC721Received
// 0x150b7a02
bytes4 constant internal ERC721_RECEIVED = bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));

event TokenReceived(
address operator,
address from,
uint256 tokenId,
bytes data
);

/**
* @notice Handle the receipt of an NFT
* @dev The ERC721 smart contract calls this function on the recipient
* after a `safetransfer`. This function MAY throw to revert and reject the
* transfer. This function MUST use 50,000 gas or less. Return of other
* than the magic value MUST result in the transaction being reverted.
* Note: the contract address is always the message sender.
* @param _from The sending address
* @param _tokenId The NFT identifier which is being transfered
* @param _data Additional data with no specified format
* @return `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
*/
/// @notice Handle the receipt of an NFT
/// @dev The ERC721 smart contract calls this function on the recipient
/// after a `transfer`. This function MAY throw to revert and reject the
/// transfer. Return of other than the magic value MUST result in the
/// transaction being reverted.
/// Note: the contract address is always the message sender.
/// @param _operator The address which called `safeTransferFrom` function
/// @param _from The address which previously owned the token
/// @param _tokenId The NFT identifier which is being transferred
/// @param _data Additional data with no specified format
/// @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
/// unless throwing
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes _data
)
public
external
returns (bytes4)
{
emit TokenReceived(_from, _tokenId, _data);
emit TokenReceived(
_operator,
_from,
_tokenId,
_data
);
return ERC721_RECEIVED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2018 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.24;

import "../../tokens/ERC721Token/IERC721Receiver.sol";


contract InvalidERC721Receiver is
IERC721Receiver
{
// Actual function signature is `onERC721Received(address,address,uint256,bytes)`
bytes4 constant internal INVALID_ERC721_RECEIVED = bytes4(keccak256("onERC721Received(address,uint256,bytes)"));

event TokenReceived(
address operator,
address from,
uint256 tokenId,
bytes data
);

/// @notice Handle the receipt of an NFT
/// @dev The ERC721 smart contract calls this function on the recipient
/// after a `transfer`. This function MAY throw to revert and reject the
/// transfer. Return of other than the magic value MUST result in the
/// transaction being reverted.
/// Note: the contract address is always the message sender.
/// @param _operator The address which called `safeTransferFrom` function
/// @param _from The address which previously owned the token
/// @param _tokenId The NFT identifier which is being transferred
/// @param _data Additional data with no specified format
/// @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
/// unless throwing
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes _data
)
external
returns (bytes4)
{
emit TokenReceived(
_operator,
_from,
_tokenId,
_data
);
return INVALID_ERC721_RECEIVED;
}
}
Loading

0 comments on commit ddf8511

Please sign in to comment.