diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-08-17 15:15:52 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-08-23 06:39:03 +0800 |
commit | 3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7 (patch) | |
tree | b53c5e88376bc6cfc52c8d20b645f9c7ceba1812 | |
parent | 2ef867f398db381ebd8ec715bd108b46d8fa60f3 (diff) | |
download | dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.gz dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.bz2 dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.lz dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.xz dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.zst dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.zip |
Remove marketSell and add to marketBuy implementation
-rw-r--r-- | packages/forwarder-helper/src/constants.ts | 5 | ||||
-rw-r--r-- | packages/forwarder-helper/src/forwarder_helper_impl.ts | 60 | ||||
-rw-r--r-- | packages/forwarder-helper/src/types.ts | 37 | ||||
-rw-r--r-- | packages/order-utils/CHANGELOG.json | 4 | ||||
-rw-r--r-- | packages/order-utils/src/market_utils.ts | 4 | ||||
-rw-r--r-- | yarn.lock | 6 |
6 files changed, 51 insertions, 65 deletions
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 @@ -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" |