Merge branch 'development'
* development: (143 commits) Fix connect CHANGELOG version Publish Fix npm auth issues Revert "Publish" Publish Add actual version to CHANGELOG Add blockchainLifecycle management to the ExpirationWatcher test Update connect CHANGELOG.md in preperation for publishing Add TODO comment before BigNumber.config() call Prepare connect package for publishing Last renames Refactor while condition Fix tests Fix a typo in postpublish utils tags -> tag Publish Revert "Publish" Publish Add instanceOf assertion Rename toDecimal to hexToDecimal Add PR numbers ...
This commit is contained in:
@@ -24,7 +24,5 @@ jobs:
|
||||
name: testrpc
|
||||
command: npm run testrpc -- --db testrpc_snapshot
|
||||
background: true
|
||||
- run: yarn lerna:run test:coverage
|
||||
- run: yarn lerna:run report_test_coverage
|
||||
- run: if [ $CIRCLE_BRANCH = "development" ]; then yarn lerna:run test:umd; fi
|
||||
- run: yarn lerna:run test:circleci
|
||||
- run: yarn lerna:run lint
|
||||
|
||||
16
README.md
16
README.md
@@ -6,10 +6,22 @@
|
||||
|
||||
This repository contains all the 0x developer tools written in TypeScript. Our hope is that these tools make it easy to build Relayers and other DApps that use the 0x protocol.
|
||||
|
||||
[website-url]: https://0xproject.com/
|
||||
[whitepaper-url]: https://0xproject.com/pdfs/0x_white_paper.pdf
|
||||
|
||||
[](https://circleci.com/gh/0xProject/0x.js)
|
||||
[](https://badge.fury.io/js/0x.js)
|
||||
[](https://coveralls.io/github/0xProject/0x.js?branch=master)
|
||||
[](http://slack.0xProject.com)
|
||||
[](https://chat.0xproject.com)
|
||||
[](https://gitter.im/0xProject/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://opensource.org/licenses/Apache-2.0)
|
||||
[](https://greenkeeper.io/)
|
||||
|
||||
### Core Packages
|
||||
|
||||
| Package | Version | Description |
|
||||
|--------|-------|------------|
|
||||
| [`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/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 |
|
||||
|
||||
@@ -6,12 +6,17 @@
|
||||
],
|
||||
"scripts": {
|
||||
"testrpc": "testrpc -p 8545 --networkId 50 -m \"${npm_package_config_mnemonic}\"",
|
||||
"lerna:run": "lerna run"
|
||||
"lerna:run": "lerna run",
|
||||
"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"
|
||||
},
|
||||
"devDependencies": {
|
||||
"lerna": "^2.5.1"
|
||||
"lerna": "^2.5.1",
|
||||
"async-child-process": "^1.1.1",
|
||||
"semver-sort": "^0.0.4",
|
||||
"publish-release": "0xproject/publish-release",
|
||||
"es6-promisify": "^5.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
# CHANGELOG
|
||||
|
||||
v0.26.0
|
||||
------------------------
|
||||
* 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)
|
||||
* In OrderWatcher subscribe to ZRX Token Transfer and Approval events when maker token is different (#225)
|
||||
|
||||
v0.25.1 - _November 13, 2017_
|
||||
------------------------
|
||||
* Standardise on Cancelled over Canceled (#217)
|
||||
* Add missing `DecodedLogEvent` type to exported types (#205)
|
||||
* Normalized the transactionReceipt status to be `null|0|1`, 1 meaning transaction execution successful, 0 unsuccessful and `null` if it is a pre-byzantinium transaction. (#200)
|
||||
|
||||
v0.23.0 - _November 12, 2017_
|
||||
------------------------
|
||||
* Fixed unhandled promise rejection error in subscribe methods (#209)
|
||||
* Subscribe callbacks now receive an error object as their first argument
|
||||
|
||||
v0.22.6 - _November 10, 2017_
|
||||
------------------------
|
||||
* Add a timeout parameter to transaction awaiting (#206)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
0x.js
|
||||
-----
|
||||
|
||||
## Installation
|
||||
|
||||
0x.js ships as both a [UMD](https://github.com/umdjs/umd) module and a [CommonJS](https://en.wikipedia.org/wiki/CommonJS) package.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "0x.js",
|
||||
"version": "0.23.0",
|
||||
"version": "0.26.1",
|
||||
"description": "A javascript library for interacting with the 0x protocol",
|
||||
"keywords": [
|
||||
"0x.js",
|
||||
@@ -13,20 +13,16 @@
|
||||
"types": "lib/src/index.d.ts",
|
||||
"scripts": {
|
||||
"prebuild": "npm run clean",
|
||||
"build": "run-p build:umd:prod build:commonjs",
|
||||
"prepublishOnly": "run-p build",
|
||||
"postpublish": "run-s release docs:json upload_docs_json",
|
||||
"release": "publish-release --assets _bundles/index.js,_bundles/index.min.js --tag $(git describe --tags) --owner 0xProject --repo 0x.js",
|
||||
"upload_docs_json": "aws s3 cp docs/index.json s3://0xjs-docs-jsons/$(git describe --tags).json --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type aplication/json",
|
||||
"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",
|
||||
"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",
|
||||
"test:coverage": "nyc npm run test --all",
|
||||
"report_test_coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||
"update_contracts": "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/build/contracts/$i.json ../0x.js/src/artifacts; done;",
|
||||
"docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json docs/index.json .",
|
||||
"docs:generate": "typedoc --out docs .",
|
||||
"docs:open": "opn docs/index.html",
|
||||
"clean": "shx rm -rf _bundles lib test_temp",
|
||||
"build:umd:dev": "webpack",
|
||||
"build:umd:prod": "NODE_ENV=production webpack",
|
||||
@@ -48,6 +44,8 @@
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@types/bintrees": "^1.0.2",
|
||||
"@types/jsonschema": "^1.1.1",
|
||||
"@types/lodash": "^4.14.64",
|
||||
"@types/mocha": "^2.2.41",
|
||||
@@ -75,8 +73,7 @@
|
||||
"sinon": "^4.0.0",
|
||||
"source-map-support": "^0.5.0",
|
||||
"truffle-hdwallet-provider": "^0.0.3",
|
||||
"tslint": "~5.5.0",
|
||||
"tslint-config-0xproject": "^0.0.2",
|
||||
"tslint": "5.8.0",
|
||||
"typedoc": "~0.8.0",
|
||||
"types-bn": "^0.0.1",
|
||||
"types-ethereumjs-util": "0xProject/types-ethereumjs-util",
|
||||
@@ -86,8 +83,11 @@
|
||||
"webpack": "^3.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"0x-json-schemas": "^0.6.1",
|
||||
"bignumber.js": "^4.1.0",
|
||||
"@0xproject/assert": "^0.0.5",
|
||||
"@0xproject/json-schemas": "^0.6.8",
|
||||
"bignumber.js": "~4.1.0",
|
||||
"bintrees": "^1.0.2",
|
||||
"bn.js": "4.11.8",
|
||||
"compare-versions": "^3.0.1",
|
||||
"es6-promisify": "^5.0.0",
|
||||
"ethereumjs-abi": "^0.6.4",
|
||||
@@ -96,7 +96,6 @@
|
||||
"find-versions": "^2.0.0",
|
||||
"js-sha3": "^0.6.1",
|
||||
"lodash": "^4.17.4",
|
||||
"publish-release": "^1.3.3",
|
||||
"uuid": "^3.1.0",
|
||||
"web3": "^0.20.0"
|
||||
}
|
||||
|
||||
43
packages/0x.js/scripts/postpublish.js
Normal file
43
packages/0x.js/scripts/postpublish.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const execAsync = require('async-child-process').execAsync;
|
||||
const postpublish_utils = require('../../../scripts/postpublish_utils');
|
||||
const packageJSON = require('../package.json');
|
||||
|
||||
const cwd = __dirname + '/..';
|
||||
const subPackageName = packageJSON.name;
|
||||
const S3BucketPath = 's3://0xjs-docs-jsons/';
|
||||
|
||||
let tag;
|
||||
let version;
|
||||
postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
|
||||
.then(function(result) {
|
||||
tag = result.tag;
|
||||
version = result.version;
|
||||
const releaseName = postpublish_utils.getReleaseName(subPackageName, version);
|
||||
const assets = [
|
||||
__dirname + '/../_bundles/index.js',
|
||||
__dirname + '/../_bundles/index.min.js',
|
||||
];
|
||||
return postpublish_utils.publishReleaseNotes(tag, releaseName, assets);
|
||||
})
|
||||
.then(function(release) {
|
||||
console.log('POSTPUBLISH: Release successful, generating docs...');
|
||||
return execAsync(
|
||||
'JSON_FILE_PATH=' + __dirname + '/../docs/index.json PROJECT_DIR=' + __dirname + '/.. yarn docs:json',
|
||||
{
|
||||
cwd,
|
||||
}
|
||||
);
|
||||
})
|
||||
.then(function(result) {
|
||||
if (result.stderr !== '') {
|
||||
throw new Error(result.stderr);
|
||||
}
|
||||
const fileName = 'v' + version + '.json';
|
||||
console.log('POSTPUBLISH: Doc generation successful, uploading docs... as ', fileName);
|
||||
const s3Url = S3BucketPath + fileName;
|
||||
return execAsync('S3_URL=' + s3Url + ' yarn upload_docs_json', {
|
||||
cwd,
|
||||
});
|
||||
}).catch (function(err) {
|
||||
throw err;
|
||||
});
|
||||
@@ -3,5 +3,4 @@
|
||||
# UMD tests should only be run after building the commonjs because they reuse some of the commonjs build artifacts
|
||||
run-s substitute_umd_bundle run_mocha
|
||||
return_code=$?
|
||||
npm run clean
|
||||
exit $return_code
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {SchemaValidator, schemas} from '0x-json-schemas';
|
||||
import {SchemaValidator, schemas} from '@0xproject/json-schemas';
|
||||
import {bigNumberConfigs} from './bignumber_config';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
import {Web3Wrapper} from './web3_wrapper';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as Web3 from 'web3';
|
||||
import * as _ from 'lodash';
|
||||
import promisify = require('es6-promisify');
|
||||
import {SchemaValidator, schemas} from '0x-json-schemas';
|
||||
import {SchemaValidator, schemas} from '@0xproject/json-schemas';
|
||||
import {AbiType} from './types';
|
||||
|
||||
export class Contract implements Web3.ContractInstance {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {schemas} from '0x-json-schemas';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {
|
||||
ECSignature,
|
||||
@@ -95,7 +95,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* @param orderHash The hex encoded orderHash for which you would like to retrieve the
|
||||
* unavailable takerAmount.
|
||||
* @param methodOpts Optional arguments this method accepts.
|
||||
* @return The amount of the order (in taker tokens) that has either been filled or canceled.
|
||||
* @return The amount of the order (in taker tokens) that has either been filled or cancelled.
|
||||
*/
|
||||
public async getUnavailableTakerAmountAsync(orderHash: string,
|
||||
methodOpts?: MethodOpts): Promise<BigNumber> {
|
||||
@@ -133,7 +133,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* @param methodOpts Optional arguments this method accepts.
|
||||
* @return The amount of the order (in taker tokens) that has been cancelled.
|
||||
*/
|
||||
public async getCanceledTakerAmountAsync(orderHash: string, methodOpts?: MethodOpts): Promise<BigNumber> {
|
||||
public async getCancelledTakerAmountAsync(orderHash: string, methodOpts?: MethodOpts): Promise<BigNumber> {
|
||||
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
|
||||
|
||||
const exchangeContract = await this._getExchangeContractAsync();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as _ from 'lodash';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {schemas} from '0x-json-schemas';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {Web3Wrapper} from '../web3_wrapper';
|
||||
import {assert} from '../utils/assert';
|
||||
import {constants} from '../utils/constants';
|
||||
|
||||
@@ -6,8 +6,6 @@ export {
|
||||
ECSignature,
|
||||
ZeroExError,
|
||||
EventCallback,
|
||||
EventCallbackAsync,
|
||||
EventCallbackSync,
|
||||
ExchangeContractErrs,
|
||||
ContractEvent,
|
||||
Token,
|
||||
|
||||
@@ -12,7 +12,7 @@ import {intervalUtils} from '../utils/interval_utils';
|
||||
import {assert} from '../utils/assert';
|
||||
import {utils} from '../utils/utils';
|
||||
|
||||
const DEFAULT_EVENT_POLLING_INTERVAL = 200;
|
||||
const DEFAULT_EVENT_POLLING_INTERVAL_MS = 200;
|
||||
|
||||
enum LogEventState {
|
||||
Removed,
|
||||
@@ -28,11 +28,11 @@ export class EventWatcher {
|
||||
private _pollingIntervalMs: number;
|
||||
private _intervalIdIfExists?: NodeJS.Timer;
|
||||
private _lastEvents: Web3.LogEntry[] = [];
|
||||
constructor(web3Wrapper: Web3Wrapper, pollingIntervalMs: undefined|number) {
|
||||
constructor(web3Wrapper: Web3Wrapper, pollingIntervalIfExistsMs: undefined|number) {
|
||||
this._web3Wrapper = web3Wrapper;
|
||||
this._pollingIntervalMs = _.isUndefined(pollingIntervalMs) ?
|
||||
DEFAULT_EVENT_POLLING_INTERVAL :
|
||||
pollingIntervalMs;
|
||||
this._pollingIntervalMs = _.isUndefined(pollingIntervalIfExistsMs) ?
|
||||
DEFAULT_EVENT_POLLING_INTERVAL_MS :
|
||||
pollingIntervalIfExistsMs;
|
||||
}
|
||||
public subscribe(callback: EventWatcherCallback): void {
|
||||
assert.isFunction('callback', callback);
|
||||
@@ -81,7 +81,7 @@ export class EventWatcher {
|
||||
...log,
|
||||
};
|
||||
if (!_.isUndefined(this._intervalIdIfExists)) {
|
||||
await callback(logEvent);
|
||||
callback(logEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
76
packages/0x.js/src/order_watcher/expiration_watcher.ts
Normal file
76
packages/0x.js/src/order_watcher/expiration_watcher.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
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 {ZeroEx} from '../0x';
|
||||
|
||||
const DEFAULT_EXPIRATION_MARGIN_MS = 0;
|
||||
const DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS = 50;
|
||||
|
||||
/**
|
||||
* This class includes the functionality to detect expired orders.
|
||||
* It stores them in a min heap by expiration time and checks for expired ones every `orderExpirationCheckingIntervalMs`
|
||||
*/
|
||||
export class ExpirationWatcher {
|
||||
private orderHashByExpirationRBTree: RBTree<string>;
|
||||
private expiration: {[orderHash: string]: BigNumber} = {};
|
||||
private orderExpirationCheckingIntervalMs: number;
|
||||
private expirationMarginMs: number;
|
||||
private orderExpirationCheckingIntervalIdIfExists?: NodeJS.Timer;
|
||||
constructor(expirationMarginIfExistsMs?: number,
|
||||
orderExpirationCheckingIntervalIfExistsMs?: number) {
|
||||
this.expirationMarginMs = expirationMarginIfExistsMs ||
|
||||
DEFAULT_EXPIRATION_MARGIN_MS;
|
||||
this.orderExpirationCheckingIntervalMs = expirationMarginIfExistsMs ||
|
||||
DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS;
|
||||
const scoreFunction = (orderHash: string) => this.expiration[orderHash].toNumber();
|
||||
const comparator = (lhs: string, rhs: string) => scoreFunction(lhs) - scoreFunction(rhs);
|
||||
this.orderHashByExpirationRBTree = new RBTree(comparator);
|
||||
}
|
||||
public subscribe(callbackAsync: (orderHash: string) => Promise<void>): void {
|
||||
if (!_.isUndefined(this.orderExpirationCheckingIntervalIdIfExists)) {
|
||||
throw new Error(ZeroExError.SubscriptionAlreadyPresent);
|
||||
}
|
||||
this.orderExpirationCheckingIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
|
||||
this.pruneExpiredOrdersAsync.bind(this, callbackAsync), this.orderExpirationCheckingIntervalMs,
|
||||
);
|
||||
}
|
||||
public unsubscribe(): void {
|
||||
if (_.isUndefined(this.orderExpirationCheckingIntervalIdIfExists)) {
|
||||
throw new Error(ZeroExError.SubscriptionNotFound);
|
||||
}
|
||||
intervalUtils.clearAsyncExcludingInterval(this.orderExpirationCheckingIntervalIdIfExists);
|
||||
delete this.orderExpirationCheckingIntervalIdIfExists;
|
||||
}
|
||||
public addOrder(orderHash: string, expirationUnixTimestampMs: BigNumber): void {
|
||||
this.expiration[orderHash] = expirationUnixTimestampMs;
|
||||
this.orderHashByExpirationRBTree.insert(orderHash);
|
||||
}
|
||||
public removeOrder(orderHash: string): void {
|
||||
this.orderHashByExpirationRBTree.remove(orderHash);
|
||||
delete this.expiration[orderHash];
|
||||
}
|
||||
private async pruneExpiredOrdersAsync(callbackAsync: (orderHash: string) => Promise<void>): Promise<void> {
|
||||
const currentUnixTimestampMs = utils.getCurrentUnixTimestampMs();
|
||||
while (true) {
|
||||
const hasTrakedOrders = this.orderHashByExpirationRBTree.size === 0;
|
||||
if (hasTrakedOrders) {
|
||||
break;
|
||||
}
|
||||
const nextOrderHashToExpire = this.orderHashByExpirationRBTree.min();
|
||||
const hasNoExpiredOrders = this.expiration[nextOrderHashToExpire].greaterThan(
|
||||
currentUnixTimestampMs.plus(this.expirationMarginMs),
|
||||
);
|
||||
const isSubscriptionActive = _.isUndefined(this.orderExpirationCheckingIntervalIdIfExists);
|
||||
if (hasNoExpiredOrders || isSubscriptionActive) {
|
||||
break;
|
||||
}
|
||||
const orderHash = this.orderHashByExpirationRBTree.min();
|
||||
this.orderHashByExpirationRBTree.remove(orderHash);
|
||||
delete this.expiration[orderHash];
|
||||
await callbackAsync(orderHash);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
import * as _ from 'lodash';
|
||||
import {schemas} from '0x-json-schemas';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {ZeroEx} from '../0x';
|
||||
import {EventWatcher} from './event_watcher';
|
||||
import {assert} from '../utils/assert';
|
||||
import {utils} from '../utils/utils';
|
||||
import {artifacts} from '../artifacts';
|
||||
import {AbiDecoder} from '../utils/abi_decoder';
|
||||
import {intervalUtils} from '../utils/interval_utils';
|
||||
import {OrderStateUtils} from '../utils/order_state_utils';
|
||||
import {
|
||||
LogEvent,
|
||||
@@ -24,14 +25,14 @@ import {
|
||||
ExchangeEvents,
|
||||
TokenEvents,
|
||||
ZeroExError,
|
||||
ExchangeContractErrs,
|
||||
} from '../types';
|
||||
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';
|
||||
|
||||
const DEFAULT_NUM_CONFIRMATIONS = 0;
|
||||
import {ExpirationWatcher} from './expiration_watcher';
|
||||
|
||||
interface DependentOrderHashes {
|
||||
[makerAddress: string]: {
|
||||
@@ -52,10 +53,11 @@ interface OrderByOrderHash {
|
||||
export class OrderStateWatcher {
|
||||
private _orderByOrderHash: OrderByOrderHash = {};
|
||||
private _dependentOrderHashes: DependentOrderHashes = {};
|
||||
private _callbackIfExistsAsync?: OnOrderStateChangeCallback;
|
||||
private _callbackIfExists?: OnOrderStateChangeCallback;
|
||||
private _eventWatcher: EventWatcher;
|
||||
private _web3Wrapper: Web3Wrapper;
|
||||
private _abiDecoder: AbiDecoder;
|
||||
private _expirationWatcher: ExpirationWatcher;
|
||||
private _orderStateUtils: OrderStateUtils;
|
||||
private _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
|
||||
private _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
|
||||
@@ -65,38 +67,53 @@ export class OrderStateWatcher {
|
||||
) {
|
||||
this._abiDecoder = abiDecoder;
|
||||
this._web3Wrapper = web3Wrapper;
|
||||
const eventPollingIntervalMs = _.isUndefined(config) ? undefined : config.eventPollingIntervalMs;
|
||||
this._eventWatcher = new EventWatcher(web3Wrapper, eventPollingIntervalMs);
|
||||
const pollingIntervalIfExistsMs = _.isUndefined(config) ? undefined : config.eventPollingIntervalMs;
|
||||
this._eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalIfExistsMs);
|
||||
this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore(token);
|
||||
this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(exchange);
|
||||
this._orderStateUtils = new OrderStateUtils(
|
||||
this._balanceAndProxyAllowanceLazyStore, this._orderFilledCancelledLazyStore,
|
||||
);
|
||||
const orderExpirationCheckingIntervalMsIfExists = _.isUndefined(config) ?
|
||||
undefined :
|
||||
config.orderExpirationCheckingIntervalMs;
|
||||
const expirationMarginIfExistsMs = _.isUndefined(config) ?
|
||||
undefined :
|
||||
config.expirationMarginMs;
|
||||
this._expirationWatcher = new ExpirationWatcher(
|
||||
expirationMarginIfExistsMs, orderExpirationCheckingIntervalMsIfExists,
|
||||
);
|
||||
}
|
||||
/**
|
||||
* 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 addOrder(signedOrder: SignedOrder): void {
|
||||
public async addOrderAsync(signedOrder: SignedOrder): Promise<void> {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
assert.isValidSignature(orderHash, signedOrder.ecSignature, signedOrder.maker);
|
||||
this._orderByOrderHash[orderHash] = signedOrder;
|
||||
this.addToDependentOrderHashes(signedOrder, orderHash);
|
||||
await this.addToDependentOrderHashesAsync(signedOrder, orderHash);
|
||||
const expirationUnixTimestampMs = signedOrder.expirationUnixTimestampSec.times(1000);
|
||||
this._expirationWatcher.addOrder(orderHash, expirationUnixTimestampMs);
|
||||
}
|
||||
/**
|
||||
* Removes an order from the orderStateWatcher
|
||||
* @param orderHash The orderHash of the order you wish to stop watching.
|
||||
*/
|
||||
public removeOrder(orderHash: string): void {
|
||||
public async removeOrderAsync(orderHash: string): Promise<void> {
|
||||
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
|
||||
const signedOrder = this._orderByOrderHash[orderHash];
|
||||
if (_.isUndefined(signedOrder)) {
|
||||
return; // noop
|
||||
}
|
||||
delete this._orderByOrderHash[orderHash];
|
||||
const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
|
||||
const zrxTokenAddress = await exchange.getZRXTokenAddressAsync();
|
||||
this.removeFromDependentOrderHashes(signedOrder.maker, zrxTokenAddress, orderHash);
|
||||
this.removeFromDependentOrderHashes(signedOrder.maker, signedOrder.makerTokenAddress, orderHash);
|
||||
this._expirationWatcher.removeOrder(orderHash);
|
||||
}
|
||||
/**
|
||||
* Starts an orderStateWatcher subscription. The callback will be called every time a watched order's
|
||||
@@ -106,23 +123,38 @@ export class OrderStateWatcher {
|
||||
*/
|
||||
public subscribe(callback: OnOrderStateChangeCallback): void {
|
||||
assert.isFunction('callback', callback);
|
||||
if (!_.isUndefined(this._callbackIfExistsAsync)) {
|
||||
if (!_.isUndefined(this._callbackIfExists)) {
|
||||
throw new Error(ZeroExError.SubscriptionAlreadyPresent);
|
||||
}
|
||||
this._callbackIfExistsAsync = callback;
|
||||
this._callbackIfExists = callback;
|
||||
this._eventWatcher.subscribe(this._onEventWatcherCallbackAsync.bind(this));
|
||||
this._expirationWatcher.subscribe(this._onOrderExpiredAsync.bind(this));
|
||||
}
|
||||
/**
|
||||
* Ends an orderStateWatcher subscription.
|
||||
*/
|
||||
public unsubscribe(): void {
|
||||
if (_.isUndefined(this._callbackIfExistsAsync)) {
|
||||
if (_.isUndefined(this._callbackIfExists)) {
|
||||
throw new Error(ZeroExError.SubscriptionNotFound);
|
||||
}
|
||||
this._balanceAndProxyAllowanceLazyStore.deleteAll();
|
||||
this._orderFilledCancelledLazyStore.deleteAll();
|
||||
delete this._callbackIfExistsAsync;
|
||||
delete this._callbackIfExists;
|
||||
this._eventWatcher.unsubscribe();
|
||||
this._expirationWatcher.unsubscribe();
|
||||
}
|
||||
private async _onOrderExpiredAsync(orderHash: string): Promise<void> {
|
||||
const orderState: OrderState = {
|
||||
isValid: false,
|
||||
orderHash,
|
||||
error: ExchangeContractErrs.OrderFillExpired,
|
||||
};
|
||||
if (!_.isUndefined(this._orderByOrderHash[orderHash])) {
|
||||
await this.removeOrderAsync(orderHash);
|
||||
if (!_.isUndefined(this._callbackIfExists)) {
|
||||
this._callbackIfExists(orderState);
|
||||
}
|
||||
}
|
||||
}
|
||||
private async _onEventWatcherCallbackAsync(log: LogEvent): Promise<void> {
|
||||
const maybeDecodedLog = this._abiDecoder.tryToDecodeLogOrNoop(log);
|
||||
@@ -204,13 +236,13 @@ export class OrderStateWatcher {
|
||||
// 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._callbackIfExistsAsync)) {
|
||||
if (_.isUndefined(this._callbackIfExists)) {
|
||||
break; // Unsubscribe was called
|
||||
}
|
||||
await this._callbackIfExistsAsync(orderState);
|
||||
this._callbackIfExists(orderState);
|
||||
}
|
||||
}
|
||||
private addToDependentOrderHashes(signedOrder: SignedOrder, orderHash: string) {
|
||||
private async addToDependentOrderHashesAsync(signedOrder: SignedOrder, orderHash: string): Promise<void> {
|
||||
if (_.isUndefined(this._dependentOrderHashes[signedOrder.maker])) {
|
||||
this._dependentOrderHashes[signedOrder.maker] = {};
|
||||
}
|
||||
@@ -218,11 +250,17 @@ 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();
|
||||
if (_.isUndefined(this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress])) {
|
||||
this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress] = new Set();
|
||||
}
|
||||
this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress].add(orderHash);
|
||||
}
|
||||
private removeFromDependentOrderHashes(makerAddress: string, makerTokenAddress: string, orderHash: string) {
|
||||
this._dependentOrderHashes[makerAddress][makerTokenAddress].delete(orderHash);
|
||||
if (this._dependentOrderHashes[makerAddress][makerTokenAddress].size === 0) {
|
||||
delete this._dependentOrderHashes[makerAddress][makerTokenAddress];
|
||||
private removeFromDependentOrderHashes(makerAddress: string, tokenAddress: string, orderHash: string) {
|
||||
this._dependentOrderHashes[makerAddress][tokenAddress].delete(orderHash);
|
||||
if (this._dependentOrderHashes[makerAddress][tokenAddress].size === 0) {
|
||||
delete this._dependentOrderHashes[makerAddress][tokenAddress];
|
||||
}
|
||||
if (_.isEmpty(this._dependentOrderHashes[makerAddress])) {
|
||||
delete this._dependentOrderHashes[makerAddress];
|
||||
|
||||
@@ -42,7 +42,7 @@ export class OrderFilledCancelledLazyStore {
|
||||
const methodOpts = {
|
||||
defaultBlock: BlockParamLiteral.Pending,
|
||||
};
|
||||
const cancelledTakerAmount = await this.exchange.getCanceledTakerAmountAsync(orderHash, methodOpts);
|
||||
const cancelledTakerAmount = await this.exchange.getCancelledTakerAmountAsync(orderHash, methodOpts);
|
||||
this.setCancelledTakerAmount(orderHash, cancelledTakerAmount);
|
||||
}
|
||||
const cachedCancelled = this.cancelledTakerAmount[orderHash];
|
||||
|
||||
@@ -42,13 +42,8 @@ export type OrderValues = [BigNumber, BigNumber, BigNumber,
|
||||
export type LogEvent = Web3.LogEntryEvent;
|
||||
export type DecodedLogEvent<ArgsType> = Web3.DecodedLogEntryEvent<ArgsType>;
|
||||
|
||||
export type EventCallbackAsync<ArgsType> = (err: null|Error, log?: DecodedLogEvent<ArgsType>) => Promise<void>;
|
||||
export type EventCallbackSync<ArgsType> = (err: null|Error, log?: DecodedLogEvent<ArgsType>) => void;
|
||||
export type EventCallback<ArgsType> = EventCallbackSync<ArgsType>|EventCallbackAsync<ArgsType>;
|
||||
|
||||
export type EventWatcherCallbackSync = (log: LogEvent) => void;
|
||||
export type EventWatcherCallbackAsync = (log: LogEvent) => Promise<void>;
|
||||
export type EventWatcherCallback = EventWatcherCallbackSync|EventWatcherCallbackAsync;
|
||||
export type EventCallback<ArgsType> = (err: null|Error, log?: DecodedLogEvent<ArgsType>) => void;
|
||||
export type EventWatcherCallback = (log: LogEvent) => void;
|
||||
|
||||
export interface ExchangeContract extends Web3.ContractInstance {
|
||||
isValidSignature: {
|
||||
@@ -397,10 +392,15 @@ export interface JSONRPCPayload {
|
||||
}
|
||||
|
||||
/*
|
||||
* eventPollingIntervalMs: How often to poll the Ethereum node for new events
|
||||
* orderExpirationCheckingIntervalMs: How often to check for expired orders. Default: 50
|
||||
* 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
|
||||
*/
|
||||
export interface OrderStateWatcherConfig {
|
||||
orderExpirationCheckingIntervalMs?: number;
|
||||
eventPollingIntervalMs?: number;
|
||||
expirationMarginMs?: number;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -488,8 +488,9 @@ export interface OrderRelevantState {
|
||||
makerFeeBalance: BigNumber;
|
||||
makerFeeProxyAllowance: BigNumber;
|
||||
filledTakerTokenAmount: BigNumber;
|
||||
canceledTakerTokenAmount: BigNumber;
|
||||
cancelledTakerTokenAmount: BigNumber;
|
||||
remainingFillableMakerTokenAmount: BigNumber;
|
||||
remainingFillableTakerTokenAmount: BigNumber;
|
||||
}
|
||||
|
||||
export interface OrderStateValid {
|
||||
@@ -506,9 +507,7 @@ export interface OrderStateInvalid {
|
||||
|
||||
export type OrderState = OrderStateValid|OrderStateInvalid;
|
||||
|
||||
export type OnOrderStateChangeCallbackSync = (orderState: OrderState) => void;
|
||||
export type OnOrderStateChangeCallbackAsync = (orderState: OrderState) => Promise<void>;
|
||||
export type OnOrderStateChangeCallback = OnOrderStateChangeCallbackAsync|OnOrderStateChangeCallbackSync;
|
||||
export type OnOrderStateChangeCallback = (orderState: OrderState) => void;
|
||||
|
||||
export interface TransactionReceipt {
|
||||
blockHash: string;
|
||||
|
||||
@@ -34,7 +34,7 @@ export class AbiDecoder {
|
||||
value = this.padZeros(new BigNumber(value).toString(16));
|
||||
} else if (param.type === SolidityTypes.Uint256 ||
|
||||
param.type === SolidityTypes.Uint8 ||
|
||||
param.type === SolidityTypes.Uint ) {
|
||||
param.type === SolidityTypes.Uint) {
|
||||
value = new BigNumber(value);
|
||||
}
|
||||
decodedParams[param.name] = value;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import * as _ from 'lodash';
|
||||
import * as Web3 from 'web3';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import {SchemaValidator, Schema} from '0x-json-schemas';
|
||||
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';
|
||||
@@ -9,58 +10,16 @@ import {ECSignature} from '../types';
|
||||
const HEX_REGEX = /^0x[0-9A-F]*$/i;
|
||||
|
||||
export const assert = {
|
||||
isBigNumber(variableName: string, value: BigNumber): void {
|
||||
const isBigNumber = _.isObject(value) && (value as any).isBigNumber;
|
||||
this.assert(isBigNumber, this.typeAssertionMessage(variableName, 'BigNumber', value));
|
||||
},
|
||||
isValidBaseUnitAmount(variableName: string, value: BigNumber) {
|
||||
assert.isBigNumber(variableName, value);
|
||||
const hasDecimals = value.decimalPlaces() !== 0;
|
||||
this.assert(
|
||||
!hasDecimals, `${variableName} should be in baseUnits (no decimals), found value: ${value.toNumber()}`,
|
||||
);
|
||||
},
|
||||
...sharedAssert,
|
||||
isValidSignature(orderHash: string, ecSignature: ECSignature, signerAddress: string) {
|
||||
const isValidSignature = signatureUtils.isValidSignature(orderHash, ecSignature, signerAddress);
|
||||
this.assert(isValidSignature, `Expected order with hash '${orderHash}' to have a valid signature`);
|
||||
},
|
||||
isUndefined(value: any, variableName?: string): void {
|
||||
this.assert(_.isUndefined(value), this.typeAssertionMessage(variableName, 'undefined', value));
|
||||
},
|
||||
isString(variableName: string, value: string): void {
|
||||
this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value));
|
||||
},
|
||||
isFunction(variableName: string, value: any): void {
|
||||
this.assert(_.isFunction(value), this.typeAssertionMessage(variableName, 'function', value));
|
||||
},
|
||||
isHexString(variableName: string, value: string): void {
|
||||
this.assert(_.isString(value) && HEX_REGEX.test(value),
|
||||
this.typeAssertionMessage(variableName, 'HexString', value));
|
||||
},
|
||||
isETHAddressHex(variableName: string, value: string): void {
|
||||
const web3 = new Web3();
|
||||
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
||||
this.assert(
|
||||
web3.isAddress(value) && value.toLowerCase() === value,
|
||||
`Checksummed addresses are not supported. Convert ${variableName} to lower case before passing`,
|
||||
);
|
||||
},
|
||||
doesBelongToStringEnum(variableName: string, value: string,
|
||||
stringEnum: any /* There is no base type for every string enum */): void {
|
||||
const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]);
|
||||
const enumValues = _.keys(stringEnum);
|
||||
const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`);
|
||||
const enumValuesAsString = enumValuesAsStrings.join(', ');
|
||||
assert.assert(
|
||||
doesBelongToStringEnum,
|
||||
`Expected ${variableName} to be one of: ${enumValuesAsString}, encountered: ${value}`,
|
||||
);
|
||||
},
|
||||
async isSenderAddressAsync(variableName: string, senderAddressHex: string,
|
||||
web3Wrapper: Web3Wrapper): Promise<void> {
|
||||
assert.isETHAddressHex(variableName, senderAddressHex);
|
||||
sharedAssert.isETHAddressHex(variableName, senderAddressHex);
|
||||
const isSenderAddressAvailable = await web3Wrapper.isSenderAddressAvailableAsync(senderAddressHex);
|
||||
assert.assert(isSenderAddressAvailable,
|
||||
sharedAssert.assert(isSenderAddressAvailable,
|
||||
`Specified ${variableName} ${senderAddressHex} isn't available through the supplied web3 provider`,
|
||||
);
|
||||
},
|
||||
@@ -68,34 +27,4 @@ export const assert = {
|
||||
const availableAddresses = await web3Wrapper.getAvailableAddressesAsync();
|
||||
this.assert(!_.isEmpty(availableAddresses), 'No addresses were available on the provided web3 provider');
|
||||
},
|
||||
hasAtMostOneUniqueValue(value: any[], errMsg: string): void {
|
||||
this.assert(_.uniq(value).length <= 1, errMsg);
|
||||
},
|
||||
isNumber(variableName: string, value: number): void {
|
||||
this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value));
|
||||
},
|
||||
isBoolean(variableName: string, value: boolean): void {
|
||||
this.assert(_.isBoolean(value), this.typeAssertionMessage(variableName, 'boolean', value));
|
||||
},
|
||||
isWeb3Provider(variableName: string, value: Web3.Provider): void {
|
||||
const isWeb3Provider = _.isFunction((value as any).send) || _.isFunction((value as any).sendAsync);
|
||||
this.assert(isWeb3Provider, this.typeAssertionMessage(variableName, 'Web3.Provider', value));
|
||||
},
|
||||
doesConformToSchema(variableName: string, value: any, schema: Schema): void {
|
||||
const schemaValidator = new SchemaValidator();
|
||||
const validationResult = schemaValidator.validate(value, schema);
|
||||
const hasValidationErrors = validationResult.errors.length > 0;
|
||||
const msg = `Expected ${variableName} to conform to schema ${schema.id}
|
||||
Encountered: ${JSON.stringify(value, null, '\t')}
|
||||
Validation errors: ${validationResult.errors.join(', ')}`;
|
||||
this.assert(!hasValidationErrors, msg);
|
||||
},
|
||||
assert(condition: boolean, message: string): void {
|
||||
if (!condition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
},
|
||||
typeAssertionMessage(variableName: string, type: string, value: any): string {
|
||||
return `Expected ${variableName} to be of type ${type}, encountered: ${value}`;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -18,6 +18,8 @@ import {constants} from '../utils/constants';
|
||||
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
|
||||
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
|
||||
|
||||
const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001;
|
||||
|
||||
export class OrderStateUtils {
|
||||
private balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
|
||||
private orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
|
||||
@@ -67,7 +69,7 @@ export class OrderStateUtils {
|
||||
zrxTokenAddress, signedOrder.maker,
|
||||
);
|
||||
const filledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash);
|
||||
const canceledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
|
||||
const cancelledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
|
||||
orderHash,
|
||||
);
|
||||
const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash);
|
||||
@@ -78,6 +80,9 @@ export class OrderStateUtils {
|
||||
.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,
|
||||
@@ -85,13 +90,14 @@ export class OrderStateUtils {
|
||||
makerFeeBalance,
|
||||
makerFeeProxyAllowance,
|
||||
filledTakerTokenAmount,
|
||||
canceledTakerTokenAmount,
|
||||
cancelledTakerTokenAmount,
|
||||
remainingFillableMakerTokenAmount,
|
||||
remainingFillableTakerTokenAmount,
|
||||
};
|
||||
return orderRelevantState;
|
||||
}
|
||||
private validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
|
||||
const unavailableTakerTokenAmount = orderRelevantState.canceledTakerTokenAmount.add(
|
||||
const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add(
|
||||
orderRelevantState.filledTakerTokenAmount,
|
||||
);
|
||||
const availableTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
|
||||
@@ -113,6 +119,13 @@ export class OrderStateUtils {
|
||||
throw new Error(ExchangeContractErrs.InsufficientMakerFeeAllowance);
|
||||
}
|
||||
}
|
||||
const minFillableTakerTokenAmountWithinNoRoundingErrorRange = signedOrder.takerTokenAmount
|
||||
.dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR)
|
||||
.dividedBy(signedOrder.makerTokenAmount);
|
||||
if (orderRelevantState.remainingFillableTakerTokenAmount
|
||||
.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
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ export class OrderValidationUtils {
|
||||
if (order.takerTokenAmount.eq(unavailableTakerTokenAmount)) {
|
||||
throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
|
||||
}
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestamp();
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
if (order.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
|
||||
throw new Error(ExchangeContractErrs.OrderCancelExpired);
|
||||
}
|
||||
@@ -150,7 +150,7 @@ export class OrderValidationUtils {
|
||||
}
|
||||
}
|
||||
private validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber) {
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestamp();
|
||||
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
if (expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
|
||||
throw new Error(ExchangeContractErrs.OrderFillExpired);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,10 @@ export const utils = {
|
||||
const hashHex = ethUtil.bufferToHex(hashBuff);
|
||||
return hashHex;
|
||||
},
|
||||
getCurrentUnixTimestamp(): BigNumber {
|
||||
return new BigNumber(Date.now() / 1000);
|
||||
getCurrentUnixTimestampSec(): BigNumber {
|
||||
return new BigNumber(Date.now() / 1000).round();
|
||||
},
|
||||
getCurrentUnixTimestampMs(): BigNumber {
|
||||
return new BigNumber(Date.now());
|
||||
},
|
||||
};
|
||||
|
||||
@@ -5,6 +5,17 @@ import promisify = require('es6-promisify');
|
||||
import {ZeroExError, Artifact, TransactionReceipt} from './types';
|
||||
import {Contract} from './contract';
|
||||
|
||||
interface RawLogEntry {
|
||||
logIndex: string|null;
|
||||
transactionIndex: string|null;
|
||||
transactionHash: string;
|
||||
blockHash: string|null;
|
||||
blockNumber: string|null;
|
||||
address: string;
|
||||
data: string;
|
||||
topics: string[];
|
||||
}
|
||||
|
||||
export class Web3Wrapper {
|
||||
private web3: Web3;
|
||||
private defaults: Partial<Web3.TxData>;
|
||||
@@ -39,7 +50,9 @@ export class Web3Wrapper {
|
||||
}
|
||||
public async getTransactionReceiptAsync(txHash: string): Promise<TransactionReceipt> {
|
||||
const transactionReceipt = await promisify(this.web3.eth.getTransactionReceipt)(txHash);
|
||||
transactionReceipt.status = this.normalizeTxReceiptStatus(transactionReceipt.status);
|
||||
if (!_.isNull(transactionReceipt)) {
|
||||
transactionReceipt.status = this.normalizeTxReceiptStatus(transactionReceipt.status);
|
||||
}
|
||||
return transactionReceipt;
|
||||
}
|
||||
public getCurrentProvider(): Web3.Provider {
|
||||
@@ -137,8 +150,9 @@ export class Web3Wrapper {
|
||||
method: 'eth_getLogs',
|
||||
params: [serializedFilter],
|
||||
};
|
||||
const logs = await this.sendRawPayloadAsync(payload);
|
||||
return logs;
|
||||
const rawLogs = await this.sendRawPayloadAsync<RawLogEntry[]>(payload);
|
||||
const formattedLogs = _.map(rawLogs, this.formatLog.bind(this));
|
||||
return formattedLogs;
|
||||
}
|
||||
private getContractInstance<A extends Web3.ContractInstance>(abi: Web3.ContractAbi, address: string): A {
|
||||
const web3ContractInstance = this.web3.eth.contract(abi).at(address);
|
||||
@@ -149,7 +163,7 @@ export class Web3Wrapper {
|
||||
const networkId = await promisify(this.web3.version.getNetwork)();
|
||||
return networkId;
|
||||
}
|
||||
private async sendRawPayloadAsync(payload: Web3.JSONRPCRequestPayload): Promise<any> {
|
||||
private async sendRawPayloadAsync<A>(payload: Web3.JSONRPCRequestPayload): Promise<A> {
|
||||
const sendAsync = this.web3.currentProvider.sendAsync.bind(this.web3.currentProvider);
|
||||
const response = await promisify(sendAsync)(payload);
|
||||
const result = response.result;
|
||||
@@ -169,4 +183,20 @@ export class Web3Wrapper {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
private formatLog(rawLog: RawLogEntry): Web3.LogEntry {
|
||||
const formattedLog = {
|
||||
...rawLog,
|
||||
logIndex: this.hexToDecimal(rawLog.logIndex),
|
||||
blockNumber: this.hexToDecimal(rawLog.blockNumber),
|
||||
transactionIndex: this.hexToDecimal(rawLog.transactionIndex),
|
||||
};
|
||||
return formattedLog;
|
||||
}
|
||||
private hexToDecimal(hex: string|null): number|null {
|
||||
if (_.isNull(hex)) {
|
||||
return null;
|
||||
}
|
||||
const decimal = this.web3.toDecimal(hex);
|
||||
return decimal;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -443,7 +443,7 @@ describe('ExchangeWrapper', () => {
|
||||
it('should cancel an order', async () => {
|
||||
const txHash = await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmount);
|
||||
await zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex);
|
||||
const cancelledAmount = await zeroEx.exchange.getCancelledTakerAmountAsync(orderHashHex);
|
||||
expect(cancelledAmount).to.be.bignumber.equal(cancelAmount);
|
||||
});
|
||||
});
|
||||
@@ -502,8 +502,8 @@ describe('ExchangeWrapper', () => {
|
||||
describe('successful batch cancels', () => {
|
||||
it('should cancel a batch of orders', async () => {
|
||||
await zeroEx.exchange.batchCancelOrdersAsync(cancelBatch);
|
||||
const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex);
|
||||
const anotherCancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(
|
||||
const cancelledAmount = await zeroEx.exchange.getCancelledTakerAmountAsync(orderHashHex);
|
||||
const anotherCancelledAmount = await zeroEx.exchange.getCancelledTakerAmountAsync(
|
||||
anotherOrderHashHex,
|
||||
);
|
||||
expect(cancelledAmount).to.be.bignumber.equal(cancelAmount);
|
||||
@@ -592,23 +592,23 @@ describe('ExchangeWrapper', () => {
|
||||
expect(filledValueT).to.be.bignumber.equal(partialFillAmount);
|
||||
});
|
||||
});
|
||||
describe('#getCanceledTakerAmountAsync', () => {
|
||||
describe('#getCancelledTakerAmountAsync', () => {
|
||||
it('should throw if passed an invalid orderHash', async () => {
|
||||
const invalidOrderHashHex = '0x123';
|
||||
return expect(zeroEx.exchange.getCanceledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected();
|
||||
return expect(zeroEx.exchange.getCancelledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected();
|
||||
});
|
||||
it('should return zero if passed a valid but non-existent orderHash', async () => {
|
||||
const cancelledValueT = await zeroEx.exchange.getCanceledTakerAmountAsync(NON_EXISTENT_ORDER_HASH);
|
||||
const cancelledValueT = await zeroEx.exchange.getCancelledTakerAmountAsync(NON_EXISTENT_ORDER_HASH);
|
||||
expect(cancelledValueT).to.be.bignumber.equal(0);
|
||||
});
|
||||
it('should return the cancelledValueT for a valid and partially filled orderHash', async () => {
|
||||
const cancelledValueT = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHash);
|
||||
const cancelledValueT = await zeroEx.exchange.getCancelledTakerAmountAsync(orderHash);
|
||||
expect(cancelledValueT).to.be.bignumber.equal(0);
|
||||
});
|
||||
it('should return the cancelledValueT for a valid and cancelled orderHash', async () => {
|
||||
const cancelAmount = fillableAmount.minus(partialFillAmount);
|
||||
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmount);
|
||||
const cancelledValueT = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHash);
|
||||
const cancelledValueT = await zeroEx.exchange.getCancelledTakerAmountAsync(orderHash);
|
||||
expect(cancelledValueT).to.be.bignumber.equal(cancelAmount);
|
||||
});
|
||||
});
|
||||
|
||||
138
packages/0x.js/test/expiration_watcher_test.ts
Normal file
138
packages/0x.js/test/expiration_watcher_test.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import 'mocha';
|
||||
import * as chai from 'chai';
|
||||
import * as _ from 'lodash';
|
||||
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 {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 {FillScenarios} from './utils/fill_scenarios';
|
||||
import {reportCallbackErrors} from './utils/report_callback_errors';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
const blockchainLifecycle = new BlockchainLifecycle();
|
||||
|
||||
describe('ExpirationWatcher', () => {
|
||||
let web3: Web3;
|
||||
let zeroEx: ZeroEx;
|
||||
let tokenUtils: TokenUtils;
|
||||
let tokens: Token[];
|
||||
let userAddresses: string[];
|
||||
let zrxTokenAddress: string;
|
||||
let fillScenarios: FillScenarios;
|
||||
let exchangeContractAddress: string;
|
||||
let makerTokenAddress: string;
|
||||
let takerTokenAddress: string;
|
||||
let coinbase: string;
|
||||
let makerAddress: string;
|
||||
let takerAddress: string;
|
||||
let feeRecipient: string;
|
||||
const fillableAmount = new BigNumber(5);
|
||||
let currentUnixTimestampSec: BigNumber;
|
||||
let timer: Sinon.SinonFakeTimers;
|
||||
let expirationWatcher: ExpirationWatcher;
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
exchangeContractAddress = await zeroEx.exchange.getContractAddressAsync();
|
||||
userAddresses = await zeroEx.getAvailableAddressesAsync();
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
tokenUtils = new TokenUtils(tokens);
|
||||
zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address;
|
||||
fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens, zrxTokenAddress, exchangeContractAddress);
|
||||
[coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses;
|
||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||
const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens();
|
||||
makerTokenAddress = makerToken.address;
|
||||
takerTokenAddress = takerToken.address;
|
||||
});
|
||||
beforeEach(async () => {
|
||||
await blockchainLifecycle.startAsync();
|
||||
const sinonTimerConfig = {shouldAdvanceTime: true} as any;
|
||||
// This constructor has incorrect types
|
||||
timer = Sinon.useFakeTimers(sinonTimerConfig);
|
||||
currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
|
||||
expirationWatcher = new ExpirationWatcher();
|
||||
});
|
||||
afterEach(async () => {
|
||||
await blockchainLifecycle.revertAsync();
|
||||
timer.restore();
|
||||
expirationWatcher.unsubscribe();
|
||||
});
|
||||
it('correctly emits events when order expires', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const orderLifetimeSec = 60;
|
||||
const expirationUnixTimestampSec = currentUnixTimestampSec.plus(orderLifetimeSec);
|
||||
const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
|
||||
expirationUnixTimestampSec,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(1000));
|
||||
const callbackAsync = reportCallbackErrors(done)(async (hash: string) => {
|
||||
expect(hash).to.be.equal(orderHash);
|
||||
expect(utils.getCurrentUnixTimestampSec()).to.be.bignumber.gte(expirationUnixTimestampSec);
|
||||
done();
|
||||
});
|
||||
expirationWatcher.subscribe(callbackAsync);
|
||||
timer.tick(orderLifetimeSec * 1000);
|
||||
})().catch(done);
|
||||
});
|
||||
it('doesn\'t emit events before order expires', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const orderLifetimeSec = 60;
|
||||
const expirationUnixTimestampSec = currentUnixTimestampSec.plus(orderLifetimeSec);
|
||||
const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
|
||||
expirationUnixTimestampSec,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(1000));
|
||||
const callbackAsync = reportCallbackErrors(done)(async (hash: string) => {
|
||||
done(new Error('Emitted expiration went before the order actually expired'));
|
||||
});
|
||||
expirationWatcher.subscribe(callbackAsync);
|
||||
const notEnoughTime = orderLifetimeSec - 1;
|
||||
timer.tick(notEnoughTime * 1000);
|
||||
done();
|
||||
})().catch(done);
|
||||
});
|
||||
it('emits events in correct order', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const order1Lifetime = 60;
|
||||
const order2Lifetime = 120;
|
||||
const order1ExpirationUnixTimestampSec = currentUnixTimestampSec.plus(order1Lifetime);
|
||||
const order2ExpirationUnixTimestampSec = currentUnixTimestampSec.plus(order2Lifetime);
|
||||
const signedOrder1 = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
|
||||
order1ExpirationUnixTimestampSec,
|
||||
);
|
||||
const signedOrder2 = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
|
||||
order2ExpirationUnixTimestampSec,
|
||||
);
|
||||
const orderHash1 = ZeroEx.getOrderHashHex(signedOrder1);
|
||||
const orderHash2 = ZeroEx.getOrderHashHex(signedOrder2);
|
||||
expirationWatcher.addOrder(orderHash2, signedOrder2.expirationUnixTimestampSec.times(1000));
|
||||
expirationWatcher.addOrder(orderHash1, signedOrder1.expirationUnixTimestampSec.times(1000));
|
||||
const expirationOrder = [orderHash1, orderHash2];
|
||||
const callbackAsync = reportCallbackErrors(done)(async (hash: string) => {
|
||||
const orderHash = expirationOrder.shift();
|
||||
expect(hash).to.be.equal(orderHash);
|
||||
if (_.isEmpty(expirationOrder)) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
expirationWatcher.subscribe(callbackAsync);
|
||||
timer.tick(order2Lifetime * 1000);
|
||||
})().catch(done);
|
||||
});
|
||||
});
|
||||
@@ -47,7 +47,7 @@ describe('OrderStateWatcher', () => {
|
||||
let taker: string;
|
||||
let web3Wrapper: Web3Wrapper;
|
||||
let signedOrder: SignedOrder;
|
||||
const fillableAmount = new BigNumber(5);
|
||||
const fillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(5), 18);
|
||||
before(async () => {
|
||||
web3 = web3Factory.create();
|
||||
zeroEx = new ZeroEx(web3.currentProvider);
|
||||
@@ -61,19 +61,25 @@ describe('OrderStateWatcher', () => {
|
||||
[makerToken, takerToken] = tokenUtils.getNonProtocolTokens();
|
||||
web3Wrapper = (zeroEx as any)._web3Wrapper;
|
||||
});
|
||||
beforeEach(async () => {
|
||||
await blockchainLifecycle.startAsync();
|
||||
});
|
||||
afterEach(async () => {
|
||||
await blockchainLifecycle.revertAsync();
|
||||
});
|
||||
describe('#removeOrder', async () => {
|
||||
it('should successfully remove existing order', async () => {
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(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);
|
||||
zeroEx.orderStateWatcher.removeOrder(orderHash);
|
||||
await zeroEx.orderStateWatcher.removeOrderAsync(orderHash);
|
||||
expect((zeroEx.orderStateWatcher as any)._orderByOrderHash).to.not.include({
|
||||
[orderHash]: signedOrder,
|
||||
});
|
||||
@@ -86,7 +92,7 @@ describe('OrderStateWatcher', () => {
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
const nonExistentOrderHash = `0x${orderHash.substr(2).split('').reverse().join('')}`;
|
||||
zeroEx.orderStateWatcher.removeOrder(nonExistentOrderHash);
|
||||
await zeroEx.orderStateWatcher.removeOrderAsync(nonExistentOrderHash);
|
||||
});
|
||||
});
|
||||
describe('#subscribe', async () => {
|
||||
@@ -103,7 +109,7 @@ describe('OrderStateWatcher', () => {
|
||||
afterEach(async () => {
|
||||
zeroEx.orderStateWatcher.unsubscribe();
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.removeOrder(orderHash);
|
||||
await zeroEx.orderStateWatcher.removeOrderAsync(orderHash);
|
||||
});
|
||||
it('should emit orderStateInvalid when maker allowance set to 0 for watched order', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
@@ -111,7 +117,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
const invalidOrderState = orderState as OrderStateInvalid;
|
||||
@@ -129,7 +135,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
throw new Error('OrderState callback fired for irrelevant order');
|
||||
});
|
||||
@@ -150,7 +156,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
const invalidOrderState = orderState as OrderStateInvalid;
|
||||
@@ -170,7 +176,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
let eventCount = 0;
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
@@ -202,7 +208,7 @@ describe('OrderStateWatcher', () => {
|
||||
|
||||
const fillAmountInBaseUnits = new BigNumber(2);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
let eventCount = 0;
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
@@ -215,6 +221,8 @@ describe('OrderStateWatcher', () => {
|
||||
const remainingFillable = fillableAmount.minus(fillAmountInBaseUnits);
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingFillable);
|
||||
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingFillable);
|
||||
expect(orderRelevantState.makerBalance).to.be.bignumber.equal(remainingMakerBalance);
|
||||
if (eventCount === 2) {
|
||||
done();
|
||||
@@ -227,84 +235,108 @@ describe('OrderStateWatcher', () => {
|
||||
);
|
||||
})().catch(done);
|
||||
});
|
||||
describe('remainingFillableMakerTokenAmount', () => {
|
||||
it('should calculate correct remaining fillable', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const takerFillableAmount = new BigNumber(10);
|
||||
const makerFillableAmount = new BigNumber(20);
|
||||
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 = new BigNumber(2);
|
||||
const orderHash = ZeroEx.getOrderHashHex(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);
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
new BigNumber(16));
|
||||
if (eventCount === 2) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
await zeroEx.exchange.fillOrderAsync(
|
||||
signedOrder, fillAmountInBaseUnits, shouldThrowOnInsufficientBalanceOrAllowance, taker,
|
||||
);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should equal approved amount when approved amount is lowest', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
it('should trigger the callback when orders backing ZRX allowance changes', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const makerFee = ZeroEx.toBaseUnitAmount(new BigNumber(2), 18);
|
||||
const takerFee = ZeroEx.toBaseUnitAmount(new BigNumber(0), 18);
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync(
|
||||
makerToken.address, takerToken.address, makerFee, takerFee, maker, taker, fillableAmount,
|
||||
taker);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, new BigNumber(0));
|
||||
})().catch(done);
|
||||
});
|
||||
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);
|
||||
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 orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(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);
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(16), 18));
|
||||
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
|
||||
ZeroEx.toBaseUnitAmount(new BigNumber(8), 18));
|
||||
if (eventCount === 2) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
const shouldThrowOnInsufficientBalanceOrAllowance = true;
|
||||
await zeroEx.exchange.fillOrderAsync(
|
||||
signedOrder, fillAmountInBaseUnits, shouldThrowOnInsufficientBalanceOrAllowance, taker,
|
||||
);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should equal approved amount when approved amount is lowest', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
|
||||
const changedMakerApprovalAmount = new BigNumber(3);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const changedMakerApprovalAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), 18);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
changedMakerApprovalAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, changedMakerApprovalAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should equal balance amount when balance amount is lowest', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
changedMakerApprovalAmount);
|
||||
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
|
||||
changedMakerApprovalAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, changedMakerApprovalAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should equal balance amount when balance amount is lowest', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
|
||||
|
||||
const remainingAmount = new BigNumber(1);
|
||||
const transferAmount = makerBalance.sub(remainingAmount);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
const remainingAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18);
|
||||
const transferAmount = makerBalance.sub(remainingAmount);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.transferAsync(
|
||||
makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingAmount);
|
||||
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
|
||||
remainingAmount);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.token.transferAsync(
|
||||
makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
});
|
||||
it('should emit orderStateInvalid when watched order cancelled', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
@@ -312,7 +344,7 @@ describe('OrderStateWatcher', () => {
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.false();
|
||||
@@ -327,6 +359,28 @@ describe('OrderStateWatcher', () => {
|
||||
await zeroEx.exchange.cancelOrderAsync(signedOrder, fillableAmount);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should emit orderStateInvalid when within rounding error range', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
const remainingFillableAmountInBaseUnits = new BigNumber(100);
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
makerToken.address, takerToken.address, maker, taker, fillableAmount,
|
||||
);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
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.OrderFillRoundingError);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
await zeroEx.exchange.cancelOrderAsync(
|
||||
signedOrder, fillableAmount.minus(remainingFillableAmountInBaseUnits),
|
||||
);
|
||||
})().catch(done);
|
||||
});
|
||||
it('should emit orderStateValid when watched order partially cancelled', (done: DoneCallback) => {
|
||||
(async () => {
|
||||
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||
@@ -338,14 +392,14 @@ describe('OrderStateWatcher', () => {
|
||||
|
||||
const cancelAmountInBaseUnits = new BigNumber(2);
|
||||
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
|
||||
zeroEx.orderStateWatcher.addOrder(signedOrder);
|
||||
await zeroEx.orderStateWatcher.addOrderAsync(signedOrder);
|
||||
|
||||
const callback = reportCallbackErrors(done)((orderState: OrderState) => {
|
||||
expect(orderState.isValid).to.be.true();
|
||||
const validOrderState = orderState as OrderStateValid;
|
||||
expect(validOrderState.orderHash).to.be.equal(orderHash);
|
||||
const orderRelevantState = validOrderState.orderRelevantState;
|
||||
expect(orderRelevantState.canceledTakerTokenAmount).to.be.bignumber.equal(cancelAmountInBaseUnits);
|
||||
expect(orderRelevantState.cancelledTakerTokenAmount).to.be.bignumber.equal(cancelAmountInBaseUnits);
|
||||
done();
|
||||
});
|
||||
zeroEx.orderStateWatcher.subscribe(callback);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as _ from 'lodash';
|
||||
import 'mocha';
|
||||
import * as chai from 'chai';
|
||||
import {SchemaValidator, schemas} from '0x-json-schemas';
|
||||
import {SchemaValidator, schemas} from '@0xproject/json-schemas';
|
||||
import {chaiSetup} from './utils/chai_setup';
|
||||
import {web3Factory} from './utils/web3_factory';
|
||||
import {ZeroEx, Token} from '../src';
|
||||
|
||||
@@ -361,6 +361,9 @@ 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(args._from).to.be.equal(coinbase);
|
||||
expect(args._to).to.be.equal(addressWithoutFunds);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { DoneCallback } from '../../src/types';
|
||||
|
||||
export const reportCallbackErrors = (done: DoneCallback) => {
|
||||
return (f: (...args: any[]) => void) => {
|
||||
const wrapped = (...args: any[]) => {
|
||||
return (fAsync: (...args: any[]) => void|Promise<void>) => {
|
||||
const wrapped = async (...args: any[]) => {
|
||||
try {
|
||||
f(...args);
|
||||
await fAsync(...args);
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": [
|
||||
"tslint-config-0xproject"
|
||||
"@0xproject/tslint-config"
|
||||
]
|
||||
}
|
||||
|
||||
6
packages/assert/CHANGELOG.md
Normal file
6
packages/assert/CHANGELOG.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# CHANGELOG
|
||||
|
||||
v0.0.4 - _Nov. 14, 2017_
|
||||
------------------------
|
||||
* Re-publish Assert previously published under NPM package @0xproject/0x-assert
|
||||
* Added assertion isValidBaseUnitAmount which checks both that the value is a valid bigNumber and that it does not contain decimals.
|
||||
10
packages/assert/README.md
Normal file
10
packages/assert/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
assert
|
||||
------
|
||||
|
||||
Standard type and schema assertions to be used across all 0x projects and packages
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install @0xproject/assert --save
|
||||
```
|
||||
46
packages/assert/package.json
Normal file
46
packages/assert/package.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "@0xproject/assert",
|
||||
"version": "0.0.5",
|
||||
"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",
|
||||
"run_mocha": "mocha lib/test/**/*_test.js",
|
||||
"prepublishOnly": "run-p build",
|
||||
"test": "run-s clean build run_mocha",
|
||||
"test:circleci": "yarn test"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/0xProject/0x.js.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/0xProject/0x.js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/0xProject/0x.js/packages/assert/README.md",
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@types/lodash": "^4.14.78",
|
||||
"@types/mocha": "^2.2.42",
|
||||
"@types/valid-url": "^1.0.2",
|
||||
"chai": "^4.0.1",
|
||||
"chai-typescript-typings": "^0.0.1",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"mocha": "^4.0.1",
|
||||
"npm-run-all": "^4.1.1",
|
||||
"shx": "^0.2.2",
|
||||
"tslint": "5.8.0",
|
||||
"typescript": "^2.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@0xproject/json-schemas": "^0.6.8",
|
||||
"bignumber.js": "~4.1.0",
|
||||
"ethereum-address": "^0.0.4",
|
||||
"lodash": "^4.17.4",
|
||||
"valid-url": "^1.0.9"
|
||||
}
|
||||
}
|
||||
14
packages/assert/scripts/postpublish.js
Normal file
14
packages/assert/scripts/postpublish.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const postpublish_utils = require('../../../scripts/postpublish_utils');
|
||||
const packageJSON = require('../package.json');
|
||||
|
||||
const subPackageName = packageJSON.name;
|
||||
|
||||
postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
|
||||
.then(function(result) {
|
||||
const releaseName = postpublish_utils.getReleaseName(subPackageName, result.version);
|
||||
const assets = [];
|
||||
return postpublish_utils.publishReleaseNotes(result.tag, releaseName, assets);
|
||||
})
|
||||
.catch (function(err) {
|
||||
throw err;
|
||||
});
|
||||
5
packages/assert/src/globals.d.ts
vendored
Normal file
5
packages/assert/src/globals.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
declare module 'dirty-chai';
|
||||
|
||||
declare module 'ethereum-address' {
|
||||
const isAddress: (arg: any) => boolean;
|
||||
}
|
||||
93
packages/assert/src/index.ts
Normal file
93
packages/assert/src/index.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
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;
|
||||
|
||||
export const assert = {
|
||||
isBigNumber(variableName: string, value: BigNumber): void {
|
||||
const isBigNumber = _.isObject(value) && (value as any).isBigNumber;
|
||||
this.assert(isBigNumber, this.typeAssertionMessage(variableName, 'BigNumber', value));
|
||||
},
|
||||
isValidBaseUnitAmount(variableName: string, value: BigNumber) {
|
||||
assert.isBigNumber(variableName, value);
|
||||
const hasDecimals = value.decimalPlaces() !== 0;
|
||||
this.assert(
|
||||
!hasDecimals, `${variableName} should be in baseUnits (no decimals), found value: ${value.toNumber()}`,
|
||||
);
|
||||
},
|
||||
isUndefined(value: any, variableName?: string): void {
|
||||
this.assert(_.isUndefined(value), this.typeAssertionMessage(variableName, 'undefined', value));
|
||||
},
|
||||
isString(variableName: string, value: string): void {
|
||||
this.assert(_.isString(value), this.typeAssertionMessage(variableName, 'string', value));
|
||||
},
|
||||
isFunction(variableName: string, value: any): void {
|
||||
this.assert(_.isFunction(value), this.typeAssertionMessage(variableName, 'function', value));
|
||||
},
|
||||
isHexString(variableName: string, value: string): void {
|
||||
this.assert(_.isString(value) && HEX_REGEX.test(value),
|
||||
this.typeAssertionMessage(variableName, 'HexString', value));
|
||||
},
|
||||
isETHAddressHex(variableName: string, value: string): void {
|
||||
this.assert(ethereum_address.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
|
||||
this.assert(
|
||||
ethereum_address.isAddress(value) && value.toLowerCase() === value,
|
||||
`Checksummed addresses are not supported. Convert ${variableName} to lower case before passing`,
|
||||
);
|
||||
},
|
||||
doesBelongToStringEnum(variableName: string, value: string,
|
||||
stringEnum: any /* There is no base type for every string enum */): void {
|
||||
const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]);
|
||||
const enumValues = _.keys(stringEnum);
|
||||
const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`);
|
||||
const enumValuesAsString = enumValuesAsStrings.join(', ');
|
||||
assert.assert(
|
||||
doesBelongToStringEnum,
|
||||
`Expected ${variableName} to be one of: ${enumValuesAsString}, encountered: ${value}`,
|
||||
);
|
||||
},
|
||||
hasAtMostOneUniqueValue(value: any[], errMsg: string): void {
|
||||
this.assert(_.uniq(value).length <= 1, errMsg);
|
||||
},
|
||||
isNumber(variableName: string, value: number): void {
|
||||
this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value));
|
||||
},
|
||||
isBoolean(variableName: string, value: boolean): void {
|
||||
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);
|
||||
this.assert(isWeb3Provider, this.typeAssertionMessage(variableName, 'Web3.Provider', value));
|
||||
},
|
||||
doesConformToSchema(variableName: string, value: any, schema: Schema): void {
|
||||
const schemaValidator = new SchemaValidator();
|
||||
const validationResult = schemaValidator.validate(value, schema);
|
||||
const hasValidationErrors = validationResult.errors.length > 0;
|
||||
const msg = `Expected ${variableName} to conform to schema ${schema.id}
|
||||
Encountered: ${JSON.stringify(value, null, '\t')}
|
||||
Validation errors: ${validationResult.errors.join(', ')}`;
|
||||
this.assert(!hasValidationErrors, msg);
|
||||
},
|
||||
isHttpUrl(variableName: string, value: any): void {
|
||||
const isValidUrl = validUrl.isWebUri(value);
|
||||
this.assert(isValidUrl, this.typeAssertionMessage(variableName, 'http url', value));
|
||||
},
|
||||
isUri(variableName: string, value: any): void {
|
||||
const isValidUri = validUrl.isUri(value);
|
||||
this.assert(isValidUri, this.typeAssertionMessage(variableName, 'uri', value));
|
||||
},
|
||||
assert(condition: boolean, message: string): void {
|
||||
if (!condition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
},
|
||||
typeAssertionMessage(variableName: string, type: string, value: any): string {
|
||||
return `Expected ${variableName} to be of type ${type}, encountered: ${value}`;
|
||||
},
|
||||
};
|
||||
338
packages/assert/test/assert_test.ts
Normal file
338
packages/assert/test/assert_test.ts
Normal file
@@ -0,0 +1,338 @@
|
||||
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 {assert} from '../src/index';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('Assertions', () => {
|
||||
const variableName = 'variable';
|
||||
describe('#isBigNumber', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
new BigNumber(23),
|
||||
new BigNumber('45'),
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
'test',
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isUndefined', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
undefined,
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isUndefined.bind(assert, input, variableName)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
'test',
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isUndefined.bind(assert, input, variableName)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isString', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
'hello',
|
||||
'goodbye',
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isString.bind(assert, variableName, input)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isString.bind(assert, variableName, input)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isFunction', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
BigNumber,
|
||||
assert.isString.bind(this),
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isFunction.bind(assert, variableName, input)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isFunction.bind(assert, variableName, input)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isHexString', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
'0x61a3ed31B43c8780e905a260a35faefEc527be7516aa11c0256729b5b351bc33',
|
||||
'0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isHexString.bind(assert, variableName, input)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
'0x61a3ed31B43c8780e905a260a35faYfEc527be7516aa11c0256729b5b351bc33',
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isHexString.bind(assert, variableName, input)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isETHAddressHex', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
'0x0000000000000000000000000000000000000000',
|
||||
'0x6fffd0ae3f7d88c9b4925323f54c6e4b2918c5fd',
|
||||
'0x12459c951127e0c374ff9105dda097662a027093',
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.isETHAddressHex.bind(assert, variableName, input)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
'0x6FFFd0ae3f7d88c9b4925323f54c6e4b2918c5fd',
|
||||
'0x6FFFd0ae3f7d88c9b4925323f54c6e4',
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.isETHAddressHex.bind(assert, variableName, input)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#doesBelongToStringEnum', () => {
|
||||
enum TestEnums {
|
||||
Test1 = 'Test1',
|
||||
Test2 = 'Test2',
|
||||
}
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
TestEnums.Test1,
|
||||
TestEnums.Test2,
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.doesBelongToStringEnum.bind(assert, variableName, input, TestEnums)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.doesBelongToStringEnum.bind(assert, variableName, input, TestEnums)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#hasAtMostOneUniqueValue', () => {
|
||||
const errorMsg = 'more than one unique value';
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
['hello'],
|
||||
['goodbye', 'goodbye', 'goodbye'],
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.hasAtMostOneUniqueValue.bind(assert, input, errorMsg)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
['hello', 'goodbye'],
|
||||
['goodbye', 42, false, false],
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.hasAtMostOneUniqueValue.bind(assert, input, errorMsg)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#isNumber', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
42,
|
||||
0.00,
|
||||
21e+42,
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isNumber.bind(assert, variableName, input)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
false,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isNumber.bind(assert, variableName, input)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isBoolean', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
true,
|
||||
false,
|
||||
];
|
||||
validInputs.forEach(input => expect(assert.isBoolean.bind(assert, variableName, input)).to.not.throw());
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input => expect(assert.isBoolean.bind(assert, variableName, input)).to.throw());
|
||||
});
|
||||
});
|
||||
describe('#isWeb3Provider', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
{ send: () => 45 },
|
||||
{ sendAsync: () => 45 },
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.isWeb3Provider.bind(assert, variableName, input)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.isWeb3Provider.bind(assert, variableName, input)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#doesConformToSchema', () => {
|
||||
const schema = schemas.addressSchema;
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
'0x6fffd0ae3f7d88c9b4925323f54c6e4b2918c5fd',
|
||||
'0x12459c951127e0c374ff9105dda097662a027093',
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.doesConformToSchema.bind(assert, variableName, input, schema)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.doesConformToSchema.bind(assert, variableName, input, schema)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#isHttpUrl', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
'http://www.google.com',
|
||||
'https://api.example-relayer.net',
|
||||
'https://api.radarrelay.com/0x/v0/',
|
||||
'https://zeroex.beta.radarrelay.com:8000/0x/v0/',
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.isHttpUrl.bind(assert, variableName, input)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
'ws://www.api.example-relayer.net',
|
||||
'www.google.com',
|
||||
'api.example-relayer.net',
|
||||
'user:password@api.example-relayer.net',
|
||||
'//api.example-relayer.net',
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.isHttpUrl.bind(assert, variableName, input)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#isUri', () => {
|
||||
it('should not throw for valid input', () => {
|
||||
const validInputs = [
|
||||
'http://www.google.com',
|
||||
'https://api.example-relayer.net',
|
||||
'https://api.radarrelay.com/0x/v0/',
|
||||
'https://zeroex.beta.radarrelay.com:8000/0x/v0/',
|
||||
'ws://www.api.example-relayer.net',
|
||||
'wss://www.api.example-relayer.net',
|
||||
'user:password@api.example-relayer.net',
|
||||
];
|
||||
validInputs.forEach(input =>
|
||||
expect(assert.isUri.bind(assert, variableName, input)).to.not.throw(),
|
||||
);
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
const invalidInputs = [
|
||||
42,
|
||||
{ random: 'test' },
|
||||
undefined,
|
||||
new BigNumber(45),
|
||||
'www.google.com',
|
||||
'api.example-relayer.net',
|
||||
'//api.example-relayer.net',
|
||||
];
|
||||
invalidInputs.forEach(input =>
|
||||
expect(assert.isUri.bind(assert, variableName, input)).to.throw(),
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('#assert', () => {
|
||||
const assertMessage = 'assert not satisfied';
|
||||
it('should not throw for valid input', () => {
|
||||
expect(assert.assert.bind(assert, true, assertMessage)).to.not.throw();
|
||||
});
|
||||
it('should throw for invalid input', () => {
|
||||
expect(assert.assert.bind(assert, false, assertMessage)).to.throw();
|
||||
});
|
||||
});
|
||||
describe('#typeAssertionMessage', () => {
|
||||
it('should render correct message', () => {
|
||||
expect(assert.typeAssertionMessage('variable', 'string', 'number'))
|
||||
.to.equal(`Expected variable to be of type string, encountered: number`);
|
||||
});
|
||||
});
|
||||
});
|
||||
18
packages/assert/tsconfig.json
Normal file
18
packages/assert/tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"lib": [ "es2017", "dom"],
|
||||
"outDir": "lib",
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*",
|
||||
"./test/**/*",
|
||||
"../../node_modules/chai-typescript-typings/index.d.ts",
|
||||
"../../node_modules/web3-typescript-typings/index.d.ts"
|
||||
]
|
||||
}
|
||||
5
packages/assert/tslint.json
Normal file
5
packages/assert/tslint.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": [
|
||||
"@0xproject/tslint-config"
|
||||
]
|
||||
}
|
||||
5
packages/connect/CHANGELOG.md
Normal file
5
packages/connect/CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# CHANGELOG
|
||||
|
||||
v0.1.0 - _November 22, 2017_
|
||||
------------------------
|
||||
* Provide a HttpClient class for interacting with standard relayer api compliant HTTP urls
|
||||
1
packages/connect/README.md
Normal file
1
packages/connect/README.md
Normal file
@@ -0,0 +1 @@
|
||||
This repository contains 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)
|
||||
70
packages/connect/package.json
Normal file
70
packages/connect/package.json
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"name": "@0xproject/connect",
|
||||
"version": "0.1.0",
|
||||
"description": "A javascript library for interacting with the standard relayer api",
|
||||
"keywords": [
|
||||
"connect",
|
||||
"0xproject",
|
||||
"ethereum",
|
||||
"tokens",
|
||||
"exchange"
|
||||
],
|
||||
"main": "lib/src/index.js",
|
||||
"types": "lib/src/index.d.ts",
|
||||
"scripts": {
|
||||
"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",
|
||||
"copy_test_fixtures": "copyfiles -u 2 './test/fixtures/**/*.json' ./lib/test/fixtures",
|
||||
"lint": "tslint 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"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/0xProject/0x.js.git"
|
||||
},
|
||||
"author": "Brandon Millman",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/0xProject/0x.js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/0xProject/0x.js/packages/connect/README.md",
|
||||
"dependencies": {
|
||||
"0x.js": "^0.26.1",
|
||||
"@0xproject/assert": "^0.0.5",
|
||||
"@0xproject/json-schemas": "^0.6.8",
|
||||
"bignumber.js": "~4.1.0",
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"lodash": "^4.17.4",
|
||||
"query-string": "^5.0.1",
|
||||
"websocket": "^1.0.25"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@types/fetch-mock": "^5.12.1",
|
||||
"@types/lodash": "^4.14.77",
|
||||
"@types/mocha": "^2.2.42",
|
||||
"@types/query-string": "^5.0.1",
|
||||
"@types/websocket": "^0.0.34",
|
||||
"chai": "^4.0.1",
|
||||
"chai-as-promised": "^7.1.0",
|
||||
"chai-as-promised-typescript-typings": "0.0.3",
|
||||
"chai-typescript-typings": "^0.0.1",
|
||||
"copyfiles": "^1.2.0",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"fetch-mock": "^5.13.1",
|
||||
"mocha": "^4.0.0",
|
||||
"npm-run-all": "^4.0.2",
|
||||
"shx": "^0.2.2",
|
||||
"tslint": "5.8.0",
|
||||
"typedoc": "~0.8.0",
|
||||
"typescript": "~2.6.1",
|
||||
"web3-typescript-typings": "^0.7.1"
|
||||
}
|
||||
}
|
||||
39
packages/connect/scripts/postpublish.js
Normal file
39
packages/connect/scripts/postpublish.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const execAsync = require('async-child-process').execAsync;
|
||||
const postpublish_utils = require('../../../scripts/postpublish_utils');
|
||||
const packageJSON = require('../package.json');
|
||||
|
||||
const cwd = __dirname + '/..';
|
||||
const subPackageName = packageJSON.name;
|
||||
const S3BucketPath = 's3://connect-docs-jsons/';
|
||||
|
||||
let tag;
|
||||
let version;
|
||||
postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
|
||||
.then(function(result) {
|
||||
tag = result.tag;
|
||||
version = result.version;
|
||||
const releaseName = postpublish_utils.getReleaseName(subPackageName, version);
|
||||
return postpublish_utils.publishReleaseNotes(tag, releaseName);
|
||||
})
|
||||
.then(function(release) {
|
||||
console.log('POSTPUBLISH: Release successful, generating docs...');
|
||||
return execAsync(
|
||||
'JSON_FILE_PATH=' + __dirname + '/../docs/index.json PROJECT_DIR=' + __dirname + '/.. yarn docs:json',
|
||||
{
|
||||
cwd,
|
||||
}
|
||||
);
|
||||
})
|
||||
.then(function(result) {
|
||||
if (result.stderr !== '') {
|
||||
throw new Error(result.stderr);
|
||||
}
|
||||
const fileName = 'v' + version + '.json';
|
||||
console.log('POSTPUBLISH: Doc generation successful, uploading docs... as ', fileName);
|
||||
const s3Url = S3BucketPath + fileName;
|
||||
return execAsync('S3_URL=' + s3Url + ' yarn upload_docs_json', {
|
||||
cwd,
|
||||
});
|
||||
}).catch (function(err) {
|
||||
throw err;
|
||||
});
|
||||
6
packages/connect/src/globals.d.ts
vendored
Normal file
6
packages/connect/src/globals.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
declare module 'dirty-chai';
|
||||
|
||||
declare module '*.json' {
|
||||
const value: any;
|
||||
export default value;
|
||||
}
|
||||
177
packages/connect/src/http_client.ts
Normal file
177
packages/connect/src/http_client.ts
Normal file
@@ -0,0 +1,177 @@
|
||||
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 {
|
||||
Client,
|
||||
FeesRequest,
|
||||
FeesResponse,
|
||||
OrderbookRequest,
|
||||
OrderbookResponse,
|
||||
OrdersRequest,
|
||||
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
|
||||
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
|
||||
*/
|
||||
export class HttpClient implements Client {
|
||||
private apiEndpointUrl: string;
|
||||
/**
|
||||
* Instantiates a new HttpClient instance
|
||||
* @param url The base url for making API calls
|
||||
* @return An instance of HttpClient
|
||||
*/
|
||||
constructor(url: string) {
|
||||
assert.isHttpUrl('url', url);
|
||||
this.apiEndpointUrl = url;
|
||||
}
|
||||
/**
|
||||
* Retrieve token pair info from the API
|
||||
* @param request A TokenPairsRequest instance describing specific token information
|
||||
* to retrieve
|
||||
* @return The resulting TokenPairsItems that match the request
|
||||
*/
|
||||
public async getTokenPairsAsync(request?: TokenPairsRequest): Promise<TokenPairsItem[]> {
|
||||
if (!_.isUndefined(request)) {
|
||||
assert.doesConformToSchema('request', request, clientSchemas.relayerTokenPairsRequestSchema);
|
||||
}
|
||||
const requestOpts = {
|
||||
params: request,
|
||||
};
|
||||
const tokenPairs = await this._requestAsync('/token_pairs', RequestType.Get, requestOpts);
|
||||
assert.doesConformToSchema(
|
||||
'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema);
|
||||
_.each(tokenPairs, (tokenPair: object) => {
|
||||
typeConverters.convertStringsFieldsToBigNumbers(tokenPair, [
|
||||
'tokenA.minAmount',
|
||||
'tokenA.maxAmount',
|
||||
'tokenB.minAmount',
|
||||
'tokenB.maxAmount',
|
||||
]);
|
||||
});
|
||||
return tokenPairs;
|
||||
}
|
||||
/**
|
||||
* Retrieve orders from the API
|
||||
* @param request An OrdersRequest instance describing specific orders to retrieve
|
||||
* @return The resulting SignedOrders that match the request
|
||||
*/
|
||||
public async getOrdersAsync(request?: OrdersRequest): Promise<SignedOrder[]> {
|
||||
if (!_.isUndefined(request)) {
|
||||
assert.doesConformToSchema('request', request, clientSchemas.relayerOrdersRequestSchema);
|
||||
}
|
||||
const requestOpts = {
|
||||
params: request,
|
||||
};
|
||||
const orders = await this._requestAsync(`/orders`, RequestType.Get, requestOpts);
|
||||
assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
|
||||
_.each(orders, (order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order));
|
||||
return orders;
|
||||
}
|
||||
/**
|
||||
* Retrieve a specific order from the API
|
||||
* @param orderHash An orderHash generated from the desired order
|
||||
* @return The SignedOrder that matches the supplied orderHash
|
||||
*/
|
||||
public async getOrderAsync(orderHash: string): Promise<SignedOrder> {
|
||||
assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema);
|
||||
const order = await this._requestAsync(`/order/${orderHash}`, RequestType.Get);
|
||||
assert.doesConformToSchema('order', order, schemas.signedOrderSchema);
|
||||
typeConverters.convertOrderStringFieldsToBigNumber(order);
|
||||
return order;
|
||||
}
|
||||
/**
|
||||
* Retrieve an orderbook from the API
|
||||
* @param request An OrderbookRequest instance describing the specific orderbook to retrieve
|
||||
* @return The resulting OrderbookResponse that matches the request
|
||||
*/
|
||||
public async getOrderbookAsync(request: OrderbookRequest): Promise<OrderbookResponse> {
|
||||
assert.doesConformToSchema('request', request, clientSchemas.relayerOrderBookRequestSchema);
|
||||
const requestOpts = {
|
||||
params: request,
|
||||
};
|
||||
const orderBook = await this._requestAsync('/orderbook', RequestType.Get, requestOpts);
|
||||
assert.doesConformToSchema('orderBook', orderBook, schemas.relayerApiOrderBookResponseSchema);
|
||||
typeConverters.convertOrderbookStringFieldsToBigNumber(orderBook);
|
||||
return orderBook;
|
||||
}
|
||||
/**
|
||||
* Retrieve fee information from the API
|
||||
* @param request A FeesRequest instance describing the specific fees to retrieve
|
||||
* @return The resulting FeesResponse that matches the request
|
||||
*/
|
||||
public async getFeesAsync(request: FeesRequest): Promise<FeesResponse> {
|
||||
assert.doesConformToSchema('request', request, schemas.relayerApiFeesPayloadSchema);
|
||||
typeConverters.convertBigNumberFieldsToStrings(request, [
|
||||
'makerTokenAmount',
|
||||
'takerTokenAmount',
|
||||
'expirationUnixTimestampSec',
|
||||
'salt',
|
||||
]);
|
||||
const requestOpts = {
|
||||
payload: request,
|
||||
};
|
||||
const fees = await this._requestAsync('/fees', RequestType.Post, requestOpts);
|
||||
assert.doesConformToSchema('fees', fees, schemas.relayerApiFeesResponseSchema);
|
||||
typeConverters.convertStringsFieldsToBigNumbers(fees, ['makerFee', 'takerFee']);
|
||||
return fees;
|
||||
}
|
||||
/**
|
||||
* Submit a signed order to the API
|
||||
* @param signedOrder A SignedOrder instance to submit
|
||||
*/
|
||||
public async submitOrderAsync(signedOrder: SignedOrder): Promise<void> {
|
||||
assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema);
|
||||
const requestOpts = {
|
||||
payload: signedOrder,
|
||||
};
|
||||
await this._requestAsync('/order', RequestType.Post, requestOpts);
|
||||
}
|
||||
private async _requestAsync(path: string, requestType: RequestType, requestOptions?: RequestOptions): Promise<any> {
|
||||
const params = _.get(requestOptions, 'params');
|
||||
const payload = _.get(requestOptions, 'payload');
|
||||
let query = '';
|
||||
if (!_.isUndefined(params) && !_.isEmpty(params)) {
|
||||
const stringifiedParams = queryString.stringify(params);
|
||||
query = `?${stringifiedParams}`;
|
||||
}
|
||||
const url = `${this.apiEndpointUrl}/v0${path}${query}`;
|
||||
const headers = new Headers({
|
||||
'content-type': 'application/json',
|
||||
});
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: requestType,
|
||||
body: JSON.stringify(payload),
|
||||
headers,
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
11
packages/connect/src/index.ts
Normal file
11
packages/connect/src/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export {HttpClient} from './http_client';
|
||||
export {
|
||||
Client,
|
||||
FeesRequest,
|
||||
FeesResponse,
|
||||
OrderbookRequest,
|
||||
OrderbookResponse,
|
||||
OrdersRequest,
|
||||
TokenPairsItem,
|
||||
TokenPairsRequest,
|
||||
} from './types';
|
||||
@@ -0,0 +1,8 @@
|
||||
export const relayerOrderBookRequestSchema = {
|
||||
id: '/RelayerOrderBookRequest',
|
||||
type: 'object',
|
||||
properties: {
|
||||
baseTokenAddress: {$ref: '/Address'},
|
||||
quoteTokenAddress: {$ref: '/Address'},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
export const relayerOrderBookRequestSchema = {
|
||||
id: '/RelayerOrderBookRequest',
|
||||
type: 'object',
|
||||
properties: {
|
||||
baseTokenAddress: {$ref: '/Address'},
|
||||
quoteTokenAddress: {$ref: '/Address'},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
export const relayerOrdersRequestSchema = {
|
||||
id: '/RelayerOrdersRequest',
|
||||
type: 'object',
|
||||
properties: {
|
||||
exchangeContractAddress: {$ref: '/Address'},
|
||||
tokenAddress: {$ref: '/Address'},
|
||||
makerTokenAddress: {$ref: '/Address'},
|
||||
takerTokenAddress: {$ref: '/Address'},
|
||||
tokenA: {$ref: '/Address'},
|
||||
tokenB: {$ref: '/Address'},
|
||||
maker: {$ref: '/Address'},
|
||||
taker: {$ref: '/Address'},
|
||||
trader: {$ref: '/Address'},
|
||||
feeRecipient: {$ref: '/Address'},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
export const relayerTokenPairsRequestSchema = {
|
||||
id: '/RelayerTokenPairsRequest',
|
||||
type: 'object',
|
||||
properties: {
|
||||
tokenA: {$ref: '/Address'},
|
||||
tokenB: {$ref: '/Address'},
|
||||
},
|
||||
};
|
||||
15
packages/connect/src/schemas/schemas.ts
Normal file
15
packages/connect/src/schemas/schemas.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import {
|
||||
relayerOrderBookRequestSchema,
|
||||
} from './relayer_orderbook_request_schema';
|
||||
import {
|
||||
relayerOrdersRequestSchema,
|
||||
} from './relayer_orders_request_schema';
|
||||
import {
|
||||
relayerTokenPairsRequestSchema,
|
||||
} from './relayer_token_pairs_request_schema';
|
||||
|
||||
export const schemas = {
|
||||
relayerOrderBookRequestSchema,
|
||||
relayerOrdersRequestSchema,
|
||||
relayerTokenPairsRequestSchema,
|
||||
};
|
||||
120
packages/connect/src/types.ts
Normal file
120
packages/connect/src/types.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import {SignedOrder} from '0x.js';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
export interface Client {
|
||||
getTokenPairsAsync: (request?: TokenPairsRequest) => Promise<TokenPairsItem[]>;
|
||||
getOrdersAsync: (request?: OrdersRequest) => Promise<SignedOrder[]>;
|
||||
getOrderAsync: (orderHash: string) => Promise<SignedOrder>;
|
||||
getOrderbookAsync: (request: OrderbookRequest) => Promise<OrderbookResponse>;
|
||||
getFeesAsync: (request: FeesRequest) => Promise<FeesResponse>;
|
||||
submitOrderAsync: (signedOrder: SignedOrder) => Promise<void>;
|
||||
}
|
||||
|
||||
export interface OrderbookChannel {
|
||||
subscribe: (subscriptionOpts: OrderbookChannelSubscriptionOpts, handler: OrderbookChannelHandler) => void;
|
||||
close: () => void;
|
||||
}
|
||||
|
||||
export interface OrderbookChannelHandler {
|
||||
onSnapshot: (channel: OrderbookChannel, snapshot: OrderbookResponse) => void;
|
||||
onUpdate: (channel: OrderbookChannel, order: SignedOrder) => void;
|
||||
onError: (channel: OrderbookChannel, err: Error) => void;
|
||||
onClose: (channel: OrderbookChannel) => void;
|
||||
}
|
||||
|
||||
export type OrderbookChannelMessage =
|
||||
SnapshotOrderbookChannelMessage |
|
||||
UpdateOrderbookChannelMessage |
|
||||
UnknownOrderbookChannelMessage;
|
||||
|
||||
export enum OrderbookChannelMessageTypes {
|
||||
Snapshot = 'snapshot',
|
||||
Update = 'update',
|
||||
Unknown = 'unknown',
|
||||
}
|
||||
|
||||
export interface SnapshotOrderbookChannelMessage {
|
||||
type: OrderbookChannelMessageTypes.Snapshot;
|
||||
payload: OrderbookResponse;
|
||||
}
|
||||
|
||||
export interface UpdateOrderbookChannelMessage {
|
||||
type: OrderbookChannelMessageTypes.Update;
|
||||
payload: SignedOrder;
|
||||
}
|
||||
|
||||
export interface UnknownOrderbookChannelMessage {
|
||||
type: OrderbookChannelMessageTypes.Unknown;
|
||||
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 interface TokenPairsRequest {
|
||||
tokenA?: string;
|
||||
tokenB?: string;
|
||||
}
|
||||
|
||||
export interface TokenPairsItem {
|
||||
tokenA: TokenTradeInfo;
|
||||
tokenB: TokenTradeInfo;
|
||||
}
|
||||
|
||||
export interface TokenTradeInfo {
|
||||
address: string;
|
||||
minAmount: BigNumber;
|
||||
maxAmount: BigNumber;
|
||||
precision: number;
|
||||
}
|
||||
|
||||
export interface OrdersRequest {
|
||||
exchangeContractAddress?: string;
|
||||
tokenAddress?: string;
|
||||
makerTokenAddress?: string;
|
||||
takerTokenAddress?: string;
|
||||
tokenA?: string;
|
||||
tokenB?: string;
|
||||
maker?: string;
|
||||
taker?: string;
|
||||
trader?: string;
|
||||
feeRecipient?: string;
|
||||
}
|
||||
|
||||
export interface OrderbookRequest {
|
||||
baseTokenAddress: string;
|
||||
quoteTokenAddress: string;
|
||||
}
|
||||
|
||||
export interface OrderbookResponse {
|
||||
bids: SignedOrder[];
|
||||
asks: SignedOrder[];
|
||||
}
|
||||
|
||||
export interface FeesRequest {
|
||||
exchangeContractAddress: string;
|
||||
maker: string;
|
||||
taker: string;
|
||||
makerTokenAddress: string;
|
||||
takerTokenAddress: string;
|
||||
makerTokenAmount: BigNumber;
|
||||
takerTokenAmount: BigNumber;
|
||||
expirationUnixTimestampSec: BigNumber;
|
||||
salt: BigNumber;
|
||||
}
|
||||
|
||||
export interface FeesResponse {
|
||||
feeRecipient: string;
|
||||
makerFee: BigNumber;
|
||||
takerFee: BigNumber;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import * as _ from 'lodash';
|
||||
import {SignedOrder} from '0x.js';
|
||||
import {assert} from '@0xproject/assert';
|
||||
import {schemas} from '@0xproject/json-schemas';
|
||||
import {
|
||||
OrderbookChannelMessage,
|
||||
OrderbookChannelMessageTypes,
|
||||
} from '../types';
|
||||
import {typeConverters} from './type_converters';
|
||||
|
||||
export const orderbookChannelMessageParsers = {
|
||||
parser(utf8Data: string): OrderbookChannelMessage {
|
||||
const messageObj = JSON.parse(utf8Data);
|
||||
const type: string = _.get(messageObj, 'type');
|
||||
assert.assert(!_.isUndefined(type), `Message is missing a type parameter: ${utf8Data}`);
|
||||
switch (type) {
|
||||
case (OrderbookChannelMessageTypes.Snapshot): {
|
||||
assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema);
|
||||
const orderbook = messageObj.payload;
|
||||
typeConverters.convertOrderbookStringFieldsToBigNumber(orderbook);
|
||||
return {
|
||||
type,
|
||||
payload: orderbook,
|
||||
};
|
||||
}
|
||||
case (OrderbookChannelMessageTypes.Update): {
|
||||
assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema);
|
||||
const order = messageObj.payload;
|
||||
typeConverters.convertOrderStringFieldsToBigNumber(order);
|
||||
return {
|
||||
type,
|
||||
payload: order,
|
||||
};
|
||||
}
|
||||
default: {
|
||||
return {
|
||||
type: OrderbookChannelMessageTypes.Unknown,
|
||||
payload: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
31
packages/connect/src/utils/type_converters.ts
Normal file
31
packages/connect/src/utils/type_converters.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import * as _ from 'lodash';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
// TODO: convert all of these to non-mutating, pure functions
|
||||
export const typeConverters = {
|
||||
convertOrderbookStringFieldsToBigNumber(orderbook: object): void {
|
||||
_.each(orderbook, (orders: object[]) => {
|
||||
_.each(orders, (order: object) => this.convertOrderStringFieldsToBigNumber(order));
|
||||
});
|
||||
},
|
||||
convertOrderStringFieldsToBigNumber(order: object): void {
|
||||
this.convertStringsFieldsToBigNumbers(order, [
|
||||
'makerTokenAmount',
|
||||
'takerTokenAmount',
|
||||
'makerFee',
|
||||
'takerFee',
|
||||
'expirationUnixTimestampSec',
|
||||
'salt',
|
||||
]);
|
||||
},
|
||||
convertBigNumberFieldsToStrings(obj: object, fields: string[]): void {
|
||||
_.each(fields, field => {
|
||||
_.update(obj, field, (value: BigNumber) => value.toString());
|
||||
});
|
||||
},
|
||||
convertStringsFieldsToBigNumbers(obj: object, fields: string[]): void {
|
||||
_.each(fields, field => {
|
||||
_.update(obj, field, (value: string) => new BigNumber(value));
|
||||
});
|
||||
},
|
||||
};
|
||||
127
packages/connect/src/ws_orderbook_channel.ts
Normal file
127
packages/connect/src/ws_orderbook_channel.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
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 {
|
||||
OrderbookChannel,
|
||||
OrderbookChannelHandler,
|
||||
OrderbookChannelMessageTypes,
|
||||
OrderbookChannelSubscriptionOpts,
|
||||
} 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
|
||||
*/
|
||||
export class WebSocketOrderbookChannel implements OrderbookChannel {
|
||||
private apiEndpointUrl: string;
|
||||
private client: WebSocket.client;
|
||||
private connectionIfExists?: WebSocket.connection;
|
||||
/**
|
||||
* Instantiates a new WebSocketOrderbookChannel instance
|
||||
* @param url The base url for making API calls
|
||||
* @return An instance of WebSocketOrderbookChannel
|
||||
*/
|
||||
constructor(url: string) {
|
||||
assert.isUri('url', url);
|
||||
this.apiEndpointUrl = url;
|
||||
this.client = new WebSocket.client();
|
||||
}
|
||||
/**
|
||||
* Subscribe to orderbook snapshots and updates from the websocket
|
||||
* @param subscriptionOpts An OrderbookChannelSubscriptionOpts instance describing which
|
||||
* token pair to subscribe to
|
||||
* @param handler An OrderbookChannelHandler instance that responds to various
|
||||
* channel updates
|
||||
*/
|
||||
public subscribe(subscriptionOpts: OrderbookChannelSubscriptionOpts, handler: OrderbookChannelHandler): void {
|
||||
assert.doesConformToSchema(
|
||||
'subscriptionOpts', subscriptionOpts, schemas.relayerApiOrderbookChannelSubscribePayload);
|
||||
assert.isFunction('handler.onSnapshot', _.get(handler, 'onSnapshot'));
|
||||
assert.isFunction('handler.onUpdate', _.get(handler, 'onUpdate'));
|
||||
assert.isFunction('handler.onError', _.get(handler, 'onError'));
|
||||
assert.isFunction('handler.onClose', _.get(handler, 'onClose'));
|
||||
const subscribeMessage = {
|
||||
type: 'subscribe',
|
||||
channel: 'orderbook',
|
||||
payload: subscriptionOpts,
|
||||
};
|
||||
this._getConnection((error, connection) => {
|
||||
if (!_.isUndefined(error)) {
|
||||
handler.onError(this, error);
|
||||
} else if (!_.isUndefined(connection) && connection.connected) {
|
||||
connection.on(ConnectionEventType.Error, wsError => {
|
||||
handler.onError(this, wsError);
|
||||
});
|
||||
connection.on(ConnectionEventType.Close, () => {
|
||||
handler.onClose(this);
|
||||
});
|
||||
connection.on(ConnectionEventType.Message, message => {
|
||||
this._handleWebSocketMessage(message, handler);
|
||||
});
|
||||
connection.sendUTF(JSON.stringify(subscribeMessage));
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Close the websocket and stop receiving updates
|
||||
*/
|
||||
public close() {
|
||||
if (!_.isUndefined(this.connectionIfExists)) {
|
||||
this.connectionIfExists.close();
|
||||
}
|
||||
}
|
||||
private _getConnection(callback: (error?: Error, connection?: WebSocket.connection) => void) {
|
||||
if (!_.isUndefined(this.connectionIfExists) && this.connectionIfExists.connected) {
|
||||
callback(undefined, this.connectionIfExists);
|
||||
} else {
|
||||
this.client.on(ClientEventType.Connect, connection => {
|
||||
this.connectionIfExists = connection;
|
||||
callback(undefined, this.connectionIfExists);
|
||||
});
|
||||
this.client.on(ClientEventType.ConnectFailed, error => {
|
||||
callback(error, undefined);
|
||||
});
|
||||
this.client.connect(this.apiEndpointUrl);
|
||||
}
|
||||
}
|
||||
private _handleWebSocketMessage(message: WebSocket.IMessage, handler: OrderbookChannelHandler): void {
|
||||
if (!_.isUndefined(message.utf8Data)) {
|
||||
try {
|
||||
const utf8Data = message.utf8Data;
|
||||
const parserResult = orderbookChannelMessageParsers.parser(utf8Data);
|
||||
const type = parserResult.type;
|
||||
switch (parserResult.type) {
|
||||
case (OrderbookChannelMessageTypes.Snapshot): {
|
||||
handler.onSnapshot(this, parserResult.payload);
|
||||
break;
|
||||
}
|
||||
case (OrderbookChannelMessageTypes.Update): {
|
||||
handler.onUpdate(this, parserResult.payload);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
handler.onError(this, new Error(`Message has missing a type parameter: ${utf8Data}`));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
handler.onError(this, error);
|
||||
}
|
||||
} else {
|
||||
handler.onError(this, new Error(`Message does not contain utf8Data`));
|
||||
}
|
||||
}
|
||||
}
|
||||
5
packages/connect/test/fixtures/standard_relayer_api/fees.json
vendored
Normal file
5
packages/connect/test/fixtures/standard_relayer_api/fees.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"feeRecipient": "0x323b5d4c32345ced77393b3530b1eed0f346429d",
|
||||
"makerFee": "10000000000000000",
|
||||
"takerFee": "30000000000000000"
|
||||
}
|
||||
8
packages/connect/test/fixtures/standard_relayer_api/fees.ts
vendored
Normal file
8
packages/connect/test/fixtures/standard_relayer_api/fees.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import {FeesResponse} from '../../../src/types';
|
||||
|
||||
export const feesResponse: FeesResponse = {
|
||||
feeRecipient: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
makerFee: new BigNumber('10000000000000000'),
|
||||
takerFee: new BigNumber('30000000000000000'),
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"maker": "0x9e56625509c2f60af937f23b7b532600390e8c8b",
|
||||
"taker": "0xa2b31dacf30a9c50ca473337c01d8a201ae33e32",
|
||||
"makerFee": "100000000000000",
|
||||
"takerFee": "200000000000000",
|
||||
"makerTokenAmount": "10000000000000000",
|
||||
"takerTokenAmount": "20000000000000000",
|
||||
"makerTokenAddress": "0x323b5d4c32345ced77393b3530b1eed0f346429d",
|
||||
"takerTokenAddress": "0xef7fff64389b814a946f3e92105513705ca6b990",
|
||||
"salt": "256",
|
||||
"feeRecipient": "0xb046140686d052fff581f63f8136cce132e857da",
|
||||
"exchangeContractAddress": "0x12459c951127e0c374ff9105dda097662a027093",
|
||||
"expirationUnixTimestampSec": "42",
|
||||
"ecSignature": {
|
||||
"v": 27,
|
||||
"r": "0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33",
|
||||
"s": "0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
export const orderResponse = {
|
||||
maker: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
taker: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||
makerFee: new BigNumber('100000000000000'),
|
||||
takerFee: new BigNumber('200000000000000'),
|
||||
makerTokenAmount: new BigNumber('10000000000000000'),
|
||||
takerTokenAmount: new BigNumber('20000000000000000'),
|
||||
makerTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
takerTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
salt: new BigNumber('256'),
|
||||
feeRecipient: '0xb046140686d052fff581f63f8136cce132e857da',
|
||||
exchangeContractAddress: '0x12459c951127e0c374ff9105dda097662a027093',
|
||||
expirationUnixTimestampSec: new BigNumber('42'),
|
||||
ecSignature: {
|
||||
v: 27,
|
||||
r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
|
||||
s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
|
||||
},
|
||||
};
|
||||
44
packages/connect/test/fixtures/standard_relayer_api/orderbook.json
vendored
Normal file
44
packages/connect/test/fixtures/standard_relayer_api/orderbook.json
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"bids": [
|
||||
{
|
||||
"maker": "0x9e56625509c2f60af937f23b7b532600390e8c8b",
|
||||
"taker": "0xa2b31dacf30a9c50ca473337c01d8a201ae33e32",
|
||||
"makerFee": "100000000000000",
|
||||
"takerFee": "200000000000000",
|
||||
"makerTokenAmount": "10000000000000000",
|
||||
"takerTokenAmount": "20000000000000000",
|
||||
"makerTokenAddress": "0x323b5d4c32345ced77393b3530b1eed0f346429d",
|
||||
"takerTokenAddress": "0xef7fff64389b814a946f3e92105513705ca6b990",
|
||||
"salt": "256",
|
||||
"feeRecipient": "0xb046140686d052fff581f63f8136cce132e857da",
|
||||
"exchangeContractAddress": "0x12459c951127e0c374ff9105dda097662a027093",
|
||||
"expirationUnixTimestampSec": "42",
|
||||
"ecSignature": {
|
||||
"v": 27,
|
||||
"r": "0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33",
|
||||
"s": "0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254"
|
||||
}
|
||||
}
|
||||
],
|
||||
"asks": [
|
||||
{
|
||||
"maker": "0x9e56625509c2f60af937f23b7b532600390e8c8b",
|
||||
"taker": "0xa2b31dacf30a9c50ca473337c01d8a201ae33e32",
|
||||
"makerFee": "100000000000000",
|
||||
"takerFee": "200000000000000",
|
||||
"makerTokenAmount": "10000000000000000",
|
||||
"takerTokenAmount": "20000000000000000",
|
||||
"makerTokenAddress": "0x323b5d4c32345ced77393b3530b1eed0f346429d",
|
||||
"takerTokenAddress": "0xef7fff64389b814a946f3e92105513705ca6b990",
|
||||
"salt": "256",
|
||||
"feeRecipient": "0xb046140686d052fff581f63f8136cce132e857da",
|
||||
"exchangeContractAddress": "0x12459c951127e0c374ff9105dda097662a027093",
|
||||
"expirationUnixTimestampSec": "42",
|
||||
"ecSignature": {
|
||||
"v": 27,
|
||||
"r": "0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33",
|
||||
"s": "0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
46
packages/connect/test/fixtures/standard_relayer_api/orderbook.ts
vendored
Normal file
46
packages/connect/test/fixtures/standard_relayer_api/orderbook.ts
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
export const orderbookResponse = {
|
||||
bids: [
|
||||
{
|
||||
maker: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
taker: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||
makerFee: new BigNumber('100000000000000'),
|
||||
takerFee: new BigNumber('200000000000000'),
|
||||
makerTokenAmount: new BigNumber('10000000000000000'),
|
||||
takerTokenAmount: new BigNumber('20000000000000000'),
|
||||
makerTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
takerTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
salt: new BigNumber('256'),
|
||||
feeRecipient: '0xb046140686d052fff581f63f8136cce132e857da',
|
||||
exchangeContractAddress: '0x12459c951127e0c374ff9105dda097662a027093',
|
||||
expirationUnixTimestampSec: new BigNumber('42'),
|
||||
ecSignature: {
|
||||
v: 27,
|
||||
r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
|
||||
s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
|
||||
},
|
||||
},
|
||||
],
|
||||
asks: [
|
||||
{
|
||||
maker: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
taker: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||
makerFee: new BigNumber('100000000000000'),
|
||||
takerFee: new BigNumber('200000000000000'),
|
||||
makerTokenAmount: new BigNumber('10000000000000000'),
|
||||
takerTokenAmount: new BigNumber('20000000000000000'),
|
||||
makerTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
takerTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
salt: new BigNumber('256'),
|
||||
feeRecipient: '0xb046140686d052fff581f63f8136cce132e857da',
|
||||
exchangeContractAddress: '0x12459c951127e0c374ff9105dda097662a027093',
|
||||
expirationUnixTimestampSec: new BigNumber('42'),
|
||||
ecSignature: {
|
||||
v: 27,
|
||||
r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
|
||||
s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
21
packages/connect/test/fixtures/standard_relayer_api/orders.json
vendored
Normal file
21
packages/connect/test/fixtures/standard_relayer_api/orders.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
[
|
||||
{
|
||||
"maker": "0x9e56625509c2f60af937f23b7b532600390e8c8b",
|
||||
"taker": "0xa2b31dacf30a9c50ca473337c01d8a201ae33e32",
|
||||
"makerFee": "100000000000000",
|
||||
"takerFee": "200000000000000",
|
||||
"makerTokenAmount": "10000000000000000",
|
||||
"takerTokenAmount": "20000000000000000",
|
||||
"makerTokenAddress": "0x323b5d4c32345ced77393b3530b1eed0f346429d",
|
||||
"takerTokenAddress": "0xef7fff64389b814a946f3e92105513705ca6b990",
|
||||
"salt": "256",
|
||||
"feeRecipient": "0x9e56625509c2f60af937f23b7b532600390e8c8b",
|
||||
"exchangeContractAddress": "0x9e56625509c2f60af937f23b7b532600390e8c8b",
|
||||
"expirationUnixTimestampSec": "42",
|
||||
"ecSignature": {
|
||||
"v": 27,
|
||||
"r": "0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33",
|
||||
"s": "0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254"
|
||||
}
|
||||
}
|
||||
]
|
||||
23
packages/connect/test/fixtures/standard_relayer_api/orders.ts
vendored
Normal file
23
packages/connect/test/fixtures/standard_relayer_api/orders.ts
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
|
||||
export const ordersResponse = [
|
||||
{
|
||||
maker: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
taker: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||
makerFee: new BigNumber('100000000000000'),
|
||||
takerFee: new BigNumber('200000000000000'),
|
||||
makerTokenAmount: new BigNumber('10000000000000000'),
|
||||
takerTokenAmount: new BigNumber('20000000000000000'),
|
||||
makerTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
takerTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
salt: new BigNumber('256'),
|
||||
feeRecipient: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
exchangeContractAddress: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
expirationUnixTimestampSec: new BigNumber('42'),
|
||||
ecSignature: {
|
||||
v: 27,
|
||||
r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33',
|
||||
s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254',
|
||||
},
|
||||
},
|
||||
];
|
||||
17
packages/connect/test/fixtures/standard_relayer_api/snapshot_orderbook_channel_message.ts
vendored
Normal file
17
packages/connect/test/fixtures/standard_relayer_api/snapshot_orderbook_channel_message.ts
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import * as orderbookJSON from './orderbook.json';
|
||||
|
||||
const orderbookJsonString = JSON.stringify(orderbookJSON);
|
||||
|
||||
export const snapshotOrderbookChannelMessage = `{
|
||||
"type": "snapshot",
|
||||
"channel": "orderbook",
|
||||
"channelId": 1,
|
||||
"payload": ${orderbookJsonString}
|
||||
}`;
|
||||
|
||||
export const malformedSnapshotOrderbookChannelMessage = `{
|
||||
"type": "snapshot",
|
||||
"channel": "orderbook",
|
||||
"channelId": 1,
|
||||
"payload": {}
|
||||
}`;
|
||||
16
packages/connect/test/fixtures/standard_relayer_api/token_pairs.json
vendored
Normal file
16
packages/connect/test/fixtures/standard_relayer_api/token_pairs.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
[
|
||||
{
|
||||
"tokenA": {
|
||||
"address": "0x323b5d4c32345ced77393b3530b1eed0f346429d",
|
||||
"minAmount": "0",
|
||||
"maxAmount": "10000000000000000000",
|
||||
"precision": 5
|
||||
},
|
||||
"tokenB": {
|
||||
"address": "0xef7fff64389b814a946f3e92105513705ca6b990",
|
||||
"minAmount": "0",
|
||||
"maxAmount": "50000000000000000000",
|
||||
"precision": 5
|
||||
}
|
||||
}
|
||||
]
|
||||
19
packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts
vendored
Normal file
19
packages/connect/test/fixtures/standard_relayer_api/token_pairs.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import {TokenPairsItem} from '../../../src/types';
|
||||
|
||||
export const tokenPairsResponse: TokenPairsItem[] = [
|
||||
{
|
||||
tokenA: {
|
||||
address: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
minAmount: new BigNumber(0),
|
||||
maxAmount: new BigNumber('10000000000000000000'),
|
||||
precision: 5,
|
||||
},
|
||||
tokenB: {
|
||||
address: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
minAmount: new BigNumber(0),
|
||||
maxAmount: new BigNumber('50000000000000000000'),
|
||||
precision: 5,
|
||||
},
|
||||
},
|
||||
];
|
||||
10
packages/connect/test/fixtures/standard_relayer_api/unknown_orderbook_channel_message.ts
vendored
Normal file
10
packages/connect/test/fixtures/standard_relayer_api/unknown_orderbook_channel_message.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
import * as orderResponseJSON from './order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f.json';
|
||||
|
||||
const orderJSONString = JSON.stringify(orderResponseJSON);
|
||||
|
||||
export const unknownOrderbookChannelMessage = `{
|
||||
"type": "superGoodUpdate",
|
||||
"channel": "orderbook",
|
||||
"channelId": 1,
|
||||
"payload": ${orderJSONString}
|
||||
}`;
|
||||
17
packages/connect/test/fixtures/standard_relayer_api/update_orderbook_channel_message.ts
vendored
Normal file
17
packages/connect/test/fixtures/standard_relayer_api/update_orderbook_channel_message.ts
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import * as orderResponseJSON from './order/0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f.json';
|
||||
|
||||
const orderJSONString = JSON.stringify(orderResponseJSON);
|
||||
|
||||
export const updateOrderbookChannelMessage = `{
|
||||
"type": "update",
|
||||
"channel": "orderbook",
|
||||
"channelId": 1,
|
||||
"payload": ${orderJSONString}
|
||||
}`;
|
||||
|
||||
export const malformedUpdateOrderbookChannelMessage = `{
|
||||
"type": "update",
|
||||
"channel": "orderbook",
|
||||
"channelId": 1,
|
||||
"payload": {}
|
||||
}`;
|
||||
130
packages/connect/test/http_client_test.ts
Normal file
130
packages/connect/test/http_client_test.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import 'mocha';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as fetchMock from 'fetch-mock';
|
||||
import {BigNumber} from 'bignumber.js';
|
||||
import {HttpClient} from '../src/index';
|
||||
import {feesResponse} from './fixtures/standard_relayer_api/fees';
|
||||
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 * as orderbookJSON from './fixtures/standard_relayer_api/orderbook.json';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
chai.use(chaiAsPromised);
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('HttpClient', () => {
|
||||
const relayUrl = 'https://example.com';
|
||||
const relayerClient = new HttpClient(relayUrl);
|
||||
afterEach(() => {
|
||||
fetchMock.restore();
|
||||
});
|
||||
describe('#getTokenPairsAsync', () => {
|
||||
const url = `${relayUrl}/v0/token_pairs`;
|
||||
it('gets token pairs', async () => {
|
||||
fetchMock.get(url, tokenPairsResponseJSON);
|
||||
const tokenPairs = await relayerClient.getTokenPairsAsync();
|
||||
expect(tokenPairs).to.be.deep.equal(tokenPairsResponse);
|
||||
});
|
||||
it('gets specfic token pairs for request', async () => {
|
||||
const tokenAddress = '0x323b5d4c32345ced77393b3530b1eed0f346429d';
|
||||
const tokenPairsRequest = {
|
||||
tokenA: tokenAddress,
|
||||
};
|
||||
const urlWithQuery = `${url}?tokenA=${tokenAddress}`;
|
||||
fetchMock.get(urlWithQuery, tokenPairsResponseJSON);
|
||||
const tokenPairs = await relayerClient.getTokenPairsAsync(tokenPairsRequest);
|
||||
expect(tokenPairs).to.be.deep.equal(tokenPairsResponse);
|
||||
});
|
||||
it('throws an error for invalid JSON response', async () => {
|
||||
fetchMock.get(url, {test: 'dummy'});
|
||||
expect(relayerClient.getTokenPairsAsync()).to.be.rejected();
|
||||
});
|
||||
});
|
||||
describe('#getOrdersAsync', () => {
|
||||
const url = `${relayUrl}/v0/orders`;
|
||||
it('gets orders', async () => {
|
||||
fetchMock.get(url, ordersResponseJSON);
|
||||
const orders = await relayerClient.getOrdersAsync();
|
||||
expect(orders).to.be.deep.equal(ordersResponse);
|
||||
});
|
||||
it('gets specfic orders for request', async () => {
|
||||
const tokenAddress = '0x323b5d4c32345ced77393b3530b1eed0f346429d';
|
||||
const ordersRequest = {
|
||||
tokenA: tokenAddress,
|
||||
};
|
||||
const urlWithQuery = `${url}?tokenA=${tokenAddress}`;
|
||||
fetchMock.get(urlWithQuery, ordersResponseJSON);
|
||||
const orders = await relayerClient.getOrdersAsync(ordersRequest);
|
||||
expect(orders).to.be.deep.equal(ordersResponse);
|
||||
});
|
||||
it('throws an error for invalid JSON response', async () => {
|
||||
fetchMock.get(url, {test: 'dummy'});
|
||||
expect(relayerClient.getOrdersAsync()).to.be.rejected();
|
||||
});
|
||||
});
|
||||
describe('#getOrderAsync', () => {
|
||||
const orderHash = '0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
|
||||
const url = `${relayUrl}/v0/order/${orderHash}`;
|
||||
it('gets order', async () => {
|
||||
fetchMock.get(url, orderResponseJSON);
|
||||
const order = await relayerClient.getOrderAsync(orderHash);
|
||||
expect(order).to.be.deep.equal(orderResponse);
|
||||
});
|
||||
it('throws an error for invalid JSON response', async () => {
|
||||
fetchMock.get(url, {test: 'dummy'});
|
||||
expect(relayerClient.getOrderAsync(orderHash)).to.be.rejected();
|
||||
});
|
||||
});
|
||||
describe('#getOrderBookAsync', () => {
|
||||
const request = {
|
||||
baseTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
quoteTokenAddress: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||
};
|
||||
// tslint:disable-next-line:max-line-length
|
||||
const url = `${relayUrl}/v0/orderbook?baseTokenAddress=${request.baseTokenAddress}"eTokenAddress=${request.quoteTokenAddress}`;
|
||||
it('gets order book', async () => {
|
||||
fetchMock.get(url, orderbookJSON);
|
||||
const orderbook = await relayerClient.getOrderbookAsync(request);
|
||||
expect(orderbook).to.be.deep.equal(orderbookResponse);
|
||||
});
|
||||
it('throws an error for invalid JSON response', async () => {
|
||||
fetchMock.get(url, {test: 'dummy'});
|
||||
expect(relayerClient.getOrderbookAsync(request)).to.be.rejected();
|
||||
});
|
||||
});
|
||||
describe('#getFeesAsync', () => {
|
||||
const request = {
|
||||
exchangeContractAddress: '0x12459c951127e0c374ff9105dda097662a027093',
|
||||
maker: '0x9e56625509c2f60af937f23b7b532600390e8c8b',
|
||||
taker: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||
makerTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
takerTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
makerTokenAmount: new BigNumber('10000000000000000000'),
|
||||
takerTokenAmount: new BigNumber('30000000000000000000'),
|
||||
salt: new BigNumber('256'),
|
||||
expirationUnixTimestampSec: new BigNumber('42'),
|
||||
};
|
||||
const url = `${relayUrl}/v0/fees`;
|
||||
it('gets fees', async () => {
|
||||
fetchMock.post(url, feesResponseJSON);
|
||||
const fees = await relayerClient.getFeesAsync(request);
|
||||
expect(fees).to.be.deep.equal(feesResponse);
|
||||
});
|
||||
it('throws an error for invalid JSON response', async () => {
|
||||
fetchMock.post(url, {test: 'dummy'});
|
||||
expect(relayerClient.getFeesAsync(request)).to.be.rejected();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
import 'mocha';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
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';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('orderbookChannelMessageParsers', () => {
|
||||
describe('#parser', () => {
|
||||
it('parses snapshot messages', () => {
|
||||
const snapshotMessage = orderbookChannelMessageParsers.parser(snapshotOrderbookChannelMessage);
|
||||
expect(snapshotMessage.type).to.be.equal('snapshot');
|
||||
expect(snapshotMessage.payload).to.be.deep.equal(orderbookResponse);
|
||||
});
|
||||
it('parses update messages', () => {
|
||||
const updateMessage = orderbookChannelMessageParsers.parser(updateOrderbookChannelMessage);
|
||||
expect(updateMessage.type).to.be.equal('update');
|
||||
expect(updateMessage.payload).to.be.deep.equal(orderResponse);
|
||||
});
|
||||
it('returns unknown message for messages with unsupported types', () => {
|
||||
const unknownMessage = orderbookChannelMessageParsers.parser(unknownOrderbookChannelMessage);
|
||||
expect(unknownMessage.type).to.be.equal('unknown');
|
||||
expect(unknownMessage.payload).to.be.undefined();
|
||||
});
|
||||
it('throws when message does not include a type', () => {
|
||||
const typelessMessage = `{
|
||||
"channel": "orderbook",
|
||||
"channelId": 1,
|
||||
"payload": {}
|
||||
}`;
|
||||
const badCall = () => orderbookChannelMessageParsers.parser(typelessMessage);
|
||||
expect(badCall).throws(`Message is missing a type parameter: ${typelessMessage}`);
|
||||
});
|
||||
it('throws when snapshot message has malformed payload', () => {
|
||||
const badCall = () =>
|
||||
orderbookChannelMessageParsers.parser(malformedSnapshotOrderbookChannelMessage);
|
||||
// tslint:disable-next-line:max-line-length
|
||||
const errMsg = 'Validation errors: instance.payload requires property "bids", instance.payload requires property "asks"';
|
||||
expect(badCall).throws(errMsg);
|
||||
});
|
||||
it('throws when update message has malformed payload', () => {
|
||||
const badCall = () =>
|
||||
orderbookChannelMessageParsers.parser(malformedUpdateOrderbookChannelMessage);
|
||||
expect(badCall).throws(/^Expected message to conform to schema/);
|
||||
});
|
||||
it('throws when input message is not valid JSON', () => {
|
||||
const nonJsonString = 'h93b{sdfs9fsd f';
|
||||
const badCall = () => orderbookChannelMessageParsers.parser(nonJsonString);
|
||||
expect(badCall).throws('Unexpected token h in JSON at position 0');
|
||||
});
|
||||
});
|
||||
});
|
||||
46
packages/connect/test/ws_orderbook_channel_test.ts
Normal file
46
packages/connect/test/ws_orderbook_channel_test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'mocha';
|
||||
import * as _ from 'lodash';
|
||||
import * as dirtyChai from 'dirty-chai';
|
||||
import * as chai from 'chai';
|
||||
import {
|
||||
WebSocketOrderbookChannel,
|
||||
} from '../src/ws_orderbook_channel';
|
||||
|
||||
chai.config.includeStack = true;
|
||||
chai.use(dirtyChai);
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('WebSocketOrderbookChannel', () => {
|
||||
const websocketUrl = 'ws://localhost:8080';
|
||||
const orderbookChannel = new WebSocketOrderbookChannel(websocketUrl);
|
||||
const subscriptionOpts = {
|
||||
baseTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||
quoteTokenAddress: '0xef7fff64389b814a946f3e92105513705ca6b990',
|
||||
snapshot: true,
|
||||
limit: 100,
|
||||
};
|
||||
const emptyOrderbookChannelHandler = {
|
||||
onSnapshot: () => { _.noop(); },
|
||||
onUpdate: () => { _.noop(); },
|
||||
onError: () => { _.noop(); },
|
||||
onClose: () => { _.noop(); },
|
||||
};
|
||||
describe('#subscribe', () => {
|
||||
it('throws when subscriptionOpts does not conform to schema', () => {
|
||||
const badSubscribeCall = orderbookChannel.subscribe.bind(
|
||||
orderbookChannel, {}, emptyOrderbookChannelHandler);
|
||||
// tslint:disable-next-line:max-line-length
|
||||
expect(badSubscribeCall).throws('Expected subscriptionOpts to conform to schema /RelayerApiOrderbookChannelSubscribePayload\nEncountered: {}\nValidation errors: instance requires property "baseTokenAddress", instance requires property "quoteTokenAddress"');
|
||||
});
|
||||
it('throws when handler has the incorrect members', () => {
|
||||
const badSubscribeCall = orderbookChannel.subscribe.bind(orderbookChannel, subscriptionOpts, {});
|
||||
expect(badSubscribeCall)
|
||||
.throws('Expected handler.onSnapshot to be of type function, encountered: undefined');
|
||||
});
|
||||
it('does not throw when inputs are of correct types', () => {
|
||||
const goodSubscribeCall = orderbookChannel.subscribe.bind(
|
||||
orderbookChannel, subscriptionOpts, emptyOrderbookChannelHandler);
|
||||
expect(goodSubscribeCall).to.not.throw();
|
||||
});
|
||||
});
|
||||
});
|
||||
19
packages/connect/tsconfig.json
Normal file
19
packages/connect/tsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"lib": [ "es2015", "dom" ],
|
||||
"outDir": "lib",
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*",
|
||||
"./test/**/*",
|
||||
"../../node_modules/chai-as-promised-typescript-typings/index.d.ts",
|
||||
"../../node_modules/chai-typescript-typings/index.d.ts",
|
||||
"../../node_modules/web3-typescript-typings/index.d.ts"
|
||||
]
|
||||
}
|
||||
5
packages/connect/tslint.json
Normal file
5
packages/connect/tslint.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": [
|
||||
"@0xproject/tslint-config"
|
||||
]
|
||||
}
|
||||
5
packages/json-schemas/CHANGELOG.md
Normal file
5
packages/json-schemas/CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# CHANGELOG
|
||||
|
||||
v0.6.7 - _Nov. 14, 2017_
|
||||
------------------------
|
||||
* Re-publish JSON-schema previously published under NPM package 0x-json-schemas
|
||||
24
packages/json-schemas/README.md
Normal file
24
packages/json-schemas/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
json-schemas
|
||||
------------
|
||||
|
||||
Contains 0x-related json schemas
|
||||
|
||||
## Install:
|
||||
|
||||
```bash
|
||||
npm install @0xproject/json-schemas --save
|
||||
```
|
||||
|
||||
## Usage:
|
||||
```
|
||||
import {SchemaValidator, ValidatorResult, schemas} from '@0xproject/json-schemas';
|
||||
|
||||
const {orderSchema} = schemas;
|
||||
const validator = new SchemaValidator();
|
||||
|
||||
const order = {
|
||||
...
|
||||
};
|
||||
const validatorResult: ValidatorResult = validator.validate(order, orderSchema); // Contains all errors
|
||||
const isValid: boolean = validator.isValid(order, orderSchema); // Only returns boolean
|
||||
```
|
||||
46
packages/json-schemas/package.json
Normal file
46
packages/json-schemas/package.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "@0xproject/json-schemas",
|
||||
"version": "0.6.8",
|
||||
"description": "0x-related json schemas",
|
||||
"main": "lib/src/index.js",
|
||||
"types": "lib/src/index.d.ts",
|
||||
"scripts": {
|
||||
"lint": "tslint src/*.ts test/*.ts",
|
||||
"test": "run-s clean build run_mocha",
|
||||
"test:circleci": "yarn test",
|
||||
"run_mocha": "mocha lib/test/**/*_test.js",
|
||||
"clean": "shx rm -rf _bundles lib test_temp",
|
||||
"build": "tsc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/0xProject/0x.js.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/0xProject/0x.js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/0xProject/0x.js/packages/json-schemas/README.md",
|
||||
"dependencies": {
|
||||
"es6-promisify": "^5.0.0",
|
||||
"jsonschema": "^1.2.0",
|
||||
"lodash.values": "^4.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@0xproject/tslint-config": "^0.1.1",
|
||||
"@types/lodash.foreach": "^4.5.3",
|
||||
"@types/lodash.values": "^4.3.3",
|
||||
"@types/mocha": "^2.2.42",
|
||||
"bignumber.js": "^4.0.2",
|
||||
"chai": "^4.1.1",
|
||||
"chai-typescript-typings": "^0.0.1",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"lodash.foreach": "^4.5.0",
|
||||
"mocha": "^4.0.1",
|
||||
"npm-run-all": "^4.1.1",
|
||||
"shx": "^0.2.2",
|
||||
"tslint": "5.8.0",
|
||||
"typescript": "~2.6.1"
|
||||
}
|
||||
}
|
||||
11
packages/json-schemas/schemas/basic_type_schemas.ts
Normal file
11
packages/json-schemas/schemas/basic_type_schemas.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const addressSchema = {
|
||||
id: '/Address',
|
||||
type: 'string',
|
||||
pattern: '^0x[0-9a-f]{40}$',
|
||||
};
|
||||
|
||||
export const numberSchema = {
|
||||
id: '/Number',
|
||||
type: 'string',
|
||||
pattern: '^\\d+(\\.\\d+)?$',
|
||||
};
|
||||
20
packages/json-schemas/schemas/ec_signature_schema.ts
Normal file
20
packages/json-schemas/schemas/ec_signature_schema.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export const ecSignatureParameterSchema = {
|
||||
id: '/ECSignatureParameter',
|
||||
type: 'string',
|
||||
pattern: '^0[xX][0-9A-Fa-f]{64}$',
|
||||
};
|
||||
|
||||
export const ecSignatureSchema = {
|
||||
id: '/ECSignature',
|
||||
properties: {
|
||||
v: {
|
||||
type: 'number',
|
||||
minimum: 27,
|
||||
maximum: 28,
|
||||
},
|
||||
r: {$ref: '/ECSignatureParameter'},
|
||||
s: {$ref: '/ECSignatureParameter'},
|
||||
},
|
||||
required: ['v', 'r', 's'],
|
||||
type: 'object',
|
||||
};
|
||||
11
packages/json-schemas/schemas/index_filter_values_schema.ts
Normal file
11
packages/json-schemas/schemas/index_filter_values_schema.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const indexFilterValuesSchema = {
|
||||
id: '/IndexFilterValues',
|
||||
additionalProperties: {
|
||||
oneOf: [
|
||||
{$ref: '/Number'},
|
||||
{$ref: '/Address'},
|
||||
{$ref: '/OrderHashSchema'},
|
||||
],
|
||||
},
|
||||
type: 'object',
|
||||
};
|
||||
12
packages/json-schemas/schemas/order_cancel_schema.ts
Normal file
12
packages/json-schemas/schemas/order_cancel_schema.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export const orderCancellationRequestsSchema = {
|
||||
id: '/OrderCancellationRequests',
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
order: {$ref: '/Order'},
|
||||
takerTokenCancelAmount: {$ref: '/Number'},
|
||||
},
|
||||
required: ['order', 'takerTokenCancelAmount'],
|
||||
type: 'object',
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
export const orderFillOrKillRequestsSchema = {
|
||||
id: '/OrderFillOrKillRequests',
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
signedOrder: {$ref: '/SignedOrder'},
|
||||
fillTakerAmount: {$ref: '/Number'},
|
||||
},
|
||||
required: ['signedOrder', 'fillTakerAmount'],
|
||||
type: 'object',
|
||||
},
|
||||
};
|
||||
12
packages/json-schemas/schemas/order_fill_requests_schema.ts
Normal file
12
packages/json-schemas/schemas/order_fill_requests_schema.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export const orderFillRequestsSchema = {
|
||||
id: '/OrderFillRequests',
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
signedOrder: {$ref: '/SignedOrder'},
|
||||
takerTokenFillAmount: {$ref: '/Number'},
|
||||
},
|
||||
required: ['signedOrder', 'takerTokenFillAmount'],
|
||||
type: 'object',
|
||||
},
|
||||
};
|
||||
5
packages/json-schemas/schemas/order_hash_schema.ts
Normal file
5
packages/json-schemas/schemas/order_hash_schema.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const orderHashSchema = {
|
||||
id: '/OrderHashSchema',
|
||||
type: 'string',
|
||||
pattern: '^0x[0-9a-fA-F]{64}$',
|
||||
};
|
||||
35
packages/json-schemas/schemas/order_schemas.ts
Normal file
35
packages/json-schemas/schemas/order_schemas.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
export const orderSchema = {
|
||||
id: '/Order',
|
||||
properties: {
|
||||
maker: {$ref: '/Address'},
|
||||
taker: {$ref: '/Address'},
|
||||
makerFee: {$ref: '/Number'},
|
||||
takerFee: {$ref: '/Number'},
|
||||
makerTokenAmount: {$ref: '/Number'},
|
||||
takerTokenAmount: {$ref: '/Number'},
|
||||
makerTokenAddress: {$ref: '/Address'},
|
||||
takerTokenAddress: {$ref: '/Address'},
|
||||
salt: {$ref: '/Number'},
|
||||
feeRecipient: {$ref: '/Address'},
|
||||
expirationUnixTimestampSec: {$ref: '/Number'},
|
||||
exchangeContractAddress: {$ref: '/Address'},
|
||||
},
|
||||
required: [
|
||||
'maker', 'taker', 'makerFee', 'takerFee', 'makerTokenAmount', 'takerTokenAmount',
|
||||
'salt', 'feeRecipient', 'expirationUnixTimestampSec', 'exchangeContractAddress',
|
||||
],
|
||||
type: 'object',
|
||||
};
|
||||
|
||||
export const signedOrderSchema = {
|
||||
id: '/SignedOrder',
|
||||
allOf: [
|
||||
{ $ref: '/Order' },
|
||||
{
|
||||
properties: {
|
||||
ecSignature: {$ref: '/ECSignature'},
|
||||
},
|
||||
required: ['ecSignature'],
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
export const relayerApiErrorResponseSchema = {
|
||||
id: '/RelayerApiErrorResponse',
|
||||
type: 'object',
|
||||
properties: {
|
||||
code: {type: 'number'},
|
||||
reason: {type: 'string'},
|
||||
validationErrors: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
field: {type: 'string'},
|
||||
code: {type: 'number'},
|
||||
reason: {type: 'string'},
|
||||
},
|
||||
required: ['field', 'code', 'reason'],
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['code', 'reason'],
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
export const relayerApiFeesPayloadSchema = {
|
||||
id: '/RelayerApiFeesPayload',
|
||||
type: 'object',
|
||||
properties: {
|
||||
exchangeContractAddress: {$ref: '/Address'},
|
||||
maker: {$ref: '/Address'},
|
||||
taker: {$ref: '/Address'},
|
||||
makerTokenAddress: {$ref: '/Address'},
|
||||
takerTokenAddress: {$ref: '/Address'},
|
||||
makerTokenAmount: {$ref: '/Number'},
|
||||
takerTokenAmount: {$ref: '/Number'},
|
||||
expirationUnixTimestampSec: {$ref: '/Number'},
|
||||
salt: {$ref: '/Number'},
|
||||
},
|
||||
required: [
|
||||
'exchangeContractAddress', 'maker', 'taker', 'makerTokenAddress', 'takerTokenAddress',
|
||||
'expirationUnixTimestampSec', 'salt',
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
export const relayerApiFeesResponseSchema = {
|
||||
id: '/RelayerApiFeesResponse',
|
||||
type: 'object',
|
||||
properties: {
|
||||
makerFee: {$ref: '/Number'},
|
||||
takerFee: {$ref: '/Number'},
|
||||
feeRecipient: {$ref: '/Address'},
|
||||
},
|
||||
required: ['makerFee', 'takerFee', 'feeRecipient'],
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
export const relayerApiOrderbookChannelSubscribeSchema = {
|
||||
id: '/RelayerApiOrderbookChannelSubscribe',
|
||||
type: 'object',
|
||||
properties: {
|
||||
type: {enum: ['subscribe']},
|
||||
channel: {enum: ['orderbook']},
|
||||
payload: {$ref: '/RelayerApiOrderbookChannelSubscribePayload'},
|
||||
},
|
||||
required: ['type', 'channel', 'payload'],
|
||||
};
|
||||
|
||||
export const relayerApiOrderbookChannelSubscribePayload = {
|
||||
id: '/RelayerApiOrderbookChannelSubscribePayload',
|
||||
type: 'object',
|
||||
properties: {
|
||||
baseTokenAddress: {$ref: '/Address'},
|
||||
quoteTokenAddress: {$ref: '/Address'},
|
||||
snapshot: {type: 'boolean'},
|
||||
limit: {type: 'number'},
|
||||
},
|
||||
required: ['baseTokenAddress', 'quoteTokenAddress'],
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
export const relayerApiOrderbookChannelSnapshotSchema = {
|
||||
id: '/RelayerApiOrderbookChannelSnapshot',
|
||||
type: 'object',
|
||||
properties: {
|
||||
type: {enum: ['snapshot']},
|
||||
channel: {enum: ['orderbook']},
|
||||
channelId: {type: 'number'},
|
||||
payload: {$ref: '/RelayerApiOrderbookChannelSnapshotPayload'},
|
||||
},
|
||||
required: ['type', 'channel', 'channelId', 'payload'],
|
||||
};
|
||||
|
||||
export const relayerApiOrderbookChannelSnapshotPayload = {
|
||||
id: '/RelayerApiOrderbookChannelSnapshotPayload',
|
||||
type: 'object',
|
||||
properties: {
|
||||
bids: {$ref: '/signedOrdersSchema'},
|
||||
asks: {$ref: '/signedOrdersSchema'},
|
||||
},
|
||||
required: ['bids', 'asks'],
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
export const relayerApiOrderbookChannelUpdateSchema = {
|
||||
id: '/RelayerApiOrderbookChannelUpdate',
|
||||
type: 'object',
|
||||
properties: {
|
||||
type: {enum: ['update']},
|
||||
channel: {enum: ['orderbook']},
|
||||
channelId: {type: 'number'},
|
||||
payload: {$ref: '/SignedOrder'},
|
||||
},
|
||||
required: ['type', 'channel', 'channelId', 'payload'],
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
export const relayerApiOrderBookResponseSchema = {
|
||||
id: '/RelayerApiOrderBookResponse',
|
||||
type: 'object',
|
||||
properties: {
|
||||
bids: {$ref: '/signedOrdersSchema'},
|
||||
asks: {$ref: '/signedOrdersSchema'},
|
||||
},
|
||||
required: ['bids', 'asks'],
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
export const relayerApiTokenPairsResponseSchema = {
|
||||
id: '/RelayerApiTokenPairsResponse',
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
tokenA: {$ref: '/RelayerApiTokenTradeInfo'},
|
||||
tokenB: {$ref: '/RelayerApiTokenTradeInfo'},
|
||||
},
|
||||
required: ['tokenA', 'tokenB'],
|
||||
type: 'object',
|
||||
},
|
||||
};
|
||||
|
||||
export const relayerApiTokenTradeInfoSchema = {
|
||||
id: '/RelayerApiTokenTradeInfo',
|
||||
type: 'object',
|
||||
properties: {
|
||||
address: {$ref: '/Address'},
|
||||
minAmount: {$ref: '/Number'},
|
||||
maxAmount: {$ref: '/Number'},
|
||||
precision: {type: 'number'},
|
||||
},
|
||||
required: ['address'],
|
||||
};
|
||||
5
packages/json-schemas/schemas/signed_orders_schema.ts
Normal file
5
packages/json-schemas/schemas/signed_orders_schema.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const signedOrdersSchema = {
|
||||
id: '/signedOrdersSchema',
|
||||
type: 'array',
|
||||
items: {$ref: '/SignedOrder'},
|
||||
};
|
||||
20
packages/json-schemas/schemas/subscription_opts_schema.ts
Normal file
20
packages/json-schemas/schemas/subscription_opts_schema.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export const blockParamSchema = {
|
||||
id: '/BlockParam',
|
||||
oneOf: [
|
||||
{
|
||||
type: 'number',
|
||||
},
|
||||
{
|
||||
enum: ['latest', 'earliest', 'pending'],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const subscriptionOptsSchema = {
|
||||
id: '/SubscriptionOpts',
|
||||
properties: {
|
||||
fromBlock: {$ref: '/BlockParam'},
|
||||
toBlock: {$ref: '/BlockParam'},
|
||||
},
|
||||
type: 'object',
|
||||
};
|
||||
11
packages/json-schemas/schemas/token_schema.ts
Normal file
11
packages/json-schemas/schemas/token_schema.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const tokenSchema = {
|
||||
id: '/Token',
|
||||
properties: {
|
||||
name: {type: 'string'},
|
||||
symbol: {type: 'string'},
|
||||
decimals: {type: 'number'},
|
||||
address: {$ref: '/Address'},
|
||||
},
|
||||
required: ['name', 'symbol', 'decimals', 'address'],
|
||||
type: 'object',
|
||||
};
|
||||
42
packages/json-schemas/schemas/tx_data_schema.ts
Normal file
42
packages/json-schemas/schemas/tx_data_schema.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
export const jsNumber = {
|
||||
id: '/JsNumber',
|
||||
type: 'number',
|
||||
minimum: 0,
|
||||
};
|
||||
|
||||
export const txDataSchema = {
|
||||
id: '/TxData',
|
||||
properties: {
|
||||
from: {$ref: '/Address'},
|
||||
to: {$ref: '/Address'},
|
||||
value: {
|
||||
oneOf: [
|
||||
{$ref: '/Number'},
|
||||
{$ref: '/JsNumber'},
|
||||
],
|
||||
},
|
||||
gas: {
|
||||
oneOf: [
|
||||
{$ref: '/Number'},
|
||||
{$ref: '/JsNumber'},
|
||||
],
|
||||
},
|
||||
gasPrice: {
|
||||
oneOf: [
|
||||
{$ref: '/Number'},
|
||||
{$ref: '/JsNumber'},
|
||||
],
|
||||
},
|
||||
data: {
|
||||
type: 'string',
|
||||
pattern: '^0x[0-9a-f]*$',
|
||||
},
|
||||
nonce: {
|
||||
type: 'number',
|
||||
minimum: 0,
|
||||
},
|
||||
},
|
||||
required: ['from'],
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
};
|
||||
14
packages/json-schemas/scripts/postpublish.js
Normal file
14
packages/json-schemas/scripts/postpublish.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const postpublish_utils = require('../../../scripts/postpublish_utils');
|
||||
const packageJSON = require('../package.json');
|
||||
|
||||
const subPackageName = packageJSON.name;
|
||||
|
||||
postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
|
||||
.then(function(result) {
|
||||
const releaseName = postpublish_utils.getReleaseName(subPackageName, result.version);
|
||||
const assets = [];
|
||||
return postpublish_utils.publishReleaseNotes(result.tag, releaseName, assets);
|
||||
})
|
||||
.catch (function(err) {
|
||||
throw err;
|
||||
});
|
||||
7
packages/json-schemas/src/globals.d.ts
vendored
Normal file
7
packages/json-schemas/src/globals.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
declare module 'dirty-chai';
|
||||
|
||||
// es6-promisify declarations
|
||||
declare function promisify(original: any, settings?: any): ((...arg: any[]) => Promise<any>);
|
||||
declare module 'es6-promisify' {
|
||||
export = promisify;
|
||||
}
|
||||
4
packages/json-schemas/src/index.ts
Normal file
4
packages/json-schemas/src/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export {ValidatorResult, Schema} from 'jsonschema';
|
||||
|
||||
export {SchemaValidator} from './schema_validator';
|
||||
export {schemas} from './schemas';
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user