From 93f7e33f6a1d0a056198a9f22bcb10ef3e4f4f25 Mon Sep 17 00:00:00 2001 From: fragosti Date: Wed, 19 Sep 2018 00:57:05 +0200 Subject: Add isValidPercentage assert --- packages/asset-buyer/src/asset_buyer.ts | 20 +++++++------------- packages/asset-buyer/src/utils/assert.ts | 7 +++++++ 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'packages/asset-buyer') diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts index a2c0f37c9..7becb8285 100644 --- a/packages/asset-buyer/src/asset_buyer.ts +++ b/packages/asset-buyer/src/asset_buyer.ts @@ -159,15 +159,14 @@ export class AssetBuyer { * the next orderRefreshIntervalMs. Defaults to false. * @return An object that conforms to BuyQuote that satisfies the request. See type definition for more information. */ - public async getBuyQuoteAsync( - assetBuyAmount: BigNumber, - options: Partial, - ): Promise { - const { feePercentage, forceOrderRefresh, slippagePercentage } = { ...options, ...constants.DEFAULT_BUY_QUOTE_REQUEST_OPTS }; + public async getBuyQuoteAsync(assetBuyAmount: BigNumber, options: Partial): Promise { + const { feePercentage, forceOrderRefresh, slippagePercentage } = { + ...options, + ...constants.DEFAULT_BUY_QUOTE_REQUEST_OPTS, + }; assert.isBigNumber('assetBuyAmount', assetBuyAmount); - assert.isNumber('feePercentage', feePercentage); + assert.isValidPercentage('feePercentage', feePercentage); assert.isBoolean('forceOrderRefresh', forceOrderRefresh); - assert.isNumber('feePercentage', slippagePercentage); // we should refresh if: // we do not have any orders OR // we are forced to OR @@ -236,18 +235,13 @@ export class AssetBuyer { const desiredRate = rate || maxRate; // calculate how much eth is required to buy assetBuyAmount at the desired rate const ethAmount = assetBuyAmount.dividedToIntegerBy(desiredRate); - // TODO: critical - // update the forwarder wrapper to take in feePercentage as a number instead of a BigNumber, verify with Amir that this is being done correctly - const feePercentageBigNumber = !_.isUndefined(feePercentage) - ? Web3Wrapper.toBaseUnitAmount(new BigNumber(1), constants.ETHER_TOKEN_DECIMALS).mul(feePercentage) - : constants.ZERO_AMOUNT; const txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync( orders, assetBuyAmount, finalTakerAddress, ethAmount, feeOrders, - feePercentageBigNumber, + feePercentage, feeRecipient, ); return txHash; diff --git a/packages/asset-buyer/src/utils/assert.ts b/packages/asset-buyer/src/utils/assert.ts index edc90608c..745ba726f 100644 --- a/packages/asset-buyer/src/utils/assert.ts +++ b/packages/asset-buyer/src/utils/assert.ts @@ -41,4 +41,11 @@ export const assert = { `Expected all orders in ${variableName} to have the same makerAssetData and takerAssetData.`, ); }, + isValidPercentage(variableName: string, percentage: number): void { + assert.isNumber(variableName, percentage); + assert.assert( + percentage >= 0 && percentage <= 1, + `Expected ${variableName} to be between 0 and 1, but is ${percentage}`, + ); + }, }; -- cgit v1.2.3