diff options
author | Jacob Evans <jacob@dekz.net> | 2017-11-20 07:55:28 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2017-11-20 07:55:28 +0800 |
commit | 7460a36ce2683c61b168192ced5fd3899a4f709c (patch) | |
tree | 39f216512e547a61b4f785a161bab13dff6c5167 /packages/0x.js/src/utils | |
parent | c8f6e3f923e81ac0b024aa075e5532002a0bb38f (diff) | |
download | dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.tar dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.tar.gz dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.tar.bz2 dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.tar.lz dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.tar.xz dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.tar.zst dexon-sol-tools-7460a36ce2683c61b168192ced5fd3899a4f709c.zip |
calculate remaining maker token amount
Diffstat (limited to 'packages/0x.js/src/utils')
-rw-r--r-- | packages/0x.js/src/utils/order_state_utils.ts | 60 |
1 files changed, 35 insertions, 25 deletions
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))); |