diff options
author | Fabio Berger <me@fabioberger.com> | 2017-06-09 00:09:47 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-06-09 00:09:47 +0800 |
commit | 2b08c04d4d7b67592d3f846dc31d01892bdbeaeb (patch) | |
tree | 87ca7011419f1f0a9131096d0afbca0cb8ab4e04 /test | |
parent | f25447aa3dbbad0986d6dbd414b51a4591ccbc7c (diff) | |
parent | 31cc75bd6d2651466ebf50e9374d5cd19de6dd5e (diff) | |
download | dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.tar dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.tar.gz dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.tar.bz2 dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.tar.lz dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.tar.xz dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.tar.zst dexon-sol-tools-2b08c04d4d7b67592d3f846dc31d01892bdbeaeb.zip |
Merge branch 'master' into batchFillOrKill
# Conflicts:
# test/exchange_wrapper_test.ts
Diffstat (limited to 'test')
-rw-r--r-- | test/0x.js_test.ts | 94 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 448 | ||||
-rw-r--r-- | test/utils/fill_scenarios.ts | 26 |
3 files changed, 254 insertions, 314 deletions
diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 58f259a11..1349d6360 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -6,8 +6,9 @@ import * as BigNumber from 'bignumber.js'; import * as Sinon from 'sinon'; import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; -import {web3Factory} from './utils/web3_factory'; import {Order} from '../src/types'; +import {ECSignature} from '../src/types'; +import {web3Factory} from './utils/web3_factory'; chaiSetup.configure(); const expect = chai.expect; @@ -35,76 +36,51 @@ describe('ZeroEx library', () => { // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); - const exchangeWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); + const exchangeWeb3WrapperProvider = (zeroEx.exchange as any).web3Wrapper.getCurrentProvider(); expect((exchangeWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); - const tokenRegistryWeb3WrapperProvider = zeroEx.tokenRegistry.web3Wrapper.getCurrentProvider(); + const tokenRegistryWeb3WrapperProvider = (zeroEx.tokenRegistry as any).web3Wrapper.getCurrentProvider(); expect((tokenRegistryWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); }); }); describe('#isValidSignature', () => { - // This test data was borrowed from the JSON RPC documentation - // Source: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign - const data = '0xdeadbeaf'; + // The Exchange smart contract `isValidSignature` method only validates orderHashes and assumes + // the length of the data is exactly 32 bytes. Thus for these tests, we use data of this size. + const dataHex = '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0'; const signature = { v: 27, - r: '0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a1', - s: '0x2d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee', + r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33', + s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', }; - const address = '0x9b2055d370f73ec7d8a03e965129118dc8f5bf83'; - describe('should throw if passed a malformed signature', () => { - it('malformed v', () => { - const malformedSignature = { - v: 34, - r: signature.r, - s: signature.s, - }; - expect(() => ZeroEx.isValidSignature(data, malformedSignature, address)).to.throw(); - }); - it('r lacks 0x prefix', () => { - const malformedR = signature.r.replace('0x', ''); - const malformedSignature = { - v: signature.v, - r: malformedR, - s: signature.s, - }; - expect(() => ZeroEx.isValidSignature(data, malformedSignature, address)).to.throw(); - }); - it('r is too short', () => { - const malformedR = signature.r.substr(10); - const malformedSignature = { - v: signature.v, - r: malformedR, - s: signature.s.replace('0', 'z'), - }; - expect(() => ZeroEx.isValidSignature(data, malformedSignature, address)).to.throw(); - }); - it('s is not hex', () => { - const malformedS = signature.s.replace('0', 'z'); - const malformedSignature = { - v: signature.v, - r: signature.r, - s: malformedS, - }; - expect(() => ZeroEx.isValidSignature(data, malformedSignature, address)).to.throw(); - }); - }); - it('should return false if the data doesn\'t pertain to the signature & address', () => { - const isValid = ZeroEx.isValidSignature('0x0', signature, address); - expect(isValid).to.be.false(); + const address = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; + const web3 = web3Factory.create(); + const zeroEx = new ZeroEx(web3); + it('should return false if the data doesn\'t pertain to the signature & address', async () => { + expect(ZeroEx.isValidSignature('0x0', signature, address)).to.be.false(); + return expect( + (zeroEx.exchange as any).isValidSignatureUsingContractCallAsync('0x0', signature, address), + ).to.become(false); }); - it('should return false if the address doesn\'t pertain to the signature & data', () => { + it('should return false if the address doesn\'t pertain to the signature & data', async () => { const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; - const isValid = ZeroEx.isValidSignature(data, signature, validUnrelatedAddress); - expect(isValid).to.be.false(); + expect(ZeroEx.isValidSignature(dataHex, signature, validUnrelatedAddress)).to.be.false(); + return expect( + (zeroEx.exchange as any).isValidSignatureUsingContractCallAsync(dataHex, signature, + validUnrelatedAddress), + ).to.become(false); }); - it('should return false if the signature doesn\'t pertain to the data & address', () => { + it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => { const wrongSignature = _.assign({}, signature, {v: 28}); - const isValid = ZeroEx.isValidSignature(data, wrongSignature, address); - expect(isValid).to.be.false(); - }); - it('should return true if the signature does pertain to the data & address', () => { - const isValid = ZeroEx.isValidSignature(data, signature, address); - expect(isValid).to.be.true(); + expect(ZeroEx.isValidSignature(dataHex, wrongSignature, address)).to.be.false(); + return expect( + (zeroEx.exchange as any).isValidSignatureUsingContractCallAsync(dataHex, wrongSignature, address), + ).to.become(false); + }); + it('should return true if the signature does pertain to the dataHex & address', async () => { + const isValidSignatureLocal = ZeroEx.isValidSignature(dataHex, signature, address); + expect(isValidSignatureLocal).to.be.true(); + const isValidSignatureOnContract = await (zeroEx.exchange as any) + .isValidSignatureUsingContractCallAsync(dataHex, signature, address); + return expect(isValidSignatureOnContract).to.be.true(); }); }); describe('#generateSalt', () => { diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 972354f4e..2cd8af7f0 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -18,6 +18,7 @@ import { DoneCallback, ExchangeContractErrs, OrderCancellationRequest, + OrderFillRequest, } from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; import {TokenUtils} from './utils/token_utils'; @@ -51,76 +52,6 @@ describe('ExchangeWrapper', () => { afterEach(async () => { await blockchainLifecycle.revertAsync(); }); - describe('#isValidSignatureAsync', () => { - // The Exchange smart contract `isValidSignature` method only validates orderHashes and assumes - // the length of the data is exactly 32 bytes. Thus for these tests, we use data of this size. - const dataHex = '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0'; - const signature = { - v: 27, - r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33', - s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', - }; - const address = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; - describe('should throw if passed a malformed signature', () => { - it('malformed v', async () => { - const malformedSignature = { - v: 34, - r: signature.r, - s: signature.s, - }; - return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected(); - }); - it('r lacks 0x prefix', async () => { - const malformedR = signature.r.replace('0x', ''); - const malformedSignature = { - v: signature.v, - r: malformedR, - s: signature.s, - }; - return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected(); - }); - it('r is too short', async () => { - const malformedR = signature.r.substr(10); - const malformedSignature = { - v: signature.v, - r: malformedR, - s: signature.s.replace('0', 'z'), - }; - return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected(); - }); - it('s is not hex', async () => { - const malformedS = signature.s.replace('0', 'z'); - const malformedSignature = { - v: signature.v, - r: signature.r, - s: malformedS, - }; - return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected(); - }); - }); - it('should return false if the data doesn\'t pertain to the signature & address', async () => { - const isValid = await zeroEx.exchange.isValidSignatureAsync('0x0', signature, address); - expect(isValid).to.be.false(); - }); - it('should return false if the address doesn\'t pertain to the signature & dataHex', async () => { - const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; - const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, validUnrelatedAddress); - expect(isValid).to.be.false(); - }); - it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => { - const wrongSignature = {...signature, v: 28}; - const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, wrongSignature, address); - expect(isValid).to.be.false(); - }); - it('should return true if the signature does pertain to the dataHex & address', async () => { - const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, address); - expect(isValid).to.be.true(); - }); - }); describe('fillOrKill order(s)', () => { let makerTokenAddress: string; let takerTokenAddress: string; @@ -221,13 +152,14 @@ describe('ExchangeWrapper', () => { }); }); }); - describe('#fillOrderAsync', () => { + describe('fill order(s)', () => { let makerTokenAddress: string; let takerTokenAddress: string; let coinbase: string; let makerAddress: string; let takerAddress: string; let feeRecipient: string; + const fillableAmount = new BigNumber(5); const fillTakerAmount = new BigNumber(5); const shouldCheckTransfer = false; before(async () => { @@ -237,213 +169,227 @@ describe('ExchangeWrapper', () => { makerTokenAddress = makerToken.address; takerTokenAddress = takerToken.address; }); - describe('failed fills', () => { - it('should throw when the fill amount is zero', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - const zeroFillAmount = new BigNumber(0); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, zeroFillAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); - }); - it('should throw when sender is not a taker', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - const nonExistentSenderAddress = userAddresses[6]; - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, nonExistentSenderAddress, - )).to.be.rejectedWith(ExchangeContractErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); - }); - it('should throw when order is expired', async () => { - const expirationInPast = new BigNumber(1496826058); // 7th Jun 2017 - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast, - ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_EXPIRED); - }); - describe('should throw when not enough balance or allowance to fulfill the order', () => { - const fillableAmount = new BigNumber(5); - const balanceToSubtractFromMaker = new BigNumber(3); - const lackingAllowance = new BigNumber(3); - let signedOrder: SignedOrder; - beforeEach('create fillable signed order', async () => { - signedOrder = await fillScenarios.createFillableSignedOrderAsync( + describe('#fillOrderAsync', () => { + describe('failed fills', () => { + it('should throw when the fill amount is zero', async () => { + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); + const zeroFillAmount = new BigNumber(0); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, zeroFillAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); }); - it('should throw when taker balance is less than fill amount', async () => { - await zeroEx.token.transferAsync( - takerTokenAddress, takerAddress, coinbase, balanceToSubtractFromMaker, + it('should throw when sender is not a taker', async () => { + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); + const nonTakerAddress = userAddresses[6]; return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_BALANCE); + signedOrder, fillTakerAmount, shouldCheckTransfer, nonTakerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); }); - it('should throw when taker allowance is less than fill amount', async () => { - const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); - await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, - newAllowanceWhichIsLessThanFillAmount); + it('should throw when order is expired', async () => { + const expirationInPast = new BigNumber(1496826058); // 7th Jun 2017 + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, + fillableAmount, expirationInPast, + ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_EXPIRED); }); - it('should throw when maker balance is less than maker fill amount', async () => { - await zeroEx.token.transferAsync( - makerTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, + describe('should throw when not enough balance or allowance to fulfill the order', () => { + const balanceToSubtractFromMaker = new BigNumber(3); + const lackingAllowance = new BigNumber(3); + let signedOrder: SignedOrder; + beforeEach('create fillable signed order', async () => { + signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + }); + it('should throw when taker balance is less than fill amount', async () => { + await zeroEx.token.transferAsync( + takerTokenAddress, takerAddress, coinbase, balanceToSubtractFromMaker, + ); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_BALANCE); + }); + it('should throw when taker allowance is less than fill amount', async () => { + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); + await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, + newAllowanceWhichIsLessThanFillAmount); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); + }); + it('should throw when maker balance is less than maker fill amount', async () => { + await zeroEx.token.transferAsync( + makerTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, + ); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_BALANCE); + }); + it('should throw when maker allowance is less than maker fill amount', async () => { + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); + await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, + newAllowanceWhichIsLessThanFillAmount); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); + }); + }); + it('should throw when there a rounding error would have occurred', async () => { + const makerAmount = new BigNumber(3); + const takerAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, + makerAmount, takerAmount, ); + const fillTakerAmountThatCausesRoundingError = new BigNumber(3); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_BALANCE); + signedOrder, fillTakerAmountThatCausesRoundingError, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); }); - it('should throw when maker allowance is less than maker fill amount', async () => { - const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); - await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, - newAllowanceWhichIsLessThanFillAmount); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); + describe('should throw when not enough balance or allowance to pay fees', () => { + const makerFee = new BigNumber(2); + const takerFee = new BigNumber(2); + let signedOrder: SignedOrder; + beforeEach('setup', async () => { + signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, + makerAddress, takerAddress, fillableAmount, feeRecipient, + ); + }); + it('should throw when maker doesn\'t have enough balance to pay fees', async () => { + const balanceToSubtractFromMaker = new BigNumber(1); + await zeroEx.token.transferAsync( + zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, + ); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); + }); + it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { + const newAllowanceWhichIsLessThanFees = makerFee.minus(1); + await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, makerAddress, + newAllowanceWhichIsLessThanFees); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); + }); + it('should throw when taker doesn\'t have enough balance to pay fees', async () => { + const balanceToSubtractFromTaker = new BigNumber(1); + await zeroEx.token.transferAsync( + zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker, + ); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); + }); + it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { + const newAllowanceWhichIsLessThanFees = makerFee.minus(1); + await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, takerAddress, + newAllowanceWhichIsLessThanFees); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_ALLOWANCE); + }); }); }); - it('should throw when a rounding error would have occurred', async () => { - const makerAmount = new BigNumber(3); - const takerAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, - makerAmount, takerAmount, - ); - const fillTakerAmountThatCausesRoundingError = new BigNumber(3); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountThatCausesRoundingError, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); - }); - describe('should throw when not enough balance or allowance to pay fees', () => { - const fillableAmount = new BigNumber(5); - const makerFee = new BigNumber(2); - const takerFee = new BigNumber(2); - let signedOrder: SignedOrder; - beforeEach('setup', async () => { - signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( - makerTokenAddress, takerTokenAddress, makerFee, takerFee, - makerAddress, takerAddress, fillableAmount, feeRecipient, + describe('successful fills', () => { + it('should fill a valid order', async () => { + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) + .to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillableAmount); + await zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount)); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillTakerAmount); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillTakerAmount); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount)); }); - it('should throw when maker doesn\'t have enough balance to pay fees', async () => { - const balanceToSubtractFromMaker = new BigNumber(1); - await zeroEx.token.transferAsync( - zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, + it('should partially fill the valid order', async () => { + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); - }); - it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { - const newAllowanceWhichIsLessThanFees = makerFee.minus(1); - await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, makerAddress, - newAllowanceWhichIsLessThanFees); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); + const partialFillAmount = new BigNumber(3); + await zeroEx.exchange.fillOrderAsync( + signedOrder, partialFillAmount, shouldCheckTransfer, takerAddress); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(partialFillAmount); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) + .to.be.bignumber.equal(partialFillAmount); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); }); - it('should throw when taker doesn\'t have enough balance to pay fees', async () => { - const balanceToSubtractFromTaker = new BigNumber(1); - await zeroEx.token.transferAsync( - zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker, + it('should fill the valid orders with fees', async () => { + const makerFee = new BigNumber(1); + const takerFee = new BigNumber(2); + const signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, + makerAddress, takerAddress, fillableAmount, feeRecipient, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); - }); - it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { - const newAllowanceWhichIsLessThanFees = makerFee.minus(1); - await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, takerAddress, - newAllowanceWhichIsLessThanFees); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_ALLOWANCE); + await zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress); + expect(await zeroEx.token.getBalanceAsync(zrxTokenAddress, feeRecipient)) + .to.be.bignumber.equal(makerFee.plus(takerFee)); }); }); }); - describe('successful fills', () => { - it('should fill a valid order', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillableAmount); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) - .to.be.bignumber.equal(0); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) - .to.be.bignumber.equal(0); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillableAmount); - await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount)); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillTakerAmount); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillTakerAmount); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount)); - }); - it('should partially fill the valid order', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + describe('#batchFillOrderAsync', () => { + let signedOrder: SignedOrder; + let signedOrderHashHex: string; + let anotherSignedOrder: SignedOrder; + let anotherOrderHashHex: string; + let orderFillBatch: OrderFillRequest[]; + beforeEach(async () => { + signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - const partialFillAmount = new BigNumber(3); - await zeroEx.exchange.fillOrderAsync(signedOrder, partialFillAmount, shouldCheckTransfer, takerAddress); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) - .to.be.bignumber.equal(partialFillAmount); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) - .to.be.bignumber.equal(partialFillAmount); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); - }); - it('should fill up to remaining amount if desired fillAmount greater than available amount', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + signedOrderHashHex = await zeroEx.getOrderHashHexAsync(signedOrder); + anotherSignedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - const tooLargeFillAmount = new BigNumber(7); - const fillAmountDifference = tooLargeFillAmount.minus(fillableAmount); - await zeroEx.token.transferAsync(takerTokenAddress, coinbase, takerAddress, fillAmountDifference); - await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, tooLargeFillAmount); - await zeroEx.token.transferAsync(makerTokenAddress, coinbase, makerAddress, fillAmountDifference); - await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, tooLargeFillAmount); - - await zeroEx.exchange.fillOrderAsync(signedOrder, tooLargeFillAmount, shouldCheckTransfer, - takerAddress); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillAmountDifference); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillableAmount); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillableAmount); - expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillAmountDifference); + anotherOrderHashHex = await zeroEx.getOrderHashHexAsync(anotherSignedOrder); + orderFillBatch = [ + { + signedOrder, + takerTokenFillAmount: fillTakerAmount, + }, + { + signedOrder: anotherSignedOrder, + takerTokenFillAmount: fillTakerAmount, + }, + ]; }); - it('should fill the valid orders with fees', async () => { - const fillableAmount = new BigNumber(5); - const makerFee = new BigNumber(1); - const takerFee = new BigNumber(2); - const signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( - makerTokenAddress, takerTokenAddress, makerFee, takerFee, - makerAddress, takerAddress, fillableAmount, feeRecipient, - ); - await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress); - expect(await zeroEx.token.getBalanceAsync(zrxTokenAddress, feeRecipient)) - .to.be.bignumber.equal(makerFee.plus(takerFee)); + describe('successful batch fills', () => { + it('should no-op for an empty batch', async () => { + await zeroEx.exchange.batchFillOrderAsync([], shouldCheckTransfer, takerAddress); + }); + it('should successfully fill multiple orders', async () => { + await zeroEx.exchange.batchFillOrderAsync(orderFillBatch, shouldCheckTransfer, takerAddress); + const filledAmount = await zeroEx.exchange.getFilledTakerAmountAsync(signedOrderHashHex); + const anotherFilledAmount = await zeroEx.exchange.getFilledTakerAmountAsync(anotherOrderHashHex); + expect(filledAmount).to.be.bignumber.equal(fillTakerAmount); + expect(anotherFilledAmount).to.be.bignumber.equal(fillTakerAmount); + }); }); }); }); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index d8d6cd0b9..2860f1472 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -70,17 +70,35 @@ export class FillScenarios { makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, feeRecepient: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise<SignedOrder> { await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinbase, makerAddress, makerFillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, makerFillableAmount); + const oldMakerAllowance = await this.zeroEx.token.getProxyAllowanceAsync(makerTokenAddress, makerAddress); + const newMakerAllowance = oldMakerAllowance.plus(makerFillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync( + makerTokenAddress, makerAddress, newMakerAllowance, + ); await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinbase, takerAddress, takerFillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount); + const oldTakerAllowance = await this.zeroEx.token.getProxyAllowanceAsync(takerTokenAddress, takerAddress); + const newTakerAllowance = oldTakerAllowance.plus(takerFillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync( + takerTokenAddress, takerAddress, newTakerAllowance, + ); if (!makerFee.isZero()) { await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinbase, makerAddress, makerFee); - await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, makerAddress, makerFee); + const oldMakerFeeAllowance = + await this.zeroEx.token.getProxyAllowanceAsync(this.zrxTokenAddress, makerAddress); + const newMakerFeeAllowance = oldMakerFeeAllowance.plus(makerFee); + await this.zeroEx.token.setProxyAllowanceAsync( + this.zrxTokenAddress, makerAddress, newMakerFeeAllowance, + ); } if (!takerFee.isZero()) { await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinbase, takerAddress, takerFee); - await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, takerAddress, takerFee); + const oldTakerFeeAllowance = + await this.zeroEx.token.getProxyAllowanceAsync(this.zrxTokenAddress, takerAddress); + const newTakerFeeAllowance = oldTakerFeeAllowance.plus(takerFee); + await this.zeroEx.token.setProxyAllowanceAsync( + this.zrxTokenAddress, takerAddress, newTakerFeeAllowance, + ); } const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, |