Merge development
This commit is contained in:
@@ -1,4 +1,21 @@
|
||||
[
|
||||
{
|
||||
"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": [
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
84
contracts/utils/contracts/src/LibAddressArray.sol
Normal file
84
contracts/utils/contracts/src/LibAddressArray.sol
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
pragma solidity ^0.4.24;
|
||||
|
||||
import "./IOwnable.sol";
|
||||
import "./interfaces/IOwnable.sol";
|
||||
|
||||
|
||||
contract Ownable is
|
||||
@@ -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
|
||||
@@ -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 {
|
||||
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user