Compare commits
185 Commits
0x.js@0.26
...
@0xproject
Author | SHA1 | Date | |
---|---|---|---|
|
ab72656fdf | ||
|
b6f2f98db6 | ||
|
c051c48600 | ||
|
04fc16587b | ||
|
f1d5a7d31f | ||
|
7197356928 | ||
|
a75d547af6 | ||
|
bdecb18e3e | ||
|
e2adcaa185 | ||
|
4193893349 | ||
|
53522a98b9 | ||
|
0e856ccfab | ||
|
6093ee7824 | ||
|
46f185bbeb | ||
|
8bfa880371 | ||
|
a7f0d03611 | ||
|
9ce899d3f3 | ||
|
e71c7fdb16 | ||
|
4258e6dab1 | ||
|
e59b683047 | ||
|
3c3033586d | ||
|
166c741beb | ||
|
629a0fa3a5 | ||
|
15ce862334 | ||
|
840557be0d | ||
|
72a00ac2df | ||
|
da0af12834 | ||
|
7617b6681c | ||
|
8e13c477a3 | ||
|
ff334b0e53 | ||
|
a98d6bf496 | ||
|
bbcf669bd9 | ||
|
328ecd0533 | ||
|
ab91717199 | ||
|
e4dfdc6b5c | ||
|
9e673f0073 | ||
|
353abffba9 | ||
|
bead76a5b7 | ||
|
9f5e724aec | ||
|
50bf6a991d | ||
|
1d584b1329 | ||
|
0c5a9fbc2c | ||
|
d5e58ecdd8 | ||
|
d6a9e7520d | ||
|
36b21e6e7b | ||
|
977a6b2794 | ||
|
fcddd503b7 | ||
|
3472bdcfd4 | ||
|
78f0ab3682 | ||
|
5a59ac4c6b | ||
|
0a19ba3014 | ||
|
88bd0f5328 | ||
|
6e2a69976b | ||
|
0500602ac3 | ||
|
e6887dc9d4 | ||
|
082c5c375e | ||
|
5977fa881e | ||
|
128fccb763 | ||
|
65697f5896 | ||
|
ee93f091ea | ||
|
33a8c7a9fb | ||
|
d1065cd266 | ||
|
0e44a630f0 | ||
|
7a21c6854b | ||
|
54ef916b93 | ||
|
4a770dee84 | ||
|
426a412ba1 | ||
|
f862a2af6d | ||
|
32867c9a07 | ||
|
cf0a8b2d05 | ||
|
4a17f5e820 | ||
|
694c37150c | ||
|
b04d07815f | ||
|
e7b523074a | ||
|
4e03155588 | ||
|
3e5abc60d3 | ||
|
edf80aba1a | ||
|
25ec4cf7b0 | ||
|
48b3d85265 | ||
|
ecfee00fec | ||
|
b5ce876327 | ||
|
d1342c6326 | ||
|
d3dcb2fd40 | ||
|
c448d048fd | ||
|
f0b3ee84b4 | ||
|
ab78c54d6a | ||
|
cee0d2706f | ||
|
8dea47f038 | ||
|
b7b1721145 | ||
|
5068f1666a | ||
|
37f0051d83 | ||
|
c780d04cee | ||
|
8c54e9a873 | ||
|
062f85e506 | ||
|
b3c0d54acd | ||
|
3d11afd872 | ||
|
96d15a8931 | ||
|
50915e6007 | ||
|
52007e5864 | ||
|
0c74d5ba01 | ||
|
0cd1959706 | ||
|
55ddf62df2 | ||
|
7f69d26247 | ||
|
b3c01f6750 | ||
|
34beee1edc | ||
|
7bc6a7b23f | ||
|
4a19655fb0 | ||
|
e32c453bc3 | ||
|
7b8d9193e2 | ||
|
74633126ce | ||
|
2fa5bb2028 | ||
|
010e6f8d7f | ||
|
3d5b6ec110 | ||
|
db2917b01c | ||
|
87d34f9c7f | ||
|
d20926e150 | ||
|
c453099f6b | ||
|
08569dbe6c | ||
|
a38ef3655b | ||
|
c586d3e81d | ||
|
c72745b0b2 | ||
|
8935146240 | ||
|
66aaceea91 | ||
|
4fe28ec53c | ||
|
efe8e07854 | ||
|
63dc606a9c | ||
|
131236305b | ||
|
45c9171a2c | ||
|
311d42626a | ||
|
cbf35de4c1 | ||
|
92efc65847 | ||
|
17e41f2391 | ||
|
fcd37808d4 | ||
|
6323badc19 | ||
|
e01468b492 | ||
|
f7f2390ce1 | ||
|
6118561bb5 | ||
|
6a9f10cd36 | ||
|
e7a4e03194 | ||
|
a42e8ae873 | ||
|
bcc5d63516 | ||
|
f97074dc84 | ||
|
bd81124b5a | ||
|
5a18f43b51 | ||
|
924b96ce2a | ||
|
47236dbaec | ||
|
437ac301db | ||
|
215740fab2 | ||
|
c66fc63452 | ||
|
02aefc40f3 | ||
|
cd42ca1bbd | ||
|
05bfd764b6 | ||
|
805a055946 | ||
|
f74408f390 | ||
|
f5e0fd8de5 | ||
|
353b6f3d6d | ||
|
cb377f29c2 | ||
|
9c9ce97525 | ||
|
4bfb1fcc71 | ||
|
15628a1206 | ||
|
a1411e3d52 | ||
|
f7f1397e52 | ||
|
3660ba28d7 | ||
|
335b9629b8 | ||
|
d16f0508bd | ||
|
da03331015 | ||
|
43128234bb | ||
|
bbcee8dfa7 | ||
|
5b8f84f59a | ||
|
4bd5789203 | ||
|
fb812d59b0 | ||
|
c858ff61f7 | ||
|
d6589004c7 | ||
|
519f1318c6 | ||
|
7460a36ce2 | ||
|
c8f6e3f923 | ||
|
7b373e29ea | ||
|
94ce7a54c3 | ||
|
3bb6d8871b | ||
|
cf7727debc | ||
|
9133e764a5 | ||
|
c32938fa43 | ||
|
54c891a447 | ||
|
a2e1d28efc | ||
|
590552d3e0 |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -63,4 +63,8 @@ lib/
|
||||
_bundles
|
||||
|
||||
# generated documentation
|
||||
docs/
|
||||
generated_docs/
|
||||
|
||||
TODO.md
|
||||
|
||||
packages/website/public/bundle*
|
||||
|
@@ -23,5 +23,6 @@ This repository contains all the 0x developer tools written in TypeScript. Our h
|
||||
|--------|-------|------------|
|
||||
| [`0x.js`](/packages/0x.js) | [](https://www.npmjs.com/package/0x.js) | A Javascript library for interacting with the 0x protocol |
|
||||
| [`@0xproject/assert`](/packages/assert) | [](https://www.npmjs.com/package/@0xproject/assert) | Standard type and schema assertions |
|
||||
| [`@0xproject/connect`](/packages/connect) | [](https://www.npmjs.com/package/@0xproject/connect) | A Javascript library for interacting with the standard relayer api |
|
||||
| [`@0xproject/json-schemas`](/packages/json-schemas) | [](https://www.npmjs.com/package/@0xproject/json-schemas) | 0x-related json schemas |
|
||||
| [`@0xproject/tslint-config`](/packages/tslint-config) | [](https://www.npmjs.com/package/@0xproject/tslint-config) | Custom 0x project TSLint rules |
|
||||
|
@@ -7,7 +7,7 @@
|
||||
"scripts": {
|
||||
"testrpc": "testrpc -p 8545 --networkId 50 -m \"${npm_package_config_mnemonic}\"",
|
||||
"lerna:run": "lerna run",
|
||||
"lerna:publish": "lerna run clean; lerna run build; lerna publish"
|
||||
"lerna:publish": "lerna run clean; lerna run build; lerna publish --registry=https://registry.npmjs.org/"
|
||||
},
|
||||
"config": {
|
||||
"mnemonic": "concert load couple harbor equip island argue ramp clarify fence smart topic"
|
||||
|
@@ -1,6 +1,22 @@
|
||||
# CHANGELOG
|
||||
|
||||
v0.26.0
|
||||
v0.27.1 - _November 28, 2017_
|
||||
------------------------
|
||||
* Export `TransactionOpts` type
|
||||
|
||||
v0.27.0 - _November 28, 2017_
|
||||
------------------------
|
||||
* Make `ZeroExConfig` required parameter of `ZeroEx` constructor (#233)
|
||||
* Add a required property `networkId` to `ZeroExConfig` (#233)
|
||||
* Make all `getContractAddress` functions, `zeroEx.exchange.subscribe`, `zeroEx.exchange.getZRXTokenAddress` sync (#233)
|
||||
* Remove `ZeroExError.ContractNotFound` and replace it with contract-specific errors (#233)
|
||||
* Make `DecodedLogEvent<A>` contain `LogWithDecodedArgs<A>` under log key instead of merging it in like web3 does (#234)
|
||||
* Rename `removed` to `isRemoved` in `DecodedLogEvent<A>` (#234)
|
||||
* Add config allowing to specify gasPrice and gasLimit for every transaction sending method (#235)
|
||||
* All transaction sending methods now call `estimateGas` if no gas amount was supplied (#235)
|
||||
* Modify order validation methods to validate against the `latest` block, not against the `pending` block (#236)
|
||||
|
||||
v0.26.0 - _November 21, 2017_
|
||||
------------------------
|
||||
* Add post-formatter for logs converting `blockNumber`, `logIndex`, `transactionIndex` from hexes to numbers (#231)
|
||||
* Remove support for Async callback types when used in Subscribe functions (#222)
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "0x.js",
|
||||
"version": "0.26.0",
|
||||
"version": "0.27.1",
|
||||
"description": "A javascript library for interacting with the 0x protocol",
|
||||
"keywords": [
|
||||
"0x.js",
|
||||
@@ -15,8 +15,8 @@
|
||||
"prebuild": "npm run clean",
|
||||
"build": "run-p build:umd:prod build:commonjs; exit 0;",
|
||||
"docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_DIR",
|
||||
"upload_docs_json": "aws s3 cp docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type aplication/json",
|
||||
"lint": "tslint src/**/*.ts test/**/*.ts",
|
||||
"upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json",
|
||||
"lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'",
|
||||
"test:circleci": "run-s test:coverage report_test_coverage && if [ $CIRCLE_BRANCH = \"development\" ]; then yarn test:umd; fi",
|
||||
"test": "run-s clean test:commonjs",
|
||||
"test:umd": "./scripts/test_umd.sh",
|
||||
@@ -44,7 +44,7 @@
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@0xproject/tslint-config": "^0.2.0",
|
||||
"@types/bintrees": "^1.0.2",
|
||||
"@types/jsonschema": "^1.1.1",
|
||||
"@types/lodash": "^4.14.64",
|
||||
@@ -61,7 +61,7 @@
|
||||
"copyfiles": "^1.2.0",
|
||||
"coveralls": "^3.0.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"ethereumjs-testrpc": "4.0.1",
|
||||
"ethereumjs-testrpc": "6.0.3",
|
||||
"json-loader": "^0.5.4",
|
||||
"mocha": "^4.0.0",
|
||||
"npm-run-all": "^4.0.2",
|
||||
@@ -83,8 +83,8 @@
|
||||
"webpack": "^3.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@0xproject/assert": "^0.0.5",
|
||||
"@0xproject/json-schemas": "^0.6.8",
|
||||
"@0xproject/assert": "^0.0.6",
|
||||
"@0xproject/json-schemas": "^0.6.9",
|
||||
"bignumber.js": "~4.1.0",
|
||||
"bintrees": "^1.0.2",
|
||||
"bn.js": "4.11.8",
|
||||
|
@@ -21,8 +21,9 @@ postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
|
||||
})
|
||||
.then(function(release) {
|
||||
console.log('POSTPUBLISH: Release successful, generating docs...');
|
||||
const jsonFilePath = __dirname + '/../' + postpublish_utils.generatedDocsDirectoryName + '/index.json';
|
||||
return execAsync(
|
||||
'JSON_FILE_PATH=' + __dirname + '/../docs/index.json PROJECT_DIR=' + __dirname + '/.. yarn docs:json',
|
||||
'JSON_FILE_PATH=' + jsonFilePath + ' PROJECT_DIR=' + __dirname + '/.. yarn docs:json',
|
||||
{
|
||||
cwd,
|
||||
}
|
||||
|
@@ -1,34 +1,35 @@
|
||||
import * as _ from 'lodash';
|
||||
import {schemas, SchemaValidator} from '@0xproject/json-schemas';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {SchemaValidator, schemas} from '@0xproject/json-schemas';
|
||||
import {bigNumberConfigs} from './bignumber_config';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import {Web3Wrapper} from './web3_wrapper';
|
||||
import {constants} from './utils/constants';
|
||||
import {utils} from './utils/utils';
|
||||
import {signatureUtils} from './utils/signature_utils';
|
||||
import {assert} from './utils/assert';
|
||||
import {AbiDecoder} from './utils/abi_decoder';
|
||||
import {intervalUtils} from './utils/interval_utils';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {artifacts} from './artifacts';
|
||||
import {bigNumberConfigs} from './bignumber_config';
|
||||
import {EtherTokenWrapper} from './contract_wrappers/ether_token_wrapper';
|
||||
import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper';
|
||||
import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper';
|
||||
import {EtherTokenWrapper} from './contract_wrappers/ether_token_wrapper';
|
||||
import {TokenWrapper} from './contract_wrappers/token_wrapper';
|
||||
import {TokenTransferProxyWrapper} from './contract_wrappers/token_transfer_proxy_wrapper';
|
||||
import {TokenWrapper} from './contract_wrappers/token_wrapper';
|
||||
import {OrderStateWatcher} from './order_watcher/order_state_watcher';
|
||||
import {OrderStateUtils} from './utils/order_state_utils';
|
||||
import {zeroExConfigSchema} from './schemas/zero_ex_config_schema';
|
||||
import {
|
||||
ECSignature,
|
||||
ZeroExError,
|
||||
Order,
|
||||
OrderStateWatcherConfig,
|
||||
SignedOrder,
|
||||
TransactionReceiptWithDecodedLogs,
|
||||
Web3Provider,
|
||||
ZeroExConfig,
|
||||
OrderStateWatcherConfig,
|
||||
TransactionReceiptWithDecodedLogs,
|
||||
ZeroExError,
|
||||
} from './types';
|
||||
import {zeroExConfigSchema} from './schemas/zero_ex_config_schema';
|
||||
import {AbiDecoder} from './utils/abi_decoder';
|
||||
import {assert} from './utils/assert';
|
||||
import {constants} from './utils/constants';
|
||||
import {intervalUtils} from './utils/interval_utils';
|
||||
import {OrderStateUtils} from './utils/order_state_utils';
|
||||
import {signatureUtils} from './utils/signature_utils';
|
||||
import {utils} from './utils/utils';
|
||||
import {Web3Wrapper} from './web3_wrapper';
|
||||
|
||||
// Customize our BigNumber instances
|
||||
bigNumberConfigs.configure();
|
||||
@@ -169,56 +170,48 @@ export class ZeroEx {
|
||||
* @param config The configuration object. Look up the type for the description.
|
||||
* @return An instance of the 0x.js ZeroEx class.
|
||||
*/
|
||||
constructor(provider: Web3Provider, config?: ZeroExConfig) {
|
||||
constructor(provider: Web3Provider, config: ZeroExConfig) {
|
||||
assert.isWeb3Provider('provider', provider);
|
||||
if (!_.isUndefined(config)) {
|
||||
assert.doesConformToSchema('config', config, zeroExConfigSchema);
|
||||
}
|
||||
assert.doesConformToSchema('config', config, zeroExConfigSchema);
|
||||
const artifactJSONs = _.values(artifacts);
|
||||
const abiArrays = _.map(artifactJSONs, artifact => artifact.abi);
|
||||
this._abiDecoder = new AbiDecoder(abiArrays);
|
||||
const gasPrice = _.isUndefined(config) ? undefined : config.gasPrice;
|
||||
const defaults = {
|
||||
gasPrice,
|
||||
gasPrice: config.gasPrice,
|
||||
};
|
||||
this._web3Wrapper = new Web3Wrapper(provider, defaults);
|
||||
this._web3Wrapper = new Web3Wrapper(provider, config.networkId, defaults);
|
||||
this.proxy = new TokenTransferProxyWrapper(
|
||||
this._web3Wrapper,
|
||||
config.tokenTransferProxyContractAddress,
|
||||
);
|
||||
this.token = new TokenWrapper(
|
||||
this._web3Wrapper,
|
||||
this._abiDecoder,
|
||||
this._getTokenTransferProxyAddressAsync.bind(this),
|
||||
this.proxy,
|
||||
);
|
||||
const exchageContractAddressIfExists = _.isUndefined(config) ? undefined : config.exchangeContractAddress;
|
||||
this.exchange = new ExchangeWrapper(
|
||||
this._web3Wrapper,
|
||||
this._abiDecoder,
|
||||
this.token,
|
||||
exchageContractAddressIfExists,
|
||||
config.exchangeContractAddress,
|
||||
);
|
||||
this.proxy = new TokenTransferProxyWrapper(
|
||||
this._web3Wrapper,
|
||||
this._getTokenTransferProxyAddressAsync.bind(this),
|
||||
);
|
||||
const tokenRegistryContractAddressIfExists = _.isUndefined(config) ?
|
||||
undefined :
|
||||
config.tokenRegistryContractAddress;
|
||||
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper, tokenRegistryContractAddressIfExists);
|
||||
const etherTokenContractAddressIfExists = _.isUndefined(config) ? undefined : config.etherTokenContractAddress;
|
||||
this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token, etherTokenContractAddressIfExists);
|
||||
const orderWatcherConfig = _.isUndefined(config) ? undefined : config.orderWatcherConfig;
|
||||
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper, config.tokenRegistryContractAddress);
|
||||
this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token, config.etherTokenContractAddress);
|
||||
this.orderStateWatcher = new OrderStateWatcher(
|
||||
this._web3Wrapper, this._abiDecoder, this.token, this.exchange, orderWatcherConfig,
|
||||
this._web3Wrapper, this._abiDecoder, this.token, this.exchange, config.orderWatcherConfig,
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Sets a new web3 provider for 0x.js. Updating the provider will stop all
|
||||
* subscriptions so you will need to re-subscribe to all events relevant to your app after this call.
|
||||
* @param provider The Web3Provider you would like the 0x.js library to use from now on.
|
||||
* @param networkId The id of the network your provider is connected to
|
||||
*/
|
||||
public async setProviderAsync(provider: Web3Provider) {
|
||||
this._web3Wrapper.setProvider(provider);
|
||||
await (this.exchange as any)._invalidateContractInstancesAsync();
|
||||
public setProvider(provider: Web3Provider, networkId: number): void {
|
||||
this._web3Wrapper.setProvider(provider, networkId);
|
||||
(this.exchange as any)._invalidateContractInstances();
|
||||
(this.tokenRegistry as any)._invalidateContractInstance();
|
||||
await (this.token as any)._invalidateContractInstancesAsync();
|
||||
(this.token as any)._invalidateContractInstances();
|
||||
(this.proxy as any)._invalidateContractInstance();
|
||||
(this.etherToken as any)._invalidateContractInstance();
|
||||
}
|
||||
|
@@ -1,11 +1,13 @@
|
||||
import {Artifact} from './types';
|
||||
import * as TokenArtifact from './artifacts/Token.json';
|
||||
import * as ExchangeArtifact from './artifacts/Exchange.json';
|
||||
import * as EtherTokenArtifact from './artifacts/EtherToken.json';
|
||||
import * as ExchangeArtifact from './artifacts/Exchange.json';
|
||||
import * as TokenArtifact from './artifacts/Token.json';
|
||||
import * as TokenRegistryArtifact from './artifacts/TokenRegistry.json';
|
||||
import * as TokenTransferProxyArtifact from './artifacts/TokenTransferProxy.json';
|
||||
import * as ZRXArtifact from './artifacts/ZRX.json';
|
||||
import {Artifact} from './types';
|
||||
|
||||
export const artifacts = {
|
||||
ZRXArtifact: ZRXArtifact as any as Artifact,
|
||||
TokenArtifact: TokenArtifact as any as Artifact,
|
||||
ExchangeArtifact: ExchangeArtifact as any as Artifact,
|
||||
EtherTokenArtifact: EtherTokenArtifact as any as Artifact,
|
||||
|
@@ -233,213 +233,18 @@
|
||||
"type": "event"
|
||||
}
|
||||
],
|
||||
"unlinked_binary": "0x6060604052341561000c57fe5b5b6107598061001c6000396000f300606060405236156100935763ffffffff60e060020a60003504166306fdde0381146100a4578063095ea7b31461013457806318160ddd1461016757806323b872dd146101895780632e1a7d4d146101c2578063313ce567146101d757806370a08231146101fd57806395d89b411461022b578063a9059cbb146102bb578063d0e30db0146102ee578063dd62ed3e146102f8575b6100a25b61009f61032c565b5b565b005b34156100ac57fe5b6100b461037b565b6040805160208082528351818301528351919283929083019185019080838382156100fa575b8051825260208311156100fa57601f1990920191602091820191016100da565b505050905090810190601f1680156101265780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013c57fe5b610153600160a060020a03600435166024356103a3565b604080519115158252519081900360200190f35b341561016f57fe5b61017761040e565b60408051918252519081900360200190f35b341561019157fe5b610153600160a060020a0360043581169060243516604435610414565b604080519115158252519081900360200190f35b34156101ca57fe5b6100a2600435610537565b005b34156101df57fe5b6101e76105b8565b6040805160ff9092168252519081900360200190f35b341561020557fe5b610177600160a060020a03600435166105bd565b60408051918252519081900360200190f35b341561023357fe5b6100b46105dc565b6040805160208082528351818301528351919283929083019185019080838382156100fa575b8051825260208311156100fa57601f1990920191602091820191016100da565b505050905090810190601f1680156101265780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102c357fe5b610153600160a060020a03600435166024356105fd565b604080519115158252519081900360200190f35b6100a261032c565b005b341561030057fe5b610177600160a060020a03600435811690602435166106af565b60408051918252519081900360200190f35b600160a060020a03331660009081526020819052604090205461034f90346106dc565b600160a060020a03331660009081526020819052604090205560025461037590346106dc565b6002555b565b60408051808201909152600b815260a960020a6a22ba3432b9102a37b5b2b702602082015281565b600160a060020a03338116600081815260016020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a35060015b92915050565b60025481565b600160a060020a03808416600081815260016020908152604080832033909516835293815283822054928252819052918220548390108015906104575750828110155b801561047d5750600160a060020a03841660009081526020819052604090205483810110155b1561052957600160a060020a03808516600090815260208190526040808220805487019055918716815220805484900390556000198110156104e757600160a060020a03808616600090815260016020908152604080832033909416835292905220805484900390555b83600160a060020a031685600160a060020a031660008051602061070e833981519152856040518082815260200191505060405180910390a36001915061052e565b600091505b5b509392505050565b600160a060020a03331660009081526020819052604090205461055a90826106f6565b600160a060020a03331660009081526020819052604090205560025461058090826106f6565b600255604051600160a060020a0333169082156108fc029083906000818181858888f1935050505015156105b45760006000fd5b5b50565b601281565b600160a060020a0381166000908152602081905260409020545b919050565b604080518082019091526004815260e360020a630ae8aa8902602082015281565b600160a060020a0333166000908152602081905260408120548290108015906106405750600160a060020a03831660009081526020819052604090205482810110155b156106a057600160a060020a03338116600081815260208181526040808320805488900390559387168083529184902080548701905583518681529351919360008051602061070e833981519152929081900390910190a3506001610408565b506000610408565b5b92915050565b600160a060020a038083166000908152600160209081526040808320938516835292905220545b92915050565b6000828201838110156106eb57fe5b8091505b5092915050565b60008282111561070257fe5b508082035b929150505600ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa165627a7a72305820ec42c469bb8ddd5de28c55b9cc393c812397c063a57fb88926e3f6de246318b70029",
|
||||
"networks": {
|
||||
"1": {
|
||||
"links": {},
|
||||
"events": {
|
||||
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_from",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Transfer",
|
||||
"type": "event"
|
||||
},
|
||||
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Approval",
|
||||
"type": "event"
|
||||
}
|
||||
},
|
||||
"updated_at": 1502488087000,
|
||||
"address": "0x2956356cd2a2bf3202f771f50d3d14a367b48070"
|
||||
},
|
||||
"3": {
|
||||
"links": {},
|
||||
"events": {
|
||||
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_from",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Transfer",
|
||||
"type": "event"
|
||||
},
|
||||
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Approval",
|
||||
"type": "event"
|
||||
}
|
||||
},
|
||||
"updated_at": 1506602007000,
|
||||
"address": "0xc00fd9820cd2898cc4c054b7bf142de637ad129a"
|
||||
},
|
||||
"42": {
|
||||
"links": {},
|
||||
"events": {
|
||||
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_from",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Transfer",
|
||||
"type": "event"
|
||||
},
|
||||
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Approval",
|
||||
"type": "event"
|
||||
}
|
||||
},
|
||||
"updated_at": 1502391794392,
|
||||
"address": "0x05d090b51c40b020eab3bfcb6a2dff130df22e9c"
|
||||
},
|
||||
"50": {
|
||||
"links": {},
|
||||
"events": {
|
||||
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_from",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Transfer",
|
||||
"type": "event"
|
||||
},
|
||||
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925": {
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Approval",
|
||||
"type": "event"
|
||||
}
|
||||
},
|
||||
"updated_at": 1503318938233,
|
||||
"address": "0x48bacb9266a570d521063ef5dd96e61686dbe788"
|
||||
}
|
||||
},
|
||||
"schema_version": "0.0.5",
|
||||
"updated_at": 1503318938233
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@@ -168,9 +168,5 @@
|
||||
"name": "Approval",
|
||||
"type": "event"
|
||||
}
|
||||
],
|
||||
"unlinked_binary": "0x6060604052341561000c57fe5b5b6101e08061001c6000396000f3006060604052361561005c5763ffffffff60e060020a600035041663095ea7b3811461005e57806318160ddd1461009157806323b872dd146100b357806370a08231146100ec578063a9059cbb1461005e578063dd62ed3e1461014d575bfe5b341561006657fe5b61007d600160a060020a0360043516602435610181565b604080519115158252519081900360200190f35b341561009957fe5b6100a161018a565b60408051918252519081900360200190f35b34156100bb57fe5b61007d600160a060020a0360043581169060243516604435610190565b604080519115158252519081900360200190f35b34156100f457fe5b6100a1600160a060020a036004351661019a565b60408051918252519081900360200190f35b341561006657fe5b61007d600160a060020a0360043516602435610181565b604080519115158252519081900360200190f35b341561015557fe5b6100a1600160a060020a0360043581169060243516610181565b60408051918252519081900360200190f35b60005b92915050565b60005b90565b60005b9392505050565b60005b919050565b60005b92915050565b60005b929150505600a165627a7a723058202e3f7ac17048343c0d0ea24fccb64620577374eeeed61539e543df4025d7d0db0029",
|
||||
"networks": {},
|
||||
"schema_version": "0.0.5",
|
||||
"updated_at": 1503317882695
|
||||
}
|
||||
]
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@@ -167,8 +167,18 @@
|
||||
"type": "event"
|
||||
}
|
||||
],
|
||||
"unlinked_binary": "0x60606040525b60008054600160a060020a03191633600160a060020a03161790555b5b6106e6806100316000396000f300606060405236156100725763ffffffff60e060020a60003504166315dacbea811461007457806342f1181e146100b3578063494503d4146100d157806370712939146101005780638da5cb5b1461011e578063b91816111461014a578063d39de6e91461017a578063f2fde38b146101e5575bfe5b341561007c57fe5b61009f600160a060020a0360043581169060243581169060443516606435610203565b604080519115158252519081900360200190f35b34156100bb57fe5b6100cf600160a060020a03600435166102ae565b005b34156100d957fe5b6100e4600435610390565b60408051600160a060020a039092168252519081900360200190f35b341561010857fe5b6100cf600160a060020a03600435166103c2565b005b341561012657fe5b6100e461055a565b60408051600160a060020a039092168252519081900360200190f35b341561015257fe5b61009f600160a060020a0360043516610569565b604080519115158252519081900360200190f35b341561018257fe5b61018a61057e565b60408051602080825283518183015283519192839290830191858101910280838382156101d2575b8051825260208311156101d257601f1990920191602091820191016101b2565b5050509050019250505060405180910390f35b34156101ed57fe5b6100cf600160a060020a03600435166105e7565b005b600160a060020a03331660009081526001602052604081205460ff16151561022b5760006000fd5b6040805160006020918201819052825160e060020a6323b872dd028152600160a060020a0388811660048301528781166024830152604482018790529351938916936323b872dd9360648084019491938390030190829087803b151561028d57fe5b6102c65a03f1151561029b57fe5b5050604051519150505b5b949350505050565b60005433600160a060020a039081169116146102ca5760006000fd5b600160a060020a038116600090815260016020526040902054819060ff16156102f35760006000fd5b600160a060020a0382166000908152600160208190526040909120805460ff191682179055600280549091810161032a8382610633565b916000526020600020900160005b81546101009190910a600160a060020a0381810219909216868316918202179092556040513390911692507f94bb87f4c15c4587ff559a7584006fa01ddf9299359be6b512b94527aa961aca90600090a35b5b505b50565b600280548290811061039e57fe5b906000526020600020900160005b915054906101000a9004600160a060020a031681565b6000805433600160a060020a039081169116146103df5760006000fd5b600160a060020a038216600090815260016020526040902054829060ff1615156104095760006000fd5b600160a060020a0383166000908152600160205260408120805460ff1916905591505b6002548210156105195782600160a060020a031660028381548110151561044f57fe5b906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316141561050d5760028054600019810190811061049057fe5b906000526020600020900160005b9054906101000a9004600160a060020a03166002838154811015156104bf57fe5b906000526020600020900160005b6101000a815481600160a060020a030219169083600160a060020a0316021790555060016002818180549050039150816105079190610633565b50610519565b5b60019091019061042c565b604051600160a060020a0333811691908516907ff5b347a1e40749dd050f5f07fbdbeb7e3efa9756903044dd29401fd1d4bb4a1c90600090a35b5b505b5050565b600054600160a060020a031681565b60016020526000908152604090205460ff1681565b610586610687565b60028054806020026020016040519081016040528092919081815260200182805480156105dc57602002820191906000526020600020905b8154600160a060020a031681526001909101906020018083116105be575b505050505090505b90565b60005433600160a060020a039081169116146106035760006000fd5b600160a060020a0381161561038d5760008054600160a060020a031916600160a060020a0383161790555b5b5b50565b81548183558181151161055357600083815260209020610553918101908301610699565b5b505050565b81548183558181151161055357600083815260209020610553918101908301610699565b5b505050565b60408051602081019091526000815290565b6105e491905b808211156106b3576000815560010161069f565b5090565b905600a165627a7a72305820d2924957bb88a128789172e164d874fe5445218fc2dde2f5eb265839a1f341a20029",
|
||||
"networks": {},
|
||||
"schema_version": "0.0.5",
|
||||
"updated_at": 1503318938227
|
||||
"networks": {
|
||||
"1": {
|
||||
"address": "0x8da0d80f5007ef1e431dd2127178d224e32c2ef4"
|
||||
},
|
||||
"3": {
|
||||
"address": "0x4e9aad8184de8833365fea970cd9149372fdf1e6"
|
||||
},
|
||||
"42": {
|
||||
"address": "0x087eed4bc1ee3de49befbd66c662b434b15d49d4"
|
||||
},
|
||||
"50": {
|
||||
"address": "0x871dd7c2b4b25e1aa18728e9d5f2af4c4e431f5c"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
packages/0x.js/src/artifacts/ZRX.json
Normal file
17
packages/0x.js/src/artifacts/ZRX.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"contract_name": "ZRX",
|
||||
"networks": {
|
||||
"1": {
|
||||
"address": "0xe41d2489571d322189246dafa5ebde1f4699f498"
|
||||
},
|
||||
"3": {
|
||||
"address": "0xa8e9fa8f91e5ae138c74648c9c304f1c75003a8d"
|
||||
},
|
||||
"42": {
|
||||
"address": "0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570"
|
||||
},
|
||||
"50": {
|
||||
"address": "0x25b8fe1de9daf8ba351890744ff28cf7dfa8f5e3"
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,9 +1,14 @@
|
||||
import * as Web3 from 'web3';
|
||||
import * as _ from 'lodash';
|
||||
import {schemas, SchemaValidator} from '@0xproject/json-schemas';
|
||||
import promisify = require('es6-promisify');
|
||||
import {SchemaValidator, schemas} from '@0xproject/json-schemas';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {AbiType} from './types';
|
||||
|
||||
// HACK: Gas estimates on testrpc don't take into account gas refunds.
|
||||
// Our calls can trigger max 8 gas refunds for SSTORE per transaction for 15k gas each which gives 120k.
|
||||
const GAS_MARGIN = 120000;
|
||||
|
||||
export class Contract implements Web3.ContractInstance {
|
||||
public address: string;
|
||||
public abi: Web3.ContractAbi;
|
||||
@@ -33,9 +38,10 @@ export class Contract implements Web3.ContractInstance {
|
||||
} else {
|
||||
const cbStyleFunction = this.contract[functionAbi.name];
|
||||
const cbStyleEstimateGasFunction = this.contract[functionAbi.name].estimateGas;
|
||||
const estimateGasAsync = promisify(cbStyleEstimateGasFunction, this.contract);
|
||||
this[functionAbi.name] = {
|
||||
estimateGasAsync: promisify(cbStyleEstimateGasFunction, this.contract),
|
||||
sendTransactionAsync: this.promisifyWithDefaultParams(cbStyleFunction),
|
||||
estimateGasAsync,
|
||||
sendTransactionAsync: this.promisifyWithDefaultParams(cbStyleFunction, estimateGasAsync),
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -46,28 +52,40 @@ export class Contract implements Web3.ContractInstance {
|
||||
this[eventAbi.name] = this.contract[eventAbi.name];
|
||||
});
|
||||
}
|
||||
private promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise<any> {
|
||||
const promisifiedWithDefaultParams = (...args: any[]) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
private promisifyWithDefaultParams(
|
||||
web3CbStyleFunction: (...args: any[]) => void,
|
||||
estimateGasAsync: (...args: any[]) => Promise<number>,
|
||||
): (...args: any[]) => Promise<any> {
|
||||
const promisifiedWithDefaultParams = async (...args: any[]) => {
|
||||
const promise = new Promise(async (resolve, reject) => {
|
||||
const lastArg = args[args.length - 1];
|
||||
let txData: Partial<Web3.TxData> = {};
|
||||
if (this.isTxData(lastArg)) {
|
||||
if (!_.isUndefined(lastArg) && this.isTxData(lastArg)) {
|
||||
txData = args.pop();
|
||||
}
|
||||
// Gas amount sourced with the following priorities:
|
||||
// 1. Optional param passed in to public method call
|
||||
// 2. Global config passed in at library instantiation
|
||||
// 3. Gas estimate calculation + safety margin
|
||||
const removeUndefinedProperties = _.pickBy;
|
||||
txData = {
|
||||
...this.defaults,
|
||||
...txData,
|
||||
...removeUndefinedProperties(this.defaults),
|
||||
...removeUndefinedProperties(txData),
|
||||
};
|
||||
const callback = (err: Error, data: any) => {
|
||||
if (_.isNull(err)) {
|
||||
resolve(data);
|
||||
} else {
|
||||
if (_.isUndefined(txData.gas)) {
|
||||
try {
|
||||
const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]);
|
||||
const gas = estimatedGas + GAS_MARGIN;
|
||||
txData.gas = gas;
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
const callback = (err: Error, data: any) => _.isNull(err) ? resolve(data) : reject(err);
|
||||
args.push(txData);
|
||||
args.push(callback);
|
||||
fn.apply(this.contract, args);
|
||||
web3CbStyleFunction.apply(this.contract, args);
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
@@ -1,24 +1,25 @@
|
||||
import {Block, BlockAndLogStreamer} from 'ethereumjs-blockstream';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import {BlockAndLogStreamer, Block} from 'ethereumjs-blockstream';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
|
||||
import {
|
||||
ZeroExError,
|
||||
InternalZeroExError,
|
||||
Artifact,
|
||||
LogWithDecodedArgs,
|
||||
RawLog,
|
||||
ContractEvents,
|
||||
SubscriptionOpts,
|
||||
IndexedFilterValues,
|
||||
EventCallback,
|
||||
BlockParamLiteral,
|
||||
ContractEventArgs,
|
||||
ContractEvents,
|
||||
EventCallback,
|
||||
IndexedFilterValues,
|
||||
InternalZeroExError,
|
||||
LogWithDecodedArgs,
|
||||
RawLog,
|
||||
SubscriptionOpts,
|
||||
ZeroExError,
|
||||
} from '../types';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {constants} from '../utils/constants';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {filterUtils} from '../utils/filter_utils';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
export class ContractWrapper {
|
||||
protected _web3Wrapper: Web3Wrapper;
|
||||
@@ -95,13 +96,25 @@ export class ContractWrapper {
|
||||
await this._web3Wrapper.getContractInstanceFromArtifactAsync<ContractType>(artifact, addressIfExists);
|
||||
return contractInstance;
|
||||
}
|
||||
private _onLogStateChanged<ArgsType extends ContractEventArgs>(removed: boolean, log: Web3.LogEntry): void {
|
||||
protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string {
|
||||
if (_.isUndefined(addressIfExists)) {
|
||||
const networkId = this._web3Wrapper.getNetworkId();
|
||||
const contractAddress = artifact.networks[networkId].address;
|
||||
if (_.isUndefined(contractAddress)) {
|
||||
throw new Error(ZeroExError.ExchangeContractDoesNotExist);
|
||||
}
|
||||
return contractAddress;
|
||||
} else {
|
||||
return addressIfExists;
|
||||
}
|
||||
}
|
||||
private _onLogStateChanged<ArgsType extends ContractEventArgs>(isRemoved: boolean, log: Web3.LogEntry): void {
|
||||
_.forEach(this._filters, (filter: Web3.FilterObject, filterToken: string) => {
|
||||
if (filterUtils.matchesFilter(log, filter)) {
|
||||
const decodedLog = this._tryToDecodeLogOrNoop(log) as LogWithDecodedArgs<ArgsType>;
|
||||
const logEvent = {
|
||||
...decodedLog,
|
||||
removed,
|
||||
log: decodedLog,
|
||||
isRemoved,
|
||||
};
|
||||
this._filterCallbacks[filterToken](null, logEvent);
|
||||
}
|
||||
@@ -117,13 +130,13 @@ export class ContractWrapper {
|
||||
this._blockAndLogStreamInterval = intervalUtils.setAsyncExcludingInterval(
|
||||
this._reconcileBlockAsync.bind(this), constants.DEFAULT_BLOCK_POLLING_INTERVAL,
|
||||
);
|
||||
let removed = false;
|
||||
let isRemoved = false;
|
||||
this._onLogAddedSubscriptionToken = this._blockAndLogStreamer.subscribeToOnLogAdded(
|
||||
this._onLogStateChanged.bind(this, removed),
|
||||
this._onLogStateChanged.bind(this, isRemoved),
|
||||
);
|
||||
removed = true;
|
||||
isRemoved = true;
|
||||
this._onLogRemovedSubscriptionToken = this._blockAndLogStreamer.subscribeToOnLogRemoved(
|
||||
this._onLogStateChanged.bind(this, removed),
|
||||
this._onLogStateChanged.bind(this, isRemoved),
|
||||
);
|
||||
}
|
||||
private _stopBlockAndLogStream(): void {
|
||||
@@ -140,7 +153,7 @@ export class ContractWrapper {
|
||||
// We need to coerce to Block type cause Web3.Block includes types for mempool blocks
|
||||
if (!_.isUndefined(this._blockAndLogStreamer)) {
|
||||
// If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined
|
||||
this._blockAndLogStreamer.reconcileNewBlock(latestBlock as any as Block);
|
||||
await this._blockAndLogStreamer.reconcileNewBlock(latestBlock as any as Block);
|
||||
}
|
||||
} catch (err) {
|
||||
const filterTokens = _.keys(this._filterCallbacks);
|
||||
|
@@ -1,11 +1,13 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {artifacts} from '../artifacts';
|
||||
import {EtherTokenContract, TransactionOpts, ZeroExError} from '../types';
|
||||
import {assert} from '../utils/assert';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
import {TokenWrapper} from './token_wrapper';
|
||||
import {EtherTokenContract, ZeroExError} from '../types';
|
||||
import {assert} from '../utils/assert';
|
||||
import {artifacts} from '../artifacts';
|
||||
|
||||
/**
|
||||
* This class includes all the functionality related to interacting with a wrapped Ether ERC20 token contract.
|
||||
@@ -25,10 +27,13 @@ export class EtherTokenWrapper extends ContractWrapper {
|
||||
* to the depositor address. These wrapped ETH tokens can be used in 0x trades and are redeemable for 1-to-1
|
||||
* for ETH.
|
||||
* @param amountInWei Amount of ETH in Wei the caller wishes to deposit.
|
||||
* @param depositor The hex encoded user Ethereum address that would like to make the deposit.
|
||||
* @param depositor The hex encoded user Ethereum address that would like to make the deposit.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async depositAsync(amountInWei: BigNumber, depositor: string): Promise<string> {
|
||||
public async depositAsync(
|
||||
amountInWei: BigNumber, depositor: string, txOpts: TransactionOpts = {},
|
||||
): Promise<string> {
|
||||
assert.isValidBaseUnitAmount('amountInWei', amountInWei);
|
||||
await assert.isSenderAddressAsync('depositor', depositor, this._web3Wrapper);
|
||||
|
||||
@@ -39,6 +44,8 @@ export class EtherTokenWrapper extends ContractWrapper {
|
||||
const txHash = await wethContract.deposit.sendTransactionAsync({
|
||||
from: depositor,
|
||||
value: amountInWei,
|
||||
gas: txOpts.gasLimit,
|
||||
gasPrice: txOpts.gasPrice,
|
||||
});
|
||||
return txHash;
|
||||
}
|
||||
@@ -47,19 +54,24 @@ export class EtherTokenWrapper extends ContractWrapper {
|
||||
* equivalent number of wrapped ETH tokens.
|
||||
* @param amountInWei Amount of ETH in Wei the caller wishes to withdraw.
|
||||
* @param withdrawer The hex encoded user Ethereum address that would like to make the withdrawl.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async withdrawAsync(amountInWei: BigNumber, withdrawer: string): Promise<string> {
|
||||
public async withdrawAsync(
|
||||
amountInWei: BigNumber, withdrawer: string, txOpts: TransactionOpts = {},
|
||||
): Promise<string> {
|
||||
assert.isValidBaseUnitAmount('amountInWei', amountInWei);
|
||||
await assert.isSenderAddressAsync('withdrawer', withdrawer, this._web3Wrapper);
|
||||
|
||||
const wethContractAddress = await this.getContractAddressAsync();
|
||||
const wethContractAddress = this.getContractAddress();
|
||||
const WETHBalanceInBaseUnits = await this._tokenWrapper.getBalanceAsync(wethContractAddress, withdrawer);
|
||||
assert.assert(WETHBalanceInBaseUnits.gte(amountInWei), ZeroExError.InsufficientWEthBalanceForWithdrawal);
|
||||
|
||||
const wethContract = await this._getEtherTokenContractAsync();
|
||||
const txHash = await wethContract.withdraw.sendTransactionAsync(amountInWei, {
|
||||
from: withdrawer,
|
||||
gas: txOpts.gasLimit,
|
||||
gasPrice: txOpts.gasPrice,
|
||||
});
|
||||
return txHash;
|
||||
}
|
||||
@@ -67,9 +79,11 @@ export class EtherTokenWrapper extends ContractWrapper {
|
||||
* Retrieves the Wrapped Ether token contract address
|
||||
* @return The Wrapped Ether token contract address
|
||||
*/
|
||||
public async getContractAddressAsync(): Promise<string> {
|
||||
const wethContract = await this._getEtherTokenContractAsync();
|
||||
return wethContract.address;
|
||||
public getContractAddress(): string {
|
||||
const contractAddress = this._getContractAddress(
|
||||
artifacts.EtherTokenArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
return contractAddress;
|
||||
}
|
||||
private _invalidateContractInstance(): void {
|
||||
delete this._etherTokenContractIfExists;
|
||||
@@ -81,7 +95,7 @@ export class EtherTokenWrapper extends ContractWrapper {
|
||||
const contractInstance = await this._instantiateContractIfExistsAsync<EtherTokenContract>(
|
||||
artifacts.EtherTokenArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
this._etherTokenContractIfExists = contractInstance as EtherTokenContract;
|
||||
this._etherTokenContractIfExists = contractInstance;
|
||||
return this._etherTokenContractIfExists;
|
||||
}
|
||||
}
|
||||
|
@@ -1,44 +1,46 @@
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
import {artifacts} from '../artifacts';
|
||||
import {
|
||||
BlockParamLiteral,
|
||||
DecodedLogArgs,
|
||||
ECSignature,
|
||||
EventCallback,
|
||||
ExchangeContract,
|
||||
ExchangeContractErrCodes,
|
||||
ExchangeContractErrs,
|
||||
ZeroExError,
|
||||
OrderValues,
|
||||
OrderAddresses,
|
||||
Order,
|
||||
SignedOrder,
|
||||
ExchangeContractEventArgs,
|
||||
ExchangeEvents,
|
||||
SubscriptionOpts,
|
||||
IndexedFilterValues,
|
||||
OrderCancellationRequest,
|
||||
OrderFillRequest,
|
||||
LogCancelContractEventArgs,
|
||||
LogErrorContractEventArgs,
|
||||
LogFillContractEventArgs,
|
||||
LogCancelContractEventArgs,
|
||||
LogWithDecodedArgs,
|
||||
MethodOpts,
|
||||
ValidateOrderFillableOpts,
|
||||
Order,
|
||||
OrderAddresses,
|
||||
OrderCancellationRequest,
|
||||
OrderFillRequest,
|
||||
OrderTransactionOpts,
|
||||
OrderValues,
|
||||
RawLog,
|
||||
EventCallback,
|
||||
ExchangeContractEventArgs,
|
||||
DecodedLogArgs,
|
||||
SignedOrder,
|
||||
SubscriptionOpts,
|
||||
ValidateOrderFillableOpts,
|
||||
} from '../types';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {assert} from '../utils/assert';
|
||||
import {utils} from '../utils/utils';
|
||||
import {decorators} from '../utils/decorators';
|
||||
import {ExchangeTransferSimulator} from '../utils/exchange_transfer_simulator';
|
||||
import {OrderValidationUtils} from '../utils/order_validation_utils';
|
||||
import {utils} from '../utils/utils';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
import {TokenWrapper} from './token_wrapper';
|
||||
import {decorators} from '../utils/decorators';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {ExchangeTransferSimulator} from '../utils/exchange_transfer_simulator';
|
||||
import {artifacts} from '../artifacts';
|
||||
|
||||
const SHOULD_VALIDATE_BY_DEFAULT = true;
|
||||
|
||||
@@ -63,6 +65,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
[ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FillBalanceAllowanceError,
|
||||
};
|
||||
private _contractAddressIfExists?: string;
|
||||
private _zrxContractAddressIfExists?: string;
|
||||
private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] {
|
||||
const orderAddresses: OrderAddresses = [
|
||||
order.maker,
|
||||
@@ -166,37 +169,25 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
public async fillOrderAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber,
|
||||
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
|
||||
takerAddress: string,
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount);
|
||||
assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance);
|
||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
|
||||
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress);
|
||||
}
|
||||
|
||||
const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(signedOrder);
|
||||
|
||||
const gas = await exchangeInstance.fillOrder.estimateGasAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
fillTakerTokenAmount,
|
||||
shouldThrowOnInsufficientBalanceOrAllowance,
|
||||
signedOrder.ecSignature.v,
|
||||
signedOrder.ecSignature.r,
|
||||
signedOrder.ecSignature.s,
|
||||
{
|
||||
from: takerAddress,
|
||||
},
|
||||
);
|
||||
const txHash: string = await exchangeInstance.fillOrder.sendTransactionAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
@@ -207,7 +198,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
signedOrder.ecSignature.s,
|
||||
{
|
||||
from: takerAddress,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -233,7 +225,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
public async fillOrdersUpToAsync(signedOrders: SignedOrder[], fillTakerTokenAmount: BigNumber,
|
||||
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
|
||||
takerAddress: string,
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema);
|
||||
const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress);
|
||||
assert.hasAtMostOneUniqueValue(takerTokenAddresses,
|
||||
@@ -245,12 +237,12 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance);
|
||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
|
||||
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
for (const signedOrder of signedOrders) {
|
||||
await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress);
|
||||
@@ -275,18 +267,6 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
);
|
||||
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
const gas = await exchangeInstance.fillOrdersUpTo.estimateGasAsync(
|
||||
orderAddressesArray,
|
||||
orderValuesArray,
|
||||
fillTakerTokenAmount,
|
||||
shouldThrowOnInsufficientBalanceOrAllowance,
|
||||
vArray,
|
||||
rArray,
|
||||
sArray,
|
||||
{
|
||||
from: takerAddress,
|
||||
},
|
||||
);
|
||||
const txHash = await exchangeInstance.fillOrdersUpTo.sendTransactionAsync(
|
||||
orderAddressesArray,
|
||||
orderValuesArray,
|
||||
@@ -297,7 +277,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
sArray,
|
||||
{
|
||||
from: takerAddress,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -325,7 +306,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
public async batchFillOrdersAsync(orderFillRequests: OrderFillRequest[],
|
||||
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
|
||||
takerAddress: string,
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('orderFillRequests', orderFillRequests, schemas.orderFillRequestsSchema);
|
||||
const exchangeContractAddresses = _.map(
|
||||
orderFillRequests,
|
||||
@@ -335,12 +316,12 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
ExchangeContractErrs.BatchOrdersMustHaveSameExchangeAddress);
|
||||
assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance);
|
||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
for (const orderFillRequest of orderFillRequests) {
|
||||
await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount,
|
||||
@@ -367,18 +348,6 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
);
|
||||
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
const gas = await exchangeInstance.batchFillOrders.estimateGasAsync(
|
||||
orderAddressesArray,
|
||||
orderValuesArray,
|
||||
fillTakerTokenAmounts,
|
||||
shouldThrowOnInsufficientBalanceOrAllowance,
|
||||
vArray,
|
||||
rArray,
|
||||
sArray,
|
||||
{
|
||||
from: takerAddress,
|
||||
},
|
||||
);
|
||||
const txHash = await exchangeInstance.batchFillOrders.sendTransactionAsync(
|
||||
orderAddressesArray,
|
||||
orderValuesArray,
|
||||
@@ -389,7 +358,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
sArray,
|
||||
{
|
||||
from: takerAddress,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -408,36 +378,24 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
@decorators.contractCallErrorHandler
|
||||
public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber,
|
||||
takerAddress: string,
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount);
|
||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
|
||||
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress);
|
||||
}
|
||||
|
||||
const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(signedOrder);
|
||||
|
||||
const gas = await exchangeInstance.fillOrKillOrder.estimateGasAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.ecSignature.v,
|
||||
signedOrder.ecSignature.r,
|
||||
signedOrder.ecSignature.s,
|
||||
{
|
||||
from: takerAddress,
|
||||
},
|
||||
);
|
||||
const txHash = await exchangeInstance.fillOrKillOrder.sendTransactionAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
@@ -447,7 +405,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
signedOrder.ecSignature.s,
|
||||
{
|
||||
from: takerAddress,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -464,7 +423,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
@decorators.contractCallErrorHandler
|
||||
public async batchFillOrKillAsync(orderFillRequests: OrderFillRequest[],
|
||||
takerAddress: string,
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('orderFillRequests', orderFillRequests,
|
||||
schemas.orderFillRequestsSchema);
|
||||
const exchangeContractAddresses = _.map(
|
||||
@@ -479,12 +438,12 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
}
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
for (const orderFillRequest of orderFillRequests) {
|
||||
await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount,
|
||||
@@ -506,18 +465,6 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
// We use _.unzip<any> because _.unzip doesn't type check if values have different types :'(
|
||||
const [orderAddresses, orderValues, fillTakerTokenAmounts, vParams, rParams, sParams] =
|
||||
_.unzip<any>(orderAddressesValuesAndTakerTokenFillAmounts);
|
||||
|
||||
const gas = await exchangeInstance.batchFillOrKillOrders.estimateGasAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
fillTakerTokenAmounts,
|
||||
vParams,
|
||||
rParams,
|
||||
sParams,
|
||||
{
|
||||
from: takerAddress,
|
||||
},
|
||||
);
|
||||
const txHash = await exchangeInstance.batchFillOrKillOrders.sendTransactionAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
@@ -527,7 +474,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
sParams,
|
||||
{
|
||||
from: takerAddress,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -543,39 +491,32 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
@decorators.contractCallErrorHandler
|
||||
public async cancelOrderAsync(order: Order|SignedOrder,
|
||||
cancelTakerTokenAmount: BigNumber,
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('order', order, schemas.orderSchema);
|
||||
assert.isValidBaseUnitAmount('takerTokenCancelAmount', cancelTakerTokenAmount);
|
||||
await assert.isSenderAddressAsync('order.maker', order.maker, this._web3Wrapper);
|
||||
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
const orderHash = utils.getOrderHashHex(order);
|
||||
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
|
||||
await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync(
|
||||
OrderValidationUtils.validateCancelOrderThrowIfInvalid(
|
||||
order, cancelTakerTokenAmount, unavailableTakerTokenAmount);
|
||||
}
|
||||
|
||||
const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(order);
|
||||
const gas = await exchangeInstance.cancelOrder.estimateGasAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
cancelTakerTokenAmount,
|
||||
{
|
||||
from: order.maker,
|
||||
},
|
||||
);
|
||||
const txHash = await exchangeInstance.cancelOrder.sendTransactionAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
cancelTakerTokenAmount,
|
||||
{
|
||||
from: order.maker,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -590,7 +531,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
*/
|
||||
@decorators.contractCallErrorHandler
|
||||
public async batchCancelOrdersAsync(orderCancellationRequests: OrderCancellationRequest[],
|
||||
orderTransactionOpts?: OrderTransactionOpts): Promise<string> {
|
||||
orderTransactionOpts: OrderTransactionOpts = {}): Promise<string> {
|
||||
assert.doesConformToSchema('orderCancellationRequests', orderCancellationRequests,
|
||||
schemas.orderCancellationRequestsSchema);
|
||||
const exchangeContractAddresses = _.map(
|
||||
@@ -603,14 +544,14 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MultipleMakersInSingleCancelBatchDisallowed);
|
||||
const maker = makers[0];
|
||||
await assert.isSenderAddressAsync('maker', maker, this._web3Wrapper);
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
const shouldValidate = _.isUndefined(orderTransactionOpts.shouldValidate) ?
|
||||
SHOULD_VALIDATE_BY_DEFAULT :
|
||||
orderTransactionOpts.shouldValidate;
|
||||
if (shouldValidate) {
|
||||
for (const orderCancellationRequest of orderCancellationRequests) {
|
||||
const orderHash = utils.getOrderHashHex(orderCancellationRequest.order);
|
||||
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
|
||||
await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync(
|
||||
OrderValidationUtils.validateCancelOrderThrowIfInvalid(
|
||||
orderCancellationRequest.order, orderCancellationRequest.takerTokenCancelAmount,
|
||||
unavailableTakerTokenAmount,
|
||||
);
|
||||
@@ -630,21 +571,14 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
// We use _.unzip<any> because _.unzip doesn't type check if values have different types :'(
|
||||
const [orderAddresses, orderValues, cancelTakerTokenAmounts] =
|
||||
_.unzip<any>(orderAddressesValuesAndTakerTokenCancelAmounts);
|
||||
const gas = await exchangeInstance.batchCancelOrders.estimateGasAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
cancelTakerTokenAmounts,
|
||||
{
|
||||
from: maker,
|
||||
},
|
||||
);
|
||||
const txHash = await exchangeInstance.batchCancelOrders.sendTransactionAsync(
|
||||
orderAddresses,
|
||||
orderValues,
|
||||
cancelTakerTokenAmounts,
|
||||
{
|
||||
from: maker,
|
||||
gas,
|
||||
gas: orderTransactionOpts.gasLimit,
|
||||
gasPrice: orderTransactionOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -657,13 +591,13 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* @param callback Callback that gets called when a log is added/removed
|
||||
* @return Subscription token used later to unsubscribe
|
||||
*/
|
||||
public async subscribeAsync<ArgsType extends ExchangeContractEventArgs>(
|
||||
public subscribe<ArgsType extends ExchangeContractEventArgs>(
|
||||
eventName: ExchangeEvents, indexFilterValues: IndexedFilterValues,
|
||||
callback: EventCallback<ArgsType>): Promise<string> {
|
||||
callback: EventCallback<ArgsType>): string {
|
||||
assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents);
|
||||
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
|
||||
assert.isFunction('callback', callback);
|
||||
const exchangeContractAddress = await this.getContractAddressAsync();
|
||||
const exchangeContractAddress = this.getContractAddress();
|
||||
const subscriptionToken = this._subscribe<ArgsType>(
|
||||
exchangeContractAddress, eventName, indexFilterValues, artifacts.ExchangeArtifact.abi, callback,
|
||||
);
|
||||
@@ -690,7 +624,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents);
|
||||
assert.doesConformToSchema('subscriptionOpts', subscriptionOpts, schemas.subscriptionOptsSchema);
|
||||
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
|
||||
const exchangeContractAddress = await this.getContractAddressAsync();
|
||||
const exchangeContractAddress = this.getContractAddress();
|
||||
const logs = await this._getLogsAsync<ArgsType>(
|
||||
exchangeContractAddress, eventName, subscriptionOpts, indexFilterValues, artifacts.ExchangeArtifact.abi,
|
||||
);
|
||||
@@ -701,10 +635,9 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* that the user-passed web3 provider is connected to.
|
||||
* @returns The Ethereum address of the Exchange contract being used.
|
||||
*/
|
||||
public async getContractAddressAsync(): Promise<string> {
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
const exchangeAddress = exchangeInstance.address;
|
||||
return exchangeAddress;
|
||||
public getContractAddress(): string {
|
||||
const contractAddress = this._getContractAddress(artifacts.ExchangeArtifact, this._contractAddressIfExists);
|
||||
return contractAddress;
|
||||
}
|
||||
/**
|
||||
* Checks if order is still fillable and throws an error otherwise. Useful for orderbook
|
||||
@@ -719,9 +652,9 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
signedOrder: SignedOrder, opts?: ValidateOrderFillableOpts,
|
||||
): Promise<void> {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const expectedFillTakerTokenAmount = !_.isUndefined(opts) ? opts.expectedFillTakerTokenAmount : undefined;
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
await this._orderValidationUtils.validateOrderFillableOrThrowAsync(
|
||||
exchangeTradeEmulator, signedOrder, zrxTokenAddress, expectedFillTakerTokenAmount,
|
||||
);
|
||||
@@ -740,8 +673,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount);
|
||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress);
|
||||
}
|
||||
@@ -757,7 +690,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
assert.isValidBaseUnitAmount('cancelTakerTokenAmount', cancelTakerTokenAmount);
|
||||
const orderHash = utils.getOrderHashHex(order);
|
||||
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
|
||||
await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync(
|
||||
OrderValidationUtils.validateCancelOrderThrowIfInvalid(
|
||||
order, cancelTakerTokenAmount, unavailableTakerTokenAmount);
|
||||
}
|
||||
/**
|
||||
@@ -774,8 +707,8 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount);
|
||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);
|
||||
const zrxTokenAddress = await this.getZRXTokenAddressAsync();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper);
|
||||
const zrxTokenAddress = this.getZRXTokenAddress();
|
||||
const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper, BlockParamLiteral.Latest);
|
||||
await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress);
|
||||
}
|
||||
@@ -819,12 +752,13 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* Returns the ZRX token address used by the exchange contract.
|
||||
* @return Address of ZRX token
|
||||
*/
|
||||
public async getZRXTokenAddressAsync(): Promise<string> {
|
||||
const exchangeInstance = await this._getExchangeContractAsync();
|
||||
const ZRXtokenAddress = await exchangeInstance.ZRX_TOKEN_CONTRACT.callAsync();
|
||||
return ZRXtokenAddress;
|
||||
public getZRXTokenAddress(): string {
|
||||
const contractAddress = this._getContractAddress(
|
||||
artifacts.ZRXArtifact, this._zrxContractAddressIfExists,
|
||||
);
|
||||
return contractAddress;
|
||||
}
|
||||
private async _invalidateContractInstancesAsync(): Promise<void> {
|
||||
private _invalidateContractInstances(): void {
|
||||
this.unsubscribeAll();
|
||||
delete this._exchangeContractIfExists;
|
||||
}
|
||||
@@ -858,7 +792,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
const contractInstance = await this._instantiateContractIfExistsAsync<ExchangeContract>(
|
||||
artifacts.ExchangeArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
this._exchangeContractIfExists = contractInstance as ExchangeContract;
|
||||
this._exchangeContractIfExists = contractInstance;
|
||||
return this._exchangeContractIfExists;
|
||||
}
|
||||
private async _getTokenTransferProxyAddressAsync(): Promise<string> {
|
||||
@@ -867,4 +801,4 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
const tokenTransferProxyAddressLowerCase = tokenTransferProxyAddress.toLowerCase();
|
||||
return tokenTransferProxyAddressLowerCase;
|
||||
}
|
||||
}
|
||||
} // tslint:disable:max-file-line-count
|
||||
|
@@ -1,10 +1,12 @@
|
||||
import * as _ from 'lodash';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {assert} from '../utils/assert';
|
||||
import {Token, TokenRegistryContract, TokenMetadata} from '../types';
|
||||
import {constants} from '../utils/constants';
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
|
||||
import {artifacts} from '../artifacts';
|
||||
import {Token, TokenMetadata, TokenRegistryContract, ZeroExError} from '../types';
|
||||
import {assert} from '../utils/assert';
|
||||
import {constants} from '../utils/constants';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
|
||||
/**
|
||||
* This class includes all the functionality related to interacting with the 0x Token Registry smart contract.
|
||||
@@ -12,6 +14,18 @@ import {artifacts} from '../artifacts';
|
||||
export class TokenRegistryWrapper extends ContractWrapper {
|
||||
private _tokenRegistryContractIfExists?: TokenRegistryContract;
|
||||
private _contractAddressIfExists?: string;
|
||||
private static _createTokenFromMetadata(metadata: TokenMetadata): Token|undefined {
|
||||
if (metadata[0] === constants.NULL_ADDRESS) {
|
||||
return undefined;
|
||||
}
|
||||
const token = {
|
||||
address: metadata[0],
|
||||
name: metadata[1],
|
||||
symbol: metadata[2],
|
||||
decimals: metadata[3].toNumber(),
|
||||
};
|
||||
return token;
|
||||
}
|
||||
constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) {
|
||||
super(web3Wrapper);
|
||||
this._contractAddressIfExists = contractAddressIfExists;
|
||||
@@ -26,7 +40,7 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
||||
const addresses = await this.getTokenAddressesAsync();
|
||||
const tokenPromises: Array<Promise<Token|undefined>> = _.map(
|
||||
addresses,
|
||||
(address: string) => (this.getTokenIfExistsAsync(address)),
|
||||
async (address: string) => this.getTokenIfExistsAsync(address),
|
||||
);
|
||||
const tokens = await Promise.all(tokenPromises);
|
||||
return tokens as Token[];
|
||||
@@ -49,7 +63,7 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
||||
|
||||
const tokenRegistryContract = await this._getTokenRegistryContractAsync();
|
||||
const metadata = await tokenRegistryContract.getTokenMetaData.callAsync(address);
|
||||
const token = this._createTokenFromMetadata(metadata);
|
||||
const token = TokenRegistryWrapper._createTokenFromMetadata(metadata);
|
||||
return token;
|
||||
}
|
||||
public async getTokenAddressBySymbolIfExistsAsync(symbol: string): Promise<string|undefined> {
|
||||
@@ -74,14 +88,14 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
||||
assert.isString('symbol', symbol);
|
||||
const tokenRegistryContract = await this._getTokenRegistryContractAsync();
|
||||
const metadata = await tokenRegistryContract.getTokenBySymbol.callAsync(symbol);
|
||||
const token = this._createTokenFromMetadata(metadata);
|
||||
const token = TokenRegistryWrapper._createTokenFromMetadata(metadata);
|
||||
return token;
|
||||
}
|
||||
public async getTokenByNameIfExistsAsync(name: string): Promise<Token|undefined> {
|
||||
assert.isString('name', name);
|
||||
const tokenRegistryContract = await this._getTokenRegistryContractAsync();
|
||||
const metadata = await tokenRegistryContract.getTokenByName.callAsync(name);
|
||||
const token = this._createTokenFromMetadata(metadata);
|
||||
const token = TokenRegistryWrapper._createTokenFromMetadata(metadata);
|
||||
return token;
|
||||
}
|
||||
/**
|
||||
@@ -89,22 +103,11 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
||||
* that the user-passed web3 provider is connected to.
|
||||
* @returns The Ethereum address of the TokenRegistry contract being used.
|
||||
*/
|
||||
public async getContractAddressAsync(): Promise<string> {
|
||||
const tokenRegistryInstance = await this._getTokenRegistryContractAsync();
|
||||
const tokenRegistryAddress = tokenRegistryInstance.address;
|
||||
return tokenRegistryAddress;
|
||||
}
|
||||
private _createTokenFromMetadata(metadata: TokenMetadata): Token|undefined {
|
||||
if (metadata[0] === constants.NULL_ADDRESS) {
|
||||
return undefined;
|
||||
}
|
||||
const token = {
|
||||
address: metadata[0],
|
||||
name: metadata[1],
|
||||
symbol: metadata[2],
|
||||
decimals: metadata[3].toNumber(),
|
||||
};
|
||||
return token;
|
||||
public getContractAddress(): string {
|
||||
const contractAddress = this._getContractAddress(
|
||||
artifacts.TokenRegistryArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
return contractAddress;
|
||||
}
|
||||
private _invalidateContractInstance(): void {
|
||||
delete this._tokenRegistryContractIfExists;
|
||||
@@ -116,7 +119,7 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
||||
const contractInstance = await this._instantiateContractIfExistsAsync<TokenRegistryContract>(
|
||||
artifacts.TokenRegistryArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
this._tokenRegistryContractIfExists = contractInstance as TokenRegistryContract;
|
||||
this._tokenRegistryContractIfExists = contractInstance;
|
||||
return this._tokenRegistryContractIfExists;
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,20 @@
|
||||
import * as _ from 'lodash';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
|
||||
import {artifacts} from '../artifacts';
|
||||
import {TokenTransferProxyContract} from '../types';
|
||||
import {TokenTransferProxyContract, ZeroExError} from '../types';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
|
||||
/**
|
||||
* This class includes the functionality related to interacting with the TokenTransferProxy contract.
|
||||
*/
|
||||
export class TokenTransferProxyWrapper extends ContractWrapper {
|
||||
private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract;
|
||||
private _tokenTransferProxyContractAddressFetcher: () => Promise<string>;
|
||||
constructor(web3Wrapper: Web3Wrapper, tokenTransferProxyContractAddressFetcher: () => Promise<string>) {
|
||||
private _contractAddressIfExists?: string;
|
||||
constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) {
|
||||
super(web3Wrapper);
|
||||
this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher;
|
||||
this._contractAddressIfExists = contractAddressIfExists;
|
||||
}
|
||||
/**
|
||||
* Check if the Exchange contract address is authorized by the TokenTransferProxy contract.
|
||||
@@ -38,10 +40,11 @@ export class TokenTransferProxyWrapper extends ContractWrapper {
|
||||
* that the user-passed web3 provider is connected to.
|
||||
* @returns The Ethereum address of the TokenTransferProxy contract being used.
|
||||
*/
|
||||
public async getContractAddressAsync(): Promise<string> {
|
||||
const proxyInstance = await this._getTokenTransferProxyContractAsync();
|
||||
const proxyAddress = proxyInstance.address;
|
||||
return proxyAddress;
|
||||
public getContractAddress(): string {
|
||||
const contractAddress = this._getContractAddress(
|
||||
artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
return contractAddress;
|
||||
}
|
||||
private _invalidateContractInstance(): void {
|
||||
delete this._tokenTransferProxyContractIfExists;
|
||||
@@ -50,11 +53,10 @@ export class TokenTransferProxyWrapper extends ContractWrapper {
|
||||
if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) {
|
||||
return this._tokenTransferProxyContractIfExists;
|
||||
}
|
||||
const contractAddress = await this._tokenTransferProxyContractAddressFetcher();
|
||||
const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>(
|
||||
artifacts.TokenTransferProxyArtifact, contractAddress,
|
||||
artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists,
|
||||
);
|
||||
this._tokenTransferProxyContractIfExists = contractInstance as TokenTransferProxyContract;
|
||||
this._tokenTransferProxyContractIfExists = contractInstance;
|
||||
return this._tokenTransferProxyContractIfExists;
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,27 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {assert} from '../utils/assert';
|
||||
import {constants} from '../utils/constants';
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {artifacts} from '../artifacts';
|
||||
import {
|
||||
TokenContract,
|
||||
ZeroExError,
|
||||
TokenEvents,
|
||||
IndexedFilterValues,
|
||||
SubscriptionOpts,
|
||||
MethodOpts,
|
||||
LogWithDecodedArgs,
|
||||
EventCallback,
|
||||
IndexedFilterValues,
|
||||
LogWithDecodedArgs,
|
||||
MethodOpts,
|
||||
SubscriptionOpts,
|
||||
TokenContract,
|
||||
TokenContractEventArgs,
|
||||
TokenEvents,
|
||||
TransactionOpts,
|
||||
ZeroExError,
|
||||
} from '../types';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {assert} from '../utils/assert';
|
||||
import {constants} from '../utils/constants';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
import {ContractWrapper} from './contract_wrapper';
|
||||
import {TokenTransferProxyWrapper} from './token_transfer_proxy_wrapper';
|
||||
|
||||
const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 47275;
|
||||
|
||||
@@ -29,12 +33,12 @@ const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 47275;
|
||||
export class TokenWrapper extends ContractWrapper {
|
||||
public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
|
||||
private _tokenContractsByAddress: {[address: string]: TokenContract};
|
||||
private _tokenTransferProxyContractAddressFetcher: () => Promise<string>;
|
||||
private _tokenTransferProxyWrapper: TokenTransferProxyWrapper;
|
||||
constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder,
|
||||
tokenTransferProxyContractAddressFetcher: () => Promise<string>) {
|
||||
tokenTransferProxyWrapper: TokenTransferProxyWrapper) {
|
||||
super(web3Wrapper, abiDecoder);
|
||||
this._tokenContractsByAddress = {};
|
||||
this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher;
|
||||
this._tokenTransferProxyWrapper = tokenTransferProxyWrapper;
|
||||
}
|
||||
/**
|
||||
* Retrieves an owner's ERC20 token balance.
|
||||
@@ -63,24 +67,21 @@ export class TokenWrapper extends ContractWrapper {
|
||||
* for spenderAddress.
|
||||
* @param spenderAddress The hex encoded user Ethereum address who will be able to spend the set allowance.
|
||||
* @param amountInBaseUnits The allowance amount you would like to set.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async setAllowanceAsync(tokenAddress: string, ownerAddress: string, spenderAddress: string,
|
||||
amountInBaseUnits: BigNumber): Promise<string> {
|
||||
amountInBaseUnits: BigNumber, txOpts: TransactionOpts = {}): Promise<string> {
|
||||
await assert.isSenderAddressAsync('ownerAddress', ownerAddress, this._web3Wrapper);
|
||||
assert.isETHAddressHex('spenderAddress', spenderAddress);
|
||||
assert.isETHAddressHex('tokenAddress', tokenAddress);
|
||||
assert.isValidBaseUnitAmount('amountInBaseUnits', amountInBaseUnits);
|
||||
|
||||
const tokenContract = await this._getTokenContractAsync(tokenAddress);
|
||||
// Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception
|
||||
// on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294
|
||||
// TODO: Debug issue in testrpc and submit a PR, then remove this hack
|
||||
const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync();
|
||||
const gas = networkIdIfExists === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined;
|
||||
const txHash = await tokenContract.approve.sendTransactionAsync(spenderAddress, amountInBaseUnits, {
|
||||
from: ownerAddress,
|
||||
gas,
|
||||
gas: txOpts.gasLimit,
|
||||
gasPrice: txOpts.gasPrice,
|
||||
});
|
||||
return txHash;
|
||||
}
|
||||
@@ -93,12 +94,13 @@ export class TokenWrapper extends ContractWrapper {
|
||||
* @param ownerAddress The hex encoded user Ethereum address who would like to set an allowance
|
||||
* for spenderAddress.
|
||||
* @param spenderAddress The hex encoded user Ethereum address who will be able to spend the set allowance.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async setUnlimitedAllowanceAsync(tokenAddress: string, ownerAddress: string,
|
||||
spenderAddress: string): Promise<string> {
|
||||
spenderAddress: string, txOpts: TransactionOpts = {}): Promise<string> {
|
||||
const txHash = await this.setAllowanceAsync(
|
||||
tokenAddress, ownerAddress, spenderAddress, this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
|
||||
tokenAddress, ownerAddress, spenderAddress, this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS, txOpts,
|
||||
);
|
||||
return txHash;
|
||||
}
|
||||
@@ -133,7 +135,7 @@ export class TokenWrapper extends ContractWrapper {
|
||||
assert.isETHAddressHex('ownerAddress', ownerAddress);
|
||||
assert.isETHAddressHex('tokenAddress', tokenAddress);
|
||||
|
||||
const proxyAddress = await this._getTokenTransferProxyAddressAsync();
|
||||
const proxyAddress = this._tokenTransferProxyWrapper.getContractAddress();
|
||||
const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, methodOpts);
|
||||
return allowanceInBaseUnits;
|
||||
}
|
||||
@@ -144,16 +146,19 @@ export class TokenWrapper extends ContractWrapper {
|
||||
* @param ownerAddress The hex encoded user Ethereum address who is setting an allowance
|
||||
* for the Proxy contract.
|
||||
* @param amountInBaseUnits The allowance amount specified in baseUnits.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async setProxyAllowanceAsync(tokenAddress: string, ownerAddress: string,
|
||||
amountInBaseUnits: BigNumber): Promise<string> {
|
||||
amountInBaseUnits: BigNumber, txOpts: TransactionOpts = {}): Promise<string> {
|
||||
assert.isETHAddressHex('ownerAddress', ownerAddress);
|
||||
assert.isETHAddressHex('tokenAddress', tokenAddress);
|
||||
assert.isValidBaseUnitAmount('amountInBaseUnits', amountInBaseUnits);
|
||||
|
||||
const proxyAddress = await this._getTokenTransferProxyAddressAsync();
|
||||
const txHash = await this.setAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, amountInBaseUnits);
|
||||
const proxyAddress = this._tokenTransferProxyWrapper.getContractAddress();
|
||||
const txHash = await this.setAllowanceAsync(
|
||||
tokenAddress, ownerAddress, proxyAddress, amountInBaseUnits, txOpts,
|
||||
);
|
||||
return txHash;
|
||||
}
|
||||
/**
|
||||
@@ -164,11 +169,14 @@ export class TokenWrapper extends ContractWrapper {
|
||||
* @param tokenAddress The hex encoded contract Ethereum address where the ERC20 token is deployed.
|
||||
* @param ownerAddress The hex encoded user Ethereum address who is setting an allowance
|
||||
* for the Proxy contract.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async setUnlimitedProxyAllowanceAsync(tokenAddress: string, ownerAddress: string): Promise<string> {
|
||||
public async setUnlimitedProxyAllowanceAsync(
|
||||
tokenAddress: string, ownerAddress: string, txOpts: TransactionOpts = {},
|
||||
): Promise<string> {
|
||||
const txHash = await this.setProxyAllowanceAsync(
|
||||
tokenAddress, ownerAddress, this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
|
||||
tokenAddress, ownerAddress, this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS, txOpts,
|
||||
);
|
||||
return txHash;
|
||||
}
|
||||
@@ -178,10 +186,11 @@ export class TokenWrapper extends ContractWrapper {
|
||||
* @param fromAddress The hex encoded user Ethereum address that will send the funds.
|
||||
* @param toAddress The hex encoded user Ethereum address that will receive the funds.
|
||||
* @param amountInBaseUnits The amount (specified in baseUnits) of the token to transfer.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async transferAsync(tokenAddress: string, fromAddress: string, toAddress: string,
|
||||
amountInBaseUnits: BigNumber): Promise<string> {
|
||||
amountInBaseUnits: BigNumber, txOpts: TransactionOpts = {}): Promise<string> {
|
||||
assert.isETHAddressHex('tokenAddress', tokenAddress);
|
||||
await assert.isSenderAddressAsync('fromAddress', fromAddress, this._web3Wrapper);
|
||||
assert.isETHAddressHex('toAddress', toAddress);
|
||||
@@ -196,6 +205,8 @@ export class TokenWrapper extends ContractWrapper {
|
||||
|
||||
const txHash = await tokenContract.transfer.sendTransactionAsync(toAddress, amountInBaseUnits, {
|
||||
from: fromAddress,
|
||||
gas: txOpts.gasLimit,
|
||||
gasPrice: txOpts.gasPrice,
|
||||
});
|
||||
return txHash;
|
||||
}
|
||||
@@ -210,10 +221,11 @@ export class TokenWrapper extends ContractWrapper {
|
||||
* `fromAddress` must have set an allowance to the `senderAddress`
|
||||
* before this call.
|
||||
* @param amountInBaseUnits The amount (specified in baseUnits) of the token to transfer.
|
||||
* @param txOpts Transaction parameters.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
public async transferFromAsync(tokenAddress: string, fromAddress: string, toAddress: string,
|
||||
senderAddress: string, amountInBaseUnits: BigNumber):
|
||||
senderAddress: string, amountInBaseUnits: BigNumber, txOpts: TransactionOpts = {}):
|
||||
Promise<string> {
|
||||
assert.isETHAddressHex('tokenAddress', tokenAddress);
|
||||
assert.isETHAddressHex('fromAddress', fromAddress);
|
||||
@@ -237,6 +249,8 @@ export class TokenWrapper extends ContractWrapper {
|
||||
fromAddress, toAddress, amountInBaseUnits,
|
||||
{
|
||||
from: senderAddress,
|
||||
gas: txOpts.gasLimit,
|
||||
gasPrice: txOpts.gasPrice,
|
||||
},
|
||||
);
|
||||
return txHash;
|
||||
@@ -290,7 +304,7 @@ export class TokenWrapper extends ContractWrapper {
|
||||
);
|
||||
return logs;
|
||||
}
|
||||
private _invalidateContractInstancesAsync(): void {
|
||||
private _invalidateContractInstances(): void {
|
||||
this.unsubscribeAll();
|
||||
this._tokenContractsByAddress = {};
|
||||
}
|
||||
@@ -302,12 +316,8 @@ export class TokenWrapper extends ContractWrapper {
|
||||
const contractInstance = await this._instantiateContractIfExistsAsync<TokenContract>(
|
||||
artifacts.TokenArtifact, tokenAddress,
|
||||
);
|
||||
tokenContract = contractInstance as TokenContract;
|
||||
tokenContract = contractInstance;
|
||||
this._tokenContractsByAddress[tokenAddress] = tokenContract;
|
||||
return tokenContract;
|
||||
}
|
||||
private async _getTokenTransferProxyAddressAsync(): Promise<string> {
|
||||
const tokenTransferProxyContractAddress = await this._tokenTransferProxyContractAddressFetcher();
|
||||
return tokenTransferProxyContractAddress;
|
||||
}
|
||||
}
|
||||
|
@@ -32,6 +32,7 @@ export {
|
||||
LogWithDecodedArgs,
|
||||
MethodOpts,
|
||||
OrderTransactionOpts,
|
||||
TransactionOpts,
|
||||
FilterObject,
|
||||
LogEvent,
|
||||
DecodedLogEvent,
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import * as Web3 from 'web3';
|
||||
import * as _ from 'lodash';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {
|
||||
BlockParamLiteral,
|
||||
EventCallback,
|
||||
@@ -8,9 +8,10 @@ import {
|
||||
ZeroExError,
|
||||
} from '../types';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {assert} from '../utils/assert';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {utils} from '../utils/utils';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
const DEFAULT_EVENT_POLLING_INTERVAL_MS = 200;
|
||||
|
||||
|
@@ -1,10 +1,11 @@
|
||||
import * as _ from 'lodash';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import {RBTree} from 'bintrees';
|
||||
import {utils} from '../utils/utils';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {SignedOrder, ZeroExError} from '../types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {ZeroEx} from '../0x';
|
||||
import {SignedOrder, ZeroExError} from '../types';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {utils} from '../utils/utils';
|
||||
|
||||
const DEFAULT_EXPIRATION_MARGIN_MS = 0;
|
||||
const DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS = 50;
|
||||
@@ -29,12 +30,12 @@ export class ExpirationWatcher {
|
||||
const comparator = (lhs: string, rhs: string) => scoreFunction(lhs) - scoreFunction(rhs);
|
||||
this.orderHashByExpirationRBTree = new RBTree(comparator);
|
||||
}
|
||||
public subscribe(callbackAsync: (orderHash: string) => Promise<void>): void {
|
||||
public subscribe(callback: (orderHash: string) => void): void {
|
||||
if (!_.isUndefined(this.orderExpirationCheckingIntervalIdIfExists)) {
|
||||
throw new Error(ZeroExError.SubscriptionAlreadyPresent);
|
||||
}
|
||||
this.orderExpirationCheckingIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
|
||||
this.pruneExpiredOrdersAsync.bind(this, callbackAsync), this.orderExpirationCheckingIntervalMs,
|
||||
this.pruneExpiredOrders.bind(this, callback), this.orderExpirationCheckingIntervalMs,
|
||||
);
|
||||
}
|
||||
public unsubscribe(): void {
|
||||
@@ -52,7 +53,7 @@ export class ExpirationWatcher {
|
||||
this.orderHashByExpirationRBTree.remove(orderHash);
|
||||
delete this.expiration[orderHash];
|
||||
}
|
||||
private async pruneExpiredOrdersAsync(callbackAsync: (orderHash: string) => Promise<void>): Promise<void> {
|
||||
private pruneExpiredOrders(callback: (orderHash: string) => void): void {
|
||||
const currentUnixTimestampMs = utils.getCurrentUnixTimestampMs();
|
||||
while (true) {
|
||||
const hasTrakedOrders = this.orderHashByExpirationRBTree.size === 0;
|
||||
@@ -70,7 +71,7 @@ export class ExpirationWatcher {
|
||||
const orderHash = this.orderHashByExpirationRBTree.min();
|
||||
this.orderHashByExpirationRBTree.remove(orderHash);
|
||||
delete this.expiration[orderHash];
|
||||
await callbackAsync(orderHash);
|
||||
callback(orderHash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,42 +1,44 @@
|
||||
import * as _ from 'lodash';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {ZeroEx} from '../0x';
|
||||
import {EventWatcher} from './event_watcher';
|
||||
import {assert} from '../utils/assert';
|
||||
import {utils} from '../utils/utils';
|
||||
import {artifacts} from '../artifacts';
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
|
||||
import {
|
||||
ApprovalContractEventArgs,
|
||||
BlockParamLiteral,
|
||||
ContractEventArgs,
|
||||
ExchangeContractErrs,
|
||||
ExchangeEvents,
|
||||
LogCancelContractEventArgs,
|
||||
LogEvent,
|
||||
LogFillContractEventArgs,
|
||||
LogWithDecodedArgs,
|
||||
OnOrderStateChangeCallback,
|
||||
OrderState,
|
||||
OrderStateWatcherConfig,
|
||||
SignedOrder,
|
||||
TokenEvents,
|
||||
TransferContractEventArgs,
|
||||
Web3Provider,
|
||||
ZeroExError,
|
||||
} from '../types';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {assert} from '../utils/assert';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {OrderStateUtils} from '../utils/order_state_utils';
|
||||
import {
|
||||
LogEvent,
|
||||
OrderState,
|
||||
SignedOrder,
|
||||
Web3Provider,
|
||||
BlockParamLiteral,
|
||||
LogWithDecodedArgs,
|
||||
ContractEventArgs,
|
||||
OnOrderStateChangeCallback,
|
||||
OrderStateWatcherConfig,
|
||||
ApprovalContractEventArgs,
|
||||
TransferContractEventArgs,
|
||||
LogFillContractEventArgs,
|
||||
LogCancelContractEventArgs,
|
||||
ExchangeEvents,
|
||||
TokenEvents,
|
||||
ZeroExError,
|
||||
ExchangeContractErrs,
|
||||
} from '../types';
|
||||
import {utils} from '../utils/utils';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
|
||||
import {EventWatcher} from './event_watcher';
|
||||
import {ExpirationWatcher} from './expiration_watcher';
|
||||
|
||||
interface DependentOrderHashes {
|
||||
[makerAddress: string]: {
|
||||
[makerToken: string]: Set<string>,
|
||||
[makerToken: string]: Set<string>;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -44,6 +46,12 @@ interface OrderByOrderHash {
|
||||
[orderHash: string]: SignedOrder;
|
||||
}
|
||||
|
||||
interface OrderStateByOrderHash {
|
||||
[orderHash: string]: OrderState;
|
||||
}
|
||||
|
||||
const DEFAULT_CLEANUP_JOB_INTERVAL_MS = 1000 * 60 * 60; // 1h
|
||||
|
||||
/**
|
||||
* This class includes all the functionality related to watching a set of orders
|
||||
* for potential changes in order validity/fillability. The orderWatcher notifies
|
||||
@@ -51,6 +59,7 @@ interface OrderByOrderHash {
|
||||
* the order should be deemed invalid.
|
||||
*/
|
||||
export class OrderStateWatcher {
|
||||
private _orderStateByOrderHashCache: OrderStateByOrderHash = {};
|
||||
private _orderByOrderHash: OrderByOrderHash = {};
|
||||
private _dependentOrderHashes: DependentOrderHashes = {};
|
||||
private _callbackIfExists?: OnOrderStateChangeCallback;
|
||||
@@ -61,6 +70,8 @@ export class OrderStateWatcher {
|
||||
private _orderStateUtils: OrderStateUtils;
|
||||
private _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
|
||||
private _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
|
||||
private _cleanupJobInterval: number;
|
||||
private _cleanupJobIntervalIdIfExists?: NodeJS.Timer;
|
||||
constructor(
|
||||
web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder, token: TokenWrapper, exchange: ExchangeWrapper,
|
||||
config?: OrderStateWatcherConfig,
|
||||
@@ -69,7 +80,9 @@ export class OrderStateWatcher {
|
||||
this._web3Wrapper = web3Wrapper;
|
||||
const pollingIntervalIfExistsMs = _.isUndefined(config) ? undefined : config.eventPollingIntervalMs;
|
||||
this._eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalIfExistsMs);
|
||||
this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore(token);
|
||||
this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore(
|
||||
token, BlockParamLiteral.Pending,
|
||||
);
|
||||
this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(exchange);
|
||||
this._orderStateUtils = new OrderStateUtils(
|
||||
this._balanceAndProxyAllowanceLazyStore, this._orderFilledCancelledLazyStore,
|
||||
@@ -83,18 +96,21 @@ export class OrderStateWatcher {
|
||||
this._expirationWatcher = new ExpirationWatcher(
|
||||
expirationMarginIfExistsMs, orderExpirationCheckingIntervalMsIfExists,
|
||||
);
|
||||
this._cleanupJobInterval = _.isUndefined(config) || _.isUndefined(config.cleanupJobIntervalMs) ?
|
||||
DEFAULT_CLEANUP_JOB_INTERVAL_MS :
|
||||
config.cleanupJobIntervalMs;
|
||||
}
|
||||
/**
|
||||
* Add an order to the orderStateWatcher. Before the order is added, it's
|
||||
* signature is verified.
|
||||
* @param signedOrder The order you wish to start watching.
|
||||
*/
|
||||
public async addOrderAsync(signedOrder: SignedOrder): Promise<void> {
|
||||
public addOrder(signedOrder: SignedOrder): void {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
assert.isValidSignature(orderHash, signedOrder.ecSignature, signedOrder.maker);
|
||||
this._orderByOrderHash[orderHash] = signedOrder;
|
||||
await this.addToDependentOrderHashesAsync(signedOrder, orderHash);
|
||||
this.addToDependentOrderHashes(signedOrder, orderHash);
|
||||
const expirationUnixTimestampMs = signedOrder.expirationUnixTimestampSec.times(1000);
|
||||
this._expirationWatcher.addOrder(orderHash, expirationUnixTimestampMs);
|
||||
}
|
||||
@@ -102,15 +118,16 @@ export class OrderStateWatcher {
|
||||
* Removes an order from the orderStateWatcher
|
||||
* @param orderHash The orderHash of the order you wish to stop watching.
|
||||
*/
|
||||
public async removeOrderAsync(orderHash: string): Promise<void> {
|
||||
public removeOrder(orderHash: string): void {
|
||||
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
|
||||
const signedOrder = this._orderByOrderHash[orderHash];
|
||||
if (_.isUndefined(signedOrder)) {
|
||||
return; // noop
|
||||
}
|
||||
delete this._orderByOrderHash[orderHash];
|
||||
delete this._orderStateByOrderHashCache[orderHash];
|
||||
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
|
||||
const zrxTokenAddress = await exchange.getZRXTokenAddressAsync();
|
||||
const zrxTokenAddress = exchange.getZRXTokenAddress();
|
||||
this.removeFromDependentOrderHashes(signedOrder.maker, zrxTokenAddress, orderHash);
|
||||
this.removeFromDependentOrderHashes(signedOrder.maker, signedOrder.makerTokenAddress, orderHash);
|
||||
this._expirationWatcher.removeOrder(orderHash);
|
||||
@@ -128,13 +145,16 @@ export class OrderStateWatcher {
|
||||
}
|
||||
this._callbackIfExists = callback;
|
||||
this._eventWatcher.subscribe(this._onEventWatcherCallbackAsync.bind(this));
|
||||
this._expirationWatcher.subscribe(this._onOrderExpiredAsync.bind(this));
|
||||
this._expirationWatcher.subscribe(this._onOrderExpired.bind(this));
|
||||
this._cleanupJobIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
|
||||
this._cleanupAsync.bind(this), this._cleanupJobInterval,
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Ends an orderStateWatcher subscription.
|
||||
*/
|
||||
public unsubscribe(): void {
|
||||
if (_.isUndefined(this._callbackIfExists)) {
|
||||
if (_.isUndefined(this._callbackIfExists) || _.isUndefined(this._cleanupJobIntervalIdIfExists)) {
|
||||
throw new Error(ZeroExError.SubscriptionNotFound);
|
||||
}
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteAll();
|
||||
@@ -142,15 +162,43 @@ export class OrderStateWatcher {
|
||||
delete this._callbackIfExists;
|
||||
this._eventWatcher.unsubscribe();
|
||||
this._expirationWatcher.unsubscribe();
|
||||
intervalUtils.clearAsyncExcludingInterval(this._cleanupJobIntervalIdIfExists);
|
||||
}
|
||||
private async _onOrderExpiredAsync(orderHash: string): Promise<void> {
|
||||
private async _cleanupAsync(): Promise<void> {
|
||||
for (const orderHash of _.keys(this._orderByOrderHash)) {
|
||||
this._cleanupOrderRelatedState(orderHash);
|
||||
await this._emitRevalidateOrdersAsync([orderHash]);
|
||||
}
|
||||
}
|
||||
private _cleanupOrderRelatedState(orderHash: string): void {
|
||||
const signedOrder = this._orderByOrderHash[orderHash];
|
||||
|
||||
this._orderFilledCancelledLazyStore.deleteFilledTakerAmount(orderHash);
|
||||
this._orderFilledCancelledLazyStore.deleteCancelledTakerAmount(orderHash);
|
||||
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteBalance(signedOrder.makerTokenAddress, signedOrder.maker);
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteProxyAllowance(signedOrder.makerTokenAddress, signedOrder.maker);
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteBalance(signedOrder.takerTokenAddress, signedOrder.taker);
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteProxyAllowance(signedOrder.takerTokenAddress, signedOrder.taker);
|
||||
|
||||
const zrxTokenAddress = this._getZRXTokenAddress();
|
||||
if (!signedOrder.makerFee.isZero()) {
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteBalance(zrxTokenAddress, signedOrder.maker);
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteProxyAllowance(zrxTokenAddress, signedOrder.maker);
|
||||
}
|
||||
if (!signedOrder.takerFee.isZero()) {
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteBalance(zrxTokenAddress, signedOrder.taker);
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteProxyAllowance(zrxTokenAddress, signedOrder.taker);
|
||||
}
|
||||
}
|
||||
private _onOrderExpired(orderHash: string): void {
|
||||
const orderState: OrderState = {
|
||||
isValid: false,
|
||||
orderHash,
|
||||
error: ExchangeContractErrs.OrderFillExpired,
|
||||
};
|
||||
if (!_.isUndefined(this._orderByOrderHash[orderHash])) {
|
||||
await this.removeOrderAsync(orderHash);
|
||||
this.removeOrder(orderHash);
|
||||
if (!_.isUndefined(this._callbackIfExists)) {
|
||||
this._callbackIfExists(orderState);
|
||||
}
|
||||
@@ -232,17 +280,23 @@ export class OrderStateWatcher {
|
||||
}
|
||||
private async _emitRevalidateOrdersAsync(orderHashes: string[]): Promise<void> {
|
||||
for (const orderHash of orderHashes) {
|
||||
const signedOrder = this._orderByOrderHash[orderHash] as SignedOrder;
|
||||
const signedOrder = this._orderByOrderHash[orderHash];
|
||||
// Most of these calls will never reach the network because the data is fetched from stores
|
||||
// and only updated when cache is invalidated
|
||||
const orderState = await this._orderStateUtils.getOrderStateAsync(signedOrder);
|
||||
if (_.isUndefined(this._callbackIfExists)) {
|
||||
break; // Unsubscribe was called
|
||||
}
|
||||
if (_.isEqual(orderState, this._orderStateByOrderHashCache[orderHash])) {
|
||||
// Actual order state didn't change
|
||||
continue;
|
||||
} else {
|
||||
this._orderStateByOrderHashCache[orderHash] = orderState;
|
||||
}
|
||||
this._callbackIfExists(orderState);
|
||||
}
|
||||
}
|
||||
private async addToDependentOrderHashesAsync(signedOrder: SignedOrder, orderHash: string): Promise<void> {
|
||||
private addToDependentOrderHashes(signedOrder: SignedOrder, orderHash: string): void {
|
||||
if (_.isUndefined(this._dependentOrderHashes[signedOrder.maker])) {
|
||||
this._dependentOrderHashes[signedOrder.maker] = {};
|
||||
}
|
||||
@@ -250,8 +304,7 @@ export class OrderStateWatcher {
|
||||
this._dependentOrderHashes[signedOrder.maker][signedOrder.makerTokenAddress] = new Set();
|
||||
}
|
||||
this._dependentOrderHashes[signedOrder.maker][signedOrder.makerTokenAddress].add(orderHash);
|
||||
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
|
||||
const zrxTokenAddress = await exchange.getZRXTokenAddressAsync();
|
||||
const zrxTokenAddress = this._getZRXTokenAddress();
|
||||
if (_.isUndefined(this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress])) {
|
||||
this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress] = new Set();
|
||||
}
|
||||
@@ -266,4 +319,9 @@ export class OrderStateWatcher {
|
||||
delete this._dependentOrderHashes[makerAddress];
|
||||
}
|
||||
}
|
||||
private _getZRXTokenAddress(): string {
|
||||
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
|
||||
const zrxTokenAddress = exchange.getZRXTokenAddress();
|
||||
return zrxTokenAddress;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,87 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
import {SignedOrder} from '../types';
|
||||
|
||||
export class RemainingFillableCalculator {
|
||||
private signedOrder: SignedOrder;
|
||||
private isMakerTokenZRX: boolean;
|
||||
// Transferrable Amount is the minimum of Approval and Balance
|
||||
private transferrableMakerTokenAmount: BigNumber;
|
||||
private transferrableMakerFeeTokenAmount: BigNumber;
|
||||
private remainingMakerTokenAmount: BigNumber;
|
||||
private remainingMakerFeeAmount: BigNumber;
|
||||
constructor(signedOrder: SignedOrder,
|
||||
isMakerTokenZRX: boolean,
|
||||
transferrableMakerTokenAmount: BigNumber,
|
||||
transferrableMakerFeeTokenAmount: BigNumber,
|
||||
remainingMakerTokenAmount: BigNumber) {
|
||||
this.signedOrder = signedOrder;
|
||||
this.isMakerTokenZRX = isMakerTokenZRX;
|
||||
this.transferrableMakerTokenAmount = transferrableMakerTokenAmount;
|
||||
this.transferrableMakerFeeTokenAmount = transferrableMakerFeeTokenAmount;
|
||||
this.remainingMakerTokenAmount = remainingMakerTokenAmount;
|
||||
this.remainingMakerFeeAmount = remainingMakerTokenAmount.times(signedOrder.makerFee)
|
||||
.dividedToIntegerBy(signedOrder.makerTokenAmount);
|
||||
}
|
||||
public computeRemainingMakerFillable(): BigNumber {
|
||||
if (this.hasSufficientFundsForFeeAndTransferAmount()) {
|
||||
return this.remainingMakerTokenAmount;
|
||||
}
|
||||
if (this.signedOrder.makerFee.isZero()) {
|
||||
return BigNumber.min(this.remainingMakerTokenAmount, this.transferrableMakerTokenAmount);
|
||||
}
|
||||
return this.calculatePartiallyFillableMakerTokenAmount();
|
||||
}
|
||||
public computeRemainingTakerFillable(): BigNumber {
|
||||
return this.computeRemainingMakerFillable().times(this.signedOrder.takerTokenAmount)
|
||||
.dividedToIntegerBy(this.signedOrder.makerTokenAmount);
|
||||
}
|
||||
private hasSufficientFundsForFeeAndTransferAmount(): boolean {
|
||||
if (this.isMakerTokenZRX) {
|
||||
const totalZRXTransferAmountRequired = this.remainingMakerTokenAmount.plus(this.remainingMakerFeeAmount);
|
||||
const hasSufficientFunds = this.transferrableMakerTokenAmount.greaterThanOrEqualTo(
|
||||
totalZRXTransferAmountRequired);
|
||||
return hasSufficientFunds;
|
||||
} else {
|
||||
const hasSufficientFundsForTransferAmount = this.transferrableMakerTokenAmount.greaterThanOrEqualTo(
|
||||
this.remainingMakerTokenAmount);
|
||||
const hasSufficientFundsForFeeAmount = this.transferrableMakerFeeTokenAmount.greaterThanOrEqualTo(
|
||||
this.remainingMakerFeeAmount);
|
||||
const hasSufficientFunds = hasSufficientFundsForTransferAmount && hasSufficientFundsForFeeAmount;
|
||||
return hasSufficientFunds;
|
||||
}
|
||||
}
|
||||
private calculatePartiallyFillableMakerTokenAmount(): BigNumber {
|
||||
// Given an order for 200 wei for 2 ZRXwei fee, find 100 wei for 1 ZRXwei. Order ratio is then 100:1
|
||||
const orderToFeeRatio = this.signedOrder.makerTokenAmount.dividedBy(this.signedOrder.makerFee);
|
||||
// The number of times the maker can fill the order, if each fill only required the transfer of a single
|
||||
// baseUnit of fee tokens.
|
||||
// Given 2 ZRXwei, the maximum amount of times Maker can fill this order, in terms of fees, is 2
|
||||
const fillableTimesInFeeTokenBaseUnits = BigNumber.min(this.transferrableMakerFeeTokenAmount,
|
||||
this.remainingMakerFeeAmount);
|
||||
// The number of times the Maker can fill the order, given the Maker Token Balance
|
||||
// Assuming a balance of 150 wei, and an orderToFeeRatio of 100:1, maker can fill this order 1 time.
|
||||
let fillableTimesInMakerTokenUnits = this.transferrableMakerTokenAmount.dividedBy(orderToFeeRatio);
|
||||
if (this.isMakerTokenZRX) {
|
||||
// If ZRX is the maker token, the Fee and the Maker amount need to be removed from the same pool;
|
||||
// 200 ZRXwei for 2ZRXwei fee can only be filled once (need 202 ZRXwei)
|
||||
const totalZRXTokenPooled = this.transferrableMakerTokenAmount;
|
||||
// The purchasing power here is less as the tokens are taken from the same Pool
|
||||
// For every one number of fills, we have to take an extra ZRX out of the pool
|
||||
fillableTimesInMakerTokenUnits = totalZRXTokenPooled.dividedBy(
|
||||
orderToFeeRatio.plus(new BigNumber(1)));
|
||||
|
||||
}
|
||||
// When Ratio is not fully divisible there can be remainders which cannot be represented, so they are floored.
|
||||
// This can result in a RoundingError being thrown by the Exchange Contract.
|
||||
const partiallyFillableMakerTokenAmount = fillableTimesInMakerTokenUnits
|
||||
.times(this.signedOrder.makerTokenAmount)
|
||||
.dividedToIntegerBy(this.signedOrder.makerFee);
|
||||
const partiallyFillableFeeTokenAmount = fillableTimesInFeeTokenBaseUnits
|
||||
.times(this.signedOrder.makerTokenAmount)
|
||||
.dividedToIntegerBy(this.signedOrder.makerFee);
|
||||
const partiallyFillableAmount = BigNumber.min(partiallyFillableMakerTokenAmount,
|
||||
partiallyFillableFeeTokenAmount);
|
||||
return partiallyFillableAmount;
|
||||
}
|
||||
}
|
@@ -1,6 +1,10 @@
|
||||
export const zeroExConfigSchema = {
|
||||
id: '/ZeroExConfig',
|
||||
properties: {
|
||||
networkId: {
|
||||
type: 'number',
|
||||
minimum: 0,
|
||||
},
|
||||
gasPrice: {$ref: '/Number'},
|
||||
exchangeContractAddress: {$ref: '/Address'},
|
||||
tokenRegistryContractAddress: {$ref: '/Address'},
|
||||
@@ -20,4 +24,5 @@ export const zeroExConfigSchema = {
|
||||
},
|
||||
},
|
||||
type: 'object',
|
||||
required: ['networkId'],
|
||||
};
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {BlockParamLiteral} from '../types';
|
||||
|
||||
@@ -9,25 +10,27 @@ import {BlockParamLiteral} from '../types';
|
||||
*/
|
||||
export class BalanceAndProxyAllowanceLazyStore {
|
||||
private token: TokenWrapper;
|
||||
private defaultBlock: BlockParamLiteral;
|
||||
private balance: {
|
||||
[tokenAddress: string]: {
|
||||
[userAddress: string]: BigNumber,
|
||||
},
|
||||
[userAddress: string]: BigNumber;
|
||||
};
|
||||
};
|
||||
private proxyAllowance: {
|
||||
[tokenAddress: string]: {
|
||||
[userAddress: string]: BigNumber,
|
||||
},
|
||||
[userAddress: string]: BigNumber;
|
||||
};
|
||||
};
|
||||
constructor(token: TokenWrapper) {
|
||||
constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) {
|
||||
this.token = token;
|
||||
this.defaultBlock = defaultBlock;
|
||||
this.balance = {};
|
||||
this.proxyAllowance = {};
|
||||
}
|
||||
public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
|
||||
if (_.isUndefined(this.balance[tokenAddress]) || _.isUndefined(this.balance[tokenAddress][userAddress])) {
|
||||
const methodOpts = {
|
||||
defaultBlock: BlockParamLiteral.Pending,
|
||||
defaultBlock: this.defaultBlock,
|
||||
};
|
||||
const balance = await this.token.getBalanceAsync(tokenAddress, userAddress, methodOpts);
|
||||
this.setBalance(tokenAddress, userAddress, balance);
|
||||
@@ -53,7 +56,7 @@ export class BalanceAndProxyAllowanceLazyStore {
|
||||
if (_.isUndefined(this.proxyAllowance[tokenAddress]) ||
|
||||
_.isUndefined(this.proxyAllowance[tokenAddress][userAddress])) {
|
||||
const methodOpts = {
|
||||
defaultBlock: BlockParamLiteral.Pending,
|
||||
defaultBlock: this.defaultBlock,
|
||||
};
|
||||
const proxyAllowance = await this.token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts);
|
||||
this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance);
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {BlockParamLiteral} from '../types';
|
||||
|
||||
@@ -10,10 +11,10 @@ import {BlockParamLiteral} from '../types';
|
||||
export class OrderFilledCancelledLazyStore {
|
||||
private exchange: ExchangeWrapper;
|
||||
private filledTakerAmount: {
|
||||
[orderHash: string]: BigNumber,
|
||||
[orderHash: string]: BigNumber;
|
||||
};
|
||||
private cancelledTakerAmount: {
|
||||
[orderHash: string]: BigNumber,
|
||||
[orderHash: string]: BigNumber;
|
||||
};
|
||||
constructor(exchange: ExchangeWrapper) {
|
||||
this.exchange = exchange;
|
||||
|
@@ -1,9 +1,13 @@
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
export enum ZeroExError {
|
||||
ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST',
|
||||
ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST',
|
||||
ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST',
|
||||
EtherTokenContractDoesNotExist = 'ETHER_TOKEN_CONTRACT_DOES_NOT_EXIST',
|
||||
TokenTransferProxyContractDoesNotExist = 'TOKEN_TRANSFER_PROXY_CONTRACT_DOES_NOT_EXIST',
|
||||
TokenRegistryContractDoesNotExist = 'TOKEN_REGISTRY_CONTRACT_DOES_NOT_EXIST',
|
||||
TokenContractDoesNotExist = 'TOKEN_CONTRACT_DOES_NOT_EXIST',
|
||||
UnhandledError = 'UNHANDLED_ERROR',
|
||||
UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES',
|
||||
InvalidSignature = 'INVALID_SIGNATURE',
|
||||
@@ -40,7 +44,10 @@ export type OrderValues = [BigNumber, BigNumber, BigNumber,
|
||||
BigNumber, BigNumber, BigNumber];
|
||||
|
||||
export type LogEvent = Web3.LogEntryEvent;
|
||||
export type DecodedLogEvent<ArgsType> = Web3.DecodedLogEntryEvent<ArgsType>;
|
||||
export interface DecodedLogEvent<ArgsType> {
|
||||
isRemoved: boolean;
|
||||
log: LogWithDecodedArgs<ArgsType>;
|
||||
}
|
||||
|
||||
export type EventCallback<ArgsType> = (err: null|Error, log?: DecodedLogEvent<ArgsType>) => void;
|
||||
export type EventWatcherCallback = (log: LogEvent) => void;
|
||||
@@ -325,6 +332,7 @@ export interface TxOpts {
|
||||
from: string;
|
||||
gas?: number;
|
||||
value?: BigNumber;
|
||||
gasPrice?: BigNumber;
|
||||
}
|
||||
|
||||
export interface TokenAddressBySymbol {
|
||||
@@ -348,9 +356,11 @@ export interface IndexedFilterValues {
|
||||
[index: string]: ContractEventArg;
|
||||
}
|
||||
|
||||
// Earliest is omitted by design. It is simply an alias for the `0` constant and
|
||||
// is thus not very helpful. Moreover, this type is used in places that only accept
|
||||
// `latest` or `pending`.
|
||||
export enum BlockParamLiteral {
|
||||
Latest = 'latest',
|
||||
Earliest = 'earliest',
|
||||
Pending = 'pending',
|
||||
}
|
||||
|
||||
@@ -378,7 +388,8 @@ export type AsyncMethod = (...args: any[]) => Promise<any>;
|
||||
/**
|
||||
* We re-export the `Web3.Provider` type specified in the Web3 Typescript typings
|
||||
* since it is the type of the `provider` argument to the `ZeroEx` constructor.
|
||||
* It is however a `Web3` library type, not a native `0x.js` type.
|
||||
* It is however a `Web3` library type, not a native `0x.js` type. To learn more
|
||||
* about providers, visit https://0xproject.com/wiki#Web3-Provider-Explained
|
||||
*/
|
||||
export type Web3Provider = Web3.Provider;
|
||||
|
||||
@@ -396,25 +407,31 @@ export interface JSONRPCPayload {
|
||||
* eventPollingIntervalMs: How often to poll the Ethereum node for new events. Defaults: 200
|
||||
* expirationMarginMs: Amount of time before order expiry that you'd like to be notified
|
||||
* of an orders expiration. Defaults: 0
|
||||
* cleanupJobIntervalMs: How often to run a cleanup job which revalidates all the orders. Defaults: 1h
|
||||
*/
|
||||
export interface OrderStateWatcherConfig {
|
||||
orderExpirationCheckingIntervalMs?: number;
|
||||
eventPollingIntervalMs?: number;
|
||||
expirationMarginMs?: number;
|
||||
cleanupJobIntervalMs?: number;
|
||||
}
|
||||
|
||||
/*
|
||||
* networkId: The id of the underlying ethereum network your provider is connected to. (1-mainnet, 42-kovan, 50-testrpc)
|
||||
* gasPrice: Gas price to use with every transaction
|
||||
* exchangeContractAddress: The address of an exchange contract to use
|
||||
* tokenRegistryContractAddress: The address of a token registry contract to use
|
||||
* etherTokenContractAddress: The address of an ether token contract to use
|
||||
* tokenTransferProxyContractAddress: The address of the token transfer proxy contract to use
|
||||
* orderWatcherConfig: All the configs related to the orderWatcher
|
||||
*/
|
||||
export interface ZeroExConfig {
|
||||
gasPrice?: BigNumber; // Gas price to use with every transaction
|
||||
networkId: number;
|
||||
gasPrice?: BigNumber;
|
||||
exchangeContractAddress?: string;
|
||||
tokenRegistryContractAddress?: string;
|
||||
etherTokenContractAddress?: string;
|
||||
tokenTransferProxyContractAddress?: string;
|
||||
orderWatcherConfig?: OrderStateWatcherConfig;
|
||||
}
|
||||
|
||||
@@ -435,11 +452,16 @@ export interface TransactionReceiptWithDecodedLogs extends TransactionReceipt {
|
||||
logs: Array<LogWithDecodedArgs<DecodedLogArgs>|Web3.LogEntry>;
|
||||
}
|
||||
|
||||
export type ArtifactContractName = 'ZRX'|'TokenTransferProxy'|'TokenRegistry'|'Token'|'Exchange'|'EtherToken';
|
||||
|
||||
export interface Artifact {
|
||||
abi: any;
|
||||
networks: {[networkId: number]: {
|
||||
address: string;
|
||||
}};
|
||||
contract_name: ArtifactContractName;
|
||||
abi: Web3.ContractAbi;
|
||||
networks: {
|
||||
[networkId: number]: {
|
||||
address: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -463,11 +485,20 @@ export interface MethodOpts {
|
||||
}
|
||||
|
||||
/*
|
||||
* shouldValidate: Flag indicating whether the library should make attempts to validate a transaction before
|
||||
* broadcasting it. For example, order has a valid signature, maker has sufficient funds, etc.
|
||||
* gasPrice: Gas price in Wei to use for a transaction
|
||||
* gasLimit: The amount of gas to send with a transaction
|
||||
*/
|
||||
export interface OrderTransactionOpts {
|
||||
shouldValidate: boolean;
|
||||
export interface TransactionOpts {
|
||||
gasPrice?: BigNumber;
|
||||
gasLimit?: number;
|
||||
}
|
||||
|
||||
/*
|
||||
* shouldValidate: Flag indicating whether the library should make attempts to validate a transaction before
|
||||
* broadcasting it. For example, order has a valid signature, maker has sufficient funds, etc. Default: true
|
||||
*/
|
||||
export interface OrderTransactionOpts extends TransactionOpts {
|
||||
shouldValidate?: boolean;
|
||||
}
|
||||
|
||||
export type FilterObject = Web3.FilterObject;
|
||||
@@ -521,4 +552,4 @@ export interface TransactionReceipt {
|
||||
gasUsed: number;
|
||||
contractAddress: string|null;
|
||||
logs: Web3.LogEntry[];
|
||||
}
|
||||
} // tslint:disable:max-file-line-count
|
||||
|
@@ -1,12 +1,22 @@
|
||||
import * as Web3 from 'web3';
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {AbiType, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes, ContractEventArgs} from '../types';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import * as SolidityCoder from 'web3/lib/solidity/coder';
|
||||
|
||||
import {AbiType, ContractEventArgs, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes} from '../types';
|
||||
|
||||
export class AbiDecoder {
|
||||
private savedABIs: Web3.AbiDefinition[] = [];
|
||||
private methodIds: {[signatureHash: string]: Web3.EventAbi} = {};
|
||||
private static padZeros(address: string) {
|
||||
let formatted = address;
|
||||
if (_.startsWith(formatted, '0x')) {
|
||||
formatted = formatted.slice(2);
|
||||
}
|
||||
|
||||
formatted = _.padStart(formatted, 40, '0');
|
||||
return `0x${formatted}`;
|
||||
}
|
||||
constructor(abiArrays: Web3.AbiDefinition[][]) {
|
||||
_.map(abiArrays, this.addABI.bind(this));
|
||||
}
|
||||
@@ -31,7 +41,7 @@ export class AbiDecoder {
|
||||
// Indexed parameters are stored in topics. Non-indexed ones in decodedData
|
||||
let value = param.indexed ? log.topics[topicsIndex++] : decodedData[dataIndex++];
|
||||
if (param.type === SolidityTypes.Address) {
|
||||
value = this.padZeros(new BigNumber(value).toString(16));
|
||||
value = AbiDecoder.padZeros(new BigNumber(value).toString(16));
|
||||
} else if (param.type === SolidityTypes.Uint256 ||
|
||||
param.type === SolidityTypes.Uint8 ||
|
||||
param.type === SolidityTypes.Uint) {
|
||||
@@ -56,13 +66,4 @@ export class AbiDecoder {
|
||||
});
|
||||
this.savedABIs = this.savedABIs.concat(abiArray);
|
||||
}
|
||||
private padZeros(address: string) {
|
||||
let formatted = address;
|
||||
if (_.startsWith(formatted, '0x')) {
|
||||
formatted = formatted.slice(2);
|
||||
}
|
||||
|
||||
formatted = _.padStart(formatted, 40, '0');
|
||||
return `0x${formatted}`;
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,12 @@
|
||||
import {assert as sharedAssert} from '@0xproject/assert';
|
||||
import {Schema, SchemaValidator} from '@0xproject/json-schemas';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {SchemaValidator, Schema} from '@0xproject/json-schemas';
|
||||
import {assert as sharedAssert} from '@0xproject/assert';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {signatureUtils} from '../utils/signature_utils';
|
||||
|
||||
import {ECSignature} from '../types';
|
||||
import {signatureUtils} from '../utils/signature_utils';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
|
||||
const HEX_REGEX = /^0x[0-9A-F]*$/i;
|
||||
|
||||
|
@@ -1,7 +1,9 @@
|
||||
import * as _ from 'lodash';
|
||||
import {constants} from './constants';
|
||||
|
||||
import {AsyncMethod, ZeroExError} from '../types';
|
||||
|
||||
import {constants} from './constants';
|
||||
|
||||
export const decorators = {
|
||||
/**
|
||||
* Source: https://stackoverflow.com/a/29837695/3546986
|
||||
|
@@ -1,8 +1,9 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {ExchangeContractErrs, TradeSide, TransferType, BlockParamLiteral} from '../types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
import {BlockParamLiteral, ExchangeContractErrs, TradeSide, TransferType} from '../types';
|
||||
|
||||
enum FailureReason {
|
||||
Balance = 'balance',
|
||||
@@ -35,8 +36,13 @@ const ERR_MSG_MAPPING = {
|
||||
export class ExchangeTransferSimulator {
|
||||
private store: BalanceAndProxyAllowanceLazyStore;
|
||||
private UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber;
|
||||
constructor(token: TokenWrapper) {
|
||||
this.store = new BalanceAndProxyAllowanceLazyStore(token);
|
||||
private static throwValidationError(failureReason: FailureReason, tradeSide: TradeSide,
|
||||
transferType: TransferType): never {
|
||||
const errMsg = ERR_MSG_MAPPING[failureReason][tradeSide][transferType];
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) {
|
||||
this.store = new BalanceAndProxyAllowanceLazyStore(token, defaultBlock);
|
||||
this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
|
||||
}
|
||||
/**
|
||||
@@ -54,10 +60,10 @@ export class ExchangeTransferSimulator {
|
||||
const balance = await this.store.getBalanceAsync(tokenAddress, from);
|
||||
const proxyAllowance = await this.store.getProxyAllowanceAsync(tokenAddress, from);
|
||||
if (proxyAllowance.lessThan(amountInBaseUnits)) {
|
||||
this.throwValidationError(FailureReason.ProxyAllowance, tradeSide, transferType);
|
||||
ExchangeTransferSimulator.throwValidationError(FailureReason.ProxyAllowance, tradeSide, transferType);
|
||||
}
|
||||
if (balance.lessThan(amountInBaseUnits)) {
|
||||
this.throwValidationError(FailureReason.Balance, tradeSide, transferType);
|
||||
ExchangeTransferSimulator.throwValidationError(FailureReason.Balance, tradeSide, transferType);
|
||||
}
|
||||
await this.decreaseProxyAllowanceAsync(tokenAddress, from, amountInBaseUnits);
|
||||
await this.decreaseBalanceAsync(tokenAddress, from, amountInBaseUnits);
|
||||
@@ -80,9 +86,4 @@ export class ExchangeTransferSimulator {
|
||||
const balance = await this.store.getBalanceAsync(tokenAddress, userAddress);
|
||||
this.store.setBalance(tokenAddress, userAddress, balance.minus(amountInBaseUnits));
|
||||
}
|
||||
private throwValidationError(failureReason: FailureReason, tradeSide: TradeSide,
|
||||
transferType: TransferType): Promise<never> {
|
||||
const errMsg = ERR_MSG_MAPPING[failureReason][tradeSide][transferType];
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import * as uuid from 'uuid/v4';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import * as jsSHA3 from 'js-sha3';
|
||||
import * as _ from 'lodash';
|
||||
import * as uuid from 'uuid/v4';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {ContractEvents, IndexedFilterValues, SubscriptionOpts} from '../types';
|
||||
|
||||
const TOPIC_LENGTH = 32;
|
||||
|
@@ -1,102 +1,31 @@
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
|
||||
import {ZeroEx} from '../0x';
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {RemainingFillableCalculator} from '../order_watcher/remaining_fillable_calculator';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
|
||||
import {
|
||||
ExchangeContractErrs,
|
||||
SignedOrder,
|
||||
OrderRelevantState,
|
||||
MethodOpts,
|
||||
OrderRelevantState,
|
||||
OrderState,
|
||||
OrderStateValid,
|
||||
OrderStateInvalid,
|
||||
OrderStateValid,
|
||||
SignedOrder,
|
||||
} from '../types';
|
||||
import {ZeroEx} from '../0x';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {utils} from '../utils/utils';
|
||||
import {constants} from '../utils/constants';
|
||||
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
import {utils} from '../utils/utils';
|
||||
|
||||
const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001;
|
||||
|
||||
export class OrderStateUtils {
|
||||
private balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
|
||||
private orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
|
||||
constructor(balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore,
|
||||
orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore) {
|
||||
this.balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore;
|
||||
this.orderFilledCancelledLazyStore = orderFilledCancelledLazyStore;
|
||||
}
|
||||
public async getOrderStateAsync(signedOrder: SignedOrder): Promise<OrderState> {
|
||||
const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
try {
|
||||
this.validateIfOrderIsValid(signedOrder, orderRelevantState);
|
||||
const orderState: OrderStateValid = {
|
||||
isValid: true,
|
||||
orderHash,
|
||||
orderRelevantState,
|
||||
};
|
||||
return orderState;
|
||||
} catch (err) {
|
||||
const orderState: OrderStateInvalid = {
|
||||
isValid: false,
|
||||
orderHash,
|
||||
error: err.message,
|
||||
};
|
||||
return orderState;
|
||||
}
|
||||
}
|
||||
public async getOrderRelevantStateAsync(signedOrder: SignedOrder): Promise<OrderRelevantState> {
|
||||
// HACK: We access the private property here but otherwise the interface will be less nice.
|
||||
// If we pass it from the instantiator - there is no opportunity to get it there
|
||||
// because JS doesn't support async constructors.
|
||||
// Moreover - it's cached under the hood so it's equivalent to an async constructor.
|
||||
const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
|
||||
const zrxTokenAddress = await exchange.getZRXTokenAddressAsync();
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
|
||||
signedOrder.makerTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const makerProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
|
||||
signedOrder.makerTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const makerFeeBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
|
||||
zrxTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const makerFeeProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
|
||||
zrxTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const filledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash);
|
||||
const cancelledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
|
||||
orderHash,
|
||||
);
|
||||
const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash);
|
||||
const totalMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
const totalTakerTokenAmount = signedOrder.takerTokenAmount;
|
||||
const remainingTakerTokenAmount = totalTakerTokenAmount.minus(unavailableTakerTokenAmount);
|
||||
const remainingMakerTokenAmount = remainingTakerTokenAmount.times(totalMakerTokenAmount)
|
||||
.dividedToIntegerBy(totalTakerTokenAmount);
|
||||
const fillableMakerTokenAmount = BigNumber.min([makerProxyAllowance, makerBalance]);
|
||||
const remainingFillableMakerTokenAmount = BigNumber.min(fillableMakerTokenAmount, remainingMakerTokenAmount);
|
||||
const remainingFillableTakerTokenAmount = remainingFillableMakerTokenAmount
|
||||
.times(totalTakerTokenAmount)
|
||||
.dividedToIntegerBy(totalMakerTokenAmount);
|
||||
// TODO: Handle edge case where maker token is ZRX with fee
|
||||
const orderRelevantState = {
|
||||
makerBalance,
|
||||
makerProxyAllowance,
|
||||
makerFeeBalance,
|
||||
makerFeeProxyAllowance,
|
||||
filledTakerTokenAmount,
|
||||
cancelledTakerTokenAmount,
|
||||
remainingFillableMakerTokenAmount,
|
||||
remainingFillableTakerTokenAmount,
|
||||
};
|
||||
return orderRelevantState;
|
||||
}
|
||||
private validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
|
||||
private static validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
|
||||
const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add(
|
||||
orderRelevantState.filledTakerTokenAmount,
|
||||
);
|
||||
@@ -126,7 +55,83 @@ export class OrderStateUtils {
|
||||
.lessThan(minFillableTakerTokenAmountWithinNoRoundingErrorRange)) {
|
||||
throw new Error(ExchangeContractErrs.OrderFillRoundingError);
|
||||
}
|
||||
// TODO Add linear function solver when maker token is ZRX #badass
|
||||
// Return the max amount that's fillable
|
||||
}
|
||||
constructor(balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore,
|
||||
orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore) {
|
||||
this.balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore;
|
||||
this.orderFilledCancelledLazyStore = orderFilledCancelledLazyStore;
|
||||
}
|
||||
public async getOrderStateAsync(signedOrder: SignedOrder): Promise<OrderState> {
|
||||
const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
try {
|
||||
OrderStateUtils.validateIfOrderIsValid(signedOrder, orderRelevantState);
|
||||
const orderState: OrderStateValid = {
|
||||
isValid: true,
|
||||
orderHash,
|
||||
orderRelevantState,
|
||||
};
|
||||
return orderState;
|
||||
} catch (err) {
|
||||
const orderState: OrderStateInvalid = {
|
||||
isValid: false,
|
||||
orderHash,
|
||||
error: err.message,
|
||||
};
|
||||
return orderState;
|
||||
}
|
||||
}
|
||||
public async getOrderRelevantStateAsync(signedOrder: SignedOrder): Promise<OrderRelevantState> {
|
||||
// HACK: We access the private property here but otherwise the interface will be less nice.
|
||||
// If we pass it from the instantiator - there is no opportunity to get it there
|
||||
// because JS doesn't support async constructors.
|
||||
// Moreover - it's cached under the hood so it's equivalent to an async constructor.
|
||||
const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
|
||||
const zrxTokenAddress = exchange.getZRXTokenAddress();
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
|
||||
signedOrder.makerTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const makerProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
|
||||
signedOrder.makerTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const makerFeeBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
|
||||
zrxTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const makerFeeProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
|
||||
zrxTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const filledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash);
|
||||
const cancelledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
|
||||
orderHash,
|
||||
);
|
||||
const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash);
|
||||
const totalMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
const totalTakerTokenAmount = signedOrder.takerTokenAmount;
|
||||
const remainingTakerTokenAmount = totalTakerTokenAmount.minus(unavailableTakerTokenAmount);
|
||||
const remainingMakerTokenAmount = remainingTakerTokenAmount.times(totalMakerTokenAmount)
|
||||
.dividedToIntegerBy(totalTakerTokenAmount);
|
||||
const transferrableMakerTokenAmount = BigNumber.min([makerProxyAllowance, makerBalance]);
|
||||
const transferrableFeeTokenAmount = BigNumber.min([makerFeeProxyAllowance, makerFeeBalance]);
|
||||
|
||||
const isMakerTokenZRX = signedOrder.makerTokenAddress === zrxTokenAddress;
|
||||
const remainingFillableCalculator = new RemainingFillableCalculator(signedOrder,
|
||||
isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount,
|
||||
transferrableFeeTokenAmount,
|
||||
remainingMakerTokenAmount);
|
||||
const remainingFillableMakerTokenAmount = remainingFillableCalculator.computeRemainingMakerFillable();
|
||||
const remainingFillableTakerTokenAmount = remainingFillableCalculator.computeRemainingTakerFillable();
|
||||
const orderRelevantState = {
|
||||
makerBalance,
|
||||
makerProxyAllowance,
|
||||
makerFeeBalance,
|
||||
makerFeeProxyAllowance,
|
||||
filledTakerTokenAmount,
|
||||
cancelledTakerTokenAmount,
|
||||
remainingFillableMakerTokenAmount,
|
||||
remainingFillableTakerTokenAmount,
|
||||
};
|
||||
return orderRelevantState;
|
||||
}
|
||||
}
|
||||
|
@@ -1,16 +1,89 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {ExchangeContractErrs, SignedOrder, Order, ZeroExError, TradeSide, TransferType} from '../types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {ZeroEx} from '../0x';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
|
||||
import {utils} from '../utils/utils';
|
||||
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
|
||||
import {ExchangeContractErrs, Order, SignedOrder, TradeSide, TransferType, ZeroExError} from '../types';
|
||||
import {constants} from '../utils/constants';
|
||||
import {utils} from '../utils/utils';
|
||||
|
||||
import {ExchangeTransferSimulator} from './exchange_transfer_simulator';
|
||||
|
||||
export class OrderValidationUtils {
|
||||
private tokenWrapper: TokenWrapper;
|
||||
private exchangeWrapper: ExchangeWrapper;
|
||||
public static validateCancelOrderThrowIfInvalid(
|
||||
order: Order, cancelTakerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber,
|
||||
): void {
|
||||
if (cancelTakerTokenAmount.eq(0)) {
|
||||
throw new Error(ExchangeContractErrs.OrderCancelAmountZero);
|
||||
}
|
||||
if (order.takerTokenAmount.eq(unavailableTakerTokenAmount)) {
|
||||
throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
|
||||
}
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
if (order.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
|
||||
throw new Error(ExchangeContractErrs.OrderCancelExpired);
|
||||
}
|
||||
}
|
||||
public static async validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder,
|
||||
fillTakerTokenAmount: BigNumber, senderAddress: string, zrxTokenAddress: string,
|
||||
): Promise<void> {
|
||||
const fillMakerTokenAmount = OrderValidationUtils.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.makerTokenAmount,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
signedOrder.makerTokenAddress, signedOrder.maker, senderAddress, fillMakerTokenAmount,
|
||||
TradeSide.Maker, TransferType.Trade,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
signedOrder.takerTokenAddress, senderAddress, signedOrder.maker, fillTakerTokenAmount,
|
||||
TradeSide.Taker, TransferType.Trade,
|
||||
);
|
||||
const makerFeeAmount = OrderValidationUtils.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.makerFee,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
zrxTokenAddress, signedOrder.maker, signedOrder.feeRecipient, makerFeeAmount, TradeSide.Maker,
|
||||
TransferType.Fee,
|
||||
);
|
||||
const takerFeeAmount = OrderValidationUtils.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.takerFee,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
zrxTokenAddress, senderAddress, signedOrder.feeRecipient, takerFeeAmount, TradeSide.Taker,
|
||||
TransferType.Fee,
|
||||
);
|
||||
}
|
||||
private static validateRemainingFillAmountNotZeroOrThrow(
|
||||
takerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber,
|
||||
) {
|
||||
if (takerTokenAmount.eq(unavailableTakerTokenAmount)) {
|
||||
throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
|
||||
}
|
||||
}
|
||||
private static validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber) {
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
if (expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
|
||||
throw new Error(ExchangeContractErrs.OrderFillExpired);
|
||||
}
|
||||
}
|
||||
private static getPartialAmount(numerator: BigNumber, denominator: BigNumber,
|
||||
target: BigNumber): BigNumber {
|
||||
const fillMakerTokenAmount = numerator
|
||||
.mul(target)
|
||||
.div(denominator)
|
||||
.round(0);
|
||||
return fillMakerTokenAmount;
|
||||
}
|
||||
constructor(tokenWrapper: TokenWrapper, exchangeWrapper: ExchangeWrapper) {
|
||||
this.tokenWrapper = tokenWrapper;
|
||||
this.exchangeWrapper = exchangeWrapper;
|
||||
@@ -20,15 +93,15 @@ export class OrderValidationUtils {
|
||||
expectedFillTakerTokenAmount?: BigNumber): Promise<void> {
|
||||
const orderHash = utils.getOrderHashHex(signedOrder);
|
||||
const unavailableTakerTokenAmount = await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash);
|
||||
this.validateRemainingFillAmountNotZeroOrThrow(
|
||||
OrderValidationUtils.validateRemainingFillAmountNotZeroOrThrow(
|
||||
signedOrder.takerTokenAmount, unavailableTakerTokenAmount,
|
||||
);
|
||||
this.validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
|
||||
OrderValidationUtils.validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
|
||||
let fillTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
|
||||
if (!_.isUndefined(expectedFillTakerTokenAmount)) {
|
||||
fillTakerTokenAmount = expectedFillTakerTokenAmount;
|
||||
}
|
||||
const fillMakerTokenAmount = this.getPartialAmount(
|
||||
const fillMakerTokenAmount = OrderValidationUtils.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.makerTokenAmount,
|
||||
@@ -37,7 +110,7 @@ export class OrderValidationUtils {
|
||||
signedOrder.makerTokenAddress, signedOrder.maker, signedOrder.taker, fillMakerTokenAmount,
|
||||
TradeSide.Maker, TransferType.Trade,
|
||||
);
|
||||
const makerFeeAmount = this.getPartialAmount(
|
||||
const makerFeeAmount = OrderValidationUtils.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.makerFee,
|
||||
@@ -59,18 +132,18 @@ export class OrderValidationUtils {
|
||||
throw new Error(ZeroExError.InvalidSignature);
|
||||
}
|
||||
const unavailableTakerTokenAmount = await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash);
|
||||
this.validateRemainingFillAmountNotZeroOrThrow(
|
||||
OrderValidationUtils.validateRemainingFillAmountNotZeroOrThrow(
|
||||
signedOrder.takerTokenAmount, unavailableTakerTokenAmount,
|
||||
);
|
||||
if (signedOrder.taker !== constants.NULL_ADDRESS && signedOrder.taker !== takerAddress) {
|
||||
throw new Error(ExchangeContractErrs.TransactionSenderIsNotFillOrderTaker);
|
||||
}
|
||||
this.validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
|
||||
OrderValidationUtils.validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
|
||||
const remainingTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
|
||||
const filledTakerTokenAmount = remainingTakerTokenAmount.lessThan(fillTakerTokenAmount) ?
|
||||
remainingTakerTokenAmount :
|
||||
fillTakerTokenAmount;
|
||||
await this.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator, signedOrder, filledTakerTokenAmount, takerAddress, zrxTokenAddress,
|
||||
);
|
||||
|
||||
@@ -92,75 +165,4 @@ export class OrderValidationUtils {
|
||||
throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount);
|
||||
}
|
||||
}
|
||||
public async validateCancelOrderThrowIfInvalidAsync(order: Order,
|
||||
cancelTakerTokenAmount: BigNumber,
|
||||
unavailableTakerTokenAmount: BigNumber,
|
||||
): Promise<void> {
|
||||
if (cancelTakerTokenAmount.eq(0)) {
|
||||
throw new Error(ExchangeContractErrs.OrderCancelAmountZero);
|
||||
}
|
||||
if (order.takerTokenAmount.eq(unavailableTakerTokenAmount)) {
|
||||
throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
|
||||
}
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
if (order.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
|
||||
throw new Error(ExchangeContractErrs.OrderCancelExpired);
|
||||
}
|
||||
}
|
||||
public async validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder,
|
||||
fillTakerTokenAmount: BigNumber, senderAddress: string, zrxTokenAddress: string): Promise<void> {
|
||||
const fillMakerTokenAmount = this.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.makerTokenAmount,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
signedOrder.makerTokenAddress, signedOrder.maker, senderAddress, fillMakerTokenAmount,
|
||||
TradeSide.Maker, TransferType.Trade,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
signedOrder.takerTokenAddress, senderAddress, signedOrder.maker, fillTakerTokenAmount,
|
||||
TradeSide.Taker, TransferType.Trade,
|
||||
);
|
||||
const makerFeeAmount = this.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.makerFee,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
zrxTokenAddress, signedOrder.maker, signedOrder.feeRecipient, makerFeeAmount, TradeSide.Maker,
|
||||
TransferType.Fee,
|
||||
);
|
||||
const takerFeeAmount = this.getPartialAmount(
|
||||
fillTakerTokenAmount,
|
||||
signedOrder.takerTokenAmount,
|
||||
signedOrder.takerFee,
|
||||
);
|
||||
await exchangeTradeEmulator.transferFromAsync(
|
||||
zrxTokenAddress, senderAddress, signedOrder.feeRecipient, takerFeeAmount, TradeSide.Taker,
|
||||
TransferType.Fee,
|
||||
);
|
||||
}
|
||||
private validateRemainingFillAmountNotZeroOrThrow(
|
||||
takerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber,
|
||||
) {
|
||||
if (takerTokenAmount.eq(unavailableTakerTokenAmount)) {
|
||||
throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
|
||||
}
|
||||
}
|
||||
private validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber) {
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
if (expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
|
||||
throw new Error(ExchangeContractErrs.OrderFillExpired);
|
||||
}
|
||||
}
|
||||
private getPartialAmount(numerator: BigNumber, denominator: BigNumber,
|
||||
target: BigNumber): BigNumber {
|
||||
const fillMakerTokenAmount = numerator
|
||||
.mul(target)
|
||||
.div(denominator)
|
||||
.round(0);
|
||||
return fillMakerTokenAmount;
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
|
||||
import {ECSignature} from '../types';
|
||||
|
||||
export const signatureUtils = {
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as ethABI from 'ethereumjs-abi';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import {Order, SignedOrder, SolidityTypes} from '../types';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import BN = require('bn.js');
|
||||
import * as ethABI from 'ethereumjs-abi';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {Order, SignedOrder, SolidityTypes} from '../types';
|
||||
|
||||
export const utils = {
|
||||
/**
|
||||
|
@@ -1,9 +1,10 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import promisify = require('es6-promisify');
|
||||
import {ZeroExError, Artifact, TransactionReceipt} from './types';
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {Contract} from './contract';
|
||||
import {Artifact, ArtifactContractName, TransactionReceipt, ZeroExError} from './types';
|
||||
|
||||
interface RawLogEntry {
|
||||
logIndex: string|null;
|
||||
@@ -16,12 +17,21 @@ interface RawLogEntry {
|
||||
topics: string[];
|
||||
}
|
||||
|
||||
const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} = {
|
||||
ZRX: ZeroExError.ZRXContractDoesNotExist,
|
||||
EtherToken: ZeroExError.EtherTokenContractDoesNotExist,
|
||||
Token: ZeroExError.TokenContractDoesNotExist,
|
||||
TokenRegistry: ZeroExError.TokenRegistryContractDoesNotExist,
|
||||
TokenTransferProxy: ZeroExError.TokenTransferProxyContractDoesNotExist,
|
||||
Exchange: ZeroExError.ExchangeContractDoesNotExist,
|
||||
};
|
||||
|
||||
export class Web3Wrapper {
|
||||
private web3: Web3;
|
||||
private networkId: number;
|
||||
private defaults: Partial<Web3.TxData>;
|
||||
private networkIdIfExists?: number;
|
||||
private jsonRpcRequestId: number;
|
||||
constructor(provider: Web3.Provider, defaults?: Partial<Web3.TxData>) {
|
||||
constructor(provider: Web3.Provider, networkId: number, defaults?: Partial<Web3.TxData>) {
|
||||
if (_.isUndefined((provider as any).sendAsync)) {
|
||||
// Web3@1.0 provider doesn't support synchronous http requests,
|
||||
// so it only has an async `send` method, instead of a `send` and `sendAsync` in web3@0.x.x`
|
||||
@@ -29,12 +39,13 @@ export class Web3Wrapper {
|
||||
(provider as any).sendAsync = (provider as any).send;
|
||||
}
|
||||
this.web3 = new Web3();
|
||||
this.networkId = networkId;
|
||||
this.web3.setProvider(provider);
|
||||
this.defaults = defaults || {};
|
||||
this.jsonRpcRequestId = 0;
|
||||
}
|
||||
public setProvider(provider: Web3.Provider) {
|
||||
delete this.networkIdIfExists;
|
||||
public setProvider(provider: Web3.Provider, networkId: number) {
|
||||
this.networkId = networkId;
|
||||
this.web3.setProvider(provider);
|
||||
}
|
||||
public isAddress(address: string): boolean {
|
||||
@@ -58,37 +69,24 @@ export class Web3Wrapper {
|
||||
public getCurrentProvider(): Web3.Provider {
|
||||
return this.web3.currentProvider;
|
||||
}
|
||||
public async getNetworkIdIfExistsAsync(): Promise<number|undefined> {
|
||||
if (!_.isUndefined(this.networkIdIfExists)) {
|
||||
return this.networkIdIfExists;
|
||||
}
|
||||
|
||||
try {
|
||||
const networkId = await this.getNetworkAsync();
|
||||
this.networkIdIfExists = Number(networkId);
|
||||
return this.networkIdIfExists;
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
public getNetworkId(): number {
|
||||
return this.networkId;
|
||||
}
|
||||
public async getContractInstanceFromArtifactAsync<A extends Web3.ContractInstance>(artifact: Artifact,
|
||||
address?: string): Promise<A> {
|
||||
let contractAddress: string;
|
||||
if (_.isUndefined(address)) {
|
||||
const networkIdIfExists = await this.getNetworkIdIfExistsAsync();
|
||||
if (_.isUndefined(networkIdIfExists)) {
|
||||
throw new Error(ZeroExError.NoNetworkId);
|
||||
}
|
||||
if (_.isUndefined(artifact.networks[networkIdIfExists])) {
|
||||
const networkId = this.getNetworkId();
|
||||
if (_.isUndefined(artifact.networks[networkId])) {
|
||||
throw new Error(ZeroExError.ContractNotDeployedOnNetwork);
|
||||
}
|
||||
contractAddress = artifact.networks[networkIdIfExists].address.toLowerCase();
|
||||
contractAddress = artifact.networks[networkId].address.toLowerCase();
|
||||
} else {
|
||||
contractAddress = address;
|
||||
}
|
||||
const doesContractExist = await this.doesContractExistAtAddressAsync(contractAddress);
|
||||
if (!doesContractExist) {
|
||||
throw new Error(ZeroExError.ContractDoesNotExist);
|
||||
throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contract_name]);
|
||||
}
|
||||
const contractInstance = this.getContractInstance<A>(
|
||||
artifact.abi, contractAddress,
|
||||
|
@@ -1,14 +1,16 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as chai from 'chai';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import 'mocha';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as Sinon from 'sinon';
|
||||
import {ZeroEx, Order, ZeroExError, LogWithDecodedArgs, ApprovalContractEventArgs, TokenEvents} from '../src';
|
||||
|
||||
import {ApprovalContractEventArgs, LogWithDecodedArgs, Order, TokenEvents, ZeroEx, ZeroExError} from '../src';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
|
||||
const blockchainLifecycle = new BlockchainLifecycle();
|
||||
chaiSetup.configure();
|
||||
@@ -16,7 +18,10 @@ const expect = chai.expect;
|
||||
|
||||
describe('ZeroEx library', () => {
|
||||
const web3 = web3Factory.create();
|
||||
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
describe('#setProvider', () => {
|
||||
it('overrides provider in nested web3s and invalidates contractInstances', async () => {
|
||||
// Instantiate the contract instances with the current provider
|
||||
@@ -28,7 +33,7 @@ describe('ZeroEx library', () => {
|
||||
const newProvider = web3Factory.getRpcProvider();
|
||||
// Add property to newProvider so that we can differentiate it from old provider
|
||||
(newProvider as any).zeroExTestId = 1;
|
||||
await zeroEx.setProviderAsync(newProvider);
|
||||
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
|
||||
|
||||
// Check that contractInstances with old provider are removed after provider update
|
||||
expect((zeroEx.exchange as any)._exchangeContractIfExists).to.be.undefined();
|
||||
@@ -36,11 +41,11 @@ describe('ZeroEx library', () => {
|
||||
|
||||
// Check that all nested web3 wrapper instances return the updated provider
|
||||
const nestedWeb3WrapperProvider = (zeroEx as any)._web3Wrapper.getCurrentProvider();
|
||||
expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number');
|
||||
expect((nestedWeb3WrapperProvider).zeroExTestId).to.be.a('number');
|
||||
const exchangeWeb3WrapperProvider = (zeroEx.exchange as any)._web3Wrapper.getCurrentProvider();
|
||||
expect((exchangeWeb3WrapperProvider as any).zeroExTestId).to.be.a('number');
|
||||
expect((exchangeWeb3WrapperProvider).zeroExTestId).to.be.a('number');
|
||||
const tokenRegistryWeb3WrapperProvider = (zeroEx.tokenRegistry as any)._web3Wrapper.getCurrentProvider();
|
||||
expect((tokenRegistryWeb3WrapperProvider as any).zeroExTestId).to.be.a('number');
|
||||
expect((tokenRegistryWeb3WrapperProvider).zeroExTestId).to.be.a('number');
|
||||
});
|
||||
});
|
||||
describe('#isValidSignature', () => {
|
||||
@@ -220,7 +225,7 @@ describe('ZeroEx library', () => {
|
||||
const tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
const tokenUtils = new TokenUtils(tokens);
|
||||
const zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address;
|
||||
const proxyAddress = await zeroEx.proxy.getContractAddressAsync();
|
||||
const proxyAddress = zeroEx.proxy.getContractAddress();
|
||||
const txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(zrxTokenAddress, coinbase);
|
||||
const txReceiptWithDecodedLogs = await zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
const log = txReceiptWithDecodedLogs.logs[0] as LogWithDecodedArgs<ApprovalContractEventArgs>;
|
||||
@@ -232,28 +237,29 @@ describe('ZeroEx library', () => {
|
||||
});
|
||||
describe('#config', () => {
|
||||
it('allows to specify exchange contract address', async () => {
|
||||
const config = {
|
||||
const zeroExConfig = {
|
||||
exchangeContractAddress: ZeroEx.NULL_ADDRESS,
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const zeroExWithWrongExchangeAddress = new ZeroEx(web3.currentProvider, config);
|
||||
return expect(zeroExWithWrongExchangeAddress.exchange.getContractAddressAsync())
|
||||
.to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
|
||||
const zeroExWithWrongExchangeAddress = new ZeroEx(web3.currentProvider, zeroExConfig);
|
||||
expect(zeroExWithWrongExchangeAddress.exchange.getContractAddress()).to.be.equal(ZeroEx.NULL_ADDRESS);
|
||||
});
|
||||
it('allows to specify ether token contract address', async () => {
|
||||
const config = {
|
||||
const zeroExConfig = {
|
||||
etherTokenContractAddress: ZeroEx.NULL_ADDRESS,
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const zeroExWithWrongEtherTokenAddress = new ZeroEx(web3.currentProvider, config);
|
||||
return expect(zeroExWithWrongEtherTokenAddress.etherToken.getContractAddressAsync())
|
||||
.to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
|
||||
const zeroExWithWrongEtherTokenAddress = new ZeroEx(web3.currentProvider, zeroExConfig);
|
||||
expect(zeroExWithWrongEtherTokenAddress.etherToken.getContractAddress()).to.be.equal(ZeroEx.NULL_ADDRESS);
|
||||
});
|
||||
it('allows to specify token registry token contract address', async () => {
|
||||
const config = {
|
||||
const zeroExConfig = {
|
||||
tokenRegistryContractAddress: ZeroEx.NULL_ADDRESS,
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const zeroExWithWrongTokenRegistryAddress = new ZeroEx(web3.currentProvider, config);
|
||||
return expect(zeroExWithWrongTokenRegistryAddress.tokenRegistry.getContractAddressAsync())
|
||||
.to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
|
||||
const zeroExWithWrongTokenRegistryAddress = new ZeroEx(web3.currentProvider, zeroExConfig);
|
||||
expect(zeroExWithWrongTokenRegistryAddress.tokenRegistry.getContractAddress())
|
||||
.to.be.equal(ZeroEx.NULL_ADDRESS);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -1,8 +1,10 @@
|
||||
import * as fs from 'fs';
|
||||
import * as chai from 'chai';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import * as fs from 'fs';
|
||||
import HDWalletProvider = require('truffle-hdwallet-provider');
|
||||
|
||||
import {ZeroEx} from '../src';
|
||||
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
|
||||
chaiSetup.configure();
|
||||
@@ -18,15 +20,18 @@ describe('Artifacts', () => {
|
||||
const packageJSON = JSON.parse(packageJSONContent);
|
||||
const mnemonic = packageJSON.config.mnemonic;
|
||||
const web3Provider = new HDWalletProvider(mnemonic, kovanRpcUrl);
|
||||
const zeroEx = new ZeroEx(web3Provider);
|
||||
const config = {
|
||||
networkId: constants.KOVAN_NETWORK_ID,
|
||||
};
|
||||
const zeroEx = new ZeroEx(web3Provider, config);
|
||||
it('token registry contract is deployed', async () => {
|
||||
await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync();
|
||||
}).timeout(TIMEOUT);
|
||||
it('proxy contract is deployed', async () => {
|
||||
await (zeroEx.token as any)._getTokenTransferProxyAddressAsync();
|
||||
await (zeroEx.proxy as any)._getTokenTransferProxyContractAsync();
|
||||
}).timeout(TIMEOUT);
|
||||
it('exchange contract is deployed', async () => {
|
||||
await zeroEx.exchange.getContractAddressAsync();
|
||||
await (zeroEx.exchange as any)._getExchangeContractAsync();
|
||||
}).timeout(TIMEOUT);
|
||||
});
|
||||
describe('contracts are deployed on ropsten', () => {
|
||||
@@ -35,15 +40,18 @@ describe('Artifacts', () => {
|
||||
const packageJSON = JSON.parse(packageJSONContent);
|
||||
const mnemonic = packageJSON.config.mnemonic;
|
||||
const web3Provider = new HDWalletProvider(mnemonic, ropstenRpcUrl);
|
||||
const zeroEx = new ZeroEx(web3Provider);
|
||||
const config = {
|
||||
networkId: constants.ROPSTEN_NETWORK_ID,
|
||||
};
|
||||
const zeroEx = new ZeroEx(web3Provider, config);
|
||||
it('token registry contract is deployed', async () => {
|
||||
await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync();
|
||||
}).timeout(TIMEOUT);
|
||||
it('proxy contract is deployed', async () => {
|
||||
await (zeroEx.token as any)._getTokenTransferProxyAddressAsync();
|
||||
await (zeroEx.proxy as any)._getTokenTransferProxyContractAsync();
|
||||
}).timeout(TIMEOUT);
|
||||
it('exchange contract is deployed', async () => {
|
||||
await zeroEx.exchange.getContractAddressAsync();
|
||||
await (zeroEx.exchange as any)._getExchangeContractAsync();
|
||||
}).timeout(TIMEOUT);
|
||||
});
|
||||
});
|
||||
|
@@ -1,14 +1,20 @@
|
||||
import * as chai from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import {ZeroEx} from '../src';
|
||||
import {assert} from '../src/utils/assert';
|
||||
|
||||
import {constants} from './utils/constants';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('Assertion library', () => {
|
||||
const web3 = web3Factory.create();
|
||||
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
describe('#isSenderAddressHexAsync', () => {
|
||||
it('throws when address is invalid', async () => {
|
||||
const address = '0xdeadbeef';
|
||||
|
@@ -1,11 +1,14 @@
|
||||
import 'mocha';
|
||||
import * as chai from 'chai';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import * as chai from 'chai';
|
||||
import 'mocha';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {ZeroEx, ZeroExError} from '../src';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -15,7 +18,7 @@ const blockchainLifecycle = new BlockchainLifecycle();
|
||||
// a small amount of ETH will be used to pay this gas cost. We therefore check that the difference between
|
||||
// the expected balance and actual balance (given the amount of ETH deposited), only deviates by the amount
|
||||
// required to pay gas costs.
|
||||
const MAX_REASONABLE_GAS_COST_IN_WEI = 62237;
|
||||
const MAX_REASONABLE_GAS_COST_IN_WEI = 62517;
|
||||
|
||||
describe('EtherTokenWrapper', () => {
|
||||
let web3: Web3;
|
||||
@@ -28,13 +31,14 @@ describe('EtherTokenWrapper', () => {
|
||||
const gasPrice = new BigNumber(1);
|
||||
const zeroExConfig = {
|
||||
gasPrice,
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig);
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
addressWithETH = userAddresses[0];
|
||||
wethContractAddress = await zeroEx.etherToken.getContractAddressAsync();
|
||||
wethContractAddress = zeroEx.etherToken.getContractAddress();
|
||||
depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5));
|
||||
decimalPlaces = 7;
|
||||
});
|
||||
|
@@ -1,19 +1,22 @@
|
||||
import 'mocha';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as Sinon from 'sinon';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {Web3Wrapper} from '../src/web3_wrapper';
|
||||
import {EventWatcher} from '../src/order_watcher/event_watcher';
|
||||
|
||||
import {
|
||||
ZeroEx,
|
||||
LogEvent,
|
||||
DecodedLogEvent,
|
||||
LogEvent,
|
||||
ZeroEx,
|
||||
} from '../src';
|
||||
import {EventWatcher} from '../src/order_watcher/event_watcher';
|
||||
import {DoneCallback} from '../src/types';
|
||||
import {Web3Wrapper} from '../src/web3_wrapper';
|
||||
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -57,7 +60,7 @@ describe('EventWatcher', () => {
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
const pollingIntervalMs = 10;
|
||||
web3Wrapper = new Web3Wrapper(web3.currentProvider);
|
||||
web3Wrapper = new Web3Wrapper(web3.currentProvider, constants.TESTRPC_NETWORK_ID);
|
||||
eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs);
|
||||
});
|
||||
afterEach(() => {
|
||||
|
@@ -1,19 +1,25 @@
|
||||
import * as chai from 'chai';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {ZeroEx, ExchangeContractErrs, Token} from '../src';
|
||||
import {TradeSide, TransferType} from '../src/types';
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import * as chai from 'chai';
|
||||
|
||||
import {ExchangeContractErrs, Token, ZeroEx} from '../src';
|
||||
import {BlockParamLiteral, TradeSide, TransferType} from '../src/types';
|
||||
import {ExchangeTransferSimulator} from '../src/utils/exchange_transfer_simulator';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
const blockchainLifecycle = new BlockchainLifecycle();
|
||||
|
||||
describe('ExchangeTransferSimulator', () => {
|
||||
const web3 = web3Factory.create();
|
||||
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
const transferAmount = new BigNumber(5);
|
||||
let userAddresses: string[];
|
||||
let tokens: Token[];
|
||||
@@ -37,7 +43,7 @@ describe('ExchangeTransferSimulator', () => {
|
||||
});
|
||||
describe('#transferFromAsync', () => {
|
||||
beforeEach(() => {
|
||||
exchangeTransferSimulator = new ExchangeTransferSimulator(zeroEx.token);
|
||||
exchangeTransferSimulator = new ExchangeTransferSimulator(zeroEx.token, BlockParamLiteral.Latest);
|
||||
});
|
||||
it('throws if the user doesn\'t have enough allowance', async () => {
|
||||
return expect(exchangeTransferSimulator.transferFromAsync(
|
||||
|
@@ -1,27 +1,30 @@
|
||||
import 'mocha';
|
||||
import * as chai from 'chai';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import * as chai from 'chai';
|
||||
import 'mocha';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {
|
||||
ZeroEx,
|
||||
Token,
|
||||
SignedOrder,
|
||||
SubscriptionOpts,
|
||||
ExchangeEvents,
|
||||
DecodedLogEvent,
|
||||
ExchangeContractErrs,
|
||||
OrderCancellationRequest,
|
||||
OrderFillRequest,
|
||||
LogFillContractEventArgs,
|
||||
ExchangeEvents,
|
||||
LogCancelContractEventArgs,
|
||||
LogEvent,
|
||||
DecodedLogEvent,
|
||||
LogFillContractEventArgs,
|
||||
OrderCancellationRequest,
|
||||
OrderFillRequest,
|
||||
SignedOrder,
|
||||
SubscriptionOpts,
|
||||
Token,
|
||||
ZeroEx,
|
||||
} from '../src';
|
||||
import {DoneCallback, BlockParamLiteral} from '../src/types';
|
||||
import {BlockParamLiteral, DoneCallback} from '../src/types';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {FillScenarios} from './utils/fill_scenarios';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -38,10 +41,13 @@ describe('ExchangeWrapper', () => {
|
||||
let zrxTokenAddress: string;
|
||||
let fillScenarios: FillScenarios;
|
||||
let exchangeContractAddress: string;
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
exchangeContractAddress = await zeroEx.exchange.getContractAddressAsync();
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
exchangeContractAddress = zeroEx.exchange.getContractAddress();
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
tokenUtils = new TokenUtils(tokens);
|
||||
@@ -613,7 +619,7 @@ describe('ExchangeWrapper', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('#subscribeAsync', () => {
|
||||
describe('#subscribe', () => {
|
||||
const indexFilterValues = {};
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
let makerTokenAddress: string;
|
||||
@@ -649,10 +655,10 @@ describe('ExchangeWrapper', () => {
|
||||
(async () => {
|
||||
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
|
||||
expect(logEvent.event).to.be.equal(ExchangeEvents.LogFill);
|
||||
expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogFill);
|
||||
done();
|
||||
};
|
||||
await zeroEx.exchange.subscribeAsync(
|
||||
zeroEx.exchange.subscribe(
|
||||
ExchangeEvents.LogFill, indexFilterValues, callback,
|
||||
);
|
||||
await zeroEx.exchange.fillOrderAsync(
|
||||
@@ -665,33 +671,33 @@ describe('ExchangeWrapper', () => {
|
||||
(async () => {
|
||||
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<LogCancelContractEventArgs>) => {
|
||||
expect(logEvent.event).to.be.equal(ExchangeEvents.LogCancel);
|
||||
expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogCancel);
|
||||
done();
|
||||
};
|
||||
await zeroEx.exchange.subscribeAsync(
|
||||
zeroEx.exchange.subscribe(
|
||||
ExchangeEvents.LogCancel, indexFilterValues, callback,
|
||||
);
|
||||
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelTakerAmountInBaseUnits);
|
||||
})().catch(done);
|
||||
});
|
||||
it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => {
|
||||
it('Outstanding subscriptions are cancelled when zeroEx.setProvider called', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
|
||||
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
|
||||
done(new Error('Expected this subscription to have been cancelled'));
|
||||
};
|
||||
await zeroEx.exchange.subscribeAsync(
|
||||
zeroEx.exchange.subscribe(
|
||||
ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled,
|
||||
);
|
||||
|
||||
const newProvider = web3Factory.getRpcProvider();
|
||||
await zeroEx.setProviderAsync(newProvider);
|
||||
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
|
||||
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
|
||||
expect(logEvent.event).to.be.equal(ExchangeEvents.LogFill);
|
||||
expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogFill);
|
||||
done();
|
||||
};
|
||||
await zeroEx.exchange.subscribeAsync(
|
||||
zeroEx.exchange.subscribe(
|
||||
ExchangeEvents.LogFill, indexFilterValues, callback,
|
||||
);
|
||||
await zeroEx.exchange.fillOrderAsync(
|
||||
@@ -705,7 +711,7 @@ describe('ExchangeWrapper', () => {
|
||||
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
|
||||
done(new Error('Expected this subscription to have been cancelled'));
|
||||
};
|
||||
const subscriptionToken = await zeroEx.exchange.subscribeAsync(
|
||||
const subscriptionToken = zeroEx.exchange.subscribe(
|
||||
ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled,
|
||||
);
|
||||
zeroEx.exchange.unsubscribe(subscriptionToken);
|
||||
@@ -740,8 +746,8 @@ describe('ExchangeWrapper', () => {
|
||||
});
|
||||
});
|
||||
describe('#getZRXTokenAddressAsync', () => {
|
||||
it('gets the same token as is in token registry', async () => {
|
||||
const zrxAddress = await zeroEx.exchange.getZRXTokenAddressAsync();
|
||||
it('gets the same token as is in token registry', () => {
|
||||
const zrxAddress = zeroEx.exchange.getZRXTokenAddress();
|
||||
const zrxToken = tokenUtils.getProtocolTokenOrThrow();
|
||||
expect(zrxAddress).to.equal(zrxToken.address);
|
||||
});
|
||||
@@ -754,7 +760,7 @@ describe('ExchangeWrapper', () => {
|
||||
const fillableAmount = new BigNumber(5);
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
const subscriptionOpts: SubscriptionOpts = {
|
||||
fromBlock: BlockParamLiteral.Earliest,
|
||||
fromBlock: 0,
|
||||
toBlock: BlockParamLiteral.Latest,
|
||||
};
|
||||
let txHash: string;
|
||||
@@ -821,4 +827,4 @@ describe('ExchangeWrapper', () => {
|
||||
expect(args.maker).to.be.equal(differentMakerAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
}); // tslint:disable:max-file-line-count
|
||||
|
@@ -1,20 +1,23 @@
|
||||
import 'mocha';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as Sinon from 'sinon';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
import {ZeroEx} from '../src/0x';
|
||||
import {ExpirationWatcher} from '../src/order_watcher/expiration_watcher';
|
||||
import {DoneCallback, Token} from '../src/types';
|
||||
import {constants} from '../src/utils/constants';
|
||||
import {utils} from '../src/utils/utils';
|
||||
import {Web3Wrapper} from '../src/web3_wrapper';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {ExpirationWatcher} from '../src/order_watcher/expiration_watcher';
|
||||
import {Token, DoneCallback} from '../src/types';
|
||||
import {ZeroEx} from '../src/0x';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {FillScenarios} from './utils/fill_scenarios';
|
||||
import {reportCallbackErrors} from './utils/report_callback_errors';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -41,8 +44,11 @@ describe('ExpirationWatcher', () => {
|
||||
let expirationWatcher: ExpirationWatcher;
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
exchangeContractAddress = await zeroEx.exchange.getContractAddressAsync();
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
exchangeContractAddress = zeroEx.exchange.getContractAddress();
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
tokenUtils = new TokenUtils(tokens);
|
||||
|
@@ -1,30 +1,33 @@
|
||||
import 'mocha';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import { chaiSetup } from './utils/chai_setup';
|
||||
import { web3Factory } from './utils/web3_factory';
|
||||
import { Web3Wrapper } from '../src/web3_wrapper';
|
||||
import { OrderStateWatcher } from '../src/order_watcher/order_state_watcher';
|
||||
|
||||
import {
|
||||
DecodedLogEvent,
|
||||
ExchangeContractErrs,
|
||||
LogEvent,
|
||||
OrderState,
|
||||
OrderStateInvalid,
|
||||
OrderStateValid,
|
||||
SignedOrder,
|
||||
Token,
|
||||
ZeroEx,
|
||||
LogEvent,
|
||||
DecodedLogEvent,
|
||||
ZeroExConfig,
|
||||
OrderState,
|
||||
SignedOrder,
|
||||
ZeroExError,
|
||||
OrderStateValid,
|
||||
OrderStateInvalid,
|
||||
ExchangeContractErrs,
|
||||
} from '../src';
|
||||
import { TokenUtils } from './utils/token_utils';
|
||||
import { FillScenarios } from './utils/fill_scenarios';
|
||||
import { DoneCallback } from '../src/types';
|
||||
import {OrderStateWatcher} from '../src/order_watcher/order_state_watcher';
|
||||
import {DoneCallback} from '../src/types';
|
||||
import {Web3Wrapper} from '../src/web3_wrapper';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {FillScenarios} from './utils/fill_scenarios';
|
||||
import {reportCallbackErrors} from './utils/report_callback_errors';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
const TIMEOUT_MS = 150;
|
||||
|
||||
@@ -47,11 +50,15 @@ describe('OrderStateWatcher', () => {
|
||||
let taker: string;
|
||||
let web3Wrapper: Web3Wrapper;
|
||||
let signedOrder: SignedOrder;
|
||||
const fillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(5), 18);
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
const decimals = constants.ZRX_DECIMALS;
|
||||
const fillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals);
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
exchangeContractAddress = await zeroEx.exchange.getContractAddressAsync();
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
exchangeContractAddress = zeroEx.exchange.getContractAddress();
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
[, maker, taker] = userAddresses;
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
@@ -73,13 +80,13 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
expect((zeroEx.orderStateWatcher as any)._orderByOrderHash).to.include({
|
||||
[orderHash]: signedOrder,
|
||||
});
|
||||
let dependentOrderHashes = (zeroEx.orderStateWatcher as any)._dependentOrderHashes;
|
||||
expect(dependentOrderHashes[signedOrder.maker][signedOrder.makerTokenAddress]).to.have.keys(orderHash);
|
||||
await zeroEx.orderStateWatcher.removeOrderAsync(orderHash);
|
||||
zeroEx.orderStateWatcher.removeOrder(orderHash);
|
||||
expect((zeroEx.orderStateWatcher as any)._orderByOrderHash).to.not.include({
|
||||
[orderHash]: signedOrder,
|
||||
});
|
||||
@@ -92,7 +99,7 @@ describe('OrderStateWatcher', () => {
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
const nonExistentOrderHash = `0x${orderHash.substr(2).split('').reverse().join('')}`;
|
||||
await zeroEx.orderStateWatcher.removeOrderAsync(nonExistentOrderHash);
|
||||
zeroEx.orderStateWatcher.removeOrder(nonExistentOrderHash);
|
||||
});
|
||||
});
|
||||
describe('#subscribe', async () => {
|
||||
@@ -109,7 +116,7 @@ describe('OrderStateWatcher', () => {
|
||||
afterEach(async () => {
|
||||
zeroEx.orderStateWatcher.unsubscribe();
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.removeOrderAsync(orderHash);
|
||||
zeroEx.orderStateWatcher.removeOrder(orderHash);
|
||||
});
|
||||
it('should emit orderStateInvalid when maker allowance set to 0 for watched order', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
@@ -117,7 +124,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
const invalidOrderState = orderState as OrderStateInvalid;
|
||||
@@ -135,7 +142,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
throw new Error('OrderState callback fired for irrelevant order');
|
||||
});
|
||||
@@ -156,7 +163,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
const invalidOrderState = orderState as OrderStateInvalid;
|
||||
@@ -176,18 +183,14 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
let eventCount = 0;
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
eventCount++;
|
||||
expect(orderState.isValid).to.be.false();
|
||||
const invalidOrderState = orderState as OrderStateInvalid;
|
||||
expect(invalidOrderState.orderHash).to.be.equal(orderHash);
|
||||
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero);
|
||||
if (eventCount === 2) {
|
||||
done();
|
||||
}
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
|
||||
@@ -208,11 +211,9 @@ describe('OrderStateWatcher', () => {
|
||||
|
||||
const fillAmountInBaseUnits = new BigNumber(2);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
let eventCount = 0;
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
eventCount++;
|
||||
expect(orderState.isValid).to.be.true();
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
expect(validOrderState.orderHash).to.be.equal(orderHash);
|
||||
@@ -224,9 +225,7 @@ describe('OrderStateWatcher', () => {
|
||||
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingFillable);
|
||||
expect(orderRelevantState.makerBalance).to.be.bignumber.equal(remainingMakerBalance);
|
||||
if (eventCount === 2) {
|
||||
done();
|
||||
}
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
@@ -243,7 +242,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, makerFee, takerFee, maker, taker, fillableAmount,
|
||||
taker);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
done();
|
||||
});
|
||||
@@ -254,31 +253,27 @@ describe('OrderStateWatcher', () => {
|
||||
describe('remainingFillable(M|T)akerTokenAmount', () => {
|
||||
it('should calculate correct remaining fillable', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const takerFillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(10), 18);
|
||||
const makerFillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(20), 18);
|
||||
const takerFillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(10), decimals);
|
||||
const makerFillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(20), decimals);
|
||||
signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, makerFillableAmount,
|
||||
takerFillableAmount,
|
||||
);
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
const takerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, taker);
|
||||
const fillAmountInBaseUnits = ZeroEx.toBaseUnitAmount(new BigNumber(2), 18);
|
||||
const fillAmountInBaseUnits = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
let eventCount = 0;
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
eventCount++;
|
||||
expect(orderState.isValid).to.be.true();
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
expect(validOrderState.orderHash).to.be.equal(orderHash);
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(16), 18));
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(16), decimals));
|
||||
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(8), 18));
|
||||
if (eventCount === 2) {
|
||||
done();
|
||||
}
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(8), decimals));
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
@@ -295,8 +290,8 @@ describe('OrderStateWatcher', () => {
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
|
||||
const changedMakerApprovalAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), 18);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
const changedMakerApprovalAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), decimals);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
@@ -319,11 +314,12 @@ describe('OrderStateWatcher', () => {
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
|
||||
const remainingAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18);
|
||||
const remainingAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals);
|
||||
const transferAmount = makerBalance.sub(remainingAmount);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.true();
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
@@ -337,6 +333,88 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should equal remaining amount when partially cancelled and order has fees', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const takerFee = ZeroEx.toBaseUnitAmount(new BigNumber(0), decimals);
|
||||
const makerFee = ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals);
|
||||
const feeRecipient = taker;
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync(
|
||||
makerToken.address, takerToken.address, makerFee, takerFee, maker,
|
||||
taker, fillableAmount, feeRecipient);
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
|
||||
const remainingTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(4), decimals);
|
||||
const transferTokenAmount = makerFee.sub(remainingTokenAmount);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.true();
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingTokenAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.exchange.cancelOrderAsync(signedOrder, transferTokenAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should equal ratio amount when fee balance is lowered', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const takerFee = ZeroEx.toBaseUnitAmount(new BigNumber(0), decimals);
|
||||
const makerFee = ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals);
|
||||
const feeRecipient = taker;
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync(
|
||||
makerToken.address, takerToken.address, makerFee, takerFee, maker,
|
||||
taker, fillableAmount, feeRecipient);
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
|
||||
const remainingFeeAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), decimals);
|
||||
const transferFeeAmount = makerFee.sub(remainingFeeAmount);
|
||||
|
||||
const remainingTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(4), decimals);
|
||||
const transferTokenAmount = makerFee.sub(remainingTokenAmount);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingFeeAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, remainingFeeAmount);
|
||||
await zeroEx.token.transferAsync(
|
||||
makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferTokenAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should calculate full amount when all available and non-divisible', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const takerFee = ZeroEx.toBaseUnitAmount(new BigNumber(0), decimals);
|
||||
const makerFee = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
|
||||
const feeRecipient = taker;
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync(
|
||||
makerToken.address, takerToken.address, makerFee, takerFee, maker,
|
||||
taker, fillableAmount, feeRecipient);
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
fillableAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.setProxyAllowanceAsync(
|
||||
makerToken.address, maker, ZeroEx.toBaseUnitAmount(new BigNumber(100), decimals));
|
||||
})().catch(done);
|
||||
});
|
||||
});
|
||||
it('should emit orderStateInvalid when watched order cancelled', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
@@ -344,7 +422,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
@@ -366,7 +444,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
@@ -392,7 +470,7 @@ describe('OrderStateWatcher', () => {
|
||||
|
||||
const cancelAmountInBaseUnits = new BigNumber(2);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.true();
|
||||
@@ -407,4 +485,4 @@ describe('OrderStateWatcher', () => {
|
||||
})().catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
}); // tslint:disable:max-file-line-count
|
||||
|
@@ -1,16 +1,19 @@
|
||||
import * as chai from 'chai';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as Sinon from 'sinon';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {ZeroEx, SignedOrder, Token, ExchangeContractErrs, ZeroExError} from '../src';
|
||||
import {TradeSide, TransferType} from '../src/types';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {FillScenarios} from './utils/fill_scenarios';
|
||||
import {OrderValidationUtils} from '../src/utils/order_validation_utils';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {ExchangeContractErrs, SignedOrder, Token, ZeroEx, ZeroExError} from '../src';
|
||||
import {BlockParamLiteral, TradeSide, TransferType} from '../src/types';
|
||||
import {ExchangeTransferSimulator} from '../src/utils/exchange_transfer_simulator';
|
||||
import {OrderValidationUtils} from '../src/utils/order_validation_utils';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {FillScenarios} from './utils/fill_scenarios';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -34,10 +37,13 @@ describe('OrderValidation', () => {
|
||||
let orderValidationUtils: OrderValidationUtils;
|
||||
const fillableAmount = new BigNumber(5);
|
||||
const fillTakerAmount = new BigNumber(5);
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
exchangeContractAddress = await zeroEx.exchange.getContractAddressAsync();
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
exchangeContractAddress = zeroEx.exchange.getContractAddress();
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
[coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses;
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
@@ -110,7 +116,7 @@ describe('OrderValidation', () => {
|
||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
|
||||
);
|
||||
// 27 <--> 28
|
||||
signedOrder.ecSignature.v = 27 + (28 - signedOrder.ecSignature.v);
|
||||
signedOrder.ecSignature.v = (28 - signedOrder.ecSignature.v) + 27;
|
||||
return expect(zeroEx.exchange.validateFillOrderThrowIfInvalidAsync(
|
||||
signedOrder, fillableAmount, takerAddress,
|
||||
)).to.be.rejectedWith(ZeroExError.InvalidSignature);
|
||||
@@ -215,7 +221,7 @@ describe('OrderValidation', () => {
|
||||
return Sinon.match((value: BigNumber) => value.eq(expected));
|
||||
};
|
||||
beforeEach('create exchangeTransferSimulator', async () => {
|
||||
exchangeTransferSimulator = new ExchangeTransferSimulator(zeroEx.token);
|
||||
exchangeTransferSimulator = new ExchangeTransferSimulator(zeroEx.token, BlockParamLiteral.Latest);
|
||||
transferFromAsync = Sinon.spy();
|
||||
exchangeTransferSimulator.transferFromAsync = transferFromAsync as any;
|
||||
});
|
||||
@@ -226,7 +232,7 @@ describe('OrderValidation', () => {
|
||||
makerTokenAddress, takerTokenAddress, makerFee, takerFee,
|
||||
makerAddress, takerAddress, fillableAmount, feeRecipient,
|
||||
);
|
||||
await orderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTransferSimulator, signedOrder, fillableAmount, takerAddress, zrxTokenAddress,
|
||||
);
|
||||
expect(transferFromAsync.callCount).to.be.equal(4);
|
||||
@@ -262,7 +268,7 @@ describe('OrderValidation', () => {
|
||||
makerTokenAddress, takerTokenAddress, makerFee, takerFee,
|
||||
makerAddress, ZeroEx.NULL_ADDRESS, fillableAmount, feeRecipient,
|
||||
);
|
||||
await orderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTransferSimulator, signedOrder, fillableAmount, takerAddress, zrxTokenAddress,
|
||||
);
|
||||
expect(transferFromAsync.callCount).to.be.equal(4);
|
||||
@@ -297,7 +303,7 @@ describe('OrderValidation', () => {
|
||||
const signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync(
|
||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, makerTokenAmount, takerTokenAmount,
|
||||
);
|
||||
await orderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTransferSimulator, signedOrder, takerTokenAmount, takerAddress, zrxTokenAddress,
|
||||
);
|
||||
expect(transferFromAsync.callCount).to.be.equal(4);
|
||||
@@ -312,7 +318,7 @@ describe('OrderValidation', () => {
|
||||
fillableAmount, ZeroEx.NULL_ADDRESS,
|
||||
);
|
||||
const fillTakerTokenAmount = fillableAmount.div(2).round(0);
|
||||
await orderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync(
|
||||
exchangeTransferSimulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress,
|
||||
);
|
||||
const makerPartialFee = makerFee.div(2);
|
||||
|
178
packages/0x.js/test/remaining_fillable_calculator_test.ts
Normal file
178
packages/0x.js/test/remaining_fillable_calculator_test.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import { ZeroEx } from '../src/0x';
|
||||
import { RemainingFillableCalculator } from '../src/order_watcher/remaining_fillable_calculator';
|
||||
import { ECSignature, SignedOrder } from '../src/types';
|
||||
|
||||
import { chaiSetup } from './utils/chai_setup';
|
||||
import { TokenUtils } from './utils/token_utils';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('RemainingFillableCalculator', () => {
|
||||
let calculator: RemainingFillableCalculator;
|
||||
let signedOrder: SignedOrder;
|
||||
let transferrableMakerTokenAmount: BigNumber;
|
||||
let transferrableMakerFeeTokenAmount: BigNumber;
|
||||
let remainingMakerTokenAmount: BigNumber;
|
||||
let makerAmount: BigNumber;
|
||||
let takerAmount: BigNumber;
|
||||
let makerFeeAmount: BigNumber;
|
||||
let isMakerTokenZRX: boolean;
|
||||
const makerToken: string = '0x1';
|
||||
const takerToken: string = '0x2';
|
||||
const decimals: number = 4;
|
||||
const zero: BigNumber = new BigNumber(0);
|
||||
const zeroAddress = '0x0';
|
||||
const signature: ECSignature = { v: 27, r: '', s: ''};
|
||||
beforeEach(async () => {
|
||||
[makerAmount, takerAmount, makerFeeAmount] = [ZeroEx.toBaseUnitAmount(new BigNumber(50), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals)];
|
||||
[transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount] = [
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(50), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals)];
|
||||
});
|
||||
function buildSignedOrder(): SignedOrder {
|
||||
return { ecSignature: signature,
|
||||
exchangeContractAddress: zeroAddress,
|
||||
feeRecipient: zeroAddress,
|
||||
maker: zeroAddress,
|
||||
taker: zeroAddress,
|
||||
makerFee: makerFeeAmount,
|
||||
takerFee: zero,
|
||||
makerTokenAmount: makerAmount,
|
||||
takerTokenAmount: takerAmount,
|
||||
makerTokenAddress: makerToken,
|
||||
takerTokenAddress: takerToken,
|
||||
salt: zero,
|
||||
expirationUnixTimestampSec: zero };
|
||||
}
|
||||
describe('Maker token is NOT ZRX', () => {
|
||||
before(async () => {
|
||||
isMakerTokenZRX = false;
|
||||
});
|
||||
it('calculates the correct amount when unfilled and funds available', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
|
||||
});
|
||||
it('calculates the correct amount when partially filled and funds available', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
remainingMakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals);
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
|
||||
});
|
||||
it('calculates the amount to be 0 when all fee funds are transferred', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
transferrableMakerFeeTokenAmount = zero;
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(zero);
|
||||
});
|
||||
it('calculates the correct amount when balance is less than remaining fillable', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
const partiallyFilledAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount);
|
||||
transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount);
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(transferrableMakerTokenAmount);
|
||||
});
|
||||
describe('Order to Fee Ratio is < 1', () => {
|
||||
beforeEach(async () => {
|
||||
[makerAmount, takerAmount, makerFeeAmount] = [ZeroEx.toBaseUnitAmount(new BigNumber(3), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(6), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(6), decimals)];
|
||||
});
|
||||
it('calculates the correct amount when funds unavailable', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
const transferredAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
|
||||
transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(transferredAmount);
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount,
|
||||
remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(transferrableMakerTokenAmount);
|
||||
});
|
||||
});
|
||||
describe('Ratio is not evenly divisble', () => {
|
||||
beforeEach(async () => {
|
||||
[makerAmount, takerAmount, makerFeeAmount] = [ZeroEx.toBaseUnitAmount(new BigNumber(3), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(7), decimals),
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(7), decimals)];
|
||||
});
|
||||
it('calculates the correct amount when funds unavailable', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
const transferredAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
|
||||
transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(transferredAmount);
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount,
|
||||
remainingMakerTokenAmount);
|
||||
const calculatedFillableAmount = calculator.computeRemainingMakerFillable();
|
||||
expect(calculatedFillableAmount.lessThanOrEqualTo(transferrableMakerTokenAmount)).to.be.true();
|
||||
expect(calculatedFillableAmount).to.be.bignumber.greaterThan(new BigNumber(0));
|
||||
const orderToFeeRatio = signedOrder.makerTokenAmount.dividedBy(signedOrder.makerFee);
|
||||
const calculatedFeeAmount = calculatedFillableAmount.dividedBy(orderToFeeRatio);
|
||||
expect(calculatedFeeAmount).to.be.bignumber.lessThan(transferrableMakerFeeTokenAmount);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('Maker Token is ZRX', () => {
|
||||
before(async () => {
|
||||
isMakerTokenZRX = true;
|
||||
});
|
||||
it('calculates the correct amount when unfilled and funds available', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
transferrableMakerTokenAmount = makerAmount.plus(makerFeeAmount);
|
||||
transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount;
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
|
||||
});
|
||||
it('calculates the correct amount when partially filled and funds available', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
remainingMakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals);
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
|
||||
});
|
||||
it('calculates the amount to be 0 when all fee funds are transferred', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
transferrableMakerTokenAmount = zero;
|
||||
transferrableMakerFeeTokenAmount = zero;
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount;
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(zero);
|
||||
});
|
||||
it('calculates the correct amount when balance is less than remaining fillable', () => {
|
||||
signedOrder = buildSignedOrder();
|
||||
const partiallyFilledAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
|
||||
remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount);
|
||||
transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount);
|
||||
transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount;
|
||||
|
||||
const orderToFeeRatio = signedOrder.makerTokenAmount.dividedToIntegerBy(signedOrder.makerFee);
|
||||
const expectedFillableAmount = new BigNumber(450980);
|
||||
calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX,
|
||||
transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount);
|
||||
const calculatedFillableAmount = calculator.computeRemainingMakerFillable();
|
||||
const numberOfFillsInRatio = calculatedFillableAmount.dividedToIntegerBy(orderToFeeRatio);
|
||||
const calculatedFillableAmountPlusFees = calculatedFillableAmount.plus(numberOfFillsInRatio);
|
||||
expect(calculatedFillableAmountPlusFees).to.be.bignumber.lessThan(transferrableMakerTokenAmount);
|
||||
expect(calculatedFillableAmountPlusFees).to.be.bignumber.lessThan(remainingMakerTokenAmount);
|
||||
expect(calculatedFillableAmount).to.be.bignumber.equal(expectedFillableAmount);
|
||||
expect(numberOfFillsInRatio.decimalPlaces()).to.be.equal(0);
|
||||
});
|
||||
});
|
||||
});
|
@@ -1,23 +1,27 @@
|
||||
import 'mocha';
|
||||
import * as _ from 'lodash';
|
||||
import * as chai from 'chai';
|
||||
import * as Sinon from 'sinon';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import promisify = require('es6-promisify');
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as Sinon from 'sinon';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {
|
||||
ApprovalContractEventArgs,
|
||||
DecodedLogEvent,
|
||||
Token,
|
||||
TokenEvents,
|
||||
ZeroEx,
|
||||
ZeroExError,
|
||||
Token,
|
||||
ApprovalContractEventArgs,
|
||||
TokenEvents,
|
||||
DecodedLogEvent,
|
||||
} from '../src';
|
||||
import {BlockParamLiteral, DoneCallback} from '../src/types';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {reportCallbackErrors} from './utils/report_callback_errors';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {DoneCallback, BlockParamLiteral} from '../src/types';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -31,9 +35,12 @@ describe('SubscriptionTest', () => {
|
||||
let tokenUtils: TokenUtils;
|
||||
let coinbase: string;
|
||||
let addressWithoutFunds: string;
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
tokenUtils = new TokenUtils(tokens);
|
||||
@@ -62,22 +69,44 @@ describe('SubscriptionTest', () => {
|
||||
_.each(stubs, s => s.restore());
|
||||
stubs = [];
|
||||
});
|
||||
it('Should receive the Error when an error occurs', (done: DoneCallback) => {
|
||||
it('Should receive the Error when an error occurs while fetching the block', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
|
||||
expect(err).to.not.be.null();
|
||||
expect(logEvent).to.be.undefined();
|
||||
done();
|
||||
};
|
||||
const errMsg = 'Error fetching block';
|
||||
const callback = reportCallbackErrors(done)(
|
||||
(err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
|
||||
expect(err.message).to.be.equal(errMsg);
|
||||
expect(logEvent).to.be.undefined();
|
||||
done();
|
||||
},
|
||||
);
|
||||
stubs = [
|
||||
Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync')
|
||||
.throws('JSON RPC error'),
|
||||
.throws(new Error(errMsg)),
|
||||
];
|
||||
zeroEx.token.subscribe(
|
||||
tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
|
||||
await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
});
|
||||
it('Should receive the Error when an error occurs while reconciling the new block', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const errMsg = 'Error fetching logs';
|
||||
const callback = reportCallbackErrors(done)(
|
||||
(err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
|
||||
expect(err.message).to.be.equal(errMsg);
|
||||
expect(logEvent).to.be.undefined();
|
||||
done();
|
||||
},
|
||||
);
|
||||
stubs = [
|
||||
Sinon.stub((zeroEx as any)._web3Wrapper, 'getLogsAsync')
|
||||
.throws(new Error(errMsg)),
|
||||
];
|
||||
zeroEx.token.subscribe(
|
||||
tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
|
||||
await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('Should allow unsubscribeAll to be called successfully after an error', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => _.noop;
|
||||
@@ -85,11 +114,11 @@ describe('SubscriptionTest', () => {
|
||||
tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
|
||||
stubs = [
|
||||
Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync')
|
||||
.throws('JSON RPC error'),
|
||||
.throws(new Error('JSON RPC error')),
|
||||
];
|
||||
zeroEx.token.unsubscribeAll();
|
||||
done();
|
||||
})().catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -1,11 +1,14 @@
|
||||
import {schemas, SchemaValidator} from '@0xproject/json-schemas';
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as chai from 'chai';
|
||||
import {SchemaValidator, schemas} from '@0xproject/json-schemas';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {ZeroEx, Token} from '../src';
|
||||
|
||||
import {Token, ZeroEx} from '../src';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -24,9 +27,12 @@ describe('TokenRegistryWrapper', () => {
|
||||
const registeredName = '0x Protocol Token';
|
||||
const unregisteredSymbol = 'MAL';
|
||||
const unregisteredName = 'Malicious Token';
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
const web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
_.map(tokens, token => {
|
||||
tokenAddressBySymbol[token.symbol] = token.address;
|
||||
|
@@ -1,17 +1,23 @@
|
||||
import * as chai from 'chai';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
import {ZeroEx} from '../src';
|
||||
import {TokenTransferProxyWrapper} from '../src/contract_wrappers/token_transfer_proxy_wrapper';
|
||||
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('TokenTransferProxyWrapper', () => {
|
||||
let zeroEx: ZeroEx;
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
const web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
});
|
||||
describe('#isAuthorizedAsync', () => {
|
||||
it('should return false if the address is not authorized', async () => {
|
||||
|
@@ -1,27 +1,30 @@
|
||||
import 'mocha';
|
||||
import * as chai from 'chai';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import promisify = require('es6-promisify');
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import 'mocha';
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {
|
||||
ApprovalContractEventArgs,
|
||||
ContractEvent,
|
||||
DecodedLogEvent,
|
||||
LogEvent,
|
||||
LogWithDecodedArgs,
|
||||
SubscriptionOpts,
|
||||
Token,
|
||||
TokenContractEventArgs,
|
||||
TokenEvents,
|
||||
TransferContractEventArgs,
|
||||
ZeroEx,
|
||||
ZeroExError,
|
||||
Token,
|
||||
SubscriptionOpts,
|
||||
TokenEvents,
|
||||
ContractEvent,
|
||||
TransferContractEventArgs,
|
||||
ApprovalContractEventArgs,
|
||||
TokenContractEventArgs,
|
||||
LogWithDecodedArgs,
|
||||
LogEvent,
|
||||
DecodedLogEvent,
|
||||
} from '../src';
|
||||
import {BlockParamLiteral, DoneCallback} from '../src/types';
|
||||
|
||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {constants} from './utils/constants';
|
||||
import {TokenUtils} from './utils/token_utils';
|
||||
import {DoneCallback, BlockParamLiteral} from '../src/types';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
@@ -35,9 +38,12 @@ describe('TokenWrapper', () => {
|
||||
let tokenUtils: TokenUtils;
|
||||
let coinbase: string;
|
||||
let addressWithoutFunds: string;
|
||||
const config = {
|
||||
networkId: constants.TESTRPC_NETWORK_ID,
|
||||
};
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
zeroEx = new ZeroEx(web3.currentProvider, config);
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
tokenUtils = new TokenUtils(tokens);
|
||||
@@ -80,7 +86,7 @@ describe('TokenWrapper', () => {
|
||||
const toAddress = coinbase;
|
||||
return expect(zeroEx.token.transferAsync(
|
||||
nonExistentTokenAddress, fromAddress, toAddress, transferAmount,
|
||||
)).to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
|
||||
)).to.be.rejectedWith(ZeroExError.TokenContractDoesNotExist);
|
||||
});
|
||||
});
|
||||
describe('#transferFromAsync', () => {
|
||||
@@ -153,7 +159,7 @@ describe('TokenWrapper', () => {
|
||||
const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065';
|
||||
return expect(zeroEx.token.transferFromAsync(
|
||||
nonExistentTokenAddress, fromAddress, toAddress, senderAddress, new BigNumber(42),
|
||||
)).to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
|
||||
)).to.be.rejectedWith(ZeroExError.TokenContractDoesNotExist);
|
||||
});
|
||||
});
|
||||
describe('#getBalanceAsync', () => {
|
||||
@@ -169,7 +175,7 @@ describe('TokenWrapper', () => {
|
||||
const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065';
|
||||
const ownerAddress = coinbase;
|
||||
return expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, ownerAddress))
|
||||
.to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
|
||||
.to.be.rejectedWith(ZeroExError.TokenContractDoesNotExist);
|
||||
});
|
||||
it('should return a balance of 0 for a non-existent owner address', async () => {
|
||||
const token = tokens[0];
|
||||
@@ -184,7 +190,7 @@ describe('TokenWrapper', () => {
|
||||
before(async () => {
|
||||
const hasAddresses = false;
|
||||
const web3WithoutAccounts = web3Factory.create(hasAddresses);
|
||||
zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider);
|
||||
zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config);
|
||||
});
|
||||
it('should return balance even when called with Web3 provider instance without addresses', async () => {
|
||||
const token = tokens[0];
|
||||
@@ -281,7 +287,7 @@ describe('TokenWrapper', () => {
|
||||
before(async () => {
|
||||
const hasAddresses = false;
|
||||
const web3WithoutAccounts = web3Factory.create(hasAddresses);
|
||||
zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider);
|
||||
zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider, config);
|
||||
});
|
||||
it('should get the proxy allowance', async () => {
|
||||
const token = tokens[0];
|
||||
@@ -361,10 +367,11 @@ describe('TokenWrapper', () => {
|
||||
(async () => {
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<TransferContractEventArgs>) => {
|
||||
expect(logEvent).to.not.be.undefined();
|
||||
expect(logEvent.logIndex).to.be.equal(0);
|
||||
expect(logEvent.transactionIndex).to.be.equal(0);
|
||||
expect(logEvent.blockNumber).to.be.a('number');
|
||||
const args = logEvent.args;
|
||||
expect(logEvent.isRemoved).to.be.false();
|
||||
expect(logEvent.log.logIndex).to.be.equal(0);
|
||||
expect(logEvent.log.transactionIndex).to.be.equal(0);
|
||||
expect(logEvent.log.blockNumber).to.be.a('number');
|
||||
const args = logEvent.log.args;
|
||||
expect(args._from).to.be.equal(coinbase);
|
||||
expect(args._to).to.be.equal(addressWithoutFunds);
|
||||
expect(args._value).to.be.bignumber.equal(transferAmount);
|
||||
@@ -379,7 +386,8 @@ describe('TokenWrapper', () => {
|
||||
(async () => {
|
||||
const callback = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
|
||||
expect(logEvent).to.not.be.undefined();
|
||||
const args = logEvent.args;
|
||||
expect(logEvent.isRemoved).to.be.false();
|
||||
const args = logEvent.log.args;
|
||||
expect(args._owner).to.be.equal(coinbase);
|
||||
expect(args._spender).to.be.equal(addressWithoutFunds);
|
||||
expect(args._value).to.be.bignumber.equal(allowanceAmount);
|
||||
@@ -390,7 +398,7 @@ describe('TokenWrapper', () => {
|
||||
await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => {
|
||||
it('Outstanding subscriptions are cancelled when zeroEx.setProvider called', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
|
||||
done(new Error('Expected this subscription to have been cancelled'));
|
||||
@@ -402,7 +410,7 @@ describe('TokenWrapper', () => {
|
||||
done();
|
||||
};
|
||||
const newProvider = web3Factory.getRpcProvider();
|
||||
await zeroEx.setProviderAsync(newProvider);
|
||||
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
|
||||
zeroEx.token.subscribe(
|
||||
tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackToBeCalled,
|
||||
);
|
||||
@@ -426,14 +434,14 @@ describe('TokenWrapper', () => {
|
||||
let tokenAddress: string;
|
||||
let tokenTransferProxyAddress: string;
|
||||
const subscriptionOpts: SubscriptionOpts = {
|
||||
fromBlock: BlockParamLiteral.Earliest,
|
||||
fromBlock: 0,
|
||||
toBlock: BlockParamLiteral.Latest,
|
||||
};
|
||||
let txHash: string;
|
||||
before(async () => {
|
||||
before(() => {
|
||||
const token = tokens[0];
|
||||
tokenAddress = token.address;
|
||||
tokenTransferProxyAddress = await zeroEx.proxy.getContractAddressAsync();
|
||||
tokenTransferProxyAddress = zeroEx.proxy.getContractAddress();
|
||||
});
|
||||
it('should get logs with decoded args emitted by Approval', async () => {
|
||||
txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(tokenAddress, coinbase);
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import * as chai from 'chai';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import ChaiBigNumber = require('chai-bignumber');
|
||||
import chaiAsPromised = require('chai-as-promised');
|
||||
import ChaiBigNumber = require('chai-bignumber');
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
|
||||
export const chaiSetup = {
|
||||
configure() {
|
||||
|
@@ -2,7 +2,11 @@ export const constants = {
|
||||
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
|
||||
RPC_HOST: 'localhost',
|
||||
RPC_PORT: 8545,
|
||||
ROPSTEN_NETWORK_ID: 3,
|
||||
KOVAN_NETWORK_ID: 42,
|
||||
TESTRPC_NETWORK_ID: 50,
|
||||
KOVAN_RPC_URL: 'https://kovan.infura.io',
|
||||
ROPSTEN_RPC_URL: 'https://ropsten.infura.io',
|
||||
ZRX_DECIMALS: 18,
|
||||
GAS_ESTIMATE: 500000,
|
||||
};
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {ZeroEx, Token, SignedOrder} from '../../src';
|
||||
|
||||
import {SignedOrder, Token, ZeroEx} from '../../src';
|
||||
import {orderFactory} from '../utils/order_factory';
|
||||
|
||||
import {constants} from './constants';
|
||||
|
||||
export class FillScenarios {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {ZeroEx, SignedOrder} from '../../src';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {SignedOrder, ZeroEx} from '../../src';
|
||||
|
||||
export const orderFactory = {
|
||||
async createSignedOrderAsync(
|
||||
@@ -15,11 +16,11 @@ export const orderFactory = {
|
||||
takerTokenAddress: string,
|
||||
exchangeContractAddress: string,
|
||||
feeRecipient: string,
|
||||
expirationUnixTimestampSec?: BigNumber): Promise<SignedOrder> {
|
||||
expirationUnixTimestampSecIfExists?: BigNumber): Promise<SignedOrder> {
|
||||
const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite
|
||||
expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ?
|
||||
const expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSecIfExists) ?
|
||||
defaultExpirationUnixTimestampSec :
|
||||
expirationUnixTimestampSec;
|
||||
expirationUnixTimestampSecIfExists;
|
||||
const order = {
|
||||
maker,
|
||||
taker,
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import { DoneCallback } from '../../src/types';
|
||||
|
||||
export const reportCallbackErrors = (done: DoneCallback) => {
|
||||
return (fAsync: (...args: any[]) => void|Promise<void>) => {
|
||||
return (f: (...args: any[]) => void) => {
|
||||
const wrapped = async (...args: any[]) => {
|
||||
try {
|
||||
await fAsync(...args);
|
||||
f(...args);
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import * as request from 'request-promise-native';
|
||||
|
||||
import {constants} from './constants';
|
||||
|
||||
export class RPC {
|
||||
|
@@ -1,11 +1,13 @@
|
||||
import {JSONRPCPayload} from '../types';
|
||||
import {JSONRPCPayload} from '../../../src/types';
|
||||
|
||||
/*
|
||||
* This class implements the web3-provider-engine subprovider interface and returns
|
||||
* that the provider has no addresses when queried.
|
||||
* Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js
|
||||
*/
|
||||
export class EmptyWalletSubProvider {
|
||||
export class EmptyWalletSubprovider {
|
||||
// This method needs to be here to satisfy the interface but linter wants it to be static.
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
public handleRequest(payload: JSONRPCPayload, next: () => void, end: (err: Error|null, result: any) => void) {
|
||||
switch (payload.method) {
|
||||
case 'eth_accounts':
|
||||
@@ -18,6 +20,7 @@ export class EmptyWalletSubProvider {
|
||||
}
|
||||
}
|
||||
// Required to implement this method despite not needing it for this subprovider
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
public setEngine(engine: any) {
|
||||
// noop
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
import {JSONRPCPayload} from '../../../src/types';
|
||||
|
||||
/*
|
||||
* This class implements the web3-provider-engine subprovider interface and returns
|
||||
* the constant gas estimate when queried.
|
||||
* HACK: We need this so that our tests don't use testrpc gas estimation which sometimes kills the node.
|
||||
* Source: https://github.com/trufflesuite/ganache-cli/issues/417
|
||||
* Source: https://github.com/trufflesuite/ganache-cli/issues/437
|
||||
* Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js
|
||||
*/
|
||||
export class FakeGasEstimateSubprovider {
|
||||
private constantGasAmount: number;
|
||||
constructor(constantGasAmount: number) {
|
||||
this.constantGasAmount = constantGasAmount;
|
||||
}
|
||||
// This method needs to be here to satisfy the interface but linter wants it to be static.
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
public handleRequest(payload: JSONRPCPayload, next: () => void, end: (err: Error|null, result: any) => void) {
|
||||
switch (payload.method) {
|
||||
case 'eth_estimateGas':
|
||||
end(null, this.constantGasAmount);
|
||||
return;
|
||||
|
||||
default:
|
||||
next();
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Required to implement this method despite not needing it for this subprovider
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
public setEngine(engine: any) {
|
||||
// noop
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
import * as _ from 'lodash';
|
||||
import {Token, InternalZeroExError} from '../../src/types';
|
||||
|
||||
import {InternalZeroExError, Token} from '../../src/types';
|
||||
|
||||
const PROTOCOL_TOKEN_SYMBOL = 'ZRX';
|
||||
|
||||
|
@@ -3,11 +3,14 @@
|
||||
// we are not running in a browser env.
|
||||
// Filed issue: https://github.com/ethereum/web3.js/issues/844
|
||||
(global as any).XMLHttpRequest = undefined;
|
||||
import * as Web3 from 'web3';
|
||||
import ProviderEngine = require('web3-provider-engine');
|
||||
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
|
||||
import * as Web3 from 'web3';
|
||||
|
||||
import {EmptyWalletSubprovider} from './subproviders/empty_wallet_subprovider';
|
||||
import {FakeGasEstimateSubprovider} from './subproviders/fake_gas_estimate_subprovider';
|
||||
|
||||
import {constants} from './constants';
|
||||
import {EmptyWalletSubProvider} from '../../src/subproviders/empty_wallet_subprovider';
|
||||
|
||||
export const web3Factory = {
|
||||
create(hasAddresses: boolean = true): Web3 {
|
||||
@@ -20,8 +23,9 @@ export const web3Factory = {
|
||||
const provider = new ProviderEngine();
|
||||
const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`;
|
||||
if (!hasAddresses) {
|
||||
provider.addProvider(new EmptyWalletSubProvider());
|
||||
provider.addProvider(new EmptyWalletSubprovider());
|
||||
}
|
||||
provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_ESTIMATE));
|
||||
provider.addProvider(new RpcSubprovider({
|
||||
rpcUrl,
|
||||
}));
|
||||
|
@@ -1,29 +0,0 @@
|
||||
import * as chai from 'chai';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {ZeroEx} from '../src/';
|
||||
import {Web3Wrapper} from '../src/web3_wrapper';
|
||||
import {constants} from './utils/constants';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('Web3Wrapper', () => {
|
||||
const web3Provider = web3Factory.create().currentProvider;
|
||||
describe('#getNetworkIdIfExistsAsync', () => {
|
||||
it('caches network id requests', async () => {
|
||||
const web3Wrapper = (new ZeroEx(web3Provider) as any)._web3Wrapper as Web3Wrapper;
|
||||
expect((web3Wrapper as any).networkIdIfExists).to.be.undefined();
|
||||
const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync();
|
||||
expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID);
|
||||
});
|
||||
it('invalidates network id cache on setProvider call', async () => {
|
||||
const web3Wrapper = (new ZeroEx(web3Provider) as any)._web3Wrapper as Web3Wrapper;
|
||||
expect((web3Wrapper as any).networkIdIfExists).to.be.undefined();
|
||||
const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync();
|
||||
expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID);
|
||||
const newProvider = web3Factory.create().currentProvider;
|
||||
web3Wrapper.setProvider(newProvider);
|
||||
expect((web3Wrapper as any).networkIdIfExists).to.be.undefined();
|
||||
});
|
||||
});
|
||||
});
|
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "@0xproject/assert",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.6",
|
||||
"description": "Provides a standard way of performing type and schema validation across 0x projects",
|
||||
"main": "lib/src/index.js",
|
||||
"types": "lib/src/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"clean": "shx rm -rf _bundles lib test_temp",
|
||||
"lint": "tslint src/**/*.ts test/**/*.ts",
|
||||
"lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'",
|
||||
"run_mocha": "mocha lib/test/**/*_test.js",
|
||||
"prepublishOnly": "run-p build",
|
||||
"test": "run-s clean build run_mocha",
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/0xProject/0x.js/packages/assert/README.md",
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@0xproject/tslint-config": "^0.2.0",
|
||||
"@types/lodash": "^4.14.78",
|
||||
"@types/mocha": "^2.2.42",
|
||||
"@types/valid-url": "^1.0.2",
|
||||
@@ -37,7 +37,7 @@
|
||||
"typescript": "^2.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@0xproject/json-schemas": "^0.6.8",
|
||||
"@0xproject/json-schemas": "^0.6.9",
|
||||
"bignumber.js": "~4.1.0",
|
||||
"ethereum-address": "^0.0.4",
|
||||
"lodash": "^4.17.4",
|
||||
|
@@ -1,11 +1,11 @@
|
||||
import {
|
||||
Schema,
|
||||
SchemaValidator,
|
||||
} from '@0xproject/json-schemas';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as ethereum_address from 'ethereum-address';
|
||||
import * as _ from 'lodash';
|
||||
import * as validUrl from 'valid-url';
|
||||
import {
|
||||
SchemaValidator,
|
||||
Schema,
|
||||
} from '@0xproject/json-schemas';
|
||||
|
||||
const HEX_REGEX = /^0x[0-9A-F]*$/i;
|
||||
|
||||
@@ -62,7 +62,7 @@ export const assert = {
|
||||
this.assert(_.isBoolean(value), this.typeAssertionMessage(variableName, 'boolean', value));
|
||||
},
|
||||
isWeb3Provider(variableName: string, value: any): void {
|
||||
const isWeb3Provider = _.isFunction((value as any).send) || _.isFunction((value as any).sendAsync);
|
||||
const isWeb3Provider = _.isFunction((value).send) || _.isFunction((value).sendAsync);
|
||||
this.assert(isWeb3Provider, this.typeAssertionMessage(variableName, 'Web3.Provider', value));
|
||||
},
|
||||
doesConformToSchema(variableName: string, value: any, schema: Schema): void {
|
||||
|
@@ -1,8 +1,9 @@
|
||||
import 'mocha';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import 'mocha';
|
||||
|
||||
import {assert} from '../src/index';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
@@ -183,7 +184,7 @@ describe('Assertions', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
42,
|
||||
0.00,
|
||||
0,
|
||||
21e+42,
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isNumber.bind(assert, variableName, input)).to.not.throw());
|
||||
|
@@ -1,5 +1,11 @@
|
||||
# CHANGELOG
|
||||
|
||||
v0.0.2 - _November 22, 2017_
|
||||
v0.2.0 - _November 29, 2017_
|
||||
------------------------
|
||||
* Add SignedOrder and TokenTradeInfo to the public interface
|
||||
* Add ECSignature and Order to the public interface
|
||||
* Remove dependency on 0x.js
|
||||
|
||||
v0.1.0 - _November 22, 2017_
|
||||
------------------------
|
||||
* Provide a HttpClient class for interacting with standard relayer api compliant HTTP urls
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@0xproject/connect",
|
||||
"version": "0.0.1",
|
||||
"version": "0.2.0",
|
||||
"description": "A javascript library for interacting with the standard relayer api",
|
||||
"keywords": [
|
||||
"connect",
|
||||
@@ -15,9 +15,9 @@
|
||||
"build": "tsc",
|
||||
"clean": "shx rm -rf _bundles lib test_temp",
|
||||
"docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_DIR",
|
||||
"upload_docs_json": "aws s3 cp docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type aplication/json",
|
||||
"upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json",
|
||||
"copy_test_fixtures": "copyfiles -u 2 './test/fixtures/**/*.json' ./lib/test/fixtures",
|
||||
"lint": "tslint src/**/*.ts test/**/*.ts",
|
||||
"lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'",
|
||||
"run_mocha": "mocha lib/test/**/*_test.js",
|
||||
"test": "run-s clean build copy_test_fixtures run_mocha",
|
||||
"test:circleci": "yarn test"
|
||||
@@ -36,9 +36,8 @@
|
||||
},
|
||||
"homepage": "https://github.com/0xProject/0x.js/packages/connect/README.md",
|
||||
"dependencies": {
|
||||
"0x.js": "^0.26.0",
|
||||
"@0xproject/assert": "^0.0.5",
|
||||
"@0xproject/json-schemas": "^0.6.8",
|
||||
"@0xproject/assert": "^0.0.6",
|
||||
"@0xproject/json-schemas": "^0.6.9",
|
||||
"bignumber.js": "~4.1.0",
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"lodash": "^4.17.4",
|
||||
@@ -46,7 +45,7 @@
|
||||
"websocket": "^1.0.25"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@0xproject/tslint-config": "^0.2.0",
|
||||
"@types/fetch-mock": "^5.12.1",
|
||||
"@types/lodash": "^4.14.77",
|
||||
"@types/mocha": "^2.2.42",
|
||||
|
@@ -17,8 +17,9 @@ postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
|
||||
})
|
||||
.then(function(release) {
|
||||
console.log('POSTPUBLISH: Release successful, generating docs...');
|
||||
const jsonFilePath = __dirname + '/../' + postpublish_utils.generatedDocsDirectoryName + '/index.json';
|
||||
return execAsync(
|
||||
'JSON_FILE_PATH=' + __dirname + '/../docs/index.json PROJECT_DIR=' + __dirname + '/.. yarn docs:json',
|
||||
'JSON_FILE_PATH=' + jsonFilePath + ' PROJECT_DIR=' + __dirname + '/.. yarn docs:json',
|
||||
{
|
||||
cwd,
|
||||
}
|
||||
|
@@ -1,21 +1,24 @@
|
||||
import 'isomorphic-fetch';
|
||||
import * as _ from 'lodash';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import * as queryString from 'query-string';
|
||||
import {assert} from '@0xproject/assert';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {SignedOrder} from '0x.js';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import 'isomorphic-fetch';
|
||||
import * as _ from 'lodash';
|
||||
import * as queryString from 'query-string';
|
||||
|
||||
import {schemas as clientSchemas} from './schemas/schemas';
|
||||
import {
|
||||
Client,
|
||||
FeesRequest,
|
||||
FeesResponse,
|
||||
HttpRequestOptions,
|
||||
HttpRequestType,
|
||||
OrderbookRequest,
|
||||
OrderbookResponse,
|
||||
OrdersRequest,
|
||||
SignedOrder,
|
||||
TokenPairsItem,
|
||||
TokenPairsRequest,
|
||||
} from './types';
|
||||
import {schemas as clientSchemas} from './schemas/schemas';
|
||||
import {typeConverters} from './utils/type_converters';
|
||||
|
||||
// TODO: move this and bigNumberConfigs in the 0x.js package into one place
|
||||
@@ -23,16 +26,6 @@ BigNumber.config({
|
||||
EXPONENTIAL_AT: 1000,
|
||||
});
|
||||
|
||||
interface RequestOptions {
|
||||
params?: object;
|
||||
payload?: object;
|
||||
}
|
||||
|
||||
enum RequestType {
|
||||
Get = 'GET',
|
||||
Post = 'POST',
|
||||
}
|
||||
|
||||
/**
|
||||
* This class includes all the functionality related to interacting with a set of HTTP endpoints
|
||||
* that implement the standard relayer API v0
|
||||
@@ -41,7 +34,7 @@ export class HttpClient implements Client {
|
||||
private apiEndpointUrl: string;
|
||||
/**
|
||||
* Instantiates a new HttpClient instance
|
||||
* @param url The base url for making API calls
|
||||
* @param url The relayer API base HTTP url you would like to interact with
|
||||
* @return An instance of HttpClient
|
||||
*/
|
||||
constructor(url: string) {
|
||||
@@ -61,7 +54,7 @@ export class HttpClient implements Client {
|
||||
const requestOpts = {
|
||||
params: request,
|
||||
};
|
||||
const tokenPairs = await this._requestAsync('/token_pairs', RequestType.Get, requestOpts);
|
||||
const tokenPairs = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts);
|
||||
assert.doesConformToSchema(
|
||||
'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema);
|
||||
_.each(tokenPairs, (tokenPair: object) => {
|
||||
@@ -86,7 +79,7 @@ export class HttpClient implements Client {
|
||||
const requestOpts = {
|
||||
params: request,
|
||||
};
|
||||
const orders = await this._requestAsync(`/orders`, RequestType.Get, requestOpts);
|
||||
const orders = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts);
|
||||
assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
|
||||
_.each(orders, (order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order));
|
||||
return orders;
|
||||
@@ -98,7 +91,7 @@ export class HttpClient implements Client {
|
||||
*/
|
||||
public async getOrderAsync(orderHash: string): Promise<SignedOrder> {
|
||||
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
|
||||
const order = await this._requestAsync(`/order/${orderHash}`, RequestType.Get);
|
||||
const order = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get);
|
||||
assert.doesConformToSchema('order', order, schemas.signedOrderSchema);
|
||||
typeConverters.convertOrderStringFieldsToBigNumber(order);
|
||||
return order;
|
||||
@@ -113,7 +106,7 @@ export class HttpClient implements Client {
|
||||
const requestOpts = {
|
||||
params: request,
|
||||
};
|
||||
const orderBook = await this._requestAsync('/orderbook', RequestType.Get, requestOpts);
|
||||
const orderBook = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts);
|
||||
assert.doesConformToSchema('orderBook', orderBook, schemas.relayerApiOrderBookResponseSchema);
|
||||
typeConverters.convertOrderbookStringFieldsToBigNumber(orderBook);
|
||||
return orderBook;
|
||||
@@ -134,7 +127,7 @@ export class HttpClient implements Client {
|
||||
const requestOpts = {
|
||||
payload: request,
|
||||
};
|
||||
const fees = await this._requestAsync('/fees', RequestType.Post, requestOpts);
|
||||
const fees = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts);
|
||||
assert.doesConformToSchema('fees', fees, schemas.relayerApiFeesResponseSchema);
|
||||
typeConverters.convertStringsFieldsToBigNumbers(fees, ['makerFee', 'takerFee']);
|
||||
return fees;
|
||||
@@ -148,9 +141,10 @@ export class HttpClient implements Client {
|
||||
const requestOpts = {
|
||||
payload: signedOrder,
|
||||
};
|
||||
await this._requestAsync('/order', RequestType.Post, requestOpts);
|
||||
await this._requestAsync('/order', HttpRequestType.Post, requestOpts);
|
||||
}
|
||||
private async _requestAsync(path: string, requestType: RequestType, requestOptions?: RequestOptions): Promise<any> {
|
||||
private async _requestAsync(path: string, requestType: HttpRequestType,
|
||||
requestOptions?: HttpRequestOptions): Promise<any> {
|
||||
const params = _.get(requestOptions, 'params');
|
||||
const payload = _.get(requestOptions, 'payload');
|
||||
let query = '';
|
||||
|
@@ -1,11 +1,15 @@
|
||||
export {HttpClient} from './http_client';
|
||||
export {
|
||||
Client,
|
||||
ECSignature,
|
||||
FeesRequest,
|
||||
FeesResponse,
|
||||
Order,
|
||||
OrderbookRequest,
|
||||
OrderbookResponse,
|
||||
OrdersRequest,
|
||||
SignedOrder,
|
||||
TokenPairsItem,
|
||||
TokenPairsRequest,
|
||||
TokenTradeInfo,
|
||||
} from './types';
|
||||
|
@@ -1,6 +1,34 @@
|
||||
import {SignedOrder} from '0x.js';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
// TODO: Consolidate Order, SignedOrder and ECSignature into a shared package instead of duplicating them from 0x.js
|
||||
export interface Order {
|
||||
maker: string;
|
||||
taker: string;
|
||||
makerFee: BigNumber;
|
||||
takerFee: BigNumber;
|
||||
makerTokenAmount: BigNumber;
|
||||
takerTokenAmount: BigNumber;
|
||||
makerTokenAddress: string;
|
||||
takerTokenAddress: string;
|
||||
salt: BigNumber;
|
||||
exchangeContractAddress: string;
|
||||
feeRecipient: string;
|
||||
expirationUnixTimestampSec: BigNumber;
|
||||
}
|
||||
|
||||
export interface SignedOrder extends Order {
|
||||
ecSignature: ECSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Elliptic Curve signature
|
||||
*/
|
||||
export interface ECSignature {
|
||||
v: number;
|
||||
r: string;
|
||||
s: string;
|
||||
}
|
||||
|
||||
export interface Client {
|
||||
getTokenPairsAsync: (request?: TokenPairsRequest) => Promise<TokenPairsItem[]>;
|
||||
getOrdersAsync: (request?: OrdersRequest) => Promise<SignedOrder[]>;
|
||||
@@ -15,6 +43,19 @@ export interface OrderbookChannel {
|
||||
close: () => void;
|
||||
}
|
||||
|
||||
/*
|
||||
* baseTokenAddress: The address of token designated as the baseToken in the currency pair calculation of price
|
||||
* quoteTokenAddress: The address of token designated as the quoteToken in the currency pair calculation of price
|
||||
* snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook
|
||||
* limit: Maximum number of bids and asks in orderbook snapshot
|
||||
*/
|
||||
export interface OrderbookChannelSubscriptionOpts {
|
||||
baseTokenAddress: string;
|
||||
quoteTokenAddress: string;
|
||||
snapshot: boolean;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export interface OrderbookChannelHandler {
|
||||
onSnapshot: (channel: OrderbookChannel, snapshot: OrderbookResponse) => void;
|
||||
onUpdate: (channel: OrderbookChannel, order: SignedOrder) => void;
|
||||
@@ -48,17 +89,15 @@ export interface UnknownOrderbookChannelMessage {
|
||||
payload: undefined;
|
||||
}
|
||||
|
||||
/*
|
||||
* baseTokenAddress: The address of token designated as the baseToken in the currency pair calculation of price
|
||||
* quoteTokenAddress: The address of token designated as the quoteToken in the currency pair calculation of price
|
||||
* snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook
|
||||
* limit: Maximum number of bids and asks in orderbook snapshot
|
||||
*/
|
||||
export interface OrderbookChannelSubscriptionOpts {
|
||||
baseTokenAddress: string;
|
||||
quoteTokenAddress: string;
|
||||
snapshot: boolean;
|
||||
limit: number;
|
||||
export enum WebsocketConnectionEventType {
|
||||
Close = 'close',
|
||||
Error = 'error',
|
||||
Message = 'message',
|
||||
}
|
||||
|
||||
export enum WebsocketClientEventType {
|
||||
Connect = 'connect',
|
||||
ConnectFailed = 'connectFailed',
|
||||
}
|
||||
|
||||
export interface TokenPairsRequest {
|
||||
@@ -118,3 +157,13 @@ export interface FeesResponse {
|
||||
makerFee: BigNumber;
|
||||
takerFee: BigNumber;
|
||||
}
|
||||
|
||||
export interface HttpRequestOptions {
|
||||
params?: object;
|
||||
payload?: object;
|
||||
}
|
||||
|
||||
export enum HttpRequestType {
|
||||
Get = 'GET',
|
||||
Post = 'POST',
|
||||
}
|
||||
|
@@ -1,11 +1,13 @@
|
||||
import * as _ from 'lodash';
|
||||
import {SignedOrder} from '0x.js';
|
||||
import {assert} from '@0xproject/assert';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import {
|
||||
OrderbookChannelMessage,
|
||||
OrderbookChannelMessageTypes,
|
||||
SignedOrder,
|
||||
} from '../types';
|
||||
|
||||
import {typeConverters} from './type_converters';
|
||||
|
||||
export const orderbookChannelMessageParsers = {
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import * as _ from 'lodash';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
// TODO: convert all of these to non-mutating, pure functions
|
||||
export const typeConverters = {
|
||||
|
@@ -1,27 +1,19 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as WebSocket from 'websocket';
|
||||
import {assert} from '@0xproject/assert';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {SignedOrder} from '0x.js';
|
||||
import * as _ from 'lodash';
|
||||
import * as WebSocket from 'websocket';
|
||||
|
||||
import {
|
||||
OrderbookChannel,
|
||||
OrderbookChannelHandler,
|
||||
OrderbookChannelMessageTypes,
|
||||
OrderbookChannelSubscriptionOpts,
|
||||
SignedOrder,
|
||||
WebsocketClientEventType,
|
||||
WebsocketConnectionEventType,
|
||||
} from './types';
|
||||
import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_parsers';
|
||||
|
||||
enum ConnectionEventType {
|
||||
Close = 'close',
|
||||
Error = 'error',
|
||||
Message = 'message',
|
||||
}
|
||||
|
||||
enum ClientEventType {
|
||||
Connect = 'connect',
|
||||
ConnectFailed = 'connectFailed',
|
||||
}
|
||||
|
||||
/**
|
||||
* This class includes all the functionality related to interacting with a websocket endpoint
|
||||
* that implements the standard relayer API v0
|
||||
@@ -63,13 +55,13 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
|
||||
if (!_.isUndefined(error)) {
|
||||
handler.onError(this, error);
|
||||
} else if (!_.isUndefined(connection) && connection.connected) {
|
||||
connection.on(ConnectionEventType.Error, wsError => {
|
||||
connection.on(WebsocketConnectionEventType.Error, wsError => {
|
||||
handler.onError(this, wsError);
|
||||
});
|
||||
connection.on(ConnectionEventType.Close, () => {
|
||||
connection.on(WebsocketConnectionEventType.Close, () => {
|
||||
handler.onClose(this);
|
||||
});
|
||||
connection.on(ConnectionEventType.Message, message => {
|
||||
connection.on(WebsocketConnectionEventType.Message, message => {
|
||||
this._handleWebSocketMessage(message, handler);
|
||||
});
|
||||
connection.sendUTF(JSON.stringify(subscribeMessage));
|
||||
@@ -88,11 +80,11 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
|
||||
if (!_.isUndefined(this.connectionIfExists) && this.connectionIfExists.connected) {
|
||||
callback(undefined, this.connectionIfExists);
|
||||
} else {
|
||||
this.client.on(ClientEventType.Connect, connection => {
|
||||
this.client.on(WebsocketClientEventType.Connect, connection => {
|
||||
this.connectionIfExists = connection;
|
||||
callback(undefined, this.connectionIfExists);
|
||||
});
|
||||
this.client.on(ClientEventType.ConnectFailed, error => {
|
||||
this.client.on(WebsocketClientEventType.ConnectFailed, error => {
|
||||
callback(error, undefined);
|
||||
});
|
||||
this.client.connect(this.apiEndpointUrl);
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
import {FeesResponse} from '../../../src/types';
|
||||
|
||||
export const feesResponse: FeesResponse = {
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
import {TokenPairsItem} from '../../../src/types';
|
||||
|
||||
export const tokenPairsResponse: TokenPairsItem[] = [
|
||||
|
@@ -1,23 +1,25 @@
|
||||
import 'mocha';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as fetchMock from 'fetch-mock';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import 'mocha';
|
||||
|
||||
import {HttpClient} from '../src/index';
|
||||
|
||||
import {feesResponse} from './fixtures/standard_relayer_api/fees';
|
||||
import * as feesResponseJSON from './fixtures/standard_relayer_api/fees.json';
|
||||
import {
|
||||
orderResponse,
|
||||
} from './fixtures/standard_relayer_api/order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
|
||||
import {ordersResponse} from './fixtures/standard_relayer_api/orders';
|
||||
import {tokenPairsResponse} from './fixtures/standard_relayer_api/token_pairs';
|
||||
import {orderbookResponse} from './fixtures/standard_relayer_api/orderbook';
|
||||
import * as feesResponseJSON from './fixtures/standard_relayer_api/fees.json';
|
||||
// tslint:disable-next-line:max-line-length
|
||||
import * as orderResponseJSON from './fixtures/standard_relayer_api/order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f.json';
|
||||
import * as ordersResponseJSON from './fixtures/standard_relayer_api/orders.json';
|
||||
import * as tokenPairsResponseJSON from './fixtures/standard_relayer_api/token_pairs.json';
|
||||
import {orderbookResponse} from './fixtures/standard_relayer_api/orderbook';
|
||||
import * as orderbookJSON from './fixtures/standard_relayer_api/orderbook.json';
|
||||
import {ordersResponse} from './fixtures/standard_relayer_api/orders';
|
||||
import * as ordersResponseJSON from './fixtures/standard_relayer_api/orders.json';
|
||||
import {tokenPairsResponse} from './fixtures/standard_relayer_api/token_pairs';
|
||||
import * as tokenPairsResponseJSON from './fixtures/standard_relayer_api/token_pairs.json';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
|
@@ -1,19 +1,21 @@
|
||||
import 'mocha';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import 'mocha';
|
||||
|
||||
import {orderbookChannelMessageParsers} from '../src/utils/orderbook_channel_message_parsers';
|
||||
import {
|
||||
snapshotOrderbookChannelMessage,
|
||||
malformedSnapshotOrderbookChannelMessage,
|
||||
} from './fixtures/standard_relayer_api/snapshot_orderbook_channel_message';
|
||||
import {
|
||||
updateOrderbookChannelMessage,
|
||||
malformedUpdateOrderbookChannelMessage,
|
||||
} from './fixtures/standard_relayer_api/update_orderbook_channel_message';
|
||||
import {unknownOrderbookChannelMessage} from './fixtures/standard_relayer_api/unknown_orderbook_channel_message';
|
||||
import {orderbookResponse} from './fixtures/standard_relayer_api/orderbook';
|
||||
|
||||
// tslint:disable-next-line:max-line-length
|
||||
import {orderResponse} from './fixtures/standard_relayer_api/order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
|
||||
import {orderbookResponse} from './fixtures/standard_relayer_api/orderbook';
|
||||
import {
|
||||
malformedSnapshotOrderbookChannelMessage,
|
||||
snapshotOrderbookChannelMessage,
|
||||
} from './fixtures/standard_relayer_api/snapshot_orderbook_channel_message';
|
||||
import {unknownOrderbookChannelMessage} from './fixtures/standard_relayer_api/unknown_orderbook_channel_message';
|
||||
import {
|
||||
malformedUpdateOrderbookChannelMessage,
|
||||
updateOrderbookChannelMessage,
|
||||
} from './fixtures/standard_relayer_api/update_orderbook_channel_message';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
|
@@ -1,7 +1,8 @@
|
||||
import 'mocha';
|
||||
import * as _ from 'lodash';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
|
||||
import {
|
||||
WebSocketOrderbookChannel,
|
||||
} from '../src/ws_orderbook_channel';
|
||||
|
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "@0xproject/json-schemas",
|
||||
"version": "0.6.8",
|
||||
"version": "0.6.9",
|
||||
"description": "0x-related json schemas",
|
||||
"main": "lib/src/index.js",
|
||||
"types": "lib/src/index.d.ts",
|
||||
"scripts": {
|
||||
"lint": "tslint src/*.ts test/*.ts",
|
||||
"lint": "tslint --project . src/*.ts test/*.ts",
|
||||
"test": "run-s clean build run_mocha",
|
||||
"test:circleci": "yarn test",
|
||||
"run_mocha": "mocha lib/test/**/*_test.js",
|
||||
@@ -28,7 +28,7 @@
|
||||
"lodash.values": "^4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@0xproject/tslint-config": "^0.2.0",
|
||||
"@types/lodash.foreach": "^4.5.3",
|
||||
"@types/lodash.values": "^4.3.3",
|
||||
"@types/mocha": "^2.2.42",
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import {Schema, Validator, ValidatorResult} from 'jsonschema';
|
||||
import values = require('lodash.values');
|
||||
import {Validator, ValidatorResult, Schema} from 'jsonschema';
|
||||
|
||||
import {schemas} from './schemas';
|
||||
|
||||
export class SchemaValidator {
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import {
|
||||
numberSchema,
|
||||
addressSchema,
|
||||
numberSchema,
|
||||
} from '../schemas/basic_type_schemas';
|
||||
import {
|
||||
ecSignatureSchema,
|
||||
ecSignatureParameterSchema,
|
||||
ecSignatureSchema,
|
||||
} from '../schemas/ec_signature_schema';
|
||||
import {
|
||||
indexFilterValuesSchema,
|
||||
@@ -25,25 +25,26 @@ import {
|
||||
orderSchema,
|
||||
signedOrderSchema,
|
||||
} from '../schemas/order_schemas';
|
||||
import {
|
||||
blockParamSchema,
|
||||
subscriptionOptsSchema,
|
||||
} from '../schemas/subscription_opts_schema';
|
||||
import {
|
||||
tokenSchema,
|
||||
} from '../schemas/token_schema';
|
||||
import {
|
||||
signedOrdersSchema,
|
||||
} from '../schemas/signed_orders_schema';
|
||||
import {
|
||||
relayerApiErrorResponseSchema,
|
||||
} from '../schemas/relayer_api_error_response_schema';
|
||||
import {
|
||||
relayerApiFeesPayloadSchema,
|
||||
} from '../schemas/relayer_api_fees_payload_schema';
|
||||
import {
|
||||
relayerApiFeesResponseSchema,
|
||||
} from '../schemas/relayer_api_fees_response_schema';
|
||||
import {
|
||||
relayerApiFeesPayloadSchema,
|
||||
} from '../schemas/relayer_api_fees_payload_schema';
|
||||
relayerApiOrderbookChannelSubscribePayload,
|
||||
relayerApiOrderbookChannelSubscribeSchema,
|
||||
} from '../schemas/relayer_api_orberbook_channel_subscribe_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelSnapshotPayload,
|
||||
relayerApiOrderbookChannelSnapshotSchema,
|
||||
} from '../schemas/relayer_api_orderbook_channel_snapshot_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelUpdateSchema,
|
||||
} from '../schemas/relayer_api_orderbook_channel_update_response_schema';
|
||||
import {
|
||||
relayerApiOrderBookResponseSchema,
|
||||
} from '../schemas/relayer_api_orderbook_response_schema';
|
||||
@@ -51,21 +52,20 @@ import {
|
||||
relayerApiTokenPairsResponseSchema,
|
||||
relayerApiTokenTradeInfoSchema,
|
||||
} from '../schemas/relayer_api_token_pairs_response_schema';
|
||||
import {
|
||||
signedOrdersSchema,
|
||||
} from '../schemas/signed_orders_schema';
|
||||
import {
|
||||
blockParamSchema,
|
||||
subscriptionOptsSchema,
|
||||
} from '../schemas/subscription_opts_schema';
|
||||
import {
|
||||
tokenSchema,
|
||||
} from '../schemas/token_schema';
|
||||
import {
|
||||
jsNumber,
|
||||
txDataSchema,
|
||||
} from '../schemas/tx_data_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelSubscribeSchema,
|
||||
relayerApiOrderbookChannelSubscribePayload,
|
||||
} from '../schemas/relayer_api_orberbook_channel_subscribe_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelUpdateSchema,
|
||||
} from '../schemas/relayer_api_orderbook_channel_update_response_schema';
|
||||
import {
|
||||
relayerApiOrderbookChannelSnapshotSchema,
|
||||
relayerApiOrderbookChannelSnapshotPayload,
|
||||
} from '../schemas/relayer_api_orderbook_channel_snapshot_schema';
|
||||
|
||||
export const schemas = {
|
||||
numberSchema,
|
||||
|
@@ -1,10 +1,11 @@
|
||||
import 'mocha';
|
||||
import forEach = require('lodash.foreach');
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import * as chai from 'chai';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import promisify = require('es6-promisify');
|
||||
import {SchemaValidator, schemas} from '../src/index';
|
||||
import forEach = require('lodash.foreach');
|
||||
import 'mocha';
|
||||
|
||||
import {schemas, SchemaValidator} from '../src/index';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
@@ -969,4 +970,4 @@ describe('Schema', () => {
|
||||
validateAgainstSchema(testCases, txDataSchema, shouldFail);
|
||||
});
|
||||
});
|
||||
});
|
||||
}); // tslint:disable:max-file-line-count
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@0xproject/tslint-config",
|
||||
"version": "0.1.1",
|
||||
"version": "0.2.0",
|
||||
"description": "Lint rules related to 0xProject for TSLint",
|
||||
"main": "tslint.json",
|
||||
"files": [
|
||||
|
@@ -4,27 +4,79 @@
|
||||
"tslint-react"
|
||||
],
|
||||
"rules": {
|
||||
"adjacent-overload-signatures": true,
|
||||
"arrow-parens": [true, "ban-single-arg-parens"],
|
||||
"ordered-imports": false,
|
||||
"quotemark": [true, "single", "avoid-escape", "jsx-double"],
|
||||
"arrow-return-shorthand": true,
|
||||
"await-promise": true,
|
||||
"binary-expression-operand-order": true,
|
||||
"callable-types": true,
|
||||
"class-name": true,
|
||||
"completed-docs": [
|
||||
true,
|
||||
{
|
||||
"functions": {"visibilities": ["exported"]},
|
||||
"methods": {"locations": "instance", "privacies": ["public", "protected"]}
|
||||
}
|
||||
],
|
||||
"curly": true,
|
||||
"eofline": true,
|
||||
"encoding": true,
|
||||
"import-spacing": true,
|
||||
"indent": [true, "spaces", 4],
|
||||
"interface-name": false,
|
||||
"interface-over-type-literal": true,
|
||||
"object-literal-sort-keys": false,
|
||||
"linebreak-style": [true, "LF"],
|
||||
"max-classes-per-file": false,
|
||||
"max-classes-per-file": [true, 1],
|
||||
"max-file-line-count": [true, 500],
|
||||
"max-line-length": [true, 120],
|
||||
"member-access": true,
|
||||
"member-ordering": [true,
|
||||
"public-before-private",
|
||||
"static-before-instance",
|
||||
"variables-before-functions"
|
||||
],
|
||||
"newline-before-return": false,
|
||||
"new-parens": true,
|
||||
"no-angle-bracket-type-assertion": true,
|
||||
"no-boolean-literal-compare": true,
|
||||
"no-default-export": true,
|
||||
"no-empty-interface": false,
|
||||
"no-floating-promises": true,
|
||||
"no-non-null-assertion": true,
|
||||
"no-parameter-reassignment": true,
|
||||
"no-redundant-jsdoc": true,
|
||||
"no-return-await": true,
|
||||
"no-string-throw": true,
|
||||
"no-submodule-imports": false,
|
||||
"no-unnecessary-type-assertion": true,
|
||||
"no-implicit-dependencies": [true, "dev"],
|
||||
"number-literal-format": true,
|
||||
"object-literal-sort-keys": false,
|
||||
"ordered-imports": [
|
||||
true,
|
||||
{
|
||||
"grouped-imports": true
|
||||
}
|
||||
],
|
||||
"prefer-const": true,
|
||||
"prefer-for-of": true,
|
||||
"prefer-function-over-method": true,
|
||||
"promise-function-async": true,
|
||||
"quotemark": [true, "single", "avoid-escape", "jsx-double"],
|
||||
"semicolon": [true, "always"],
|
||||
"space-before-function-paren": [
|
||||
true,
|
||||
{
|
||||
"anonymous": "never",
|
||||
"named": "never",
|
||||
"method": "never",
|
||||
"constructor": "never",
|
||||
"asyncArrow": "always"
|
||||
}
|
||||
],
|
||||
"space-within-parens": false,
|
||||
"type-literal-delimiter": true,
|
||||
"variable-name": [true,
|
||||
"ban-keywords",
|
||||
"allow-pascal-case"
|
||||
|
60
packages/website/README.md
Normal file
60
packages/website/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
<img src="https://github.com/0xProject/branding/blob/master/0x_Black_CMYK.png" width="200px" >
|
||||
|
||||
---
|
||||
|
||||
[0x][website-url] is an open protocol that facilitates trustless, low friction exchange of Ethereum-based assets. A full description of the protocol may be found in our [whitepaper][whitepaper-url].
|
||||
|
||||
This repository contains our website and [0x Portal DApp][portal-url] (over-the-counter exchange), facilitating trustless over-the-counter trading of Ethereum-based tokens using 0x protocol.
|
||||
|
||||
[website-url]: https://0xproject.com/
|
||||
[whitepaper-url]: https://0xproject.com/pdfs/0x_white_paper.pdf
|
||||
[portal-url]: https://0xproject.com/portal
|
||||
|
||||
[](https://gitter.im/0xProject/contracts?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
|
||||
### Local Dev Setup
|
||||
|
||||
Requires Node version 6.9.5 or higher.
|
||||
|
||||
Add the following to your `/etc/hosts` file:
|
||||
|
||||
```
|
||||
127.0.0.1 0xproject.dev
|
||||
```
|
||||
|
||||
Clone the [0x contracts repo](https://github.com/0xProject/contracts) into the same parent directory as this project.
|
||||
|
||||
Install [yarn](https://yarnpkg.com/lang/en/docs/install/) in order to install the project dependencies more deterministically.
|
||||
|
||||
Install dependencies:
|
||||
|
||||
```
|
||||
yarn
|
||||
```
|
||||
|
||||
Import smart contract artifacts from `contracts` repo:
|
||||
|
||||
```
|
||||
yarn run update_contracts
|
||||
```
|
||||
|
||||
Start dev server:
|
||||
|
||||
```
|
||||
yarn run dev
|
||||
```
|
||||
|
||||
Visit [0xproject.dev:3572](http://0xproject.dev:3572) in your browser.
|
||||
|
||||
|
||||
##### Recommended Atom packages:
|
||||
|
||||
- [atom-typescript](https://atom.io/packages/atom-typescript)
|
||||
- [linter-tslint](https://atom.io/packages/linter-tslint)
|
||||
|
||||
##### Resources
|
||||
|
||||
- [Material Design Icon Font](http://zavoloklom.github.io/material-design-iconic-font/icons.html#directional)
|
||||
- [BassCSS toolkit](http://basscss.com/)
|
||||
- [Material-UI](http://www.material-ui.com/#/)
|
189
packages/website/contracts/Mintable.json
Normal file
189
packages/website/contracts/Mintable.json
Normal file
@@ -0,0 +1,189 @@
|
||||
{
|
||||
"contract_name": "Mintable",
|
||||
"abi": [
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "approve",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"constant": true,
|
||||
"inputs": [],
|
||||
"name": "totalSupply",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_from",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "transferFrom",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"constant": true,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "balanceOf",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "mint",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "transfer",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"constant": true,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "allowance",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_from",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_to",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Transfer",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_owner",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"name": "_spender",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "_value",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "Approval",
|
||||
"type": "event"
|
||||
}
|
||||
],
|
||||
"unlinked_binary": "0x6060604052341561000c57fe5b5b6105018061001c6000396000f300606060405236156100675763ffffffff60e060020a600035041663095ea7b3811461006957806318160ddd1461009c57806323b872dd146100be57806370a08231146100f7578063a0712d6814610125578063a9059cbb1461013a578063dd62ed3e1461016d575bfe5b341561007157fe5b610088600160a060020a03600435166024356101a1565b604080519115158252519081900360200190f35b34156100a457fe5b6100ac61020c565b60408051918252519081900360200190f35b34156100c657fe5b610088600160a060020a0360043581169060243516604435610212565b604080519115158252519081900360200190f35b34156100ff57fe5b6100ac600160a060020a0360043516610335565b60408051918252519081900360200190f35b341561012d57fe5b610138600435610354565b005b341561014257fe5b610088600160a060020a03600435166024356103bc565b604080519115158252519081900360200190f35b341561017557fe5b6100ac600160a060020a036004358116906024351661046e565b60408051918252519081900360200190f35b600160a060020a03338116600081815260016020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a35060015b92915050565b60025481565b600160a060020a03808416600081815260016020908152604080832033909516835293815283822054928252819052918220548390108015906102555750828110155b801561027b5750600160a060020a03841660009081526020819052604090205483810110155b1561032757600160a060020a03808516600090815260208190526040808220805487019055918716815220805484900390556000198110156102e557600160a060020a03808616600090815260016020908152604080832033909416835292905220805484900390555b83600160a060020a031685600160a060020a03166000805160206104b6833981519152856040518082815260200191505060405180910390a36001915061032c565b600091505b5b509392505050565b600160a060020a0381166000908152602081905260409020545b919050565b68056bc75e2d6310000081111561036b5760006000fd5b600160a060020a03331660009081526020819052604090205461038f90829061049b565b600160a060020a0333166000908152602081905260409020556002546103b5908261049b565b6002555b50565b600160a060020a0333166000908152602081905260408120548290108015906103ff5750600160a060020a03831660009081526020819052604090205482810110155b1561045f57600160a060020a0333811660008181526020818152604080832080548890039055938716808352918490208054870190558351868152935191936000805160206104b6833981519152929081900390910190a3506001610206565b506000610206565b5b92915050565b600160a060020a038083166000908152600160209081526040808320938516835292905220545b92915050565b6000828201838110156104aa57fe5b8091505b50929150505600ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa165627a7a72305820998c8326b9629e063eb4867166e72c68a8c2e3ebca6a9d35ebc78c041c7aa47b0029",
|
||||
"networks": {},
|
||||
"schema_version": "0.0.5",
|
||||
"updated_at": 1503413048892
|
||||
}
|
136
packages/website/less/all.less
Normal file
136
packages/website/less/all.less
Normal file
@@ -0,0 +1,136 @@
|
||||
body {
|
||||
font-family: 'Roboto';
|
||||
}
|
||||
|
||||
.robotoMono {
|
||||
font-family: 'Roboto Mono';
|
||||
}
|
||||
|
||||
a {
|
||||
color: black;
|
||||
}
|
||||
|
||||
#faq {
|
||||
li {
|
||||
padding-bottom: 5px;
|
||||
}
|
||||
|
||||
a {
|
||||
color: rgb(66, 66, 66);
|
||||
}
|
||||
}
|
||||
|
||||
#landing {
|
||||
.h1, .h2, .h3, .h4 {
|
||||
font-family: 'Roboto Mono';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#portal {
|
||||
h1, h2, h3, h4 {
|
||||
font-weight: 100;
|
||||
}
|
||||
}
|
||||
|
||||
#documentation {
|
||||
p {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.comment {
|
||||
p {
|
||||
margin: 0px;
|
||||
}
|
||||
}
|
||||
|
||||
.typeTooltip {
|
||||
border: 1px solid lightgray;
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds always visible scrollbars on OSX so that user knows the content is scrollable
|
||||
* Source: https://davidwalsh.name/osx-overflow
|
||||
*/
|
||||
::-webkit-scrollbar {
|
||||
-webkit-appearance: none;
|
||||
width: 7px;
|
||||
}
|
||||
::-webkit-scrollbar-thumb {
|
||||
border-radius: 4px;
|
||||
background-color: rgba(0, 0, 0, .5);
|
||||
-webkit-box-shadow: 0 0 1px rgba(255, 255, 255, .5);
|
||||
}
|
||||
|
||||
// Hack: For some reason the animation applied to the material-ui textfield causes the overflow
|
||||
// applied to other elements to fail while the animation is underway. Adding this class to the
|
||||
// affected component fixes the issue
|
||||
// Source: http://stackoverflow.com/questions/14383632/webkit-border-radius-and-overflow-bug-when-using-any-animation-transition
|
||||
.transitionFix {
|
||||
-webkit-backface-visibility: hidden;
|
||||
-moz-backface-visibility: hidden;
|
||||
-webkit-transform: translate3d(0, 0, 0);
|
||||
-moz-transform: translate3d(0, 0, 0);
|
||||
}
|
||||
|
||||
.thin {
|
||||
font-weight: 100;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: 'Roboto';
|
||||
background-color: #f3f4f4;
|
||||
color: rgb(36, 41, 46);
|
||||
padding: 3px;
|
||||
|
||||
&.hljs {
|
||||
background-color: #dde4e9 !important; // blue gray
|
||||
border-left: 5px solid #0091EA !important; // colors.lightBlueA700
|
||||
padding: 30px;
|
||||
}
|
||||
}
|
||||
|
||||
#wiki {
|
||||
p, blockquote, ul, ol, dl, li, table, pre {
|
||||
margin: 15px 0;
|
||||
}
|
||||
|
||||
ol, ul {
|
||||
padding-bottom: 20px;
|
||||
}
|
||||
|
||||
table {
|
||||
padding: 0;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
table tr {
|
||||
border-top: 1px solid #cccccc;
|
||||
background-color: white;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
table tr:nth-child(2n) {
|
||||
background-color: #f8f8f8;
|
||||
}
|
||||
table tr th {
|
||||
font-weight: bold;
|
||||
border: 1px solid #cccccc;
|
||||
text-align: left;
|
||||
margin: 0;
|
||||
padding: 6px 13px;
|
||||
}
|
||||
table tr td {
|
||||
border: 1px solid #cccccc;
|
||||
text-align: left;
|
||||
margin: 0;
|
||||
padding: 6px 13px;
|
||||
}
|
||||
table tr th :first-child, table tr td :first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
table tr th :last-child, table tr td :last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
23
packages/website/md/docs/0xjs/async.md
Normal file
23
packages/website/md/docs/0xjs/async.md
Normal file
@@ -0,0 +1,23 @@
|
||||
0x.js is a promise-based library. This means that whenever an asynchronous call is required, the library method will return a native Javascript promise. You can therefore choose between using `promise` or `async/await` syntax when calling our async methods.
|
||||
|
||||
*Async/await syntax (recommended):*
|
||||
```javascript
|
||||
try {
|
||||
var availableAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
} catch (error) {
|
||||
console.log('Caught error: ', error);
|
||||
}
|
||||
```
|
||||
|
||||
*Promise syntax:*
|
||||
```javascript
|
||||
zeroEx.getAvailableAddressesAsync()
|
||||
.then(function(availableAddresses) {
|
||||
console.log(availableAddresses);
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.log('Caught error: ', error);
|
||||
});
|
||||
```
|
||||
|
||||
As is the convention with promise-based libraries, if an error occurs, it is thrown. It is the callers responsibility to catch thrown errors and to handle them appropriately.
|
1
packages/website/md/docs/0xjs/errors.md
Normal file
1
packages/website/md/docs/0xjs/errors.md
Normal file
@@ -0,0 +1 @@
|
||||
All error messages thrown by the 0x.js library are part of a documented string enum. Each error message is in all-caps, snake-case format. This makes the error messages easily identifiable, unique and grep-able. The error enums listing all possible errors the library could throw can be found in the `Types` section.
|
31
packages/website/md/docs/0xjs/installation.md
Normal file
31
packages/website/md/docs/0xjs/installation.md
Normal file
@@ -0,0 +1,31 @@
|
||||
0x.js ships as both a [UMD](https://github.com/umdjs/umd) module and a [CommonJS](https://en.wikipedia.org/wiki/CommonJS) package.
|
||||
|
||||
#### CommonJS *(recommended)*:
|
||||
|
||||
**Install**
|
||||
|
||||
```bash
|
||||
npm install 0x.js --save
|
||||
```
|
||||
|
||||
**Import**
|
||||
|
||||
```javascript
|
||||
import {ZeroEx} from '0x.js';
|
||||
```
|
||||
|
||||
#### UMD:
|
||||
|
||||
**Install**
|
||||
|
||||
Download the UMD module from our [releases page](https://github.com/0xProject/0x.js/releases) and add it to your project.
|
||||
|
||||
**Import**
|
||||
|
||||
```html
|
||||
<script type="text/javascript" src="0x.js"></script>
|
||||
```
|
||||
|
||||
### Wiki
|
||||
|
||||
Check out our [wiki](https://0xproject.com/wiki) for articles on how to get 0x.js setup with TestRPC, Infura and more!
|
1
packages/website/md/docs/0xjs/introduction.md
Normal file
1
packages/website/md/docs/0xjs/introduction.md
Normal file
@@ -0,0 +1 @@
|
||||
Welcome to the [0x.js](https://github.com/0xProject/0x.js) documentation! 0x.js is a Javascript library for interacting with the 0x protocol. With it, you can easily make calls to the 0x smart contracts as well as any ERC20 token. Functionality includes generating, signing, filling and cancelling orders, verifying an orders signature, setting or checking a users ERC20 token balance/allowance and much more.
|
1
packages/website/md/docs/0xjs/versioning.md
Normal file
1
packages/website/md/docs/0xjs/versioning.md
Normal file
@@ -0,0 +1 @@
|
||||
This project adheres to the [Semantic Versioning 2.0.0](http://semver.org/) specification. The library's public interface includes all the methods, properties and types included in the documentation. Since the library is still an alpha, it's public interface is not yet stable and we will introduce backward incompatible changes to the interface without incrementing the major version until the `1.0.0` release. Our convention until then will be to increment the minor version whenever we introduce backward incompatible changes to the public interface, and to increment the patch version otherwise. This way, it is safe for you to include 0x.js in your projects with the tilda (e.g `~0.22.0`) without fear that a patch update would break your app.
|
15
packages/website/md/docs/connect/installation.md
Normal file
15
packages/website/md/docs/connect/installation.md
Normal file
@@ -0,0 +1,15 @@
|
||||
**Install**
|
||||
|
||||
```bash
|
||||
npm install @0xproject/connect --save
|
||||
```
|
||||
|
||||
**Import**
|
||||
|
||||
```javascript
|
||||
import {HttpClient} from '@0xproject/connect';
|
||||
```
|
||||
|
||||
### Wiki
|
||||
|
||||
Check out our [0x Connect introduction tutorial](https://0xproject.com/wiki#Intro-Tutorial:-Connect) for information on how to integrate relayers into your application.
|
1
packages/website/md/docs/connect/introduction.md
Normal file
1
packages/website/md/docs/connect/introduction.md
Normal file
@@ -0,0 +1 @@
|
||||
Welcome to the [0x Connect](https://github.com/0xProject/0x.js/tree/development/packages/connect) documentation! 0x Connect is a Javascript library that makes it easy to interact with relayers that conform to the [Standard Relayer API](https://github.com/0xProject/standard-relayer-api). Functionality includes getting supported token pairs from a relayer, getting orders filtered by different attributes, getting individual orders specified by order hash, getting orderbooks for specific token pairs, getting fee information, and submitting orders.
|
8
packages/website/md/docs/smart_contracts/introduction.md
Normal file
8
packages/website/md/docs/smart_contracts/introduction.md
Normal file
@@ -0,0 +1,8 @@
|
||||
Welcome to the [0x smart contracts](https://github.com/0xProject/contracts) documentation! This documentation is intended for dApp developers who want to integrate 0x exchange functionality directly into their own smart contracts.
|
||||
|
||||
### Helpful wiki articles:
|
||||
|
||||
- [Overview of 0x protocol architecture](https://0xproject.com/wiki#Architecture)
|
||||
- [0x smart contract interactions](https://0xproject.com/wiki#Contract-Interactions)
|
||||
- [Deployed smart contract addresses](https://0xproject.com/wiki#Deployed-Addresses)
|
||||
- [0x protocol message format](https://0xproject.com/wiki#Message-Format)
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user