aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/utils
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2019-01-29 00:26:13 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2019-01-29 00:26:13 +0800
commit141ac0ca0be15602a1bcf466e873134e084f30c1 (patch)
treef0e08db40c46e8c671a99231b645365747a8073f /contracts/utils
parent0c12128f64f7d9a8de6088e98c2e638533d6f5bf (diff)
parent25e42c0ad47e9ec06e474cd12a488ae837660302 (diff)
downloaddexon-0x-contracts-141ac0ca0be15602a1bcf466e873134e084f30c1.tar.gz
dexon-0x-contracts-141ac0ca0be15602a1bcf466e873134e084f30c1.tar.zst
dexon-0x-contracts-141ac0ca0be15602a1bcf466e873134e084f30c1.zip
Merge development
Diffstat (limited to 'contracts/utils')
-rw-r--r--contracts/utils/CHANGELOG.json17
-rw-r--r--contracts/utils/README.md13
-rw-r--r--contracts/utils/contracts/src/LibAddressArray.sol84
-rw-r--r--contracts/utils/contracts/src/LibBytes.sol (renamed from contracts/utils/contracts/utils/LibBytes/LibBytes.sol)0
-rw-r--r--contracts/utils/contracts/src/Ownable.sol (renamed from contracts/utils/contracts/utils/Ownable/Ownable.sol)2
-rw-r--r--contracts/utils/contracts/src/ReentrancyGuard.sol (renamed from contracts/utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol)0
-rw-r--r--contracts/utils/contracts/src/SafeMath.sol (renamed from contracts/utils/contracts/utils/SafeMath/SafeMath.sol)0
-rw-r--r--contracts/utils/contracts/src/interfaces/IOwnable.sol (renamed from contracts/utils/contracts/utils/Ownable/IOwnable.sol)0
-rw-r--r--contracts/utils/contracts/test/TestConstants.sol (renamed from contracts/utils/contracts/test/TestConstants/TestConstants.sol)4
-rw-r--r--contracts/utils/contracts/test/TestLibBytes.sol (renamed from contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol)4
-rw-r--r--contracts/utils/package.json13
-rw-r--r--contracts/utils/test/lib_bytes.ts3
-rw-r--r--contracts/utils/test/libs.ts3
13 files changed, 118 insertions, 25 deletions
diff --git a/contracts/utils/CHANGELOG.json b/contracts/utils/CHANGELOG.json
index d44a81fef..94434b803 100644
--- a/contracts/utils/CHANGELOG.json
+++ b/contracts/utils/CHANGELOG.json
@@ -1,5 +1,22 @@
[
{
+ "version": "3.0.0",
+ "changes": [
+ {
+ "note": "Add LibAddressArray contract",
+ "pr": 1539
+ },
+ {
+ "note": "Do not nest contracts in redundant directories",
+ "pr": 1539
+ },
+ {
+ "note": "Rename utils directory to src",
+ "pr": 1539
+ }
+ ]
+ },
+ {
"version": "2.0.1",
"changes": [
{
diff --git a/contracts/utils/README.md b/contracts/utils/README.md
index 2f872ddb5..5c69971b6 100644
--- a/contracts/utils/README.md
+++ b/contracts/utils/README.md
@@ -1,15 +1,14 @@
## Contracts utils
-Smart contracts utils used in the 0x protocol.
+This package contains smart contract utilities and libraries that are used throughout the entire codebase of smart contracts. These contracts are all generic and may helpful to use outside of the context of 0x protocol.
-## Usage
+## Installation
-Contracts can be found in the [contracts](./contracts) directory. The contents of this directory are broken down into the following subdirectories:
+**Install**
-- [utils](./contracts/utils)
- - This directory contains libraries and utils.
-- [test](./contracts/test)
- - This directory contains mocks and other contracts that are used solely for testing contracts within the other directories.
+```bash
+npm install @0x/contracts-utils --save
+```
## Contributing
diff --git a/contracts/utils/contracts/src/LibAddressArray.sol b/contracts/utils/contracts/src/LibAddressArray.sol
new file mode 100644
index 000000000..892c486f1
--- /dev/null
+++ b/contracts/utils/contracts/src/LibAddressArray.sol
@@ -0,0 +1,84 @@
+/*
+
+ 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 "./LibBytes.sol";
+
+
+library LibAddressArray {
+
+ /// @dev Append a new address to an array of addresses.
+ /// The `addressArray` may need to be reallocated to make space
+ /// for the new address. Because of this we return the resulting
+ /// memory location of `addressArray`.
+ /// @param addressToAppend Address to append.
+ /// @return Array of addresses: [... addressArray, addressToAppend]
+ function append(address[] memory addressArray, address addressToAppend)
+ internal pure
+ returns (address[])
+ {
+ // Get stats on address array and free memory
+ uint256 freeMemPtr = 0;
+ uint256 addressArrayBeginPtr = 0;
+ uint256 addressArrayEndPtr = 0;
+ uint256 addressArrayLength = addressArray.length;
+ uint256 addressArrayMemSizeInBytes = 32 + (32 * addressArrayLength);
+ assembly {
+ freeMemPtr := mload(0x40)
+ addressArrayBeginPtr := addressArray
+ addressArrayEndPtr := add(addressArray, addressArrayMemSizeInBytes)
+ }
+
+ // Cases for `freeMemPtr`:
+ // `freeMemPtr` == `addressArrayEndPtr`: Nothing occupies memory after `addressArray`
+ // `freeMemPtr` > `addressArrayEndPtr`: Some value occupies memory after `addressArray`
+ // `freeMemPtr` < `addressArrayEndPtr`: Memory has not been managed properly.
+ require(
+ freeMemPtr >= addressArrayEndPtr,
+ "INVALID_FREE_MEMORY_PTR"
+ );
+
+ // If free memory begins at the end of `addressArray`
+ // then we can append `addressToAppend` directly.
+ // Otherwise, we must copy the array to free memory
+ // before appending new values to it.
+ if (freeMemPtr > addressArrayEndPtr) {
+ LibBytes.memCopy(freeMemPtr, addressArrayBeginPtr, addressArrayMemSizeInBytes);
+ assembly {
+ addressArray := freeMemPtr
+ addressArrayBeginPtr := addressArray
+ }
+ }
+
+ // Append `addressToAppend`
+ addressArrayLength += 1;
+ addressArrayMemSizeInBytes += 32;
+ addressArrayEndPtr = addressArrayBeginPtr + addressArrayMemSizeInBytes;
+ freeMemPtr = addressArrayEndPtr;
+ assembly {
+ // Store new array length
+ mstore(addressArray, addressArrayLength)
+
+ // Update `freeMemPtr`
+ mstore(0x40, freeMemPtr)
+ }
+ addressArray[addressArrayLength - 1] = addressToAppend;
+ return addressArray;
+ }
+}
diff --git a/contracts/utils/contracts/utils/LibBytes/LibBytes.sol b/contracts/utils/contracts/src/LibBytes.sol
index 4ee6228d5..4ee6228d5 100644
--- a/contracts/utils/contracts/utils/LibBytes/LibBytes.sol
+++ b/contracts/utils/contracts/src/LibBytes.sol
diff --git a/contracts/utils/contracts/utils/Ownable/Ownable.sol b/contracts/utils/contracts/src/Ownable.sol
index aa74a72d2..f67f241a4 100644
--- a/contracts/utils/contracts/utils/Ownable/Ownable.sol
+++ b/contracts/utils/contracts/src/Ownable.sol
@@ -1,6 +1,6 @@
pragma solidity ^0.4.24;
-import "./IOwnable.sol";
+import "./interfaces/IOwnable.sol";
contract Ownable is
diff --git a/contracts/utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol b/contracts/utils/contracts/src/ReentrancyGuard.sol
index 1a02c88a4..1a02c88a4 100644
--- a/contracts/utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol
+++ b/contracts/utils/contracts/src/ReentrancyGuard.sol
diff --git a/contracts/utils/contracts/utils/SafeMath/SafeMath.sol b/contracts/utils/contracts/src/SafeMath.sol
index d7a4a603e..d7a4a603e 100644
--- a/contracts/utils/contracts/utils/SafeMath/SafeMath.sol
+++ b/contracts/utils/contracts/src/SafeMath.sol
diff --git a/contracts/utils/contracts/utils/Ownable/IOwnable.sol b/contracts/utils/contracts/src/interfaces/IOwnable.sol
index c0cbfddfd..c0cbfddfd 100644
--- a/contracts/utils/contracts/utils/Ownable/IOwnable.sol
+++ b/contracts/utils/contracts/src/interfaces/IOwnable.sol
diff --git a/contracts/utils/contracts/test/TestConstants/TestConstants.sol b/contracts/utils/contracts/test/TestConstants.sol
index 1275d007b..bf98bafef 100644
--- a/contracts/utils/contracts/test/TestConstants/TestConstants.sol
+++ b/contracts/utils/contracts/test/TestConstants.sol
@@ -16,9 +16,9 @@
*/
-pragma solidity 0.4.24;
+pragma solidity ^0.4.24;
-import "../../utils/LibBytes/LibBytes.sol";
+import "../src/LibBytes.sol";
// solhint-disable max-line-length
diff --git a/contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol b/contracts/utils/contracts/test/TestLibBytes.sol
index 00d861e61..cd6b1456d 100644
--- a/contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol
+++ b/contracts/utils/contracts/test/TestLibBytes.sol
@@ -16,9 +16,9 @@
*/
-pragma solidity 0.4.24;
+pragma solidity ^0.4.24;
-import "../../utils/LibBytes/LibBytes.sol";
+import "../src/LibBytes.sol";
contract TestLibBytes {
diff --git a/contracts/utils/package.json b/contracts/utils/package.json
index 4e45169fd..33d94cc33 100644
--- a/contracts/utils/package.json
+++ b/contracts/utils/package.json
@@ -1,6 +1,6 @@
{
"name": "@0x/contracts-utils",
- "version": "1.0.6",
+ "version": "3.0.0",
"engines": {
"node": ">=6.12"
},
@@ -48,35 +48,30 @@
"@0x/contracts-test-utils": "^2.0.1",
"@0x/dev-utils": "^1.0.24",
"@0x/sol-compiler": "^2.0.2",
- "@0x/subproviders": "^2.1.11",
"@0x/tslint-config": "^2.0.2",
- "@types/bn.js": "^4.11.0",
"@types/lodash": "4.14.104",
"@types/node": "*",
- "@types/yargs": "^10.0.0",
- "bn.js": "^4.11.8",
+ "@types/bn.js": "^4.11.0",
"chai": "^4.0.1",
"chai-as-promised": "^7.1.0",
"chai-bignumber": "^3.0.0",
"dirty-chai": "^2.0.1",
- "ethereumjs-abi": "0.6.5",
"make-promises-safe": "^1.1.0",
"mocha": "^4.1.0",
"npm-run-all": "^4.1.2",
"shx": "^0.2.2",
"solhint": "^1.4.1",
"tslint": "5.11.0",
- "typescript": "3.0.1",
- "yargs": "^10.0.3"
+ "typescript": "3.0.1"
},
"dependencies": {
"@0x/base-contract": "^3.0.13",
- "@0x/contracts-multisig": "^1.0.6",
"@0x/order-utils": "^3.1.2",
"@0x/types": "^1.5.2",
"@0x/typescript-typings": "^3.0.8",
"@0x/utils": "^3.0.1",
"@0x/web3-wrapper": "^3.2.4",
+ "bn.js": "^4.11.8",
"ethereum-types": "^1.1.6",
"ethereumjs-util": "^5.1.1",
"lodash": "^4.17.5"
diff --git a/contracts/utils/test/lib_bytes.ts b/contracts/utils/test/lib_bytes.ts
index 6fb859c67..daad28729 100644
--- a/contracts/utils/test/lib_bytes.ts
+++ b/contracts/utils/test/lib_bytes.ts
@@ -16,8 +16,7 @@ import * as chai from 'chai';
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';
-import { TestLibBytesContract } from '../generated-wrappers/test_lib_bytes';
-import { artifacts } from '../src';
+import { artifacts, TestLibBytesContract } from '../src';
chaiSetup.configure();
const expect = chai.expect;
diff --git a/contracts/utils/test/libs.ts b/contracts/utils/test/libs.ts
index 81596b2e4..77dc6e2ba 100644
--- a/contracts/utils/test/libs.ts
+++ b/contracts/utils/test/libs.ts
@@ -2,8 +2,7 @@ import { chaiSetup, provider, txDefaults, web3Wrapper } from '@0x/contracts-test
import { BlockchainLifecycle } from '@0x/dev-utils';
import * as chai from 'chai';
-import { TestConstantsContract } from '../generated-wrappers/test_constants';
-import { artifacts } from '../src';
+import { artifacts, TestConstantsContract } from '../src';
chaiSetup.configure();
const expect = chai.expect;