From 7460a36ce2683c61b168192ced5fd3899a4f709c Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 20 Nov 2017 10:55:28 +1100 Subject: calculate remaining maker token amount --- packages/0x.js/src/utils/order_state_utils.ts | 60 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 25 deletions(-) (limited to 'packages/0x.js/src') diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index 437ff1d82..6a9a1310c 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -84,18 +84,10 @@ export class OrderStateUtils { const transferrableMakerTokenAmount = BigNumber.min([makerProxyAllowance, makerBalance]); const transferrableFeeTokenAmount = BigNumber.min([makerFeeProxyAllowance, makerFeeBalance]); - let remainingFillableMakerTokenAmount; - if ((signedOrder.makerTokenAddress !== zrxTokenAddress || signedOrder.makerFee.isZero())) { - remainingFillableMakerTokenAmount = this.calculateFillableMakerTokenAmount( + const remainingFillableMakerTokenAmount = this.calculateRemainingMakerTokenAmount( transferrableMakerTokenAmount, transferrableFeeTokenAmount, remainingMakerTokenAmount, remainingFeeTokenAmount, totalMakerTokenAmount, signedOrder.makerFee, signedOrder.makerTokenAddress, zrxTokenAddress); - } else { - remainingFillableMakerTokenAmount = this.calculatePooledFillableMakerTokenAmount( - transferrableMakerTokenAmount, transferrableFeeTokenAmount, remainingMakerTokenAmount, - remainingFeeTokenAmount, totalMakerTokenAmount, signedOrder.makerFee, signedOrder.makerTokenAddress, - zrxTokenAddress); - } const remainingFillableTakerTokenAmount = remainingFillableMakerTokenAmount .times(totalTakerTokenAmount) @@ -112,51 +104,69 @@ export class OrderStateUtils { }; return orderRelevantState; } - private calculateFillableMakerTokenAmount(makerTransferrableAmount: BigNumber, - makerFeeTransferrableAmount: BigNumber, + private calculateRemainingMakerTokenAmount(transferrableMakerTokenAmount: BigNumber, + transferrableMakerFeeTokenAmount: BigNumber, + remainingMakerAmount: BigNumber, + remainingMakerFeeAmount: BigNumber, + totalMakerAmount: BigNumber, makerFeeAmount: BigNumber, + makerTokenAddress: string, zrxTokenAddress: string): BigNumber { + if ((makerTokenAddress !== zrxTokenAddress || makerFeeAmount.isZero())) { + return this.calculateFillableMakerTokenAmount( + transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerAmount, + remainingMakerFeeAmount, totalMakerAmount, makerFeeAmount, makerTokenAddress, + zrxTokenAddress); + } else { + return this.calculatePooledFillableMakerTokenAmount( + transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerAmount, + remainingMakerFeeAmount, totalMakerAmount, makerFeeAmount, makerTokenAddress, + zrxTokenAddress); + } + } + private calculateFillableMakerTokenAmount(transferrableMakerTokenAmount: BigNumber, + transferrableMakerFeeTokenAmount: BigNumber, remainingMakerAmount: BigNumber, remainingMakerFeeAmount: BigNumber, totalMakerAmount: BigNumber, makerFeeAmount: BigNumber, makerTokenAddress: string, zrxTokenAddress: string): BigNumber { if (makerFeeAmount.isZero()) { - return BigNumber.min(remainingMakerAmount, makerTransferrableAmount); - } else if (makerTransferrableAmount.gte(remainingMakerAmount) && - makerFeeTransferrableAmount.gte(remainingMakerFeeAmount)) { - return makerTransferrableAmount; + return BigNumber.min(remainingMakerAmount, transferrableMakerTokenAmount); + } else if (transferrableMakerTokenAmount.gte(remainingMakerAmount) && + transferrableMakerFeeTokenAmount.gte(remainingMakerFeeAmount)) { + return transferrableMakerTokenAmount; } else { return this.calculatePartiallyFillableMakerTokenAmount( - makerTransferrableAmount, makerFeeTransferrableAmount, remainingMakerAmount, + transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerAmount, remainingMakerFeeAmount, totalMakerAmount, makerFeeAmount, makerTokenAddress, zrxTokenAddress); } } - private calculatePooledFillableMakerTokenAmount(makerTransferrableAmount: BigNumber, - makerFeeTransferrableAmount: BigNumber, + private calculatePooledFillableMakerTokenAmount(transferrableMakerTokenAmount: BigNumber, + transferrableMakerFeeTokenAmount: BigNumber, remainingMakerAmount: BigNumber, remainingMakerFeeAmount: BigNumber, totalMakerAmount: BigNumber, makerFeeAmount: BigNumber, makerTokenAddress: string, zrxTokenAddress: string): BigNumber { - if (makerTransferrableAmount.plus(makerFeeTransferrableAmount).gte( + if (transferrableMakerTokenAmount.plus(transferrableMakerFeeTokenAmount).gte( remainingMakerAmount.plus(remainingMakerFeeAmount))) { return remainingMakerAmount; } else { return this.calculatePartiallyFillableMakerTokenAmount( - makerTransferrableAmount, makerFeeTransferrableAmount, remainingMakerAmount, + transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerAmount, remainingMakerFeeAmount, totalMakerAmount, makerFeeAmount, makerTokenAddress, zrxTokenAddress); } } - private calculatePartiallyFillableMakerTokenAmount(makerTransferrableAmount: BigNumber, - makerFeeTransferrableAmount: BigNumber, + private calculatePartiallyFillableMakerTokenAmount(transferrableMakerTokenAmount: BigNumber, + transferrableMakerFeeTokenAmount: BigNumber, remainingMakerAmount: BigNumber, remainingMakerFeeAmount: BigNumber, totalMakerAmount: BigNumber, makerFeeAmount: BigNumber, makerTokenAddress: string, zrxTokenAddress: string): BigNumber { const orderToFeeRatio = totalMakerAmount.dividedToIntegerBy(makerFeeAmount); - const fillableTimesInFeeToken = BigNumber.min(makerFeeTransferrableAmount, remainingMakerFeeAmount); - let fillableTimesInMakerToken = makerTransferrableAmount.dividedToIntegerBy(orderToFeeRatio); + const fillableTimesInFeeToken = BigNumber.min(transferrableMakerFeeTokenAmount, remainingMakerFeeAmount); + let fillableTimesInMakerToken = transferrableMakerTokenAmount.dividedToIntegerBy(orderToFeeRatio); if (makerTokenAddress === zrxTokenAddress) { - const totalFeeTokenPool = makerTransferrableAmount.plus(makerFeeTransferrableAmount); + const totalFeeTokenPool = transferrableMakerTokenAmount.plus(transferrableMakerFeeTokenAmount); fillableTimesInMakerToken = totalFeeTokenPool.dividedToIntegerBy( orderToFeeRatio.plus( ZeroEx.toBaseUnitAmount(new BigNumber(1), 18))); -- cgit v1.2.3