diff options
author | Jacob Evans <jacob@dekz.net> | 2017-11-21 11:51:19 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2017-11-21 11:51:19 +0800 |
commit | da03331015b810505cfaae1445424f61ce05c656 (patch) | |
tree | cd8cb4796882b5a76da176f2419a30cf3037cafa /packages/0x.js/test | |
parent | 43128234bbb9715094c24c99b6a001a5290fcefd (diff) | |
download | dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.tar dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.tar.gz dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.tar.bz2 dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.tar.lz dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.tar.xz dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.tar.zst dexon-0x-contracts-da03331015b810505cfaae1445424f61ce05c656.zip |
Unit test edge case for ZRX and ZRX partial fill
Diffstat (limited to 'packages/0x.js/test')
-rw-r--r-- | packages/0x.js/test/remaining_fillable_calculator_test.ts | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/packages/0x.js/test/remaining_fillable_calculator_test.ts b/packages/0x.js/test/remaining_fillable_calculator_test.ts index 5d7cd9277..5b7fd63f1 100644 --- a/packages/0x.js/test/remaining_fillable_calculator_test.ts +++ b/packages/0x.js/test/remaining_fillable_calculator_test.ts @@ -13,26 +13,26 @@ const expect = chai.expect; describe.only('RemainingFillableCalculator', () => { let calculator: RemainingFillableCalculator; let signedOrder: SignedOrder; - let makerToken: string; - let takerToken: string; - let zrxToken: string; let transferrableMakerTokenAmount: BigNumber; let transferrableMakerFeeTokenAmount: BigNumber; let remainingMakerTokenAmount: BigNumber; let makerAmount: BigNumber; let takerAmount: BigNumber; let makerFee: BigNumber; + let isMakerTokenZRX: boolean; + const makerToken: string = '0x1'; + const takerToken: string = '0x2'; + const decimals: number = 4; const zero: BigNumber = new BigNumber(0); const zeroAddress = '0x0'; const signature: ECSignature = { v: 27, r: '', s: ''}; beforeEach(async () => { - [makerToken, takerToken, zrxToken] = ['0x1', '0x2', '0x3']; - [makerAmount, takerAmount, makerFee] = [ZeroEx.toBaseUnitAmount(new BigNumber(50), 18), - ZeroEx.toBaseUnitAmount(new BigNumber(5), 18), - ZeroEx.toBaseUnitAmount(new BigNumber(1), 18)]; + [makerAmount, takerAmount, makerFee] = [ZeroEx.toBaseUnitAmount(new BigNumber(50), decimals), + ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals), + ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals)]; [transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount] = [ - ZeroEx.toBaseUnitAmount(new BigNumber(50), 18), - ZeroEx.toBaseUnitAmount(new BigNumber(5), 18)]; + ZeroEx.toBaseUnitAmount(new BigNumber(50), decimals), + ZeroEx.toBaseUnitAmount(new BigNumber(5), decimals)]; }); function buildSignedOrder(): SignedOrder { return { ecSignature: signature, @@ -50,26 +50,20 @@ describe.only('RemainingFillableCalculator', () => { expirationUnixTimestampSec: zero }; } describe('Maker token is NOT ZRX', () => { - it('calculates the correct amount when balance is less than remaining fillable', () => { - signedOrder = buildSignedOrder(); - const partiallyFilledAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), 18); - remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount); - transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount); - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, - transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); - expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(transferrableMakerTokenAmount); + before(async () => { + isMakerTokenZRX = false; }); it('calculates the correct amount when unfilled and funds available', () => { signedOrder = buildSignedOrder(); remainingMakerTokenAmount = signedOrder.makerTokenAmount; - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount); }); it('calculates the correct amount when partially filled and funds available', () => { signedOrder = buildSignedOrder(); - remainingMakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18); - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + remainingMakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals); + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount); }); @@ -77,38 +71,37 @@ describe.only('RemainingFillableCalculator', () => { signedOrder = buildSignedOrder(); transferrableMakerFeeTokenAmount = zero; remainingMakerTokenAmount = signedOrder.makerTokenAmount; - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(zero); }); - }); - describe('Maker Token is ZRX', () => { - beforeEach(async () => { - makerToken = zrxToken; - }); it('calculates the correct amount when balance is less than remaining fillable', () => { signedOrder = buildSignedOrder(); - const partiallyFilledAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), 18); + const partiallyFilledAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals); remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount); transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount); - transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount; - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(transferrableMakerTokenAmount); }); + }); + describe('Maker Token is ZRX', () => { + before(async () => { + isMakerTokenZRX = true; + }); it('calculates the correct amount when unfilled and funds available', () => { signedOrder = buildSignedOrder(); transferrableMakerTokenAmount = makerAmount.plus(makerFee); transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount; remainingMakerTokenAmount = signedOrder.makerTokenAmount; - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount); }); it('calculates the correct amount when partially filled and funds available', () => { signedOrder = buildSignedOrder(); - remainingMakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18); - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + remainingMakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(1), decimals); + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount); }); @@ -117,9 +110,27 @@ describe.only('RemainingFillableCalculator', () => { transferrableMakerTokenAmount = zero; transferrableMakerFeeTokenAmount = zero; remainingMakerTokenAmount = signedOrder.makerTokenAmount; - calculator = new RemainingFillableCalculator(signedOrder, zrxToken, + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(zero); }); + it('calculates the correct amount when balance is less than remaining fillable', () => { + signedOrder = buildSignedOrder(); + const partiallyFilledAmount = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals); + remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount); + transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount); + transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount; + + const orderToFeeRatio = signedOrder.makerTokenAmount.dividedToIntegerBy(signedOrder.makerFee); + const expectedRemainingAmount = new BigNumber(450950); + const numberOfFillsInRatio = expectedRemainingAmount.dividedToIntegerBy(orderToFeeRatio); + calculator = new RemainingFillableCalculator(signedOrder, isMakerTokenZRX, + transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount, remainingMakerTokenAmount); + const calculatedRemainingAmount = calculator.computeRemainingMakerFillable(); + const calculatedRemainingAmountPlusFees = calculatedRemainingAmount.plus(numberOfFillsInRatio); + expect(calculatedRemainingAmount).to.be.bignumber.equal(expectedRemainingAmount); + expect(calculatedRemainingAmountPlusFees).to.be.bignumber.lessThan(transferrableMakerTokenAmount); + expect(calculatedRemainingAmountPlusFees).to.be.bignumber.lessThan(remainingMakerTokenAmount); + }); }); }); |