From 466ec6b22a51f0a9d618f6091ce6f9f533118280 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Wed, 16 Jan 2019 14:16:49 -0800 Subject: Change LiquidityForAssetData to return a BigNumber instead of a number to avoid losing precision --- packages/asset-buyer/src/asset_buyer.ts | 4 ++-- packages/asset-buyer/src/types.ts | 4 ++-- .../asset-buyer/src/utils/calculate_liquidity.ts | 4 ++-- packages/asset-buyer/test/asset_buyer_test.ts | 28 +++++++++++----------- 4 files changed, 20 insertions(+), 20 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 b4b024d62..4344f070c 100644 --- a/packages/asset-buyer/src/asset_buyer.ts +++ b/packages/asset-buyer/src/asset_buyer.ts @@ -200,8 +200,8 @@ export class AssetBuyer { const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow(); if (!assetPairs.includes(etherTokenAssetData)) { return { - tokensAvailableInBaseUnits: 0, - ethValueAvailableInWei: 0, + tokensAvailableInBaseUnits: new BigNumber(0), + ethValueAvailableInWei: new BigNumber(0), }; } diff --git a/packages/asset-buyer/src/types.ts b/packages/asset-buyer/src/types.ts index 9208a3217..46a2338ce 100644 --- a/packages/asset-buyer/src/types.ts +++ b/packages/asset-buyer/src/types.ts @@ -137,6 +137,6 @@ export interface OrdersAndFillableAmounts { * Represents available liquidity for a given assetData */ export interface LiquidityForAssetData { - tokensAvailableInBaseUnits: number; - ethValueAvailableInWei: number; + tokensAvailableInBaseUnits: BigNumber; + ethValueAvailableInWei: BigNumber; } diff --git a/packages/asset-buyer/src/utils/calculate_liquidity.ts b/packages/asset-buyer/src/utils/calculate_liquidity.ts index 910c756ac..a8d165b4b 100644 --- a/packages/asset-buyer/src/utils/calculate_liquidity.ts +++ b/packages/asset-buyer/src/utils/calculate_liquidity.ts @@ -28,7 +28,7 @@ export const calculateLiquidity = (ordersAndFillableAmounts: OrdersAndFillableAm // Turn into regular numbers return { - tokensAvailableInBaseUnits: liquidityInBigNumbers.tokensAvailableInBaseUnits.toNumber(), - ethValueAvailableInWei: liquidityInBigNumbers.ethValueAvailableInWei.toNumber(), + tokensAvailableInBaseUnits: liquidityInBigNumbers.tokensAvailableInBaseUnits, + ethValueAvailableInWei: liquidityInBigNumbers.ethValueAvailableInWei, }; }; diff --git a/packages/asset-buyer/test/asset_buyer_test.ts b/packages/asset-buyer/test/asset_buyer_test.ts index 9ed51f5e5..f117b4d7a 100644 --- a/packages/asset-buyer/test/asset_buyer_test.ts +++ b/packages/asset-buyer/test/asset_buyer_test.ts @@ -84,8 +84,8 @@ describe('AssetBuyer', () => { const assetBuyer = new AssetBuyer(mockWeb3Provider.object, mockOrderProvider.object); const liquidityResult = await assetBuyer.getLiquidityForAssetDataAsync(FAKE_ASSET_DATA); expect(liquidityResult).to.deep.equal({ - tokensAvailableInBaseUnits: 0, - ethValueAvailableInWei: 0, + tokensAvailableInBaseUnits: new BigNumber(0), + ethValueAvailableInWei: new BigNumber(0), }); }); it('should return 0s when only other asset pair supported', async () => { @@ -94,8 +94,8 @@ describe('AssetBuyer', () => { const assetBuyer = new AssetBuyer(mockWeb3Provider.object, mockOrderProvider.object); const liquidityResult = await assetBuyer.getLiquidityForAssetDataAsync(FAKE_ASSET_DATA); expect(liquidityResult).to.deep.equal({ - tokensAvailableInBaseUnits: 0, - ethValueAvailableInWei: 0, + tokensAvailableInBaseUnits: new BigNumber(0), + ethValueAvailableInWei: new BigNumber(0), }); }); }); @@ -121,8 +121,8 @@ describe('AssetBuyer', () => { remainingFillableMakerAssetAmounts: [], }; const expectedResult = { - tokensAvailableInBaseUnits: 0, - ethValueAvailableInWei: 0, + tokensAvailableInBaseUnits: new BigNumber(0), + ethValueAvailableInWei: new BigNumber(0), }; await expectLiquidityResult( mockWeb3Provider.object, @@ -142,8 +142,8 @@ describe('AssetBuyer', () => { const expectedTokensAvailable = orders[0].makerAssetAmount.plus(orders[1].makerAssetAmount); const expectedEthValueAvailable = orders[0].takerAssetAmount.plus(orders[1].takerAssetAmount); const expectedResult = { - tokensAvailableInBaseUnits: expectedTokensAvailable.toNumber(), - ethValueAvailableInWei: expectedEthValueAvailable.toNumber(), + tokensAvailableInBaseUnits: expectedTokensAvailable, + ethValueAvailableInWei: expectedEthValueAvailable, }; await expectLiquidityResult( @@ -160,8 +160,8 @@ describe('AssetBuyer', () => { remainingFillableMakerAssetAmounts: [baseUnitAmount(1)], }; const expectedResult = { - tokensAvailableInBaseUnits: baseUnitAmount(1).toNumber(), - ethValueAvailableInWei: baseUnitAmount(0.5, WETH_DECIMALS).toNumber(), + tokensAvailableInBaseUnits: baseUnitAmount(1), + ethValueAvailableInWei: baseUnitAmount(0.5, WETH_DECIMALS), }; await expectLiquidityResult( @@ -178,8 +178,8 @@ describe('AssetBuyer', () => { remainingFillableMakerAssetAmounts: [baseUnitAmount(1), baseUnitAmount(3)], }; const expectedResult = { - tokensAvailableInBaseUnits: baseUnitAmount(4).toNumber(), - ethValueAvailableInWei: baseUnitAmount(3.5, WETH_DECIMALS).toNumber(), + tokensAvailableInBaseUnits: baseUnitAmount(4), + ethValueAvailableInWei: baseUnitAmount(3.5, WETH_DECIMALS), }; await expectLiquidityResult( @@ -196,8 +196,8 @@ describe('AssetBuyer', () => { remainingFillableMakerAssetAmounts: [baseUnitAmount(0), baseUnitAmount(0)], }; const expectedResult = { - tokensAvailableInBaseUnits: baseUnitAmount(0).toNumber(), - ethValueAvailableInWei: baseUnitAmount(0, WETH_DECIMALS).toNumber(), + tokensAvailableInBaseUnits: baseUnitAmount(0), + ethValueAvailableInWei: baseUnitAmount(0, WETH_DECIMALS), }; await expectLiquidityResult( -- cgit