Add ForwarderHelperImplConfig
This commit is contained in:
@@ -15,21 +15,22 @@
|
|||||||
"test:coverage": "nyc npm run test --all && yarn coverage:report:lcov",
|
"test:coverage": "nyc npm run test --all && yarn coverage:report:lcov",
|
||||||
"coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info",
|
"coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info",
|
||||||
"test:circleci": "yarn test:coverage",
|
"test:circleci": "yarn test:coverage",
|
||||||
"run_mocha":
|
"run_mocha": "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit",
|
||||||
"mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit",
|
|
||||||
"clean": "shx rm -rf lib test_temp scripts",
|
"clean": "shx rm -rf lib test_temp scripts",
|
||||||
"build": "tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts",
|
"build": "tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts",
|
||||||
"manual:postpublish": "yarn build; node ./scripts/postpublish.js",
|
"manual:postpublish": "yarn build; node ./scripts/postpublish.js",
|
||||||
"docs:stage": "node scripts/stage_docs.js",
|
"docs:stage": "node scripts/stage_docs.js",
|
||||||
"docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES",
|
"docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES",
|
||||||
"upload_docs_json":
|
"upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json"
|
||||||
"aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json"
|
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"postpublish": {
|
"postpublish": {
|
||||||
"assets": [],
|
"assets": [],
|
||||||
"docPublishConfigs": {
|
"docPublishConfigs": {
|
||||||
"extraFileIncludes": ["../types/src/index.ts", "../ethereum-types/src/index.ts"],
|
"extraFileIncludes": [
|
||||||
|
"../types/src/index.ts",
|
||||||
|
"../ethereum-types/src/index.ts"
|
||||||
|
],
|
||||||
"s3BucketPath": "s3://doc-jsons/forwarder-helper/",
|
"s3BucketPath": "s3://doc-jsons/forwarder-helper/",
|
||||||
"s3StagingBucketPath": "s3://staging-doc-jsons/forwarder-helper/"
|
"s3StagingBucketPath": "s3://staging-doc-jsons/forwarder-helper/"
|
||||||
}
|
}
|
||||||
@@ -50,11 +51,13 @@
|
|||||||
"@0xproject/types": "^1.0.1-rc.4",
|
"@0xproject/types": "^1.0.1-rc.4",
|
||||||
"@0xproject/typescript-typings": "^1.0.4",
|
"@0xproject/typescript-typings": "^1.0.4",
|
||||||
"@0xproject/utils": "^1.0.5",
|
"@0xproject/utils": "^1.0.5",
|
||||||
"@types/node": "^8.0.53"
|
"@types/node": "^8.0.53",
|
||||||
|
"lodash": "^4.17.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@0xproject/monorepo-scripts": "^1.0.5",
|
"@0xproject/monorepo-scripts": "^1.0.5",
|
||||||
"@0xproject/tslint-config": "^1.0.5",
|
"@0xproject/tslint-config": "^1.0.5",
|
||||||
|
"@types/lodash": "^4.14.116",
|
||||||
"@types/mocha": "^2.2.42",
|
"@types/mocha": "^2.2.42",
|
||||||
"chai": "^4.0.1",
|
"chai": "^4.0.1",
|
||||||
"chai-as-promised": "^7.1.0",
|
"chai-as-promised": "^7.1.0",
|
||||||
|
|||||||
@@ -1,37 +1,93 @@
|
|||||||
import { marketUtils } from '@0xproject/order-utils';
|
import { marketUtils, sortingUtils } from '@0xproject/order-utils';
|
||||||
import { SignedOrder } from '@0xproject/types';
|
import { SignedOrder } from '@0xproject/types';
|
||||||
import { BigNumber } from '@0xproject/utils';
|
import { BigNumber } from '@0xproject/utils';
|
||||||
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import { constants } from './constants';
|
import { constants } from './constants';
|
||||||
import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types';
|
import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types';
|
||||||
|
|
||||||
const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface
|
const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface
|
||||||
|
|
||||||
|
interface SignedOrderWithAmount extends SignedOrder {
|
||||||
|
remainingFillAmount?: BigNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ForwarderHelperImplConfig {
|
||||||
|
orders: SignedOrder[];
|
||||||
|
feeOrders: SignedOrder[];
|
||||||
|
remainingFillableMakerAssetAmounts?: BigNumber[];
|
||||||
|
remainingFillableFeeAmounts?: BigNumber[];
|
||||||
|
}
|
||||||
|
|
||||||
export class ForwarderHelperImpl implements ForwarderHelper {
|
export class ForwarderHelperImpl implements ForwarderHelper {
|
||||||
private _orders: SignedOrder[];
|
private _config: ForwarderHelperImplConfig;
|
||||||
private _feeOrders: SignedOrder[];
|
private static _createSignedOrderWithAmounts(
|
||||||
private _remainingFillableMakerAssetAmountsIfExists?: BigNumber[];
|
|
||||||
private _remainingFillableFeeAmountsIfExists?: BigNumber[];
|
|
||||||
constructor(
|
|
||||||
orders: SignedOrder[],
|
orders: SignedOrder[],
|
||||||
feeOrders: SignedOrder[] = [] as SignedOrder[],
|
amounts?: BigNumber[],
|
||||||
remainingFillableMakerAssetAmounts?: BigNumber[],
|
): SignedOrderWithAmount[] {
|
||||||
remainingFillableFeeAmounts?: BigNumber[],
|
const ordersAndAmounts = _.map(orders, (order, index) => {
|
||||||
) {
|
return {
|
||||||
this._orders = orders;
|
...order,
|
||||||
this._feeOrders = feeOrders;
|
remainingFillAmount: _.nth(amounts, index),
|
||||||
this._remainingFillableMakerAssetAmountsIfExists = remainingFillableMakerAssetAmounts;
|
};
|
||||||
this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts;
|
});
|
||||||
|
return ordersAndAmounts;
|
||||||
|
}
|
||||||
|
private static _unbundleSignedOrderWithAmounts(
|
||||||
|
signedOrderWithAmounts: SignedOrderWithAmount[],
|
||||||
|
): { orders: SignedOrder[]; amounts?: BigNumber[] } {
|
||||||
|
const orders = _.map(signedOrderWithAmounts, order => {
|
||||||
|
const { remainingFillAmount, ...rest } = order;
|
||||||
|
return rest;
|
||||||
|
});
|
||||||
|
const amounts = _.map(signedOrderWithAmounts, order => {
|
||||||
|
const { remainingFillAmount, ...rest } = order;
|
||||||
|
return remainingFillAmount;
|
||||||
|
});
|
||||||
|
const compactAmounts = _.compact(amounts);
|
||||||
|
return {
|
||||||
|
orders,
|
||||||
|
amounts: compactAmounts.length > 0 ? compactAmounts : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
private static _sortConfig(opts: ForwarderHelperImplConfig): ForwarderHelperImplConfig {
|
||||||
|
const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = opts;
|
||||||
|
const orderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts(
|
||||||
|
orders,
|
||||||
|
remainingFillableMakerAssetAmounts,
|
||||||
|
);
|
||||||
|
const sortedOrderWithAmounts = sortingUtils.sortOrdersByFeeAdjustedRate(orderWithAmounts);
|
||||||
|
const unbundledSortedOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts(
|
||||||
|
sortedOrderWithAmounts,
|
||||||
|
);
|
||||||
|
const feeOrderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts(
|
||||||
|
feeOrders,
|
||||||
|
remainingFillableFeeAmounts,
|
||||||
|
);
|
||||||
|
const sortedFeeOrderWithAmounts = sortingUtils.sortFeeOrdersByFeeAdjustedRate(feeOrderWithAmounts);
|
||||||
|
const unbundledSortedFeeOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts(
|
||||||
|
sortedFeeOrderWithAmounts,
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
orders: unbundledSortedOrderWithAmounts.orders,
|
||||||
|
feeOrders: unbundledSortedFeeOrderWithAmounts.orders,
|
||||||
|
remainingFillableMakerAssetAmounts: unbundledSortedOrderWithAmounts.amounts,
|
||||||
|
remainingFillableFeeAmounts: unbundledSortedFeeOrderWithAmounts.amounts,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
constructor(opts: ForwarderHelperImplConfig) {
|
||||||
|
this._config = ForwarderHelperImpl._sortConfig(opts);
|
||||||
}
|
}
|
||||||
public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo {
|
public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo {
|
||||||
const { makerAssetFillAmount, feePercentage } = request;
|
const { makerAssetFillAmount, feePercentage } = request;
|
||||||
|
const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this._config;
|
||||||
// TODO: make the slippage percentage customizable
|
// TODO: make the slippage percentage customizable
|
||||||
const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE);
|
const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE);
|
||||||
const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
|
const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
|
||||||
this._orders,
|
orders,
|
||||||
makerAssetFillAmount,
|
makerAssetFillAmount,
|
||||||
{
|
{
|
||||||
remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists,
|
remainingFillableMakerAssetAmounts,
|
||||||
slippageBufferAmount,
|
slippageBufferAmount,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -42,10 +98,10 @@ export class ForwarderHelperImpl implements ForwarderHelper {
|
|||||||
// finding order that cover all fees, this will help with estimating ETH and minimizing gas usage
|
// finding order that cover all fees, this will help with estimating ETH and minimizing gas usage
|
||||||
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
resultOrders,
|
resultOrders,
|
||||||
this._feeOrders,
|
feeOrders,
|
||||||
{
|
{
|
||||||
remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists,
|
remainingFillableMakerAssetAmounts,
|
||||||
remainingFillableFeeAmounts: this._remainingFillableFeeAmountsIfExists,
|
remainingFillableFeeAmounts,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) {
|
if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) {
|
||||||
|
|||||||
@@ -18,6 +18,9 @@
|
|||||||
{
|
{
|
||||||
"note":
|
"note":
|
||||||
"Rename `resultOrders` to `resultFeeOrders` for object returned by `findFeeOrdersThatCoverFeesForTargetOrders` in `marketUtils` api"
|
"Rename `resultOrders` to `resultFeeOrders` for object returned by `findFeeOrdersThatCoverFeesForTargetOrders` in `marketUtils` api"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"note": "Make `sortFeeOrdersByFeeAdjustedRate` in `sortingUtils` generic"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export const sortingUtils = {
|
|||||||
* the makerAsset and WETH as the takerAsset.
|
* the makerAsset and WETH as the takerAsset.
|
||||||
* @return The input orders sorted by rate in ascending order
|
* @return The input orders sorted by rate in ascending order
|
||||||
*/
|
*/
|
||||||
sortFeeOrdersByFeeAdjustedRate(feeOrders: Order[]): Order[] {
|
sortFeeOrdersByFeeAdjustedRate<T extends Order>(feeOrders: T[]): T[] {
|
||||||
assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema);
|
assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema);
|
||||||
const rateCalculator = rateUtils.getFeeAdjustedRateOfFeeOrder.bind(rateUtils);
|
const rateCalculator = rateUtils.getFeeAdjustedRateOfFeeOrder.bind(rateUtils);
|
||||||
const sortedOrders = sortOrders(feeOrders, rateCalculator);
|
const sortedOrders = sortOrders(feeOrders, rateCalculator);
|
||||||
|
|||||||
@@ -139,11 +139,11 @@ describe('marketUtils', () => {
|
|||||||
);
|
);
|
||||||
describe('no target orders', () => {
|
describe('no target orders', () => {
|
||||||
it('returns empty and zero remainingFeeAmount', async () => {
|
it('returns empty and zero remainingFeeAmount', async () => {
|
||||||
const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
[],
|
[],
|
||||||
inputFeeOrders,
|
inputFeeOrders,
|
||||||
);
|
);
|
||||||
expect(resultOrders).to.be.empty;
|
expect(resultFeeOrders).to.be.empty;
|
||||||
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -162,14 +162,14 @@ describe('marketUtils', () => {
|
|||||||
// generate remainingFillableMakerAssetAmounts that equal the makerAssetAmount
|
// generate remainingFillableMakerAssetAmounts that equal the makerAssetAmount
|
||||||
const remainingFillableMakerAssetAmounts = [makerAssetAmount, makerAssetAmount, makerAssetAmount];
|
const remainingFillableMakerAssetAmounts = [makerAssetAmount, makerAssetAmount, makerAssetAmount];
|
||||||
it('returns empty and non-zero remainingFeeAmount', async () => {
|
it('returns empty and non-zero remainingFeeAmount', async () => {
|
||||||
const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
inputOrders,
|
inputOrders,
|
||||||
[],
|
[],
|
||||||
{
|
{
|
||||||
remainingFillableMakerAssetAmounts,
|
remainingFillableMakerAssetAmounts,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
expect(resultOrders).to.be.empty;
|
expect(resultFeeOrders).to.be.empty;
|
||||||
expect(remainingFeeAmount).to.be.bignumber.equal(new BigNumber(30));
|
expect(remainingFeeAmount).to.be.bignumber.equal(new BigNumber(30));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -183,11 +183,11 @@ describe('marketUtils', () => {
|
|||||||
3,
|
3,
|
||||||
);
|
);
|
||||||
it('returns empty and zero remainingFeeAmount', async () => {
|
it('returns empty and zero remainingFeeAmount', async () => {
|
||||||
const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
inputOrders,
|
inputOrders,
|
||||||
inputFeeOrders,
|
inputFeeOrders,
|
||||||
);
|
);
|
||||||
expect(resultOrders).to.be.empty;
|
expect(resultFeeOrders).to.be.empty;
|
||||||
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -204,11 +204,11 @@ describe('marketUtils', () => {
|
|||||||
3,
|
3,
|
||||||
);
|
);
|
||||||
it('returns input fee orders and zero remainingFeeAmount', async () => {
|
it('returns input fee orders and zero remainingFeeAmount', async () => {
|
||||||
const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
inputOrders,
|
inputOrders,
|
||||||
inputFeeOrders,
|
inputFeeOrders,
|
||||||
);
|
);
|
||||||
expect(resultOrders).to.be.deep.equal(inputFeeOrders);
|
expect(resultFeeOrders).to.be.deep.equal(inputFeeOrders);
|
||||||
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -230,14 +230,14 @@ describe('marketUtils', () => {
|
|||||||
// 3. order is completely fillable
|
// 3. order is completely fillable
|
||||||
const remainingFillableMakerAssetAmounts = [constants.ZERO_AMOUNT, new BigNumber(5), makerAssetAmount];
|
const remainingFillableMakerAssetAmounts = [constants.ZERO_AMOUNT, new BigNumber(5), makerAssetAmount];
|
||||||
it('returns first two input fee orders and zero remainingFeeAmount', async () => {
|
it('returns first two input fee orders and zero remainingFeeAmount', async () => {
|
||||||
const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
inputOrders,
|
inputOrders,
|
||||||
inputFeeOrders,
|
inputFeeOrders,
|
||||||
{
|
{
|
||||||
remainingFillableMakerAssetAmounts,
|
remainingFillableMakerAssetAmounts,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
expect(resultOrders).to.be.deep.equal([inputFeeOrders[0], inputFeeOrders[1]]);
|
expect(resultFeeOrders).to.be.deep.equal([inputFeeOrders[0], inputFeeOrders[1]]);
|
||||||
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -254,11 +254,11 @@ describe('marketUtils', () => {
|
|||||||
3,
|
3,
|
||||||
);
|
);
|
||||||
it('returns input fee orders and non-zero remainingFeeAmount', async () => {
|
it('returns input fee orders and non-zero remainingFeeAmount', async () => {
|
||||||
const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
|
||||||
inputOrders,
|
inputOrders,
|
||||||
inputFeeOrders,
|
inputFeeOrders,
|
||||||
);
|
);
|
||||||
expect(resultOrders).to.be.deep.equal(inputFeeOrders);
|
expect(resultFeeOrders).to.be.deep.equal(inputFeeOrders);
|
||||||
expect(remainingFeeAmount).to.be.bignumber.equal(new BigNumber(30));
|
expect(remainingFeeAmount).to.be.bignumber.equal(new BigNumber(30));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1036,6 +1036,10 @@
|
|||||||
version "4.14.99"
|
version "4.14.99"
|
||||||
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.99.tgz#e6e10c0a4cc16c7409b3181f1e66880d2fb7d4dc"
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.99.tgz#e6e10c0a4cc16c7409b3181f1e66880d2fb7d4dc"
|
||||||
|
|
||||||
|
"@types/lodash@^4.14.116":
|
||||||
|
version "4.14.116"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.116.tgz#5ccf215653e3e8c786a58390751033a9adca0eb9"
|
||||||
|
|
||||||
"@types/loglevel@^1.5.3":
|
"@types/loglevel@^1.5.3":
|
||||||
version "1.5.3"
|
version "1.5.3"
|
||||||
resolved "https://registry.yarnpkg.com/@types/loglevel/-/loglevel-1.5.3.tgz#adfce55383edc5998a2170ad581b3e23d6adb5b8"
|
resolved "https://registry.yarnpkg.com/@types/loglevel/-/loglevel-1.5.3.tgz#adfce55383edc5998a2170ad581b3e23d6adb5b8"
|
||||||
|
|||||||
Reference in New Issue
Block a user