From dd9c4b6e5d0f10bdaaae61f40bdf017e83e5f606 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 15 Aug 2018 14:13:07 -0700 Subject: Initial skeleton of forwarder-helper package --- packages/forwarder-helper/.npmignore | 8 +++ packages/forwarder-helper/CHANGELOG.json | 1 + packages/forwarder-helper/CHANGELOG.md | 0 packages/forwarder-helper/README.md | 83 ++++++++++++++++++++++ packages/forwarder-helper/package.json | 78 ++++++++++++++++++++ .../forwarder-helper/src/forwarder_helper_impl.ts | 0 packages/forwarder-helper/src/globals.d.ts | 6 ++ packages/forwarder-helper/src/index.ts | 0 .../src/monorepo_scripts/postpublish.ts | 8 +++ .../src/monorepo_scripts/stage_docs.ts | 8 +++ packages/forwarder-helper/src/types.ts | 0 .../test/forwarder_helper_impl_test.ts | 12 ++++ packages/forwarder-helper/test/utils/chai_setup.ts | 13 ++++ packages/forwarder-helper/tsconfig.json | 7 ++ packages/forwarder-helper/tslint.json | 3 + 15 files changed, 227 insertions(+) create mode 100644 packages/forwarder-helper/.npmignore create mode 100644 packages/forwarder-helper/CHANGELOG.json create mode 100644 packages/forwarder-helper/CHANGELOG.md create mode 100644 packages/forwarder-helper/README.md create mode 100644 packages/forwarder-helper/package.json create mode 100644 packages/forwarder-helper/src/forwarder_helper_impl.ts create mode 100644 packages/forwarder-helper/src/globals.d.ts create mode 100644 packages/forwarder-helper/src/index.ts create mode 100644 packages/forwarder-helper/src/monorepo_scripts/postpublish.ts create mode 100644 packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts create mode 100644 packages/forwarder-helper/src/types.ts create mode 100644 packages/forwarder-helper/test/forwarder_helper_impl_test.ts create mode 100644 packages/forwarder-helper/test/utils/chai_setup.ts create mode 100644 packages/forwarder-helper/tsconfig.json create mode 100644 packages/forwarder-helper/tslint.json diff --git a/packages/forwarder-helper/.npmignore b/packages/forwarder-helper/.npmignore new file mode 100644 index 000000000..5333847e7 --- /dev/null +++ b/packages/forwarder-helper/.npmignore @@ -0,0 +1,8 @@ +.* +yarn-error.log +/src/ +/scripts/ +/schemas/ +test/ +tsconfig.json +/lib/src/monorepo_scripts/ diff --git a/packages/forwarder-helper/CHANGELOG.json b/packages/forwarder-helper/CHANGELOG.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/packages/forwarder-helper/CHANGELOG.json @@ -0,0 +1 @@ +[] diff --git a/packages/forwarder-helper/CHANGELOG.md b/packages/forwarder-helper/CHANGELOG.md new file mode 100644 index 000000000..e69de29bb diff --git a/packages/forwarder-helper/README.md b/packages/forwarder-helper/README.md new file mode 100644 index 000000000..c74526910 --- /dev/null +++ b/packages/forwarder-helper/README.md @@ -0,0 +1,83 @@ +## @0xproject/forwarder-helper + +Provides convenience objects to help work with the Forwarder Contract + +### Read the [Documentation](https://0xproject.com/docs/forwarder-helper). + +## Installation + +```bash +yarn add @0xproject/forwarder-helper +``` + +**Import** + +```typescript +import { forwarderHelperFactory } from '@0xproject/forwarder-helper'; +``` + +or + +```javascript +var forwarderHelperFactory = require('@0xproject/forwarder-helper').forwarderHelperFactory; +``` + +If your project is in [TypeScript](https://www.typescriptlang.org/), add the following to your `tsconfig.json`: + +```json +"compilerOptions": { + "typeRoots": ["node_modules/@0xproject/typescript-typings/types", "node_modules/@types"], +} +``` + +## Contributing + +We welcome improvements and fixes from the wider community! To report bugs within this package, please create an issue in this repository. + +Please read our [contribution guidelines](../../CONTRIBUTING.md) before getting started. + +### Install dependencies + +If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them: + +```bash +yarn config set workspaces-experimental true +``` + +Then install dependencies + +```bash +yarn install +``` + +### Build + +To build this package and all other monorepo packages that it depends on, run the following from the monorepo root directory: + +```bash +PKG=@0xproject/forwarder-helper yarn build +``` + +Or continuously rebuild on change: + +```bash +PKG=@0xproject/forwarder-helper yarn watch +``` + +### Clean + +```bash +yarn clean +``` + +### Lint + +```bash +yarn lint +``` + +### Run Tests + +```bash +yarn test +``` diff --git a/packages/forwarder-helper/package.json b/packages/forwarder-helper/package.json new file mode 100644 index 000000000..7f7b595c2 --- /dev/null +++ b/packages/forwarder-helper/package.json @@ -0,0 +1,78 @@ +{ + "name": "@0xproject/forwarder-helper", + "version": "1.0.0-rc.1", + "engines": { + "node": ">=6.12" + }, + "description": "Convenience object for working with the forwarder contract", + "main": "lib/src/index.js", + "types": "lib/src/index.d.ts", + "scripts": { + "watch_without_deps": "tsc -w", + "lint": "tslint --project .", + "test": "yarn run_mocha", + "rebuild_and_test": "run-s clean build test", + "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", + "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", + "test:circleci": "yarn test:coverage", + "run_mocha": "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", + "clean": "shx rm -rf lib test_temp scripts", + "build": "tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", + "manual:postpublish": "yarn build; node ./scripts/postpublish.js", + "docs:stage": "node scripts/stage_docs.js", + "docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES", + "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" + }, + "config": { + "postpublish": { + "assets": [], + "docPublishConfigs": { + "extraFileIncludes": [ + "../types/src/index.ts", + "../ethereum-types/src/index.ts" + ], + "s3BucketPath": "s3://doc-jsons/forwarder-helper/", + "s3StagingBucketPath": "s3://staging-doc-jsons/forwarder-helper/" + } + } + }, + "repository": { + "type": "git", + "url": "https://github.com/0xProject/0x-monorepo.git" + }, + "author": "", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/0xProject/0x-monorepo/issues" + }, + "homepage": "https://github.com/0xProject/0x-monorepo/packages/forwarder-helper/README.md", + "dependencies": { + "@0xproject/typescript-typings": "^1.0.4", + "@types/node": "^8.0.53" + }, + "devDependencies": { + "@0xproject/monorepo-scripts": "^1.0.5", + "@0xproject/tslint-config": "^1.0.5", + "@0xproject/utils": "^1.0.5", + "@types/lodash.foreach": "^4.5.3", + "@types/lodash.values": "^4.3.3", + "@types/mocha": "^2.2.42", + "chai": "^4.0.1", + "chai-as-promised": "^7.1.0", + "chai-bignumber": "^2.0.1", + "copyfiles": "^1.2.0", + "dirty-chai": "^2.0.1", + "lodash.foreach": "^4.5.0", + "make-promises-safe": "^1.1.0", + "mocha": "^4.1.0", + "npm-run-all": "^4.1.2", + "nyc": "^11.0.1", + "shx": "^0.2.2", + "tslint": "5.11.0", + "typedoc": "0xProject/typedoc", + "typescript": "3.0.1" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts new file mode 100644 index 000000000..e69de29bb diff --git a/packages/forwarder-helper/src/globals.d.ts b/packages/forwarder-helper/src/globals.d.ts new file mode 100644 index 000000000..94e63a32d --- /dev/null +++ b/packages/forwarder-helper/src/globals.d.ts @@ -0,0 +1,6 @@ +declare module '*.json' { + const json: any; + /* tslint:disable */ + export default json; + /* tslint:enable */ +} diff --git a/packages/forwarder-helper/src/index.ts b/packages/forwarder-helper/src/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/packages/forwarder-helper/src/monorepo_scripts/postpublish.ts b/packages/forwarder-helper/src/monorepo_scripts/postpublish.ts new file mode 100644 index 000000000..dcb99d0f7 --- /dev/null +++ b/packages/forwarder-helper/src/monorepo_scripts/postpublish.ts @@ -0,0 +1,8 @@ +import { postpublishUtils } from '@0xproject/monorepo-scripts'; + +import * as packageJSON from '../package.json'; +import * as tsConfigJSON from '../tsconfig.json'; + +const cwd = `${__dirname}/..`; +// tslint:disable-next-line:no-floating-promises +postpublishUtils.runAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts b/packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts new file mode 100644 index 000000000..e732ac8eb --- /dev/null +++ b/packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts @@ -0,0 +1,8 @@ +import { postpublishUtils } from '@0xproject/monorepo-scripts'; + +import * as packageJSON from '../package.json'; +import * as tsConfigJSON from '../tsconfig.json'; + +const cwd = `${__dirname}/..`; +// tslint:disable-next-line:no-floating-promises +postpublishUtils.publishDocsToStagingAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts new file mode 100644 index 000000000..e69de29bb diff --git a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts new file mode 100644 index 000000000..1d3347d48 --- /dev/null +++ b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts @@ -0,0 +1,12 @@ +import * as chai from 'chai'; +import 'mocha'; + +import { chaiSetup } from './utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; + +describe('ForwarderHelperImpl', () => { + describe('#getMarketBuyOrdersInfo', () => {}); + describe('#getMarketSellOrdersInfo', () => {}); +}); diff --git a/packages/forwarder-helper/test/utils/chai_setup.ts b/packages/forwarder-helper/test/utils/chai_setup.ts new file mode 100644 index 000000000..1a8733093 --- /dev/null +++ b/packages/forwarder-helper/test/utils/chai_setup.ts @@ -0,0 +1,13 @@ +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import ChaiBigNumber = require('chai-bignumber'); +import * as dirtyChai from 'dirty-chai'; + +export const chaiSetup = { + configure(): void { + chai.config.includeStack = true; + chai.use(ChaiBigNumber()); + chai.use(dirtyChai); + chai.use(chaiAsPromised); + }, +}; diff --git a/packages/forwarder-helper/tsconfig.json b/packages/forwarder-helper/tsconfig.json new file mode 100644 index 000000000..e35816553 --- /dev/null +++ b/packages/forwarder-helper/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "lib" + }, + "include": ["./src/**/*", "./test/**/*"] +} diff --git a/packages/forwarder-helper/tslint.json b/packages/forwarder-helper/tslint.json new file mode 100644 index 000000000..ffaefe83a --- /dev/null +++ b/packages/forwarder-helper/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": ["@0xproject/tslint-config"] +} -- cgit v1.2.3 From 90f2813d0e3cf61c63b308d5cfe738b7beba8c94 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 15 Aug 2018 14:51:32 -0700 Subject: Flesh out types --- packages/forwarder-helper/package.json | 2 + packages/forwarder-helper/src/types.ts | 80 ++++++++++++++++++++++++++++++++++ yarn.lock | 6 +-- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/packages/forwarder-helper/package.json b/packages/forwarder-helper/package.json index 7f7b595c2..ad2b5f7cf 100644 --- a/packages/forwarder-helper/package.json +++ b/packages/forwarder-helper/package.json @@ -47,7 +47,9 @@ }, "homepage": "https://github.com/0xProject/0x-monorepo/packages/forwarder-helper/README.md", "dependencies": { + "@0xproject/types": "^0.8.2", "@0xproject/typescript-typings": "^1.0.4", + "@0xproject/utils": "^1.0.5", "@types/node": "^8.0.53" }, "devDependencies": { diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts index e69de29bb..23eaa1281 100644 --- a/packages/forwarder-helper/src/types.ts +++ b/packages/forwarder-helper/src/types.ts @@ -0,0 +1,80 @@ +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +export interface ForwarderHelper { + /** + * Given a MarketBuyOrdersInfoRequest, returns a MarketBuyOrdersInfo containing all information relevant to fulfilling the request + * using the ForwarderContract marketBuyOrdersWithEth function. + * @param request An object that conforms to MarketBuyOrdersInfoRequest. See type definition for more information. + * @return An object that conforms to MarketBuyOrdersInfo that satisfies the request. See type definition for more information. + */ + getMarketBuyOrdersInfo: (request: MarketBuyOrdersInfoRequest) => MarketBuyOrdersInfo; + /** + * Given a MarketSellOrdersInfoRequest, returns a MarketSellOrdersInfo containing all information relevant to fulfilling the request + * using the ForwarderContract marketSellOrdersWithEth function. + * @param request An object that conforms to MarketSellOrdersInfoRequest. See type definition for more information. + * @return An object that conforms to MarketSellOrdersInfo that satisfies the request. See type definition for more information. + */ + getMarketSellOrdersInfo: (request: MarketSellOrdersInfoRequest) => MarketSellOrdersInfo; +} + +export enum ForwarderHelperError { + InsufficientLiquidity = 'INSUFFICIENT_LIQUIDITY', + InsufficientZrxLiquidity = 'INSUFFICIENT_ZRX_LIQUIDITY', +} + +/** + * makerAssetFillAmount: The amount of makerAsset requesting to be filled + * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations + * acceptableEthAmountRange: maximum difference between min and max eth cost + */ +export interface MarketBuyOrdersInfoRequest { + makerAssetFillAmount: BigNumber; + feePercentage?: BigNumber; + acceptableEthAmountRange?: BigNumber; +} + +/** + * makerAssetFillAmount: The amount of makerAsset requesting to be filled + * orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested makerAssetFillAmount plus slippage + * feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above + * minEthAmount: Amount of eth in wei to send with the tx for the most optimistic case + * maxEthAmount: Amount of eth in wei to send with the tx for the worst case + * feePercentage: Affiliate fee percentage used to calculate the eth amounts above. Passed thru directly from the request + */ +export interface MarketBuyOrdersInfo { + makerAssetFillAmount: BigNumber; + orders: SignedOrder[]; + feeOrders: SignedOrder[]; + feePercentage?: BigNumber; + minEthAmount: BigNumber; + maxEthAmount: BigNumber; +} + +/** + * ethAmount: The amount of eth used to fill + * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations + * acceptableFillAmountRange: maximum difference between min and max asset filled + */ +export interface MarketSellOrdersInfoRequest { + ethAmount: BigNumber; + feePercentage?: BigNumber; + acceptableFillAmountRange?: BigNumber; +} + +/** + * ethAmount: The amount of eth used to fill + * orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested ethAmount plus slippage + * feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above + * minFillAmount: Amount of asset purchased in the worst case + * maxFillAmount: Amount of asset purchased in the best case + * feePercentage: Affiliate fee percentage used to calculate the eth amounts above. Passed thru directly from the request + */ +export interface MarketSellOrdersInfo { + ethAmount: BigNumber; + orders: SignedOrder[]; + feeOrders: SignedOrder[]; + minFillAmount: BigNumber; + maxFillAmount: BigNumber; + feePercentage?: BigNumber; +} diff --git a/yarn.lock b/yarn.lock index 7e278ac8a..508e23a0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5256,9 +5256,9 @@ ethereumjs-wallet@~0.6.0: utf8 "^3.0.0" uuid "^3.3.2" -ethers@0xproject/ethers.js#eip-838-reasons, ethers@3.0.22: - version "3.0.18" - resolved "https://codeload.github.com/0xproject/ethers.js/tar.gz/b91342bd200d142af0165d6befddf783c8ae8447" +ethers@3.0.22: + version "3.0.22" + resolved "https://registry.yarnpkg.com/ethers/-/ethers-3.0.22.tgz#7fab1ea16521705837aa43c15831877b2716b436" dependencies: aes-js "3.0.0" bn.js "^4.4.0" -- cgit v1.2.3 From 2ef867f398db381ebd8ec715bd108b46d8fa60f3 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 15 Aug 2018 15:20:25 -0700 Subject: Add stubs for ForwarderHelper interface in ForwarderHelperImpl --- packages/forwarder-helper/package.json | 18 +++----- .../forwarder-helper/src/forwarder_helper_impl.ts | 50 ++++++++++++++++++++++ packages/forwarder-helper/src/types.ts | 2 +- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/packages/forwarder-helper/package.json b/packages/forwarder-helper/package.json index ad2b5f7cf..e79154a52 100644 --- a/packages/forwarder-helper/package.json +++ b/packages/forwarder-helper/package.json @@ -15,22 +15,21 @@ "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", "test:circleci": "yarn test:coverage", - "run_mocha": "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", + "run_mocha": + "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", "clean": "shx rm -rf lib test_temp scripts", "build": "tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "manual:postpublish": "yarn build; node ./scripts/postpublish.js", "docs:stage": "node scripts/stage_docs.js", "docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES", - "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" + "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" }, "config": { "postpublish": { "assets": [], "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/", "s3StagingBucketPath": "s3://staging-doc-jsons/forwarder-helper/" } @@ -47,7 +46,8 @@ }, "homepage": "https://github.com/0xProject/0x-monorepo/packages/forwarder-helper/README.md", "dependencies": { - "@0xproject/types": "^0.8.2", + "@0xproject/order-utils": "^1.0.1-rc.3", + "@0xproject/types": "^1.0.1-rc.4", "@0xproject/typescript-typings": "^1.0.4", "@0xproject/utils": "^1.0.5", "@types/node": "^8.0.53" @@ -55,16 +55,12 @@ "devDependencies": { "@0xproject/monorepo-scripts": "^1.0.5", "@0xproject/tslint-config": "^1.0.5", - "@0xproject/utils": "^1.0.5", - "@types/lodash.foreach": "^4.5.3", - "@types/lodash.values": "^4.3.3", "@types/mocha": "^2.2.42", "chai": "^4.0.1", "chai-as-promised": "^7.1.0", "chai-bignumber": "^2.0.1", "copyfiles": "^1.2.0", "dirty-chai": "^2.0.1", - "lodash.foreach": "^4.5.0", "make-promises-safe": "^1.1.0", "mocha": "^4.1.0", "npm-run-all": "^4.1.2", diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index e69de29bb..1069bca73 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -0,0 +1,50 @@ +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import { + ForwarderHelper, + MarketBuyOrdersInfo, + MarketBuyOrdersInfoRequest, + MarketSellOrdersInfo, + MarketSellOrdersInfoRequest, +} from './types'; + +export class ForwarderHelperImpl implements ForwarderHelper { + private _orders: SignedOrder[]; + private _feeOrders: SignedOrder[]; + private _remainingFillableMakerAssetAmountsIfExists?: BigNumber[]; + private _remainingFillableFeeAmountsIfExists?: BigNumber[]; + constructor( + orders: SignedOrder[], + feeOrders: SignedOrder[] = [] as SignedOrder[], + remainingFillableMakerAssetAmounts?: BigNumber[], + remainingFillableFeeAmounts?: BigNumber[], + ) { + this._orders = orders; + this._feeOrders = feeOrders; + this._remainingFillableMakerAssetAmountsIfExists = remainingFillableMakerAssetAmounts; + this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts; + } + public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { + const { makerAssetFillAmount, feePercentage, acceptableEthAmountRange } = request; + return { + makerAssetFillAmount, + orders: this._orders, + feeOrders: this._feeOrders, + minEthAmount: new BigNumber(0), + maxEthAmount: new BigNumber(0), + feePercentage, + }; + } + public getMarketSellOrdersInfo(request: MarketSellOrdersInfoRequest): MarketSellOrdersInfo { + const { ethAmount, feePercentage, acceptableFillAmountRange } = request; + return { + ethAmount, + orders: this._orders, + feeOrders: this._feeOrders, + minFillAmount: new BigNumber(0), + maxFillAmount: new BigNumber(0), + feePercentage, + }; + } +} diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts index 23eaa1281..c6d4083a9 100644 --- a/packages/forwarder-helper/src/types.ts +++ b/packages/forwarder-helper/src/types.ts @@ -46,9 +46,9 @@ export interface MarketBuyOrdersInfo { makerAssetFillAmount: BigNumber; orders: SignedOrder[]; feeOrders: SignedOrder[]; - feePercentage?: BigNumber; minEthAmount: BigNumber; maxEthAmount: BigNumber; + feePercentage?: BigNumber; } /** -- cgit v1.2.3 From 3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Fri, 17 Aug 2018 00:15:52 -0700 Subject: Remove marketSell and add to marketBuy implementation --- packages/forwarder-helper/src/constants.ts | 5 ++ .../forwarder-helper/src/forwarder_helper_impl.ts | 60 +++++++++++++--------- packages/forwarder-helper/src/types.ts | 37 ------------- packages/order-utils/CHANGELOG.json | 4 ++ packages/order-utils/src/market_utils.ts | 4 +- yarn.lock | 6 +-- 6 files changed, 51 insertions(+), 65 deletions(-) create mode 100644 packages/forwarder-helper/src/constants.ts diff --git a/packages/forwarder-helper/src/constants.ts b/packages/forwarder-helper/src/constants.ts new file mode 100644 index 000000000..0ad30e4c0 --- /dev/null +++ b/packages/forwarder-helper/src/constants.ts @@ -0,0 +1,5 @@ +import { BigNumber } from '@0xproject/utils'; + +export const constants = { + ZERO_AMOUNT: new BigNumber(0), +}; diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 1069bca73..0d03c9f76 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -1,13 +1,11 @@ +import { marketUtils } from '@0xproject/order-utils'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; -import { - ForwarderHelper, - MarketBuyOrdersInfo, - MarketBuyOrdersInfoRequest, - MarketSellOrdersInfo, - MarketSellOrdersInfoRequest, -} from './types'; +import { constants } from './constants'; +import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types'; + +const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface export class ForwarderHelperImpl implements ForwarderHelper { private _orders: SignedOrder[]; @@ -26,24 +24,40 @@ export class ForwarderHelperImpl implements ForwarderHelper { this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts; } public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { - const { makerAssetFillAmount, feePercentage, acceptableEthAmountRange } = request; - return { + const { makerAssetFillAmount, feePercentage } = request; + // TODO: make the slippage percentage customizable + const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE); + const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( + this._orders, makerAssetFillAmount, - orders: this._orders, - feeOrders: this._feeOrders, - minEthAmount: new BigNumber(0), - maxEthAmount: new BigNumber(0), - feePercentage, - }; - } - public getMarketSellOrdersInfo(request: MarketSellOrdersInfoRequest): MarketSellOrdersInfo { - const { ethAmount, feePercentage, acceptableFillAmountRange } = request; + { + remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists, + slippageBufferAmount, + }, + ); + if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(ForwarderHelperError.InsufficientLiquidity); + } + // TODO: update this logic to find the minimum amount of feeOrders to cover the worst case as opposed to + // finding order that cover all fees, this will help with estimating ETH and minimizing gas usage + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + resultOrders, + this._feeOrders, + { + remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists, + remainingFillableFeeAmounts: this._remainingFillableFeeAmountsIfExists, + }, + ); + if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(ForwarderHelperError.InsufficientZrxLiquidity); + } + // TODO: calculate min and max eth usage return { - ethAmount, - orders: this._orders, - feeOrders: this._feeOrders, - minFillAmount: new BigNumber(0), - maxFillAmount: new BigNumber(0), + makerAssetFillAmount, + orders: resultOrders, + feeOrders: resultFeeOrders, + minEthAmount: constants.ZERO_AMOUNT, + maxEthAmount: constants.ZERO_AMOUNT, feePercentage, }; } diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts index c6d4083a9..084c3303e 100644 --- a/packages/forwarder-helper/src/types.ts +++ b/packages/forwarder-helper/src/types.ts @@ -9,13 +9,6 @@ export interface ForwarderHelper { * @return An object that conforms to MarketBuyOrdersInfo that satisfies the request. See type definition for more information. */ getMarketBuyOrdersInfo: (request: MarketBuyOrdersInfoRequest) => MarketBuyOrdersInfo; - /** - * Given a MarketSellOrdersInfoRequest, returns a MarketSellOrdersInfo containing all information relevant to fulfilling the request - * using the ForwarderContract marketSellOrdersWithEth function. - * @param request An object that conforms to MarketSellOrdersInfoRequest. See type definition for more information. - * @return An object that conforms to MarketSellOrdersInfo that satisfies the request. See type definition for more information. - */ - getMarketSellOrdersInfo: (request: MarketSellOrdersInfoRequest) => MarketSellOrdersInfo; } export enum ForwarderHelperError { @@ -26,12 +19,10 @@ export enum ForwarderHelperError { /** * makerAssetFillAmount: The amount of makerAsset requesting to be filled * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations - * acceptableEthAmountRange: maximum difference between min and max eth cost */ export interface MarketBuyOrdersInfoRequest { makerAssetFillAmount: BigNumber; feePercentage?: BigNumber; - acceptableEthAmountRange?: BigNumber; } /** @@ -50,31 +41,3 @@ export interface MarketBuyOrdersInfo { maxEthAmount: BigNumber; feePercentage?: BigNumber; } - -/** - * ethAmount: The amount of eth used to fill - * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations - * acceptableFillAmountRange: maximum difference between min and max asset filled - */ -export interface MarketSellOrdersInfoRequest { - ethAmount: BigNumber; - feePercentage?: BigNumber; - acceptableFillAmountRange?: BigNumber; -} - -/** - * ethAmount: The amount of eth used to fill - * orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested ethAmount plus slippage - * feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above - * minFillAmount: Amount of asset purchased in the worst case - * maxFillAmount: Amount of asset purchased in the best case - * feePercentage: Affiliate fee percentage used to calculate the eth amounts above. Passed thru directly from the request - */ -export interface MarketSellOrdersInfo { - ethAmount: BigNumber; - orders: SignedOrder[]; - feeOrders: SignedOrder[]; - minFillAmount: BigNumber; - maxFillAmount: BigNumber; - feePercentage?: BigNumber; -} diff --git a/packages/order-utils/CHANGELOG.json b/packages/order-utils/CHANGELOG.json index 86f0da65a..4acb3d233 100644 --- a/packages/order-utils/CHANGELOG.json +++ b/packages/order-utils/CHANGELOG.json @@ -14,6 +14,10 @@ "note": "Update marketUtils api such that all optional parameters are bundled into one optional param and more defaults are provided", "pr": 954 + }, + { + "note": + "Rename `resultOrders` to `resultFeeOrders` for object returned by `findFeeOrdersThatCoverFeesForTargetOrders` in `marketUtils` api" } ] }, diff --git a/packages/order-utils/src/market_utils.ts b/packages/order-utils/src/market_utils.ts index a0a827546..7eae4c8fc 100644 --- a/packages/order-utils/src/market_utils.ts +++ b/packages/order-utils/src/market_utils.ts @@ -84,7 +84,7 @@ export const marketUtils = { orders: T[], feeOrders: T[], opts?: FindFeeOrdersThatCoverFeesForTargetOrdersOpts, - ): { resultOrders: T[]; remainingFeeAmount: BigNumber } { + ): { resultFeeOrders: T[]; remainingFeeAmount: BigNumber } { assert.doesConformToSchema('orders', orders, schemas.ordersSchema); assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema); // try to get remainingFillableMakerAssetAmounts from opts, if it's not there, use makerAssetAmount values from orders @@ -137,7 +137,7 @@ export const marketUtils = { }, ); return { - resultOrders, + resultFeeOrders: resultOrders, remainingFeeAmount: remainingFillAmount, }; // TODO: add more orders here to cover rounding diff --git a/yarn.lock b/yarn.lock index 508e23a0f..7e278ac8a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5256,9 +5256,9 @@ ethereumjs-wallet@~0.6.0: utf8 "^3.0.0" uuid "^3.3.2" -ethers@3.0.22: - version "3.0.22" - resolved "https://registry.yarnpkg.com/ethers/-/ethers-3.0.22.tgz#7fab1ea16521705837aa43c15831877b2716b436" +ethers@0xproject/ethers.js#eip-838-reasons, ethers@3.0.22: + version "3.0.18" + resolved "https://codeload.github.com/0xproject/ethers.js/tar.gz/b91342bd200d142af0165d6befddf783c8ae8447" dependencies: aes-js "3.0.0" bn.js "^4.4.0" -- cgit v1.2.3 From e8a1950a7489e83f7f1a1127ee325f0c5c36483c Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Fri, 17 Aug 2018 13:00:54 -0700 Subject: Add ForwarderHelperImplConfig --- packages/forwarder-helper/package.json | 15 ++-- .../forwarder-helper/src/forwarder_helper_impl.ts | 94 +++++++++++++++++----- packages/order-utils/CHANGELOG.json | 3 + packages/order-utils/src/sorting_utils.ts | 2 +- packages/order-utils/test/market_utils_test.ts | 24 +++--- yarn.lock | 4 + 6 files changed, 104 insertions(+), 38 deletions(-) diff --git a/packages/forwarder-helper/package.json b/packages/forwarder-helper/package.json index e79154a52..d5f6c52b6 100644 --- a/packages/forwarder-helper/package.json +++ b/packages/forwarder-helper/package.json @@ -15,21 +15,22 @@ "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", "test:circleci": "yarn test:coverage", - "run_mocha": - "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", + "run_mocha": "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", "clean": "shx rm -rf lib test_temp scripts", "build": "tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "manual:postpublish": "yarn build; node ./scripts/postpublish.js", "docs:stage": "node scripts/stage_docs.js", "docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES", - "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" + "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" }, "config": { "postpublish": { "assets": [], "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/", "s3StagingBucketPath": "s3://staging-doc-jsons/forwarder-helper/" } @@ -50,11 +51,13 @@ "@0xproject/types": "^1.0.1-rc.4", "@0xproject/typescript-typings": "^1.0.4", "@0xproject/utils": "^1.0.5", - "@types/node": "^8.0.53" + "@types/node": "^8.0.53", + "lodash": "^4.17.10" }, "devDependencies": { "@0xproject/monorepo-scripts": "^1.0.5", "@0xproject/tslint-config": "^1.0.5", + "@types/lodash": "^4.14.116", "@types/mocha": "^2.2.42", "chai": "^4.0.1", "chai-as-promised": "^7.1.0", diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 0d03c9f76..8f4ec1c02 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -1,37 +1,93 @@ -import { marketUtils } from '@0xproject/order-utils'; +import { marketUtils, sortingUtils } from '@0xproject/order-utils'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; import { constants } from './constants'; import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types'; 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 { - private _orders: SignedOrder[]; - private _feeOrders: SignedOrder[]; - private _remainingFillableMakerAssetAmountsIfExists?: BigNumber[]; - private _remainingFillableFeeAmountsIfExists?: BigNumber[]; - constructor( + private _config: ForwarderHelperImplConfig; + private static _createSignedOrderWithAmounts( orders: SignedOrder[], - feeOrders: SignedOrder[] = [] as SignedOrder[], - remainingFillableMakerAssetAmounts?: BigNumber[], - remainingFillableFeeAmounts?: BigNumber[], - ) { - this._orders = orders; - this._feeOrders = feeOrders; - this._remainingFillableMakerAssetAmountsIfExists = remainingFillableMakerAssetAmounts; - this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts; + amounts?: BigNumber[], + ): SignedOrderWithAmount[] { + const ordersAndAmounts = _.map(orders, (order, index) => { + return { + ...order, + remainingFillAmount: _.nth(amounts, index), + }; + }); + 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 { const { makerAssetFillAmount, feePercentage } = request; + const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this._config; // TODO: make the slippage percentage customizable const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE); const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( - this._orders, + orders, makerAssetFillAmount, { - remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists, + remainingFillableMakerAssetAmounts, 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 const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( resultOrders, - this._feeOrders, + feeOrders, { - remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists, - remainingFillableFeeAmounts: this._remainingFillableFeeAmountsIfExists, + remainingFillableMakerAssetAmounts, + remainingFillableFeeAmounts, }, ); if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) { diff --git a/packages/order-utils/CHANGELOG.json b/packages/order-utils/CHANGELOG.json index 4acb3d233..c4260bc2f 100644 --- a/packages/order-utils/CHANGELOG.json +++ b/packages/order-utils/CHANGELOG.json @@ -18,6 +18,9 @@ { "note": "Rename `resultOrders` to `resultFeeOrders` for object returned by `findFeeOrdersThatCoverFeesForTargetOrders` in `marketUtils` api" + }, + { + "note": "Make `sortFeeOrdersByFeeAdjustedRate` in `sortingUtils` generic" } ] }, diff --git a/packages/order-utils/src/sorting_utils.ts b/packages/order-utils/src/sorting_utils.ts index 8811bcaf8..cd5163cf6 100644 --- a/packages/order-utils/src/sorting_utils.ts +++ b/packages/order-utils/src/sorting_utils.ts @@ -32,7 +32,7 @@ export const sortingUtils = { * the makerAsset and WETH as the takerAsset. * @return The input orders sorted by rate in ascending order */ - sortFeeOrdersByFeeAdjustedRate(feeOrders: Order[]): Order[] { + sortFeeOrdersByFeeAdjustedRate(feeOrders: T[]): T[] { assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema); const rateCalculator = rateUtils.getFeeAdjustedRateOfFeeOrder.bind(rateUtils); const sortedOrders = sortOrders(feeOrders, rateCalculator); diff --git a/packages/order-utils/test/market_utils_test.ts b/packages/order-utils/test/market_utils_test.ts index 109420a02..0c0151e57 100644 --- a/packages/order-utils/test/market_utils_test.ts +++ b/packages/order-utils/test/market_utils_test.ts @@ -139,11 +139,11 @@ describe('marketUtils', () => { ); describe('no target orders', () => { it('returns empty and zero remainingFeeAmount', async () => { - const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( [], inputFeeOrders, ); - expect(resultOrders).to.be.empty; + expect(resultFeeOrders).to.be.empty; expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT); }); }); @@ -162,14 +162,14 @@ describe('marketUtils', () => { // generate remainingFillableMakerAssetAmounts that equal the makerAssetAmount const remainingFillableMakerAssetAmounts = [makerAssetAmount, makerAssetAmount, makerAssetAmount]; it('returns empty and non-zero remainingFeeAmount', async () => { - const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( inputOrders, [], { remainingFillableMakerAssetAmounts, }, ); - expect(resultOrders).to.be.empty; + expect(resultFeeOrders).to.be.empty; expect(remainingFeeAmount).to.be.bignumber.equal(new BigNumber(30)); }); }); @@ -183,11 +183,11 @@ describe('marketUtils', () => { 3, ); it('returns empty and zero remainingFeeAmount', async () => { - const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( inputOrders, inputFeeOrders, ); - expect(resultOrders).to.be.empty; + expect(resultFeeOrders).to.be.empty; expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT); }); }); @@ -204,11 +204,11 @@ describe('marketUtils', () => { 3, ); it('returns input fee orders and zero remainingFeeAmount', async () => { - const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( inputOrders, inputFeeOrders, ); - expect(resultOrders).to.be.deep.equal(inputFeeOrders); + expect(resultFeeOrders).to.be.deep.equal(inputFeeOrders); expect(remainingFeeAmount).to.be.bignumber.equal(constants.ZERO_AMOUNT); }); }); @@ -230,14 +230,14 @@ describe('marketUtils', () => { // 3. order is completely fillable const remainingFillableMakerAssetAmounts = [constants.ZERO_AMOUNT, new BigNumber(5), makerAssetAmount]; it('returns first two input fee orders and zero remainingFeeAmount', async () => { - const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( inputOrders, inputFeeOrders, { 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); }); }); @@ -254,11 +254,11 @@ describe('marketUtils', () => { 3, ); it('returns input fee orders and non-zero remainingFeeAmount', async () => { - const { resultOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( inputOrders, inputFeeOrders, ); - expect(resultOrders).to.be.deep.equal(inputFeeOrders); + expect(resultFeeOrders).to.be.deep.equal(inputFeeOrders); expect(remainingFeeAmount).to.be.bignumber.equal(new BigNumber(30)); }); }); diff --git a/yarn.lock b/yarn.lock index 7e278ac8a..0fc6d1338 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1036,6 +1036,10 @@ version "4.14.99" 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": version "1.5.3" resolved "https://registry.yarnpkg.com/@types/loglevel/-/loglevel-1.5.3.tgz#adfce55383edc5998a2170ad581b3e23d6adb5b8" -- cgit v1.2.3 From 7e0e195c88c407f030754b6050b07698d1868d24 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Fri, 17 Aug 2018 14:00:53 -0700 Subject: Make config public readonly --- packages/forwarder-helper/src/forwarder_helper_impl.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 8f4ec1c02..39e84b477 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -20,7 +20,7 @@ export interface ForwarderHelperImplConfig { } export class ForwarderHelperImpl implements ForwarderHelper { - private _config: ForwarderHelperImplConfig; + public readonly config: ForwarderHelperImplConfig; private static _createSignedOrderWithAmounts( orders: SignedOrder[], amounts?: BigNumber[], @@ -76,11 +76,11 @@ export class ForwarderHelperImpl implements ForwarderHelper { }; } constructor(opts: ForwarderHelperImplConfig) { - this._config = ForwarderHelperImpl._sortConfig(opts); + this.config = ForwarderHelperImpl._sortConfig(opts); } public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { const { makerAssetFillAmount, feePercentage } = request; - const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this._config; + const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this.config; // TODO: make the slippage percentage customizable const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE); const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( -- cgit v1.2.3 From c2454d142841aa3a78a489f055692233f2fa93de Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 13:52:27 -0700 Subject: Add more TODO items --- packages/forwarder-helper/src/forwarder_helper_impl.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 39e84b477..43b2962da 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -56,6 +56,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { orders, remainingFillableMakerAssetAmounts, ); + // TODO: provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens const sortedOrderWithAmounts = sortingUtils.sortOrdersByFeeAdjustedRate(orderWithAmounts); const unbundledSortedOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts( sortedOrderWithAmounts, @@ -108,6 +109,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { throw new Error(ForwarderHelperError.InsufficientZrxLiquidity); } // TODO: calculate min and max eth usage + // TODO: optimize orders call data return { makerAssetFillAmount, orders: resultOrders, -- cgit v1.2.3 From 867ff3c5ece61c9119dd1dff9a769b15b39c96c6 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 13:57:55 -0700 Subject: Update yarn lock --- yarn.lock | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index 0fc6d1338..ac48dd50d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3761,6 +3761,17 @@ copy-webpack-plugin@^4.0.1: p-limit "^1.0.0" serialize-javascript "^1.4.0" +copyfiles@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/copyfiles/-/copyfiles-1.2.0.tgz#a8da3ac41aa2220ae29bd3c58b6984294f2c593c" + dependencies: + glob "^7.0.5" + ltcdr "^2.2.1" + minimatch "^3.0.3" + mkdirp "^0.5.1" + noms "0.0.0" + through2 "^2.0.1" + copyfiles@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/copyfiles/-/copyfiles-2.0.0.tgz#bbd78bb78e8fd6db5c67adf54249317b24560f2a" @@ -6013,7 +6024,7 @@ ganache-core@0xProject/ganache-core#monorepo-dep: ethereumjs-tx "0xProject/ethereumjs-tx#fake-tx-include-signature-by-default" ethereumjs-util "^5.2.0" ethereumjs-vm "2.3.5" - ethereumjs-wallet "~0.6.0" + ethereumjs-wallet "0.6.0" fake-merkle-patricia-tree "~1.0.1" heap "~0.2.6" js-scrypt "^0.2.0" @@ -8552,6 +8563,10 @@ lru-cache@~2.2.1: version "2.2.4" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.2.4.tgz#6c658619becf14031d0d0b594b16042ce4dc063d" +ltcdr@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/ltcdr/-/ltcdr-2.2.1.tgz#5ab87ad1d4c1dab8e8c08bbf037ee0c1902287cf" + ltgt@^2.1.2, ltgt@~2.2.0: version "2.2.1" resolved "https://registry.yarnpkg.com/ltgt/-/ltgt-2.2.1.tgz#f35ca91c493f7b73da0e07495304f17b31f87ee5" -- cgit v1.2.3 From 05ba049f5937be3a3a47ed90898f6a0267a67fd8 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 14:45:16 -0700 Subject: Add test for sorting that happens in constructor --- .../test/forwarder_helper_impl_test.ts | 87 +++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts index 1d3347d48..150ba5f0b 100644 --- a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts +++ b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts @@ -1,12 +1,95 @@ +import { testOrderFactory } from '@0xproject/order-utils/lib/test/utils/test_order_factory'; +import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; +import * as _ from 'lodash'; import 'mocha'; +import { ForwarderHelperImpl, ForwarderHelperImplConfig } from '../src/forwarder_helper_impl'; + import { chaiSetup } from './utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; describe('ForwarderHelperImpl', () => { - describe('#getMarketBuyOrdersInfo', () => {}); - describe('#getMarketSellOrdersInfo', () => {}); + // rate: 2 takerAsset / makerAsset + const testOrder1 = testOrderFactory.generateTestSignedOrder({ + makerAssetAmount: new BigNumber(100), + takerAssetAmount: new BigNumber(200), + }); + // rate: 1 takerAsset / makerAsset + const testOrder2 = testOrderFactory.generateTestSignedOrder({ + makerAssetAmount: new BigNumber(100), + takerAssetAmount: new BigNumber(100), + }); + // rate: 3 takerAsset / makerAsset + const testOrder3 = testOrderFactory.generateTestSignedOrder({ + makerAssetAmount: new BigNumber(100), + takerAssetAmount: new BigNumber(300), + }); + // rate: 3 WETH / ZRX + const testFeeOrder1 = testOrderFactory.generateTestSignedOrder({ + makerAssetAmount: new BigNumber(100), + takerAssetAmount: new BigNumber(300), + }); + // rate: 2 WETH / ZRX + const testFeeOrder2 = testOrderFactory.generateTestSignedOrder({ + makerAssetAmount: new BigNumber(100), + takerAssetAmount: new BigNumber(200), + }); + // rate: 1 WETH / ZRX + const testFeeOrder3 = testOrderFactory.generateTestSignedOrder({ + makerAssetAmount: new BigNumber(100), + takerAssetAmount: new BigNumber(100), + }); + describe('#constructor', () => { + const inputForwarderHelperConfig: ForwarderHelperImplConfig = { + orders: [testOrder1, testOrder2, testOrder3], + feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], + remainingFillableMakerAssetAmounts: [new BigNumber(1), new BigNumber(2), new BigNumber(3)], + remainingFillableFeeAmounts: [new BigNumber(4), new BigNumber(5), new BigNumber(6)], + }; + const inputForwarderHelperConfigNoRemainingAmounts: ForwarderHelperImplConfig = { + orders: [testOrder1, testOrder2, testOrder3], + feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], + }; + it('sorts orders', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + expect(forwarderHelper.config.orders).deep.equals([testOrder2, testOrder1, testOrder3]); + }); + it('sorts fee orders', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + expect(forwarderHelper.config.feeOrders).deep.equals([testFeeOrder3, testFeeOrder2, testFeeOrder1]); + }); + it('sorts remainingFillableMakerAssetAmounts', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + expect(forwarderHelper.config.remainingFillableMakerAssetAmounts).to.be.not.undefined(); + expect(_.nth(forwarderHelper.config.remainingFillableMakerAssetAmounts, 0)).to.bignumber.equal( + new BigNumber(2), + ); + expect(_.nth(forwarderHelper.config.remainingFillableMakerAssetAmounts, 1)).to.bignumber.equal( + new BigNumber(1), + ); + expect(_.nth(forwarderHelper.config.remainingFillableMakerAssetAmounts, 2)).to.bignumber.equal( + new BigNumber(3), + ); + }); + it('sorts remainingFillableFeeAmounts', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + expect(forwarderHelper.config.remainingFillableFeeAmounts).to.be.not.undefined(); + expect(_.nth(forwarderHelper.config.remainingFillableFeeAmounts, 0)).to.bignumber.equal(new BigNumber(6)); + expect(_.nth(forwarderHelper.config.remainingFillableFeeAmounts, 1)).to.bignumber.equal(new BigNumber(5)); + expect(_.nth(forwarderHelper.config.remainingFillableFeeAmounts, 2)).to.bignumber.equal(new BigNumber(4)); + }); + it('remainingFillableMakerAssetAmounts is undefined if none provided', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfigNoRemainingAmounts); + expect(forwarderHelper.config.remainingFillableMakerAssetAmounts).to.be.undefined(); + }); + it('remainingFillableFeeAmounts is undefined if none provided', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfigNoRemainingAmounts); + expect(forwarderHelper.config.remainingFillableFeeAmounts).to.be.undefined(); + }); + }); + // describe('#getMarketBuyOrdersInfo', () => {}); + // describe('#getMarketSellOrdersInfo', () => {}); }); -- cgit v1.2.3 From 67d33ec10c3d2467d2d073d22bfe2957353a0cc8 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 17:07:14 -0700 Subject: Fix rounding bug in marketUtils --- packages/order-utils/CHANGELOG.json | 11 +++++++++-- packages/order-utils/src/market_utils.ts | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/order-utils/CHANGELOG.json b/packages/order-utils/CHANGELOG.json index c4260bc2f..55e0b499e 100644 --- a/packages/order-utils/CHANGELOG.json +++ b/packages/order-utils/CHANGELOG.json @@ -17,10 +17,17 @@ }, { "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", + "pr": 954 + }, + { + "note": "Make `sortFeeOrdersByFeeAdjustedRate` in `sortingUtils` generic", + "pr": 954 }, { - "note": "Make `sortFeeOrdersByFeeAdjustedRate` in `sortingUtils` generic" + "note": + "Update `findFeeOrdersThatCoverFeesForTargetOrders` to round the the nearest integer when calculating required fees", + "pr": 954 } ] }, diff --git a/packages/order-utils/src/market_utils.ts b/packages/order-utils/src/market_utils.ts index 7eae4c8fc..b31a2b135 100644 --- a/packages/order-utils/src/market_utils.ts +++ b/packages/order-utils/src/market_utils.ts @@ -123,7 +123,7 @@ export const marketUtils = { const makerAssetAmountAvailable = remainingFillableMakerAssetAmounts[index]; const feeToFillMakerAssetAmountAvailable = makerAssetAmountAvailable .mul(order.takerFee) - .div(order.makerAssetAmount); + .dividedToIntegerBy(order.makerAssetAmount); return accFees.plus(feeToFillMakerAssetAmountAvailable); }, constants.ZERO_AMOUNT, -- cgit v1.2.3 From 68dfd1bb22c1d6d33a46ca1eff5fa6f62cde62ed Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 17:08:44 -0700 Subject: Add tests for getMarketBuyOrdersInfo --- .../forwarder-helper/src/forwarder_helper_impl.ts | 4 +- packages/forwarder-helper/src/types.ts | 2 +- .../test/forwarder_helper_impl_test.ts | 57 +++++++++++++++++++--- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 43b2962da..123c925f1 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -83,7 +83,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { const { makerAssetFillAmount, feePercentage } = request; const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this.config; // TODO: make the slippage percentage customizable - const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE); + const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE).round(); const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( orders, makerAssetFillAmount, @@ -93,7 +93,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { }, ); if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { - throw new Error(ForwarderHelperError.InsufficientLiquidity); + throw new Error(ForwarderHelperError.InsufficientMakerAssetLiquidity); } // TODO: update this logic to find the minimum amount of feeOrders to cover the worst case as opposed to // finding order that cover all fees, this will help with estimating ETH and minimizing gas usage diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts index 084c3303e..fb171cc90 100644 --- a/packages/forwarder-helper/src/types.ts +++ b/packages/forwarder-helper/src/types.ts @@ -12,7 +12,7 @@ export interface ForwarderHelper { } export enum ForwarderHelperError { - InsufficientLiquidity = 'INSUFFICIENT_LIQUIDITY', + InsufficientMakerAssetLiquidity = 'INSUFFICIENT_MAKER_ASSET_LIQUIDITY', InsufficientZrxLiquidity = 'INSUFFICIENT_ZRX_LIQUIDITY', } diff --git a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts index 150ba5f0b..3e2667a01 100644 --- a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts +++ b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts @@ -5,6 +5,7 @@ import * as _ from 'lodash'; import 'mocha'; import { ForwarderHelperImpl, ForwarderHelperImplConfig } from '../src/forwarder_helper_impl'; +import { ForwarderHelperError } from '../src/types'; import { chaiSetup } from './utils/chai_setup'; @@ -26,6 +27,7 @@ describe('ForwarderHelperImpl', () => { const testOrder3 = testOrderFactory.generateTestSignedOrder({ makerAssetAmount: new BigNumber(100), takerAssetAmount: new BigNumber(300), + takerFee: new BigNumber(1), }); // rate: 3 WETH / ZRX const testFeeOrder1 = testOrderFactory.generateTestSignedOrder({ @@ -42,13 +44,13 @@ describe('ForwarderHelperImpl', () => { makerAssetAmount: new BigNumber(100), takerAssetAmount: new BigNumber(100), }); + const inputForwarderHelperConfig: ForwarderHelperImplConfig = { + orders: [testOrder1, testOrder2, testOrder3], + feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], + remainingFillableMakerAssetAmounts: [new BigNumber(1), new BigNumber(2), new BigNumber(3)], + remainingFillableFeeAmounts: [new BigNumber(4), new BigNumber(5), new BigNumber(6)], + }; describe('#constructor', () => { - const inputForwarderHelperConfig: ForwarderHelperImplConfig = { - orders: [testOrder1, testOrder2, testOrder3], - feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], - remainingFillableMakerAssetAmounts: [new BigNumber(1), new BigNumber(2), new BigNumber(3)], - remainingFillableFeeAmounts: [new BigNumber(4), new BigNumber(5), new BigNumber(6)], - }; const inputForwarderHelperConfigNoRemainingAmounts: ForwarderHelperImplConfig = { orders: [testOrder1, testOrder2, testOrder3], feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], @@ -90,6 +92,45 @@ describe('ForwarderHelperImpl', () => { expect(forwarderHelper.config.remainingFillableFeeAmounts).to.be.undefined(); }); }); - // describe('#getMarketBuyOrdersInfo', () => {}); - // describe('#getMarketSellOrdersInfo', () => {}); + describe('#getMarketBuyOrdersInfo', () => { + it('throws if not enough makerAsset liquidity', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + expect(() => { + // request for 6 makerAsset units, because we have exactly 6 available we should throw because there is a built in slippage buffer + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount: new BigNumber(6), + }); + }).to.throw(ForwarderHelperError.InsufficientMakerAssetLiquidity); + }); + it('throws if not enough ZRX liquidity', () => { + const inputForwarderHelperConfigNoFees: ForwarderHelperImplConfig = { + orders: [testOrder1, testOrder2, testOrder3], + feeOrders: [], + }; + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfigNoFees); + expect(() => { + // request for 4 makerAsset units, we need fees but no fee orders exist, show we should throw + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount: new BigNumber(250), + }); + }).to.throw(ForwarderHelperError.InsufficientZrxLiquidity); + }); + it('passes the makerAssetFillAmount from the request to the info response', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + const makerAssetFillAmount = new BigNumber(4); + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount, + }); + expect(info.makerAssetFillAmount).to.bignumber.equal(makerAssetFillAmount); + }); + it('passes the feePercentage from the request to the info response', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + const feePercentage = new BigNumber(0.2); + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount: new BigNumber(4), + feePercentage, + }); + expect(info.feePercentage).to.bignumber.equal(feePercentage); + }); + }); }); -- cgit v1.2.3 From 6cef847a2759864de64d79ea02368bd85039e5ad Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 17:35:59 -0700 Subject: Write initial forwarderHelperFactory --- packages/forwarder-helper/src/forwarder_helper_factory.ts | 15 +++++++++++++++ packages/forwarder-helper/src/index.ts | 2 ++ 2 files changed, 17 insertions(+) create mode 100644 packages/forwarder-helper/src/forwarder_helper_factory.ts diff --git a/packages/forwarder-helper/src/forwarder_helper_factory.ts b/packages/forwarder-helper/src/forwarder_helper_factory.ts new file mode 100644 index 000000000..f08e3ff04 --- /dev/null +++ b/packages/forwarder-helper/src/forwarder_helper_factory.ts @@ -0,0 +1,15 @@ +import { SignedOrder } from '@0xproject/types'; + +import { ForwarderHelperImpl, ForwarderHelperImplConfig } from './forwarder_helper_impl'; +import { ForwarderHelper } from './types'; + +export const forwarderHelperFactory = { + getForwarderHelperForOrders(orders: SignedOrder[], feeOrders: SignedOrder[] = []): ForwarderHelper { + const config: ForwarderHelperImplConfig = { + orders, + feeOrders, + }; + const helper = new ForwarderHelperImpl(config); + return helper; + }, +}; diff --git a/packages/forwarder-helper/src/index.ts b/packages/forwarder-helper/src/index.ts index e69de29bb..eb3a34bd5 100644 --- a/packages/forwarder-helper/src/index.ts +++ b/packages/forwarder-helper/src/index.ts @@ -0,0 +1,2 @@ +export { forwarderHelperFactory } from './forwarder_helper_factory'; +export { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfoRequest, MarketBuyOrdersInfo } from './types'; -- cgit v1.2.3 From a5c7ddcff54fcb9e88adfda15167836b00db877d Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 22 Aug 2018 14:45:44 -0700 Subject: Add more comments --- packages/forwarder-helper/src/forwarder_helper_factory.ts | 6 ++++++ packages/forwarder-helper/src/forwarder_helper_impl.ts | 3 +++ 2 files changed, 9 insertions(+) diff --git a/packages/forwarder-helper/src/forwarder_helper_factory.ts b/packages/forwarder-helper/src/forwarder_helper_factory.ts index f08e3ff04..0fb02e3a6 100644 --- a/packages/forwarder-helper/src/forwarder_helper_factory.ts +++ b/packages/forwarder-helper/src/forwarder_helper_factory.ts @@ -4,6 +4,12 @@ import { ForwarderHelperImpl, ForwarderHelperImplConfig } from './forwarder_help import { ForwarderHelper } from './types'; export const forwarderHelperFactory = { + /** + * Given an array of orders and an array of feeOrders + * @param orders An array of objects conforming to SignedOrder. Each order should specify the same makerAssetData and takerAssetData + * @param orders An array of objects conforming to SignedOrder. Each order should specify ZRX as makerAssetData WETH as takerAssetData + * @return A ForwarderHelper, see type for definition + */ getForwarderHelperForOrders(orders: SignedOrder[], feeOrders: SignedOrder[] = []): ForwarderHelper { const config: ForwarderHelperImplConfig = { orders, diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 123c925f1..f21fab9fd 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -52,15 +52,18 @@ export class ForwarderHelperImpl implements ForwarderHelper { } private static _sortConfig(opts: ForwarderHelperImplConfig): ForwarderHelperImplConfig { const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = opts; + // Bundle orders together with their remainingFillAmounts so that we can sort them together const orderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts( orders, remainingFillableMakerAssetAmounts, ); // TODO: provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens const sortedOrderWithAmounts = sortingUtils.sortOrdersByFeeAdjustedRate(orderWithAmounts); + // Unbundle after sorting const unbundledSortedOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts( sortedOrderWithAmounts, ); + // Do the same bundling + unbundling for feeOrder sorting const feeOrderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts( feeOrders, remainingFillableFeeAmounts, -- cgit v1.2.3 From b0b3f9e339cc470860f8c1080ab6dad145d70624 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 22 Aug 2018 14:58:03 -0700 Subject: Add assertions to factory --- packages/forwarder-helper/package.json | 2 + .../src/forwarder_helper_factory.ts | 6 +- yarn.lock | 66 ++++++++++------------ 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/packages/forwarder-helper/package.json b/packages/forwarder-helper/package.json index d5f6c52b6..3a84e4620 100644 --- a/packages/forwarder-helper/package.json +++ b/packages/forwarder-helper/package.json @@ -47,6 +47,8 @@ }, "homepage": "https://github.com/0xProject/0x-monorepo/packages/forwarder-helper/README.md", "dependencies": { + "@0xproject/assert": "^1.0.5", + "@0xproject/json-schemas": "^1.0.1-rc.4", "@0xproject/order-utils": "^1.0.1-rc.3", "@0xproject/types": "^1.0.1-rc.4", "@0xproject/typescript-typings": "^1.0.4", diff --git a/packages/forwarder-helper/src/forwarder_helper_factory.ts b/packages/forwarder-helper/src/forwarder_helper_factory.ts index 0fb02e3a6..95f11f555 100644 --- a/packages/forwarder-helper/src/forwarder_helper_factory.ts +++ b/packages/forwarder-helper/src/forwarder_helper_factory.ts @@ -1,3 +1,5 @@ +import { assert } from '@0xproject/assert'; +import { schemas } from '@0xproject/json-schemas'; import { SignedOrder } from '@0xproject/types'; import { ForwarderHelperImpl, ForwarderHelperImplConfig } from './forwarder_helper_impl'; @@ -7,10 +9,12 @@ export const forwarderHelperFactory = { /** * Given an array of orders and an array of feeOrders * @param orders An array of objects conforming to SignedOrder. Each order should specify the same makerAssetData and takerAssetData - * @param orders An array of objects conforming to SignedOrder. Each order should specify ZRX as makerAssetData WETH as takerAssetData + * @param feeOrders An array of objects conforming to SignedOrder. Each order should specify ZRX as makerAssetData WETH as takerAssetData * @return A ForwarderHelper, see type for definition */ getForwarderHelperForOrders(orders: SignedOrder[], feeOrders: SignedOrder[] = []): ForwarderHelper { + assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema); + assert.doesConformToSchema('feeOrders', orders, schemas.signedOrdersSchema); const config: ForwarderHelperImplConfig = { orders, feeOrders, diff --git a/yarn.lock b/yarn.lock index ac48dd50d..0cbdf8f25 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1373,9 +1373,9 @@ aes-js@3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.0.0.tgz#e21df10ad6c2053295bcbb8dab40b09dbea87e4d" -aes-js@^3.1.1: - version "3.1.1" - resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.1.1.tgz#89fd1f94ae51b4c72d62466adc1a7323ff52f072" +aes-js@^0.2.3: + version "0.2.4" + resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-0.2.4.tgz#94b881ab717286d015fa219e08fb66709dda5a3d" ajv-keywords@^2.1.0: version "2.1.1" @@ -2433,6 +2433,10 @@ balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" +base-x@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/base-x/-/base-x-1.1.0.tgz#42d3d717474f9ea02207f6d1aa1f426913eeb7ac" + base-x@^3.0.2: version "3.0.4" resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.4.tgz#94c1788736da065edb1d68808869e357c977fa77" @@ -2836,7 +2840,7 @@ browserslist@^2.1.2: caniuse-lite "^1.0.30000792" electron-to-chromium "^1.3.30" -bs58@=4.0.1, bs58@^4.0.0: +bs58@=4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a" dependencies: @@ -2846,13 +2850,18 @@ bs58@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/bs58/-/bs58-2.0.1.tgz#55908d58f1982aba2008fa1bed8f91998a29bf8d" -bs58check@^2.1.2: - version "2.1.2" - resolved "https://registry.yarnpkg.com/bs58check/-/bs58check-2.1.2.tgz#53b018291228d82a5aa08e7d796fdafda54aebfc" +bs58@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/bs58/-/bs58-3.1.0.tgz#d4c26388bf4804cac714141b1945aa47e5eb248e" dependencies: - bs58 "^4.0.0" + base-x "^1.1.0" + +bs58check@^1.0.8: + version "1.3.4" + resolved "https://registry.yarnpkg.com/bs58check/-/bs58check-1.3.4.tgz#c52540073749117714fa042c3047eb8f9151cbf8" + dependencies: + bs58 "^3.1.0" create-hash "^1.1.0" - safe-buffer "^5.1.2" btoa@1.1.2: version "1.1.2" @@ -5204,7 +5213,7 @@ ethereumjs-util@5.1.5, ethereumjs-util@^5.0.0, ethereumjs-util@^5.0.1, ethereumj safe-buffer "^5.1.1" secp256k1 "^3.0.1" -ethereumjs-util@^4.0.1, ethereumjs-util@^4.3.0: +ethereumjs-util@^4.0.1, ethereumjs-util@^4.3.0, ethereumjs-util@^4.4.0: version "4.5.0" resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-4.5.0.tgz#3e9428b317eebda3d7260d854fddda954b1f1bc6" dependencies: @@ -5258,18 +5267,17 @@ ethereumjs-vm@^2.0.2, ethereumjs-vm@^2.1.0, ethereumjs-vm@^2.3.4: rustbn.js "~0.1.1" safe-buffer "^5.1.1" -ethereumjs-wallet@~0.6.0: - version "0.6.2" - resolved "https://registry.yarnpkg.com/ethereumjs-wallet/-/ethereumjs-wallet-0.6.2.tgz#67244b6af3e8113b53d709124b25477b64aeccda" +ethereumjs-wallet@0.6.0: + version "0.6.0" + resolved "https://registry.yarnpkg.com/ethereumjs-wallet/-/ethereumjs-wallet-0.6.0.tgz#82763b1697ee7a796be7155da9dfb49b2f98cfdb" dependencies: - aes-js "^3.1.1" - bs58check "^2.1.2" - ethereumjs-util "^5.2.0" - hdkey "^1.0.0" - safe-buffer "^5.1.2" + aes-js "^0.2.3" + bs58check "^1.0.8" + ethereumjs-util "^4.4.0" + hdkey "^0.7.0" scrypt.js "^0.2.0" - utf8 "^3.0.0" - uuid "^3.3.2" + utf8 "^2.1.1" + uuid "^2.0.1" ethers@0xproject/ethers.js#eip-838-reasons, ethers@3.0.22: version "3.0.18" @@ -6689,21 +6697,13 @@ hawk@~6.0.2: hoek "4.x.x" sntp "2.x.x" -hdkey@^0.7.1: +hdkey@^0.7.0, hdkey@^0.7.1: version "0.7.1" resolved "https://registry.yarnpkg.com/hdkey/-/hdkey-0.7.1.tgz#caee4be81aa77921e909b8d228dd0f29acaee632" dependencies: coinstring "^2.0.0" secp256k1 "^3.0.1" -hdkey@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/hdkey/-/hdkey-1.0.0.tgz#1d658dfe966aaa542c1d499586200b325e5c0cf4" - dependencies: - coinstring "^2.0.0" - safe-buffer "^5.1.1" - secp256k1 "^3.0.1" - he@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" @@ -14040,10 +14040,6 @@ utf8@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/utf8/-/utf8-2.1.2.tgz#1fa0d9270e9be850d9b05027f63519bf46457d96" -utf8@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/utf8/-/utf8-3.0.0.tgz#f052eed1364d696e769ef058b183df88c87f69d1" - util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" @@ -14089,10 +14085,6 @@ uuid@^2.0.1: version "2.0.3" resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a" -uuid@^3.3.2: - version "3.3.2" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" - uvm@1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/uvm/-/uvm-1.7.0.tgz#685d3a149ec7118fb73a73dfdc158ab46b0f0634" -- cgit v1.2.3 From cbcf8e8477cbb9061906866ea70dd885a185f358 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 22 Aug 2018 15:01:40 -0700 Subject: Add changelog entry and fix order-util changelog PR numbers --- packages/forwarder-helper/CHANGELOG.json | 12 +++++++++++- packages/order-utils/CHANGELOG.json | 6 +++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/forwarder-helper/CHANGELOG.json b/packages/forwarder-helper/CHANGELOG.json index fe51488c7..f95e7a2db 100644 --- a/packages/forwarder-helper/CHANGELOG.json +++ b/packages/forwarder-helper/CHANGELOG.json @@ -1 +1,11 @@ -[] +[ + { + "version": "1.0.0-rc.1", + "changes": [ + { + "note": "Add initial forwarderHelperFactory", + "pr": 997 + } + ] + } +] diff --git a/packages/order-utils/CHANGELOG.json b/packages/order-utils/CHANGELOG.json index 55e0b499e..149086857 100644 --- a/packages/order-utils/CHANGELOG.json +++ b/packages/order-utils/CHANGELOG.json @@ -18,16 +18,16 @@ { "note": "Rename `resultOrders` to `resultFeeOrders` for object returned by `findFeeOrdersThatCoverFeesForTargetOrders` in `marketUtils` api", - "pr": 954 + "pr": 997 }, { "note": "Make `sortFeeOrdersByFeeAdjustedRate` in `sortingUtils` generic", - "pr": 954 + "pr": 997 }, { "note": "Update `findFeeOrdersThatCoverFeesForTargetOrders` to round the the nearest integer when calculating required fees", - "pr": 954 + "pr": 997 } ] }, -- cgit v1.2.3 -- cgit v1.2.3 From 44ca6c4b71e23162821b923d2c812bbd5dd7b078 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 22 Aug 2018 15:20:53 -0700 Subject: Fix lint errors --- packages/forwarder-helper/src/forwarder_helper_impl.ts | 2 +- packages/forwarder-helper/test/forwarder_helper_impl_test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index f21fab9fd..0e59aa7d6 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -41,7 +41,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { return rest; }); const amounts = _.map(signedOrderWithAmounts, order => { - const { remainingFillAmount, ...rest } = order; + const { remainingFillAmount } = order; return remainingFillAmount; }); const compactAmounts = _.compact(amounts); diff --git a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts index 3e2667a01..3c3b6db92 100644 --- a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts +++ b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts @@ -97,7 +97,7 @@ describe('ForwarderHelperImpl', () => { const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); expect(() => { // request for 6 makerAsset units, because we have exactly 6 available we should throw because there is a built in slippage buffer - const info = forwarderHelper.getMarketBuyOrdersInfo({ + forwarderHelper.getMarketBuyOrdersInfo({ makerAssetFillAmount: new BigNumber(6), }); }).to.throw(ForwarderHelperError.InsufficientMakerAssetLiquidity); @@ -110,7 +110,7 @@ describe('ForwarderHelperImpl', () => { const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfigNoFees); expect(() => { // request for 4 makerAsset units, we need fees but no fee orders exist, show we should throw - const info = forwarderHelper.getMarketBuyOrdersInfo({ + forwarderHelper.getMarketBuyOrdersInfo({ makerAssetFillAmount: new BigNumber(250), }); }).to.throw(ForwarderHelperError.InsufficientZrxLiquidity); -- cgit v1.2.3 From 572ad4615a3a0fa5649549cbd65eb912ea7a0e92 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 22 Aug 2018 23:02:44 -0700 Subject: Fix prettier --- packages/forwarder-helper/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/forwarder-helper/CHANGELOG.md b/packages/forwarder-helper/CHANGELOG.md index e69de29bb..8b1378917 100644 --- a/packages/forwarder-helper/CHANGELOG.md +++ b/packages/forwarder-helper/CHANGELOG.md @@ -0,0 +1 @@ + -- cgit v1.2.3 From 68b060cb6ac48e21493c10d5ff27a401148003e4 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 23 Aug 2018 10:53:40 -0700 Subject: Update yarn lock --- yarn.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/yarn.lock b/yarn.lock index a72d0ee71..e4eb5bc9a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14242,6 +14242,10 @@ uuid@^2.0.1: version "2.0.3" resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a" +uuid@^3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" + uvm@1.7.0: version "1.7.0" resolved "https://registry.yarnpkg.com/uvm/-/uvm-1.7.0.tgz#685d3a149ec7118fb73a73dfdc158ab46b0f0634" -- cgit v1.2.3 From cd5c73550b969fe0a87524143ce617749935427a Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 23 Aug 2018 10:53:59 -0700 Subject: Update _sortedConfigs naming --- packages/forwarder-helper/src/forwarder_helper_impl.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 0e59aa7d6..83dc1f4b3 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -50,8 +50,8 @@ export class ForwarderHelperImpl implements ForwarderHelper { amounts: compactAmounts.length > 0 ? compactAmounts : undefined, }; } - private static _sortConfig(opts: ForwarderHelperImplConfig): ForwarderHelperImplConfig { - const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = opts; + private static _sortedConfig(config: ForwarderHelperImplConfig): ForwarderHelperImplConfig { + const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = config; // Bundle orders together with their remainingFillAmounts so that we can sort them together const orderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts( orders, @@ -79,8 +79,8 @@ export class ForwarderHelperImpl implements ForwarderHelper { remainingFillableFeeAmounts: unbundledSortedFeeOrderWithAmounts.amounts, }; } - constructor(opts: ForwarderHelperImplConfig) { - this.config = ForwarderHelperImpl._sortConfig(opts); + constructor(config: ForwarderHelperImplConfig) { + this.config = ForwarderHelperImpl._sortedConfig(config); } public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { const { makerAssetFillAmount, feePercentage } = request; -- cgit v1.2.3 From 2713cca6ac2d177c4cf3eac734ec6b55023e2ce9 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 23 Aug 2018 11:10:40 -0700 Subject: Remove legacy docs stuff --- packages/forwarder-helper/package.json | 17 ++---- .../src/monorepo_scripts/postpublish.ts | 8 --- .../src/monorepo_scripts/stage_docs.ts | 8 --- yarn.lock | 61 ++++++++++++++++++++-- 4 files changed, 61 insertions(+), 33 deletions(-) delete mode 100644 packages/forwarder-helper/src/monorepo_scripts/postpublish.ts delete mode 100644 packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts diff --git a/packages/forwarder-helper/package.json b/packages/forwarder-helper/package.json index 3a84e4620..fcb483885 100644 --- a/packages/forwarder-helper/package.json +++ b/packages/forwarder-helper/package.json @@ -15,25 +15,19 @@ "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", "test:circleci": "yarn test:coverage", - "run_mocha": "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", + "run_mocha": + "mocha --require source-map-support/register --require make-promises-safe lib/test/**/*_test.js --exit", "clean": "shx rm -rf lib test_temp scripts", "build": "tsc && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "manual:postpublish": "yarn build; node ./scripts/postpublish.js", "docs:stage": "node scripts/stage_docs.js", "docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES", - "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" + "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" }, "config": { "postpublish": { - "assets": [], - "docPublishConfigs": { - "extraFileIncludes": [ - "../types/src/index.ts", - "../ethereum-types/src/index.ts" - ], - "s3BucketPath": "s3://doc-jsons/forwarder-helper/", - "s3StagingBucketPath": "s3://staging-doc-jsons/forwarder-helper/" - } + "assets": [] } }, "repository": { @@ -57,7 +51,6 @@ "lodash": "^4.17.10" }, "devDependencies": { - "@0xproject/monorepo-scripts": "^1.0.5", "@0xproject/tslint-config": "^1.0.5", "@types/lodash": "^4.14.116", "@types/mocha": "^2.2.42", diff --git a/packages/forwarder-helper/src/monorepo_scripts/postpublish.ts b/packages/forwarder-helper/src/monorepo_scripts/postpublish.ts deleted file mode 100644 index dcb99d0f7..000000000 --- a/packages/forwarder-helper/src/monorepo_scripts/postpublish.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { postpublishUtils } from '@0xproject/monorepo-scripts'; - -import * as packageJSON from '../package.json'; -import * as tsConfigJSON from '../tsconfig.json'; - -const cwd = `${__dirname}/..`; -// tslint:disable-next-line:no-floating-promises -postpublishUtils.runAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts b/packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts deleted file mode 100644 index e732ac8eb..000000000 --- a/packages/forwarder-helper/src/monorepo_scripts/stage_docs.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { postpublishUtils } from '@0xproject/monorepo-scripts'; - -import * as packageJSON from '../package.json'; -import * as tsConfigJSON from '../tsconfig.json'; - -const cwd = `${__dirname}/..`; -// tslint:disable-next-line:no-floating-promises -postpublishUtils.publishDocsToStagingAsync(packageJSON, tsConfigJSON, cwd); diff --git a/yarn.lock b/yarn.lock index d44d76798..202bd8514 100644 --- a/yarn.lock +++ b/yarn.lock @@ -966,6 +966,12 @@ version "2.0.0" resolved "https://registry.yarnpkg.com/@types/find-versions/-/find-versions-2.0.0.tgz#a976ca032ab8dd1161116604eded0620d2b85df2" +"@types/fs-extra@5.0.0": + version "5.0.0" + resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-5.0.0.tgz#d3e225b35eb5c6d3a5a782c28219df365c781413" + dependencies: + "@types/node" "*" + "@types/fs-extra@^5.0.3": version "5.0.4" resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-5.0.4.tgz#b971134d162cc0497d221adde3dbb67502225599" @@ -980,6 +986,10 @@ "@types/minimatch" "*" "@types/node" "*" +"@types/handlebars@4.0.36": + version "4.0.36" + resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.36.tgz#ff57c77fa1ab6713bb446534ddc4d979707a3a79" + "@types/handlebars@^4.0.36": version "4.0.37" resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.37.tgz#a3bc3eba0c0f03f753cac00841a5b21e26a02c03" @@ -994,6 +1004,10 @@ dependencies: "@types/node" "*" +"@types/highlight.js@9.12.2": + version "9.12.2" + resolved "https://registry.yarnpkg.com/@types/highlight.js/-/highlight.js-9.12.2.tgz#6ee7cd395effe5ec80b515d3ff1699068cd0cd1d" + "@types/highlight.js@^9.12.3": version "9.12.3" resolved "https://registry.yarnpkg.com/@types/highlight.js/-/highlight.js-9.12.3.tgz#b672cfaac25cbbc634a0fd92c515f66faa18dbca" @@ -1040,11 +1054,11 @@ version "4.14.104" resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.104.tgz#53ee2357fa2e6e68379341d92eb2ecea4b11bb80" -"@types/lodash@^4.14.110": - version "4.14.116" - resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.116.tgz#5ccf215653e3e8c786a58390751033a9adca0eb9" +"@types/lodash@4.14.99": + version "4.14.99" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.99.tgz#e6e10c0a4cc16c7409b3181f1e66880d2fb7d4dc" -"@types/lodash@^4.14.116": +"@types/lodash@^4.14.110", "@types/lodash@^4.14.116": version "4.14.116" resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.116.tgz#5ccf215653e3e8c786a58390751033a9adca0eb9" @@ -1052,6 +1066,10 @@ version "1.5.3" resolved "https://registry.yarnpkg.com/@types/loglevel/-/loglevel-1.5.3.tgz#adfce55383edc5998a2170ad581b3e23d6adb5b8" +"@types/marked@0.3.0": + version "0.3.0" + resolved "https://registry.yarnpkg.com/@types/marked/-/marked-0.3.0.tgz#583c223dd33385a1dda01aaf77b0cd0411c4b524" + "@types/marked@^0.4.0": version "0.4.0" resolved "https://registry.yarnpkg.com/@types/marked/-/marked-0.4.0.tgz#057a6165703e7419217f8ffc6887747f980b6315" @@ -1232,6 +1250,13 @@ "@types/express-serve-static-core" "*" "@types/mime" "*" +"@types/shelljs@0.7.7": + version "0.7.7" + resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.7.7.tgz#1f7bfa28947661afea06365db9b1135bbc903ec4" + dependencies: + "@types/glob" "*" + "@types/node" "*" + "@types/shelljs@^0.8.0": version "0.8.0" resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.0.tgz#0caa56b68baae4f68f44e0dd666ab30b098e3632" @@ -8726,6 +8751,10 @@ marked@0.3.18: version "0.3.18" resolved "https://registry.npmjs.org/marked/-/marked-0.3.18.tgz#3ef058cd926101849b92a7a7c15db18c7fc76b2f" +marked@^0.3.12: + version "0.3.19" + resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.19.tgz#5d47f709c4c9fc3c216b6d46127280f40b39d790" + marked@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/marked/-/marked-0.4.0.tgz#9ad2c2a7a1791f10a852e0112f77b571dce10c66" @@ -12488,7 +12517,7 @@ shelljs@^0.7.3: interpret "^1.0.0" rechoir "^0.6.2" -shelljs@^0.8.0, shelljs@^0.8.2: +shelljs@^0.8.0, shelljs@^0.8.1, shelljs@^0.8.2: version "0.8.2" resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.2.tgz#345b7df7763f4c2340d584abb532c5f752ca9e35" dependencies: @@ -13871,6 +13900,28 @@ typedoc@0.12.0: typedoc-default-themes "^0.5.0" typescript "3.0.x" +typedoc@0xProject/typedoc: + version "0.10.0" + resolved "https://codeload.github.com/0xProject/typedoc/tar.gz/a9970a52f0924fec2df608abe30ff81153b3def2" + dependencies: + "@types/fs-extra" "5.0.0" + "@types/handlebars" "4.0.36" + "@types/highlight.js" "9.12.2" + "@types/lodash" "4.14.99" + "@types/marked" "0.3.0" + "@types/minimatch" "3.0.3" + "@types/shelljs" "0.7.7" + fs-extra "^5.0.0" + handlebars "^4.0.6" + highlight.js "^9.0.0" + lodash "^4.13.1" + marked "^0.3.12" + minimatch "^3.0.0" + progress "^2.0.0" + shelljs "^0.8.1" + typedoc-default-themes "^0.5.0" + typescript "2.7.1" + types-bn@^0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/types-bn/-/types-bn-0.0.1.tgz#4253c7c7251b14e1d77cdca6f58800e5e2b82c4b" -- cgit v1.2.3 From 6338b5bd3cbbcef149fb3fdf05116e40ea93aa05 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 23 Aug 2018 13:47:24 -0700 Subject: Move sorting phase into its own utils file --- .../forwarder-helper/src/forwarder_helper_impl.ts | 67 +--------------- .../utils/forwarder_helper_impl_config_utils.ts | 92 ++++++++++++++++++++++ 2 files changed, 95 insertions(+), 64 deletions(-) create mode 100644 packages/forwarder-helper/src/utils/forwarder_helper_impl_config_utils.ts diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 83dc1f4b3..a90edb0bb 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -1,17 +1,14 @@ -import { marketUtils, sortingUtils } from '@0xproject/order-utils'; +import { marketUtils } from '@0xproject/order-utils'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { constants } from './constants'; import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types'; +import { forwarderHelperImplConfigUtils } from './utils/forwarder_helper_impl_config_utils'; 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[]; @@ -21,66 +18,8 @@ export interface ForwarderHelperImplConfig { export class ForwarderHelperImpl implements ForwarderHelper { public readonly config: ForwarderHelperImplConfig; - private static _createSignedOrderWithAmounts( - orders: SignedOrder[], - amounts?: BigNumber[], - ): SignedOrderWithAmount[] { - const ordersAndAmounts = _.map(orders, (order, index) => { - return { - ...order, - remainingFillAmount: _.nth(amounts, index), - }; - }); - 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 } = order; - return remainingFillAmount; - }); - const compactAmounts = _.compact(amounts); - return { - orders, - amounts: compactAmounts.length > 0 ? compactAmounts : undefined, - }; - } - private static _sortedConfig(config: ForwarderHelperImplConfig): ForwarderHelperImplConfig { - const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = config; - // Bundle orders together with their remainingFillAmounts so that we can sort them together - const orderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts( - orders, - remainingFillableMakerAssetAmounts, - ); - // TODO: provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens - const sortedOrderWithAmounts = sortingUtils.sortOrdersByFeeAdjustedRate(orderWithAmounts); - // Unbundle after sorting - const unbundledSortedOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts( - sortedOrderWithAmounts, - ); - // Do the same bundling + unbundling for feeOrder sorting - 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(config: ForwarderHelperImplConfig) { - this.config = ForwarderHelperImpl._sortedConfig(config); + this.config = forwarderHelperImplConfigUtils.sortedConfig(config); } public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { const { makerAssetFillAmount, feePercentage } = request; diff --git a/packages/forwarder-helper/src/utils/forwarder_helper_impl_config_utils.ts b/packages/forwarder-helper/src/utils/forwarder_helper_impl_config_utils.ts new file mode 100644 index 000000000..253384f65 --- /dev/null +++ b/packages/forwarder-helper/src/utils/forwarder_helper_impl_config_utils.ts @@ -0,0 +1,92 @@ +import { sortingUtils } from '@0xproject/order-utils'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { ForwarderHelperImplConfig } from '../forwarder_helper_impl'; + +interface SignedOrderWithAmount extends SignedOrder { + remainingFillAmount: BigNumber; +} + +export const forwarderHelperImplConfigUtils = { + sortedConfig(config: ForwarderHelperImplConfig): ForwarderHelperImplConfig { + const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = config; + // TODO: provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens + const orderSorter = (ordersToSort: SignedOrder[]) => { + return sortingUtils.sortOrdersByFeeAdjustedRate(ordersToSort); + }; + const sortOrdersResult = sortOrdersAndRemainingFillAmounts( + orderSorter, + orders, + remainingFillableMakerAssetAmounts, + ); + const feeOrderSorter = (ordersToSort: SignedOrder[]) => { + return sortingUtils.sortFeeOrdersByFeeAdjustedRate(ordersToSort); + }; + const sortFeeOrdersResult = sortOrdersAndRemainingFillAmounts( + feeOrderSorter, + feeOrders, + remainingFillableFeeAmounts, + ); + return { + orders: sortOrdersResult.orders, + feeOrders: sortFeeOrdersResult.orders, + remainingFillableMakerAssetAmounts: sortOrdersResult.remainingFillAmounts, + remainingFillableFeeAmounts: sortFeeOrdersResult.remainingFillAmounts, + }; + }, +}; + +type OrderSorter = (orders: SignedOrder[]) => SignedOrder[]; + +function sortOrdersAndRemainingFillAmounts( + orderSorter: OrderSorter, + orders: SignedOrder[], + remainingFillAmounts?: BigNumber[], +): { orders: SignedOrder[]; remainingFillAmounts?: BigNumber[] } { + if (!_.isUndefined(remainingFillAmounts)) { + // Bundle orders together with their remainingFillAmounts so that we can sort them together + const orderWithAmounts = bundleSignedOrderWithAmounts(orders, remainingFillAmounts); + // Sort + const sortedOrderWithAmounts = orderSorter(orderWithAmounts) as SignedOrderWithAmount[]; + // Unbundle after sorting + const unbundledSortedOrderWithAmounts = unbundleSignedOrderWithAmounts(sortedOrderWithAmounts); + return { + orders: unbundledSortedOrderWithAmounts.orders, + remainingFillAmounts: unbundledSortedOrderWithAmounts.amounts, + }; + } else { + const sortedOrders = orderSorter(orders); + return { + orders: sortedOrders, + }; + } +} + +function bundleSignedOrderWithAmounts(orders: SignedOrder[], amounts: BigNumber[]): SignedOrderWithAmount[] { + const ordersAndAmounts = _.map(orders, (order, index) => { + return { + ...order, + remainingFillAmount: amounts[index], + }; + }); + return ordersAndAmounts; +} + +function 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 } = order; + return remainingFillAmount; + }); + return { + orders, + amounts, + }; +} -- cgit v1.2.3 From 6b667f8ecaa2f41bfe24f429748c0a707fb408bb Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 23 Aug 2018 14:56:39 -0700 Subject: Remove CHANGELOG.md --- packages/forwarder-helper/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 packages/forwarder-helper/CHANGELOG.md diff --git a/packages/forwarder-helper/CHANGELOG.md b/packages/forwarder-helper/CHANGELOG.md deleted file mode 100644 index 8b1378917..000000000 --- a/packages/forwarder-helper/CHANGELOG.md +++ /dev/null @@ -1 +0,0 @@ - -- cgit v1.2.3 From 21c37ba62fcc517d0a332b951be874e255763fec Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 23 Aug 2018 16:15:08 -0700 Subject: Upgrade changelog version to get around test-publis CI job --- packages/forwarder-helper/CHANGELOG.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/forwarder-helper/CHANGELOG.json b/packages/forwarder-helper/CHANGELOG.json index f95e7a2db..be5b244b3 100644 --- a/packages/forwarder-helper/CHANGELOG.json +++ b/packages/forwarder-helper/CHANGELOG.json @@ -1,6 +1,6 @@ [ { - "version": "1.0.0-rc.1", + "version": "1.0.1-rc.1", "changes": [ { "note": "Add initial forwarderHelperFactory", -- cgit v1.2.3