diff options
author | Jacob Evans <jacob@dekz.net> | 2017-11-16 00:12:40 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2017-11-16 00:12:40 +0800 |
commit | 590552d3e07a74dc6ca76f0ac2a7d814d6a987be (patch) | |
tree | 71b70cd5a1f948b40aa6a60a00fbb4b721b9062e /packages/0x.js | |
parent | 557faba31b149ab52f6cc4b1930f457205b00229 (diff) | |
download | dexon-sol-tools-590552d3e07a74dc6ca76f0ac2a7d814d6a987be.tar.gz dexon-sol-tools-590552d3e07a74dc6ca76f0ac2a7d814d6a987be.tar.zst dexon-sol-tools-590552d3e07a74dc6ca76f0ac2a7d814d6a987be.zip |
Initial tests
Diffstat (limited to 'packages/0x.js')
-rw-r--r-- | packages/0x.js/src/utils/order_state_utils.ts | 34 | ||||
-rw-r--r-- | packages/0x.js/test/order_state_watcher_test.ts | 35 |
2 files changed, 65 insertions, 4 deletions
diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index 123584f90..e11435f1d 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -78,12 +78,19 @@ export class OrderStateUtils { const remainingTakerTokenAmount = totalTakerTokenAmount.minus(unavailableTakerTokenAmount); const remainingMakerTokenAmount = remainingTakerTokenAmount.times(totalMakerTokenAmount) .dividedToIntegerBy(totalTakerTokenAmount); - const fillableMakerTokenAmount = BigNumber.min([makerProxyAllowance, makerBalance]); - const remainingFillableMakerTokenAmount = BigNumber.min(fillableMakerTokenAmount, remainingMakerTokenAmount); + const remainingFeeTokenAmount = remainingTakerTokenAmount.times(signedOrder.makerFee) + .dividedToIntegerBy(totalTakerTokenAmount); + const transferrableMakerTokenAmount = BigNumber.min([makerProxyAllowance, makerBalance]); + const transferrableFeeTokenAmount = BigNumber.min([makerFeeProxyAllowance, makerFeeBalance]); + + const remainingFillableMakerTokenAmount = this.calculateFillableMakerTokenAmount( + transferrableMakerTokenAmount, transferrableFeeTokenAmount, remainingMakerTokenAmount, + remainingFeeTokenAmount, totalMakerTokenAmount, signedOrder.makerFee, signedOrder.makerTokenAddress, + zrxTokenAddress); + const remainingFillableTakerTokenAmount = remainingFillableMakerTokenAmount .times(totalTakerTokenAmount) .dividedToIntegerBy(totalMakerTokenAmount); - // TODO: Handle edge case where maker token is ZRX with fee const orderRelevantState = { makerBalance, makerProxyAllowance, @@ -96,6 +103,27 @@ export class OrderStateUtils { }; return orderRelevantState; } + private calculateFillableMakerTokenAmount(makerTransferrable: BigNumber, makerFeeTransferrable: BigNumber, + remainingMakerAmount: BigNumber, remainingMakerFee: BigNumber, + totalMakerAmount: BigNumber, makerFee: BigNumber, + makerTokenAddress: string, zrxTokenAddress: string): BigNumber { + if (makerFee.isZero()) { + return BigNumber.min(makerTransferrable, remainingMakerAmount); + } + const orderToFeeRatio = totalMakerAmount.dividedToIntegerBy(makerFee); + console.log('Order to Fee Ratio: ', orderToFeeRatio.toString()); + let fillableTimesInMakerToken = makerTransferrable.dividedToIntegerBy(orderToFeeRatio); + console.log('Fillable Token Times: ', fillableTimesInMakerToken.toString()); + const fillableTimesInFeeToken = BigNumber.min(makerFeeTransferrable, remainingMakerFee); + console.log('Fillable Fee Times: ', fillableTimesInFeeToken.toString()); + if (makerTokenAddress === zrxTokenAddress) { + fillableTimesInMakerToken = makerTransferrable.plus(makerFeeTransferrable) + .dividedToIntegerBy(orderToFeeRatio.plus(new BigNumber(1))); + + } + return BigNumber.min(fillableTimesInMakerToken.times(orderToFeeRatio), + fillableTimesInFeeToken.times(orderToFeeRatio)); + } private validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void { const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add( orderRelevantState.filledTakerTokenAmount, diff --git a/packages/0x.js/test/order_state_watcher_test.ts b/packages/0x.js/test/order_state_watcher_test.ts index a112bac1d..3bf768769 100644 --- a/packages/0x.js/test/order_state_watcher_test.ts +++ b/packages/0x.js/test/order_state_watcher_test.ts @@ -235,7 +235,7 @@ describe('OrderStateWatcher', () => { ); })().catch(done); }); - describe('remainingFillable(M|T)akerTokenAmount', () => { + describe.only('remainingFillable(M|T)akerTokenAmount', () => { it('should calculate correct remaining fillable', (done: DoneCallback) => { (async () => { const takerFillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(10), 18); @@ -321,6 +321,39 @@ describe('OrderStateWatcher', () => { makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount); })().catch(done); }); + it('should equal ratio amount when fee balance is lowered', (done: DoneCallback) => { + (async () => { + const takerFee = ZeroEx.toBaseUnitAmount(new BigNumber(0), 18); + const makerFee = ZeroEx.toBaseUnitAmount(new BigNumber(5), 18); + const feeRecipient = taker; + signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( + makerToken.address, takerToken.address, makerFee, takerFee, maker, + taker, fillableAmount, feeRecipient); + + const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker); + + const remainingFeeAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), 18); + const transferFeeAmount = makerFee.sub(remainingFeeAmount); + + const remainingTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), 18); + const transferTokenAmount = makerFee.sub(remainingFeeAmount); + zeroEx.orderStateWatcher.addOrder(signedOrder); + + const callback = reportCallbackErrors(done)((orderState: OrderState) => { + const validOrderState = orderState as OrderStateValid; + const orderRelevantState = validOrderState.orderRelevantState; + expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( + remainingAmount); + expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( + remainingAmount); + done(); + }); + zeroEx.orderStateWatcher.subscribe(callback); + await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, remainingAmount); + // await zeroEx.token.transferAsync( + // zrxTokenAddress, maker, ZeroEx.NULL_ADDRESS, transferAmount); + })().catch(done); + }); }); it('should emit orderStateInvalid when watched order cancelled', (done: DoneCallback) => { (async () => { |