@0x:contracts-utils
Removed SafeMath and the use of the contract throughout contracts/
This commit is contained in:
parent
90193c8197
commit
41d99e77c7
@ -19,7 +19,7 @@
|
|||||||
pragma solidity ^0.5.9;
|
pragma solidity ^0.5.9;
|
||||||
|
|
||||||
import "@0x/contracts-utils/contracts/src/LibBytes.sol";
|
import "@0x/contracts-utils/contracts/src/LibBytes.sol";
|
||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
import "@0x/contracts-erc1155/contracts/src/interfaces/IERC1155.sol";
|
import "@0x/contracts-erc1155/contracts/src/interfaces/IERC1155.sol";
|
||||||
import "../archive/MixinAuthorizable.sol";
|
import "../archive/MixinAuthorizable.sol";
|
||||||
import "./interfaces/IAssetProxy.sol";
|
import "./interfaces/IAssetProxy.sol";
|
||||||
@ -27,10 +27,10 @@ import "./interfaces/IAssetProxy.sol";
|
|||||||
|
|
||||||
contract ERC1155Proxy is
|
contract ERC1155Proxy is
|
||||||
MixinAuthorizable,
|
MixinAuthorizable,
|
||||||
SafeMath,
|
|
||||||
IAssetProxy
|
IAssetProxy
|
||||||
{
|
{
|
||||||
using LibBytes for bytes;
|
using LibBytes for bytes;
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
// Id of this proxy.
|
// Id of this proxy.
|
||||||
bytes4 constant internal PROXY_ID = bytes4(keccak256("ERC1155Assets(address,uint256[],uint256[],bytes)"));
|
bytes4 constant internal PROXY_ID = bytes4(keccak256("ERC1155Assets(address,uint256[],uint256[],bytes)"));
|
||||||
@ -71,7 +71,7 @@ contract ERC1155Proxy is
|
|||||||
// to avoid copying over `ids` or `data`. This is possible if they are
|
// to avoid copying over `ids` or `data`. This is possible if they are
|
||||||
// identical to `values` and the offsets for each are pointing to the
|
// identical to `values` and the offsets for each are pointing to the
|
||||||
// same location in the ABI encoded calldata.
|
// same location in the ABI encoded calldata.
|
||||||
scaledValues[i] = _safeMul(values[i], amount);
|
scaledValues[i] = values[i].safeMul(amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute `safeBatchTransferFrom` call
|
// Execute `safeBatchTransferFrom` call
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
pragma solidity ^0.5.9;
|
pragma solidity ^0.5.9;
|
||||||
|
|
||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
import "@0x/contracts-utils/contracts/src/LibAddress.sol";
|
import "@0x/contracts-utils/contracts/src/LibAddress.sol";
|
||||||
import "./interfaces/IERC1155.sol";
|
import "./interfaces/IERC1155.sol";
|
||||||
import "./interfaces/IERC1155Receiver.sol";
|
import "./interfaces/IERC1155Receiver.sol";
|
||||||
@ -26,11 +26,11 @@ import "./MixinNonFungibleToken.sol";
|
|||||||
|
|
||||||
|
|
||||||
contract ERC1155 is
|
contract ERC1155 is
|
||||||
SafeMath,
|
|
||||||
IERC1155,
|
IERC1155,
|
||||||
MixinNonFungibleToken
|
MixinNonFungibleToken
|
||||||
{
|
{
|
||||||
using LibAddress for address;
|
using LibAddress for address;
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
// selectors for receiver callbacks
|
// selectors for receiver callbacks
|
||||||
bytes4 constant public ERC1155_RECEIVED = 0xf23a6e61;
|
bytes4 constant public ERC1155_RECEIVED = 0xf23a6e61;
|
||||||
@ -88,11 +88,11 @@ contract ERC1155 is
|
|||||||
nfOwners[id] = to;
|
nfOwners[id] = to;
|
||||||
// You could keep balance of NF type in base type id like so:
|
// You could keep balance of NF type in base type id like so:
|
||||||
// uint256 baseType = getNonFungibleBaseType(_id);
|
// uint256 baseType = getNonFungibleBaseType(_id);
|
||||||
// balances[baseType][_from] = balances[baseType][_from]._safeSub(_value);
|
// balances[baseType][_from] = balances[baseType][_from].safeSub(_value);
|
||||||
// balances[baseType][_to] = balances[baseType][_to]._safeAdd(_value);
|
// balances[baseType][_to] = balances[baseType][_to].safeAdd(_value);
|
||||||
} else {
|
} else {
|
||||||
balances[id][from] = _safeSub(balances[id][from], value);
|
balances[id][from] = balances[id][from].safeSub(value);
|
||||||
balances[id][to] = _safeAdd(balances[id][to], value);
|
balances[id][to] = balances[id][to].safeAdd(value);
|
||||||
}
|
}
|
||||||
emit TransferSingle(msg.sender, from, to, id, value);
|
emit TransferSingle(msg.sender, from, to, id, value);
|
||||||
|
|
||||||
@ -170,8 +170,8 @@ contract ERC1155 is
|
|||||||
);
|
);
|
||||||
nfOwners[id] = to;
|
nfOwners[id] = to;
|
||||||
} else {
|
} else {
|
||||||
balances[id][from] = _safeSub(balances[id][from], value);
|
balances[id][from] = balances[id][from].safeSub(value);
|
||||||
balances[id][to] = _safeAdd(balances[id][to], value);
|
balances[id][to] = balances[id][to].safeAdd(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emit TransferBatch(msg.sender, from, to, ids, values);
|
emit TransferBatch(msg.sender, from, to, ids, values);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
pragma solidity ^0.5.9;
|
pragma solidity ^0.5.9;
|
||||||
|
|
||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
import "./ERC1155.sol";
|
import "./ERC1155.sol";
|
||||||
import "./interfaces/IERC1155Mintable.sol";
|
import "./interfaces/IERC1155Mintable.sol";
|
||||||
|
|
||||||
@ -11,6 +11,7 @@ contract ERC1155Mintable is
|
|||||||
IERC1155Mintable,
|
IERC1155Mintable,
|
||||||
ERC1155
|
ERC1155
|
||||||
{
|
{
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
/// token nonce
|
/// token nonce
|
||||||
uint256 internal nonce;
|
uint256 internal nonce;
|
||||||
@ -37,7 +38,7 @@ contract ERC1155Mintable is
|
|||||||
)
|
)
|
||||||
external
|
external
|
||||||
returns (uint256 type_)
|
returns (uint256 type_)
|
||||||
{
|
{
|
||||||
// Store the type in the upper 128 bits
|
// Store the type in the upper 128 bits
|
||||||
type_ = (++nonce << 128);
|
type_ = (++nonce << 128);
|
||||||
|
|
||||||
@ -114,7 +115,7 @@ contract ERC1155Mintable is
|
|||||||
uint256 quantity = quantities[i];
|
uint256 quantity = quantities[i];
|
||||||
|
|
||||||
// Grant the items to the caller
|
// Grant the items to the caller
|
||||||
balances[id][dst] = _safeAdd(quantity, balances[id][dst]);
|
balances[id][dst] = quantity.safeAdd(balances[id][dst]);
|
||||||
|
|
||||||
// Emit the Transfer/Mint event.
|
// Emit the Transfer/Mint event.
|
||||||
// the 0x0 source address implies a mint
|
// the 0x0 source address implies a mint
|
||||||
@ -172,7 +173,7 @@ contract ERC1155Mintable is
|
|||||||
nfOwners[id] = dst;
|
nfOwners[id] = dst;
|
||||||
|
|
||||||
// You could use base-type id to store NF type balances if you wish.
|
// You could use base-type id to store NF type balances if you wish.
|
||||||
// balances[_type][dst] = quantity._safeAdd(balances[_type][dst]);
|
// balances[_type][dst] = quantity.safeAdd(balances[_type][dst]);
|
||||||
|
|
||||||
emit TransferSingle(msg.sender, address(0x0), dst, id, 1);
|
emit TransferSingle(msg.sender, address(0x0), dst, id, 1);
|
||||||
|
|
||||||
@ -194,6 +195,6 @@ contract ERC1155Mintable is
|
|||||||
|
|
||||||
// record the `maxIndex` of this nft type
|
// record the `maxIndex` of this nft type
|
||||||
// this allows us to mint more nft's of this type in a subsequent call.
|
// this allows us to mint more nft's of this type in a subsequent call.
|
||||||
maxIndex[type_] = _safeAdd(to.length, maxIndex[type_]);
|
maxIndex[type_] = to.length.safeAdd(maxIndex[type_]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,22 +18,23 @@
|
|||||||
|
|
||||||
pragma solidity ^0.5.9;
|
pragma solidity ^0.5.9;
|
||||||
|
|
||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
import "./UnlimitedAllowanceERC20Token.sol";
|
import "./UnlimitedAllowanceERC20Token.sol";
|
||||||
|
|
||||||
|
|
||||||
contract MintableERC20Token is
|
contract MintableERC20Token is
|
||||||
SafeMath,
|
|
||||||
UnlimitedAllowanceERC20Token
|
UnlimitedAllowanceERC20Token
|
||||||
{
|
{
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
/// @dev Mints new tokens
|
/// @dev Mints new tokens
|
||||||
/// @param _to Address of the beneficiary that will own the minted token
|
/// @param _to Address of the beneficiary that will own the minted token
|
||||||
/// @param _value Amount of tokens to mint
|
/// @param _value Amount of tokens to mint
|
||||||
function _mint(address _to, uint256 _value)
|
function _mint(address _to, uint256 _value)
|
||||||
internal
|
internal
|
||||||
{
|
{
|
||||||
balances[_to] = _safeAdd(_value, balances[_to]);
|
balances[_to] = _value.safeAdd(balances[_to]);
|
||||||
_totalSupply = _safeAdd(_totalSupply, _value);
|
_totalSupply = _totalSupply.safeAdd(_value);
|
||||||
|
|
||||||
emit Transfer(
|
emit Transfer(
|
||||||
address(0),
|
address(0),
|
||||||
@ -48,8 +49,8 @@ contract MintableERC20Token is
|
|||||||
function _burn(address _owner, uint256 _value)
|
function _burn(address _owner, uint256 _value)
|
||||||
internal
|
internal
|
||||||
{
|
{
|
||||||
balances[_owner] = _safeSub(balances[_owner], _value);
|
balances[_owner] = balances[_owner].safeSub(_value);
|
||||||
_totalSupply = _safeSub(_totalSupply, _value);
|
_totalSupply = _totalSupply.safeSub(_value);
|
||||||
|
|
||||||
emit Transfer(
|
emit Transfer(
|
||||||
_owner,
|
_owner,
|
||||||
|
@ -18,14 +18,17 @@
|
|||||||
|
|
||||||
pragma solidity ^0.5.5;
|
pragma solidity ^0.5.5;
|
||||||
|
|
||||||
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
import "@0x/contracts-utils/contracts/src/Ownable.sol";
|
import "@0x/contracts-utils/contracts/src/Ownable.sol";
|
||||||
import "../src/MintableERC20Token.sol";
|
import "../src/MintableERC20Token.sol";
|
||||||
|
|
||||||
|
|
||||||
contract DummyERC20Token is
|
contract DummyERC20Token is
|
||||||
Ownable,
|
Ownable,
|
||||||
MintableERC20Token
|
MintableERC20Token
|
||||||
{
|
{
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
string public name;
|
string public name;
|
||||||
string public symbol;
|
string public symbol;
|
||||||
uint256 public decimals;
|
uint256 public decimals;
|
||||||
@ -55,9 +58,9 @@ contract DummyERC20Token is
|
|||||||
{
|
{
|
||||||
uint256 currBalance = balances[_target];
|
uint256 currBalance = balances[_target];
|
||||||
if (_value < currBalance) {
|
if (_value < currBalance) {
|
||||||
_totalSupply = _safeSub(_totalSupply, _safeSub(currBalance, _value));
|
_totalSupply = _totalSupply.safeSub(currBalance.safeSub(_value));
|
||||||
} else {
|
} else {
|
||||||
_totalSupply = _safeAdd(_totalSupply, _safeSub(_value, currBalance));
|
_totalSupply = _totalSupply.safeAdd(_value.safeSub(currBalance));
|
||||||
}
|
}
|
||||||
balances[_target] = _value;
|
balances[_target] = _value;
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,14 @@ pragma solidity ^0.5.9;
|
|||||||
|
|
||||||
import "./interfaces/IERC721Token.sol";
|
import "./interfaces/IERC721Token.sol";
|
||||||
import "./interfaces/IERC721Receiver.sol";
|
import "./interfaces/IERC721Receiver.sol";
|
||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
|
|
||||||
|
|
||||||
contract ERC721Token is
|
contract ERC721Token is
|
||||||
IERC721Token,
|
IERC721Token
|
||||||
SafeMath
|
|
||||||
{
|
{
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
// Function selector for ERC721Receiver.onERC721Received
|
// Function selector for ERC721Receiver.onERC721Received
|
||||||
// 0x150b7a02
|
// 0x150b7a02
|
||||||
bytes4 constant internal ERC721_RECEIVED = bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
|
bytes4 constant internal ERC721_RECEIVED = bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
|
||||||
@ -163,7 +164,7 @@ contract ERC721Token is
|
|||||||
_approved
|
_approved
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @notice Count all NFTs assigned to an owner
|
/// @notice Count all NFTs assigned to an owner
|
||||||
/// @dev NFTs assigned to the zero address are considered invalid, and this
|
/// @dev NFTs assigned to the zero address are considered invalid, and this
|
||||||
/// function throws for queries about the zero address.
|
/// function throws for queries about the zero address.
|
||||||
@ -223,9 +224,9 @@ contract ERC721Token is
|
|||||||
}
|
}
|
||||||
|
|
||||||
owners[_tokenId] = _to;
|
owners[_tokenId] = _to;
|
||||||
balances[_from] = _safeSub(balances[_from], 1);
|
balances[_from] = balances[_from].safeSub(1);
|
||||||
balances[_to] = _safeAdd(balances[_to], 1);
|
balances[_to] = balances[_to].safeAdd(1);
|
||||||
|
|
||||||
emit Transfer(
|
emit Transfer(
|
||||||
_from,
|
_from,
|
||||||
_to,
|
_to,
|
||||||
|
@ -18,16 +18,19 @@
|
|||||||
|
|
||||||
pragma solidity ^0.5.9;
|
pragma solidity ^0.5.9;
|
||||||
|
|
||||||
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
import "./ERC721Token.sol";
|
import "./ERC721Token.sol";
|
||||||
|
|
||||||
|
|
||||||
contract MintableERC721Token is
|
contract MintableERC721Token is
|
||||||
ERC721Token
|
ERC721Token
|
||||||
{
|
{
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
/// @dev Function to mint a new token
|
/// @dev Function to mint a new token
|
||||||
/// Reverts if the given token ID already exists
|
/// Reverts if the given token ID already exists
|
||||||
/// @param _to Address of the beneficiary that will own the minted token
|
/// @param _to Address of the beneficiary that will own the minted token
|
||||||
/// @param _tokenId ID of the token to be minted by the msg.sender
|
/// @param _tokenId ID of the token to be minted by the msg.sender
|
||||||
function _mint(address _to, uint256 _tokenId)
|
function _mint(address _to, uint256 _tokenId)
|
||||||
internal
|
internal
|
||||||
{
|
{
|
||||||
@ -43,7 +46,7 @@ contract MintableERC721Token is
|
|||||||
);
|
);
|
||||||
|
|
||||||
owners[_tokenId] = _to;
|
owners[_tokenId] = _to;
|
||||||
balances[_to] = _safeAdd(balances[_to], 1);
|
balances[_to] = balances[_to].safeAdd(1);
|
||||||
|
|
||||||
emit Transfer(
|
emit Transfer(
|
||||||
address(0),
|
address(0),
|
||||||
@ -71,7 +74,7 @@ contract MintableERC721Token is
|
|||||||
);
|
);
|
||||||
|
|
||||||
owners[_tokenId] = address(0);
|
owners[_tokenId] = address(0);
|
||||||
balances[_owner] = _safeSub(balances[_owner], 1);
|
balances[_owner] = balances[_owner].safeSub(1);
|
||||||
|
|
||||||
emit Transfer(
|
emit Transfer(
|
||||||
_owner,
|
_owner,
|
||||||
|
@ -85,9 +85,9 @@ library LibMath {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
|
// safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
|
||||||
// ceil(a / b) = floor((a + b - 1) / b)
|
// ceil(a / b) = floor((a + b - 1) / b)
|
||||||
// To implement `ceil(a / b)` using _safeDiv.
|
// To implement `ceil(a / b)` using safeDiv.
|
||||||
partialAmount = numerator.safeMul(target)
|
partialAmount = numerator.safeMul(target)
|
||||||
.safeAdd(denominator.safeSub(1))
|
.safeAdd(denominator.safeSub(1))
|
||||||
.safeDiv(denominator);
|
.safeDiv(denominator);
|
||||||
@ -127,9 +127,9 @@ library LibMath {
|
|||||||
pure
|
pure
|
||||||
returns (uint256 partialAmount)
|
returns (uint256 partialAmount)
|
||||||
{
|
{
|
||||||
// _safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
|
// safeDiv computes `floor(a / b)`. We use the identity (a, b integer):
|
||||||
// ceil(a / b) = floor((a + b - 1) / b)
|
// ceil(a / b) = floor((a + b - 1) / b)
|
||||||
// To implement `ceil(a / b)` using _safeDiv.
|
// To implement `ceil(a / b)` using safeDiv.
|
||||||
partialAmount = numerator.safeMul(target)
|
partialAmount = numerator.safeMul(target)
|
||||||
.safeAdd(denominator.safeSub(1))
|
.safeAdd(denominator.safeSub(1))
|
||||||
.safeDiv(denominator);
|
.safeDiv(denominator);
|
||||||
|
@ -23,13 +23,13 @@ import "@0x/contracts-exchange/contracts/src/interfaces/IExchange.sol";
|
|||||||
import "@0x/contracts-exchange-libs/contracts/src/LibOrder.sol";
|
import "@0x/contracts-exchange-libs/contracts/src/LibOrder.sol";
|
||||||
import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol";
|
import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol";
|
||||||
import "@0x/contracts-utils/contracts/src/LibBytes.sol";
|
import "@0x/contracts-utils/contracts/src/LibBytes.sol";
|
||||||
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
|
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
|
||||||
|
|
||||||
|
|
||||||
contract DutchAuction is
|
contract DutchAuction {
|
||||||
SafeMath
|
|
||||||
{
|
|
||||||
using LibBytes for bytes;
|
using LibBytes for bytes;
|
||||||
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
// solhint-disable var-name-mixedcase
|
// solhint-disable var-name-mixedcase
|
||||||
IExchange internal EXCHANGE;
|
IExchange internal EXCHANGE;
|
||||||
@ -120,8 +120,8 @@ contract DutchAuction is
|
|||||||
address token = assetData.readAddress(16);
|
address token = assetData.readAddress(16);
|
||||||
// Calculate the excess from the buy order. This can occur if the buyer sends in a higher
|
// Calculate the excess from the buy order. This can occur if the buyer sends in a higher
|
||||||
// amount than the calculated current amount
|
// amount than the calculated current amount
|
||||||
uint256 buyerExcessAmount = _safeSub(buyOrder.makerAssetAmount, auctionDetails.currentAmount);
|
uint256 buyerExcessAmount = buyOrder.makerAssetAmount.safeSub(auctionDetails.currentAmount);
|
||||||
uint256 sellerExcessAmount = _safeSub(leftMakerAssetSpreadAmount, buyerExcessAmount);
|
uint256 sellerExcessAmount = leftMakerAssetSpreadAmount.safeSub(buyerExcessAmount);
|
||||||
// Return the difference between auctionDetails.currentAmount and sellOrder.takerAssetAmount
|
// Return the difference between auctionDetails.currentAmount and sellOrder.takerAssetAmount
|
||||||
// to the seller
|
// to the seller
|
||||||
if (sellerExcessAmount > 0) {
|
if (sellerExcessAmount > 0) {
|
||||||
@ -190,12 +190,8 @@ contract DutchAuction is
|
|||||||
// Auction end time is guaranteed by 0x Exchange due to the order expiration
|
// Auction end time is guaranteed by 0x Exchange due to the order expiration
|
||||||
auctionDetails.currentAmount = minAmount;
|
auctionDetails.currentAmount = minAmount;
|
||||||
} else {
|
} else {
|
||||||
auctionDetails.currentAmount = _safeAdd(
|
auctionDetails.currentAmount = minAmount.safeAdd(
|
||||||
minAmount,
|
remainingDurationSeconds.safeMul(amountDelta).safeDiv(auctionDurationSeconds)
|
||||||
_safeDiv(
|
|
||||||
_safeMul(remainingDurationSeconds, amountDelta),
|
|
||||||
auctionDurationSeconds
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return auctionDetails;
|
return auctionDetails;
|
||||||
|
@ -1,90 +0,0 @@
|
|||||||
pragma solidity ^0.5.9;
|
|
||||||
|
|
||||||
import "./LibRichErrors.sol";
|
|
||||||
import "./LibSafeMathRichErrors.sol";
|
|
||||||
|
|
||||||
|
|
||||||
contract SafeMath {
|
|
||||||
|
|
||||||
function _safeMul(uint256 a, uint256 b)
|
|
||||||
internal
|
|
||||||
pure
|
|
||||||
returns (uint256)
|
|
||||||
{
|
|
||||||
if (a == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
uint256 c = a * b;
|
|
||||||
if (c / a != b) {
|
|
||||||
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
|
|
||||||
LibSafeMathRichErrors.BinOpErrorCodes.MULTIPLICATION_OVERFLOW,
|
|
||||||
a,
|
|
||||||
b
|
|
||||||
));
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _safeDiv(uint256 a, uint256 b)
|
|
||||||
internal
|
|
||||||
pure
|
|
||||||
returns (uint256)
|
|
||||||
{
|
|
||||||
if (b == 0) {
|
|
||||||
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
|
|
||||||
LibSafeMathRichErrors.BinOpErrorCodes.DIVISION_BY_ZERO,
|
|
||||||
a,
|
|
||||||
b
|
|
||||||
));
|
|
||||||
}
|
|
||||||
uint256 c = a / b;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _safeSub(uint256 a, uint256 b)
|
|
||||||
internal
|
|
||||||
pure
|
|
||||||
returns (uint256)
|
|
||||||
{
|
|
||||||
if (b > a) {
|
|
||||||
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
|
|
||||||
LibSafeMathRichErrors.BinOpErrorCodes.SUBTRACTION_UNDERFLOW,
|
|
||||||
a,
|
|
||||||
b
|
|
||||||
));
|
|
||||||
}
|
|
||||||
return a - b;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _safeAdd(uint256 a, uint256 b)
|
|
||||||
internal
|
|
||||||
pure
|
|
||||||
returns (uint256)
|
|
||||||
{
|
|
||||||
uint256 c = a + b;
|
|
||||||
if (c < a) {
|
|
||||||
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
|
|
||||||
LibSafeMathRichErrors.BinOpErrorCodes.ADDITION_OVERFLOW,
|
|
||||||
a,
|
|
||||||
b
|
|
||||||
));
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _max256(uint256 a, uint256 b)
|
|
||||||
internal
|
|
||||||
pure
|
|
||||||
returns (uint256)
|
|
||||||
{
|
|
||||||
return a >= b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _min256(uint256 a, uint256 b)
|
|
||||||
internal
|
|
||||||
pure
|
|
||||||
returns (uint256)
|
|
||||||
{
|
|
||||||
return a < b ? a : b;
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,18 +18,19 @@
|
|||||||
|
|
||||||
pragma solidity ^0.5.9;
|
pragma solidity ^0.5.9;
|
||||||
|
|
||||||
import "../src/SafeMath.sol";
|
import "../src/LibSafeMath.sol";
|
||||||
|
|
||||||
|
|
||||||
contract TestSafeMath is
|
contract TestLibSafeMath {
|
||||||
SafeMath
|
|
||||||
{
|
using LibSafeMath for uint256;
|
||||||
|
|
||||||
function externalSafeMul(uint256 a, uint256 b)
|
function externalSafeMul(uint256 a, uint256 b)
|
||||||
external
|
external
|
||||||
pure
|
pure
|
||||||
returns (uint256)
|
returns (uint256)
|
||||||
{
|
{
|
||||||
return _safeMul(a, b);
|
return a.safeMul(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function externalSafeDiv(uint256 a, uint256 b)
|
function externalSafeDiv(uint256 a, uint256 b)
|
||||||
@ -37,7 +38,7 @@ contract TestSafeMath is
|
|||||||
pure
|
pure
|
||||||
returns (uint256)
|
returns (uint256)
|
||||||
{
|
{
|
||||||
return _safeDiv(a, b);
|
return a.safeDiv(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function externalSafeSub(uint256 a, uint256 b)
|
function externalSafeSub(uint256 a, uint256 b)
|
||||||
@ -45,7 +46,7 @@ contract TestSafeMath is
|
|||||||
pure
|
pure
|
||||||
returns (uint256)
|
returns (uint256)
|
||||||
{
|
{
|
||||||
return _safeSub(a, b);
|
return a.safeSub(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function externalSafeAdd(uint256 a, uint256 b)
|
function externalSafeAdd(uint256 a, uint256 b)
|
||||||
@ -53,7 +54,7 @@ contract TestSafeMath is
|
|||||||
pure
|
pure
|
||||||
returns (uint256)
|
returns (uint256)
|
||||||
{
|
{
|
||||||
return _safeAdd(a, b);
|
return a.safeAdd(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function externalMaxUint256(uint256 a, uint256 b)
|
function externalMaxUint256(uint256 a, uint256 b)
|
||||||
@ -61,7 +62,7 @@ contract TestSafeMath is
|
|||||||
pure
|
pure
|
||||||
returns (uint256)
|
returns (uint256)
|
||||||
{
|
{
|
||||||
return _max256(a, b);
|
return a.max256(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function externalMinUint256(uint256 a, uint256 b)
|
function externalMinUint256(uint256 a, uint256 b)
|
||||||
@ -69,6 +70,6 @@ contract TestSafeMath is
|
|||||||
pure
|
pure
|
||||||
returns (uint256)
|
returns (uint256)
|
||||||
{
|
{
|
||||||
return _min256(a, b);
|
return a.min256(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -36,7 +36,7 @@
|
|||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
|
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
|
||||||
"abis": "./generated-artifacts/@(Authorizable|IAuthorizable|IOwnable|LibAddress|LibAddressArray|LibAddressArrayRichErrors|LibAuthorizableRichErrors|LibBytes|LibBytesRichErrors|LibEIP1271|LibEIP712|LibFractions|LibOwnableRichErrors|LibReentrancyGuardRichErrors|LibRichErrors|LibSafeMath|LibSafeMathRichErrors|Ownable|ReentrancyGuard|Refundable|SafeMath|TestLibAddress|TestLibAddressArray|TestLibBytes|TestLibEIP712|TestLibRichErrors|TestLogDecoding|TestLogDecodingDownstream|TestOwnable|TestReentrancyGuard|TestRefundable|TestRefundableReceiver|TestSafeMath).json"
|
"abis": "./generated-artifacts/@(Authorizable|IAuthorizable|IOwnable|LibAddress|LibAddressArray|LibAddressArrayRichErrors|LibAuthorizableRichErrors|LibBytes|LibBytesRichErrors|LibEIP1271|LibEIP712|LibFractions|LibOwnableRichErrors|LibReentrancyGuardRichErrors|LibRichErrors|LibSafeMath|LibSafeMathRichErrors|Ownable|ReentrancyGuard|Refundable|TestLibAddress|TestLibAddressArray|TestLibBytes|TestLibEIP712|TestLibRichErrors|TestLibSafeMath|TestLogDecoding|TestLogDecodingDownstream|TestOwnable|TestReentrancyGuard|TestRefundable|TestRefundableReceiver).json"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -25,19 +25,18 @@ import * as LibSafeMathRichErrors from '../generated-artifacts/LibSafeMathRichEr
|
|||||||
import * as Ownable from '../generated-artifacts/Ownable.json';
|
import * as Ownable from '../generated-artifacts/Ownable.json';
|
||||||
import * as ReentrancyGuard from '../generated-artifacts/ReentrancyGuard.json';
|
import * as ReentrancyGuard from '../generated-artifacts/ReentrancyGuard.json';
|
||||||
import * as Refundable from '../generated-artifacts/Refundable.json';
|
import * as Refundable from '../generated-artifacts/Refundable.json';
|
||||||
import * as SafeMath from '../generated-artifacts/SafeMath.json';
|
|
||||||
import * as TestLibAddress from '../generated-artifacts/TestLibAddress.json';
|
import * as TestLibAddress from '../generated-artifacts/TestLibAddress.json';
|
||||||
import * as TestLibAddressArray from '../generated-artifacts/TestLibAddressArray.json';
|
import * as TestLibAddressArray from '../generated-artifacts/TestLibAddressArray.json';
|
||||||
import * as TestLibBytes from '../generated-artifacts/TestLibBytes.json';
|
import * as TestLibBytes from '../generated-artifacts/TestLibBytes.json';
|
||||||
import * as TestLibEIP712 from '../generated-artifacts/TestLibEIP712.json';
|
import * as TestLibEIP712 from '../generated-artifacts/TestLibEIP712.json';
|
||||||
import * as TestLibRichErrors from '../generated-artifacts/TestLibRichErrors.json';
|
import * as TestLibRichErrors from '../generated-artifacts/TestLibRichErrors.json';
|
||||||
|
import * as TestLibSafeMath from '../generated-artifacts/TestLibSafeMath.json';
|
||||||
import * as TestLogDecoding from '../generated-artifacts/TestLogDecoding.json';
|
import * as TestLogDecoding from '../generated-artifacts/TestLogDecoding.json';
|
||||||
import * as TestLogDecodingDownstream from '../generated-artifacts/TestLogDecodingDownstream.json';
|
import * as TestLogDecodingDownstream from '../generated-artifacts/TestLogDecodingDownstream.json';
|
||||||
import * as TestOwnable from '../generated-artifacts/TestOwnable.json';
|
import * as TestOwnable from '../generated-artifacts/TestOwnable.json';
|
||||||
import * as TestReentrancyGuard from '../generated-artifacts/TestReentrancyGuard.json';
|
import * as TestReentrancyGuard from '../generated-artifacts/TestReentrancyGuard.json';
|
||||||
import * as TestRefundable from '../generated-artifacts/TestRefundable.json';
|
import * as TestRefundable from '../generated-artifacts/TestRefundable.json';
|
||||||
import * as TestRefundableReceiver from '../generated-artifacts/TestRefundableReceiver.json';
|
import * as TestRefundableReceiver from '../generated-artifacts/TestRefundableReceiver.json';
|
||||||
import * as TestSafeMath from '../generated-artifacts/TestSafeMath.json';
|
|
||||||
export const artifacts = {
|
export const artifacts = {
|
||||||
Authorizable: Authorizable as ContractArtifact,
|
Authorizable: Authorizable as ContractArtifact,
|
||||||
LibAddress: LibAddress as ContractArtifact,
|
LibAddress: LibAddress as ContractArtifact,
|
||||||
@ -57,7 +56,6 @@ export const artifacts = {
|
|||||||
Ownable: Ownable as ContractArtifact,
|
Ownable: Ownable as ContractArtifact,
|
||||||
ReentrancyGuard: ReentrancyGuard as ContractArtifact,
|
ReentrancyGuard: ReentrancyGuard as ContractArtifact,
|
||||||
Refundable: Refundable as ContractArtifact,
|
Refundable: Refundable as ContractArtifact,
|
||||||
SafeMath: SafeMath as ContractArtifact,
|
|
||||||
IAuthorizable: IAuthorizable as ContractArtifact,
|
IAuthorizable: IAuthorizable as ContractArtifact,
|
||||||
IOwnable: IOwnable as ContractArtifact,
|
IOwnable: IOwnable as ContractArtifact,
|
||||||
TestLibAddress: TestLibAddress as ContractArtifact,
|
TestLibAddress: TestLibAddress as ContractArtifact,
|
||||||
@ -65,11 +63,11 @@ export const artifacts = {
|
|||||||
TestLibBytes: TestLibBytes as ContractArtifact,
|
TestLibBytes: TestLibBytes as ContractArtifact,
|
||||||
TestLibEIP712: TestLibEIP712 as ContractArtifact,
|
TestLibEIP712: TestLibEIP712 as ContractArtifact,
|
||||||
TestLibRichErrors: TestLibRichErrors as ContractArtifact,
|
TestLibRichErrors: TestLibRichErrors as ContractArtifact,
|
||||||
|
TestLibSafeMath: TestLibSafeMath as ContractArtifact,
|
||||||
TestLogDecoding: TestLogDecoding as ContractArtifact,
|
TestLogDecoding: TestLogDecoding as ContractArtifact,
|
||||||
TestLogDecodingDownstream: TestLogDecodingDownstream as ContractArtifact,
|
TestLogDecodingDownstream: TestLogDecodingDownstream as ContractArtifact,
|
||||||
TestOwnable: TestOwnable as ContractArtifact,
|
TestOwnable: TestOwnable as ContractArtifact,
|
||||||
TestReentrancyGuard: TestReentrancyGuard as ContractArtifact,
|
TestReentrancyGuard: TestReentrancyGuard as ContractArtifact,
|
||||||
TestRefundable: TestRefundable as ContractArtifact,
|
TestRefundable: TestRefundable as ContractArtifact,
|
||||||
TestRefundableReceiver: TestRefundableReceiver as ContractArtifact,
|
TestRefundableReceiver: TestRefundableReceiver as ContractArtifact,
|
||||||
TestSafeMath: TestSafeMath as ContractArtifact,
|
|
||||||
};
|
};
|
||||||
|
@ -23,16 +23,15 @@ export * from '../generated-wrappers/lib_safe_math_rich_errors';
|
|||||||
export * from '../generated-wrappers/ownable';
|
export * from '../generated-wrappers/ownable';
|
||||||
export * from '../generated-wrappers/reentrancy_guard';
|
export * from '../generated-wrappers/reentrancy_guard';
|
||||||
export * from '../generated-wrappers/refundable';
|
export * from '../generated-wrappers/refundable';
|
||||||
export * from '../generated-wrappers/safe_math';
|
|
||||||
export * from '../generated-wrappers/test_lib_address';
|
export * from '../generated-wrappers/test_lib_address';
|
||||||
export * from '../generated-wrappers/test_lib_address_array';
|
export * from '../generated-wrappers/test_lib_address_array';
|
||||||
export * from '../generated-wrappers/test_lib_bytes';
|
export * from '../generated-wrappers/test_lib_bytes';
|
||||||
export * from '../generated-wrappers/test_lib_e_i_p712';
|
export * from '../generated-wrappers/test_lib_e_i_p712';
|
||||||
export * from '../generated-wrappers/test_lib_rich_errors';
|
export * from '../generated-wrappers/test_lib_rich_errors';
|
||||||
|
export * from '../generated-wrappers/test_lib_safe_math';
|
||||||
export * from '../generated-wrappers/test_log_decoding';
|
export * from '../generated-wrappers/test_log_decoding';
|
||||||
export * from '../generated-wrappers/test_log_decoding_downstream';
|
export * from '../generated-wrappers/test_log_decoding_downstream';
|
||||||
export * from '../generated-wrappers/test_ownable';
|
export * from '../generated-wrappers/test_ownable';
|
||||||
export * from '../generated-wrappers/test_reentrancy_guard';
|
export * from '../generated-wrappers/test_reentrancy_guard';
|
||||||
export * from '../generated-wrappers/test_refundable';
|
export * from '../generated-wrappers/test_refundable';
|
||||||
export * from '../generated-wrappers/test_refundable_receiver';
|
export * from '../generated-wrappers/test_refundable_receiver';
|
||||||
export * from '../generated-wrappers/test_safe_math';
|
|
||||||
|
@ -2,7 +2,7 @@ import { blockchainTests, constants, describe, expect } from '@0x/contracts-test
|
|||||||
import { BigNumber, SafeMathRevertErrors } from '@0x/utils';
|
import { BigNumber, SafeMathRevertErrors } from '@0x/utils';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import { artifacts, TestSafeMathContract } from '../src';
|
import { artifacts, TestLibSafeMathContract } from '../src';
|
||||||
import * as ReferenceFunctions from '../src/reference_functions';
|
import * as ReferenceFunctions from '../src/reference_functions';
|
||||||
|
|
||||||
function toBigNumber(a: number | string): BigNumber {
|
function toBigNumber(a: number | string): BigNumber {
|
||||||
@ -11,19 +11,19 @@ function toBigNumber(a: number | string): BigNumber {
|
|||||||
|
|
||||||
blockchainTests('SafeMath', env => {
|
blockchainTests('SafeMath', env => {
|
||||||
const { ONE_ETHER } = constants;
|
const { ONE_ETHER } = constants;
|
||||||
let safeMath: TestSafeMathContract;
|
let safeMath: TestLibSafeMathContract;
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
// Deploy SafeMath
|
// Deploy SafeMath
|
||||||
safeMath = await TestSafeMathContract.deployFrom0xArtifactAsync(
|
safeMath = await TestLibSafeMathContract.deployFrom0xArtifactAsync(
|
||||||
artifacts.TestSafeMath,
|
artifacts.TestLibSafeMath,
|
||||||
env.provider,
|
env.provider,
|
||||||
env.txDefaults,
|
env.txDefaults,
|
||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_safeMul', () => {
|
describe('safeMul', () => {
|
||||||
it('should match the output of the reference function', async () => {
|
it('should match the output of the reference function', async () => {
|
||||||
const a = ONE_ETHER;
|
const a = ONE_ETHER;
|
||||||
const b = ONE_ETHER.times(2);
|
const b = ONE_ETHER.times(2);
|
||||||
@ -59,7 +59,7 @@ blockchainTests('SafeMath', env => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_safeDiv', () => {
|
describe('safeDiv', () => {
|
||||||
it('should match the output of the reference function', async () => {
|
it('should match the output of the reference function', async () => {
|
||||||
const a = ONE_ETHER;
|
const a = ONE_ETHER;
|
||||||
const b = ONE_ETHER.times(2);
|
const b = ONE_ETHER.times(2);
|
||||||
@ -100,7 +100,7 @@ blockchainTests('SafeMath', env => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_safeSub', () => {
|
describe('safeSub', () => {
|
||||||
it('should match the output of the reference function', async () => {
|
it('should match the output of the reference function', async () => {
|
||||||
const a = ONE_ETHER;
|
const a = ONE_ETHER;
|
||||||
const b = ONE_ETHER.dividedToIntegerBy(2);
|
const b = ONE_ETHER.dividedToIntegerBy(2);
|
||||||
@ -131,7 +131,7 @@ blockchainTests('SafeMath', env => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_safeAdd', () => {
|
describe('safeAdd', () => {
|
||||||
it('should match the output of the reference function', async () => {
|
it('should match the output of the reference function', async () => {
|
||||||
const a = ONE_ETHER;
|
const a = ONE_ETHER;
|
||||||
const b = ONE_ETHER.dividedToIntegerBy(2);
|
const b = ONE_ETHER.dividedToIntegerBy(2);
|
||||||
@ -167,7 +167,7 @@ blockchainTests('SafeMath', env => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_maxUint256', () => {
|
describe('maxUint256', () => {
|
||||||
it('should return first argument if it is greater than the second', async () => {
|
it('should return first argument if it is greater than the second', async () => {
|
||||||
const result = await safeMath.externalMaxUint256.callAsync(toBigNumber(13), constants.ZERO_AMOUNT);
|
const result = await safeMath.externalMaxUint256.callAsync(toBigNumber(13), constants.ZERO_AMOUNT);
|
||||||
expect(result).bignumber.to.be.eq(toBigNumber(13));
|
expect(result).bignumber.to.be.eq(toBigNumber(13));
|
||||||
@ -184,7 +184,7 @@ blockchainTests('SafeMath', env => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('_minUint256', () => {
|
describe('minUint256', () => {
|
||||||
it('should return first argument if it is less than the second', async () => {
|
it('should return first argument if it is less than the second', async () => {
|
||||||
const result = await safeMath.externalMaxUint256.callAsync(constants.ZERO_AMOUNT, toBigNumber(13));
|
const result = await safeMath.externalMaxUint256.callAsync(constants.ZERO_AMOUNT, toBigNumber(13));
|
||||||
expect(result).bignumber.to.be.eq(toBigNumber(13));
|
expect(result).bignumber.to.be.eq(toBigNumber(13));
|
@ -23,19 +23,18 @@
|
|||||||
"generated-artifacts/Ownable.json",
|
"generated-artifacts/Ownable.json",
|
||||||
"generated-artifacts/ReentrancyGuard.json",
|
"generated-artifacts/ReentrancyGuard.json",
|
||||||
"generated-artifacts/Refundable.json",
|
"generated-artifacts/Refundable.json",
|
||||||
"generated-artifacts/SafeMath.json",
|
|
||||||
"generated-artifacts/TestLibAddress.json",
|
"generated-artifacts/TestLibAddress.json",
|
||||||
"generated-artifacts/TestLibAddressArray.json",
|
"generated-artifacts/TestLibAddressArray.json",
|
||||||
"generated-artifacts/TestLibBytes.json",
|
"generated-artifacts/TestLibBytes.json",
|
||||||
"generated-artifacts/TestLibEIP712.json",
|
"generated-artifacts/TestLibEIP712.json",
|
||||||
"generated-artifacts/TestLibRichErrors.json",
|
"generated-artifacts/TestLibRichErrors.json",
|
||||||
|
"generated-artifacts/TestLibSafeMath.json",
|
||||||
"generated-artifacts/TestLogDecoding.json",
|
"generated-artifacts/TestLogDecoding.json",
|
||||||
"generated-artifacts/TestLogDecodingDownstream.json",
|
"generated-artifacts/TestLogDecodingDownstream.json",
|
||||||
"generated-artifacts/TestOwnable.json",
|
"generated-artifacts/TestOwnable.json",
|
||||||
"generated-artifacts/TestReentrancyGuard.json",
|
"generated-artifacts/TestReentrancyGuard.json",
|
||||||
"generated-artifacts/TestRefundable.json",
|
"generated-artifacts/TestRefundable.json",
|
||||||
"generated-artifacts/TestRefundableReceiver.json",
|
"generated-artifacts/TestRefundableReceiver.json"
|
||||||
"generated-artifacts/TestSafeMath.json"
|
|
||||||
],
|
],
|
||||||
"exclude": ["./deploy/solc/solc_bin"]
|
"exclude": ["./deploy/solc/solc_bin"]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user