diff options
Diffstat (limited to 'packages/contracts/test/ts/exchange/core.ts')
-rw-r--r-- | packages/contracts/test/ts/exchange/core.ts | 586 |
1 files changed, 336 insertions, 250 deletions
diff --git a/packages/contracts/test/ts/exchange/core.ts b/packages/contracts/test/ts/exchange/core.ts index daf46ca37..770ef0c43 100644 --- a/packages/contracts/test/ts/exchange/core.ts +++ b/packages/contracts/test/ts/exchange/core.ts @@ -1,28 +1,22 @@ -import {ZeroEx} from '0x.js'; -import {BigNumber} from 'bignumber.js'; +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; import ethUtil = require('ethereumjs-util'); import * as Web3 from 'web3'; -import {Artifacts} from '../../../util/artifacts'; -import {Balances} from '../../../util/balances'; -import {constants} from '../../../util/constants'; -import {crypto} from '../../../util/crypto'; -import {ExchangeWrapper} from '../../../util/exchange_wrapper'; -import {Order} from '../../../util/order'; -import {OrderFactory} from '../../../util/order_factory'; -import {BalancesByOwner, ContractInstance, ExchangeContractErrs} from '../../../util/types'; -import {chaiSetup} from '../utils/chai_setup'; +import { Artifacts } from '../../../util/artifacts'; +import { Balances } from '../../../util/balances'; +import { constants } from '../../../util/constants'; +import { crypto } from '../../../util/crypto'; +import { ExchangeWrapper } from '../../../util/exchange_wrapper'; +import { Order } from '../../../util/order'; +import { OrderFactory } from '../../../util/order_factory'; +import { BalancesByOwner, ContractInstance, ExchangeContractErrs } from '../../../util/types'; +import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; -const { - Exchange, - TokenTransferProxy, - DummyToken, - TokenRegistry, - MaliciousToken, -} = new Artifacts(artifacts); +const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken } = new Artifacts(artifacts); // In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle // with type `any` to a variable of type `Web3`. @@ -52,13 +46,11 @@ contract('Exchange', (accounts: string[]) => { let zeroEx: ZeroEx; before(async () => { - [tokenRegistry, exchange] = await Promise.all([ - TokenRegistry.deployed(), - Exchange.deployed(), - ]); + [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); exWrapper = new ExchangeWrapper(exchange); zeroEx = new ZeroEx(web3.currentProvider, { exchangeContractAddress: exchange.address, + networkId: constants.TESTRPC_NETWORK_ID, }); const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ @@ -87,18 +79,30 @@ contract('Exchange', (accounts: string[]) => { ]); dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); await Promise.all([ - rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: maker}), - rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: taker}), - rep.setBalance(maker, INITIAL_BALANCE, {from: tokenOwner}), - rep.setBalance(taker, INITIAL_BALANCE, {from: tokenOwner}), - dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: maker}), - dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: taker}), - dgd.setBalance(maker, INITIAL_BALANCE, {from: tokenOwner}), - dgd.setBalance(taker, INITIAL_BALANCE, {from: tokenOwner}), - zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: maker}), - zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: taker}), - zrx.setBalance(maker, INITIAL_BALANCE, {from: tokenOwner}), - zrx.setBalance(taker, INITIAL_BALANCE, {from: tokenOwner}), + rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }), + rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }), + rep.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), + rep.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), + dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }), + dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }), + dgd.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), + dgd.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), + zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }), + zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }), + zrx.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), + zrx.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), ]); }); @@ -132,22 +136,29 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: new BigNumber(3), }); - const filledTakerTokenAmountBefore = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount1 = new BigNumber(2); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount: fillTakerTokenAmount1}); + await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: fillTakerTokenAmount1, + }); - const filledTakerTokenAmountAfter1 = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1); const fillTakerTokenAmount2 = new BigNumber(1); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount: fillTakerTokenAmount2}); + await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: fillTakerTokenAmount2, + }); - const filledTakerTokenAmountAfter2 = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1); }); @@ -157,42 +168,51 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), }); - const filledTakerTokenAmountBefore = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount}); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]) - .to.be.bignumber.equal(balances[maker][order.params.makerToken].minus(fillMakerTokenAmount)); - expect(newBalances[maker][order.params.takerToken]) - .to.be.bignumber.equal(balances[maker][order.params.takerToken].add(fillTakerTokenAmount)); - expect(newBalances[maker][zrx.address]).to.be.bignumber - .equal(balances[maker][zrx.address].minus(paidMakerFee)); - expect(newBalances[taker][order.params.takerToken]) - .to.be.bignumber.equal(balances[taker][order.params.takerToken].minus(fillTakerTokenAmount)); - expect(newBalances[taker][order.params.makerToken]) - .to.be.bignumber.equal(balances[taker][order.params.makerToken].add(fillMakerTokenAmount)); - expect(newBalances[taker][zrx.address]).to.be.bignumber - .equal(balances[taker][zrx.address].minus(paidTakerFee)); - expect(newBalances[feeRecipient][zrx.address]) - .to.be.bignumber.equal(balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee))); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); }); it('should transfer the correct amounts when makerTokenAmount > takerTokenAmount', async () => { @@ -201,42 +221,51 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), }); - const filledTakerTokenAmountBefore = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount}); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]) - .to.be.bignumber.equal(balances[maker][order.params.makerToken].minus(fillMakerTokenAmount)); - expect(newBalances[maker][order.params.takerToken]) - .to.be.bignumber.equal(balances[maker][order.params.takerToken].add(fillTakerTokenAmount)); - expect(newBalances[maker][zrx.address]).to.be.bignumber - .equal(balances[maker][zrx.address].minus(paidMakerFee)); - expect(newBalances[taker][order.params.takerToken]) - .to.be.bignumber.equal(balances[taker][order.params.takerToken].minus(fillTakerTokenAmount)); - expect(newBalances[taker][order.params.makerToken]) - .to.be.bignumber.equal(balances[taker][order.params.makerToken].add(fillMakerTokenAmount)); - expect(newBalances[taker][zrx.address]).to.be.bignumber - .equal(balances[taker][zrx.address].minus(paidTakerFee)); - expect(newBalances[feeRecipient][zrx.address]) - .to.be.bignumber.equal(balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee))); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); }); it('should transfer the correct amounts when makerTokenAmount < takerTokenAmount', async () => { @@ -245,42 +274,51 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const filledTakerTokenAmountBefore = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount}); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]) - .to.be.bignumber.equal(balances[maker][order.params.makerToken].minus(fillMakerTokenAmount)); - expect(newBalances[maker][order.params.takerToken]) - .to.be.bignumber.equal(balances[maker][order.params.takerToken].add(fillTakerTokenAmount)); - expect(newBalances[maker][zrx.address]).to.be.bignumber - .equal(balances[maker][zrx.address].minus(paidMakerFee)); - expect(newBalances[taker][order.params.takerToken]) - .to.be.bignumber.equal(balances[taker][order.params.takerToken].minus(fillTakerTokenAmount)); - expect(newBalances[taker][order.params.makerToken]) - .to.be.bignumber.equal(balances[taker][order.params.makerToken].add(fillMakerTokenAmount)); - expect(newBalances[taker][zrx.address]).to.be.bignumber - .equal(balances[taker][zrx.address].minus(paidTakerFee)); - expect(newBalances[feeRecipient][zrx.address]) - .to.be.bignumber.equal(balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee))); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); }); it('should transfer the correct amounts when taker is specified and order is claimed by taker', async () => { @@ -290,78 +328,95 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const filledTakerTokenAmountBefore = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount}); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = - await zeroEx.exchange.getFilledTakerAmountAsync(order.params.orderHashHex); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); const expectedFillAmountTAfter = fillTakerTokenAmount.add(filledTakerTokenAmountBefore); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(expectedFillAmountTAfter); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]) - .to.be.bignumber.equal(balances[maker][order.params.makerToken].minus(fillMakerTokenAmount)); - expect(newBalances[maker][order.params.takerToken]) - .to.be.bignumber.equal(balances[maker][order.params.takerToken].add(fillTakerTokenAmount)); - expect(newBalances[maker][zrx.address]).to.be.bignumber - .equal(balances[maker][zrx.address].minus(paidMakerFee)); - expect(newBalances[taker][order.params.takerToken]) - .to.be.bignumber.equal(balances[taker][order.params.takerToken].minus(fillTakerTokenAmount)); - expect(newBalances[taker][order.params.makerToken]) - .to.be.bignumber.equal(balances[taker][order.params.makerToken].add(fillMakerTokenAmount)); - expect(newBalances[taker][zrx.address]).to.be.bignumber - .equal(balances[taker][zrx.address].minus(paidTakerFee)); - expect(newBalances[feeRecipient][zrx.address]) - .to.be.bignumber.equal(balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee))); + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); }); it('should fill remaining value if fillTakerTokenAmount > remaining takerTokenAmount', async () => { const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount}); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const res = await exWrapper.fillOrderAsync(order, taker, - {fillTakerTokenAmount: order.params.takerTokenAmount}); + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount, + }); - expect(res.logs[0].args.filledTakerTokenAmount) - .to.be.bignumber.equal(order.params.takerTokenAmount.minus(fillTakerTokenAmount)); + expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( + order.params.takerTokenAmount.minus(fillTakerTokenAmount), + ); const newBalances = await dmyBalances.getAsync(); - expect(newBalances[maker][order.params.makerToken]) - .to.be.bignumber.equal(balances[maker][order.params.makerToken].minus(order.params.makerTokenAmount)); - expect(newBalances[maker][order.params.takerToken]) - .to.be.bignumber.equal(balances[maker][order.params.takerToken].add(order.params.takerTokenAmount)); - expect(newBalances[maker][zrx.address]) - .to.be.bignumber.equal(balances[maker][zrx.address].minus(order.params.makerFee)); - expect(newBalances[taker][order.params.takerToken]) - .to.be.bignumber.equal(balances[taker][order.params.takerToken].minus(order.params.takerTokenAmount)); - expect(newBalances[taker][order.params.makerToken]) - .to.be.bignumber.equal(balances[taker][order.params.makerToken].add(order.params.makerTokenAmount)); - expect(newBalances[taker][zrx.address]) - .to.be.bignumber.equal(balances[taker][zrx.address].minus(order.params.takerFee)); - expect(newBalances[feeRecipient][zrx.address]) - .to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(order.params.makerFee.add(order.params.takerFee)), - ); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(order.params.makerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(order.params.takerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(order.params.makerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(order.params.takerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(order.params.makerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(order.params.takerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(order.params.makerFee.add(order.params.takerFee)), + ); }); it('should log 1 event with the correct arguments when order has a feeRecipient', async () => { const divisor = 2; - const res = await exWrapper.fillOrderAsync(order, taker, - {fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor)}); + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor), + }); expect(res.logs).to.have.length(1); const logArgs = res.logs[0].args; @@ -390,8 +445,9 @@ contract('Exchange', (accounts: string[]) => { feeRecipient: ZeroEx.NULL_ADDRESS, }); const divisor = 2; - const res = await exWrapper.fillOrderAsync(order, taker, - {fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor)}); + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor), + }); expect(res.logs).to.have.length(1); const logArgs = res.logs[0].args; @@ -422,7 +478,7 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if signature is invalid', async () => { @@ -432,7 +488,7 @@ contract('Exchange', (accounts: string[]) => { order.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); order.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if makerTokenAmount is 0', async () => { @@ -440,7 +496,7 @@ contract('Exchange', (accounts: string[]) => { makerTokenAmount: new BigNumber(0), }); - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if takerTokenAmount is 0', async () => { @@ -448,19 +504,21 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: new BigNumber(0), }); - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if fillTakerTokenAmount is 0', async () => { order = await orderFactory.newSignedOrderAsync(); - return expect(exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount: new BigNumber(0)})) - .to.be.rejectedWith(constants.INVALID_OPCODE); + return expect( + exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: new BigNumber(0), + }), + ).to.be.rejectedWith(constants.REVERT); }); it('should not change balances if maker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { order = await orderFactory.newSignedOrderAsync({ makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); @@ -471,19 +529,20 @@ contract('Exchange', (accounts: string[]) => { }); it('should throw if maker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', - async () => { + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { order = await orderFactory.newSignedOrderAsync({ makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); - return expect(exWrapper.fillOrderAsync(order, taker, {shouldThrowOnInsufficientBalanceOrAllowance: true})) - .to.be.rejectedWith(constants.INVALID_OPCODE); + return expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); }); it('should not change balances if taker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { order = await orderFactory.newSignedOrderAsync({ takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); @@ -494,59 +553,70 @@ contract('Exchange', (accounts: string[]) => { }); it('should throw if taker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', - async () => { + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { order = await orderFactory.newSignedOrderAsync({ takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); - return expect(exWrapper.fillOrderAsync(order, taker, {shouldThrowOnInsufficientBalanceOrAllowance: true})) - .to.be.rejectedWith(constants.INVALID_OPCODE); + return expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); }); it('should not change balances if maker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { - await rep.approve(TokenTransferProxy.address, 0, {from: maker}); + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + await rep.approve(TokenTransferProxy.address, 0, { from: maker }); await exWrapper.fillOrderAsync(order, taker); - await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: maker}); + await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }); const newBalances = await dmyBalances.getAsync(); expect(newBalances).to.be.deep.equal(balances); }); it('should throw if maker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', - async () => { - await rep.approve(TokenTransferProxy.address, 0, {from: maker}); - expect(exWrapper.fillOrderAsync(order, taker, {shouldThrowOnInsufficientBalanceOrAllowance: true})) - .to.be.rejectedWith(constants.INVALID_OPCODE); - await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: maker}); + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { + await rep.approve(TokenTransferProxy.address, 0, { from: maker }); + expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); + await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }); }); it('should not change balances if taker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { - await dgd.approve(TokenTransferProxy.address, 0, {from: taker}); + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); await exWrapper.fillOrderAsync(order, taker); - await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: taker}); + await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }); const newBalances = await dmyBalances.getAsync(); expect(newBalances).to.be.deep.equal(balances); }); it('should throw if taker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', - async () => { - await dgd.approve(TokenTransferProxy.address, 0, {from: taker}); - expect(exWrapper.fillOrderAsync(order, taker, {shouldThrowOnInsufficientBalanceOrAllowance: true})) - .to.be.rejectedWith(constants.INVALID_OPCODE); - await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: taker}); + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { + await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); + expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); + await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }); }); it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker balance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const makerZRXBalance = new BigNumber(balances[maker][zrx.address]); order = await orderFactory.newSignedOrderAsync({ makerToken: zrx.address, @@ -559,8 +629,7 @@ contract('Exchange', (accounts: string[]) => { }); it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker allowance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const makerZRXAllowance = await zrx.allowance(maker, TokenTransferProxy.address); order = await orderFactory.newSignedOrderAsync({ makerToken: zrx.address, @@ -573,8 +642,7 @@ contract('Exchange', (accounts: string[]) => { }); it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker balance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const takerZRXBalance = new BigNumber(balances[taker][zrx.address]); order = await orderFactory.newSignedOrderAsync({ takerToken: zrx.address, @@ -587,8 +655,7 @@ contract('Exchange', (accounts: string[]) => { }); it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker allowance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', - async () => { + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const takerZRXAllowance = await zrx.allowance(taker, TokenTransferProxy.address); order = await orderFactory.newSignedOrderAsync({ takerToken: zrx.address, @@ -603,14 +670,17 @@ contract('Exchange', (accounts: string[]) => { it('should throw if getBalance or getAllowance attempts to change state and \ shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const maliciousToken = await MaliciousToken.new(); - await maliciousToken.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, {from: taker}); + await maliciousToken.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); order = await orderFactory.newSignedOrderAsync({ takerToken: maliciousToken.address, }); - return expect(exWrapper.fillOrderAsync(order, taker, {shouldThrowOnInsufficientBalanceOrAllowance: false})) - .to.be.rejectedWith(constants.INVALID_OPCODE); + return expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: false, + }), + ).to.be.rejectedWith(constants.REVERT); }); it('should not change balances if an order is expired', async () => { @@ -651,7 +721,7 @@ contract('Exchange', (accounts: string[]) => { }); it('should throw if not sent by maker', async () => { - return expect(exWrapper.cancelOrderAsync(order, taker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.cancelOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if makerTokenAmount is 0', async () => { @@ -659,7 +729,7 @@ contract('Exchange', (accounts: string[]) => { makerTokenAmount: new BigNumber(0), }); - return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if takerTokenAmount is 0', async () => { @@ -667,19 +737,24 @@ contract('Exchange', (accounts: string[]) => { takerTokenAmount: new BigNumber(0), }); - return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.INVALID_OPCODE); + return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if cancelTakerTokenAmount is 0', async () => { order = await orderFactory.newSignedOrderAsync(); - return expect(exWrapper.cancelOrderAsync(order, maker, {cancelTakerTokenAmount: new BigNumber(0)})) - .to.be.rejectedWith(constants.INVALID_OPCODE); + return expect( + exWrapper.cancelOrderAsync(order, maker, { + cancelTakerTokenAmount: new BigNumber(0), + }), + ).to.be.rejectedWith(constants.REVERT); }); it('should be able to cancel a full order', async () => { await exWrapper.cancelOrderAsync(order, maker); - await exWrapper.fillOrderAsync(order, taker, {fillTakerTokenAmount: order.params.takerTokenAmount.div(2)}); + await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(2), + }); const newBalances = await dmyBalances.getAsync(); expect(newBalances).to.be.deep.equal(balances); @@ -687,43 +762,55 @@ contract('Exchange', (accounts: string[]) => { it('should be able to cancel part of an order', async () => { const cancelTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.cancelOrderAsync(order, maker, {cancelTakerTokenAmount}); + await exWrapper.cancelOrderAsync(order, maker, { + cancelTakerTokenAmount, + }); - const res = await exWrapper.fillOrderAsync(order, taker, - {fillTakerTokenAmount: order.params.takerTokenAmount}); - expect(res.logs[0].args.filledTakerTokenAmount) - .to.be.bignumber.equal(order.params.takerTokenAmount.minus(cancelTakerTokenAmount)); + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount, + }); + expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( + order.params.takerTokenAmount.minus(cancelTakerTokenAmount), + ); const newBalances = await dmyBalances.getAsync(); const cancelMakerTokenAmount = cancelTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); const paidMakerFee = order.params.makerFee - .times(cancelMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .times(cancelMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); const paidTakerFee = order.params.takerFee - .times(cancelMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]) - .to.be.bignumber.equal(balances[maker][order.params.makerToken].minus(cancelMakerTokenAmount)); - expect(newBalances[maker][order.params.takerToken]) - .to.be.bignumber.equal(balances[maker][order.params.takerToken].add(cancelTakerTokenAmount)); - expect(newBalances[maker][zrx.address]) - .to.be.bignumber.equal(balances[maker][zrx.address].minus(paidMakerFee)); - expect(newBalances[taker][order.params.takerToken]) - .to.be.bignumber.equal(balances[taker][order.params.takerToken].minus(cancelTakerTokenAmount)); - expect(newBalances[taker][order.params.makerToken]) - .to.be.bignumber.equal(balances[taker][order.params.makerToken].add(cancelMakerTokenAmount)); - expect(newBalances[taker][zrx.address]).to.be.bignumber - .equal(balances[taker][zrx.address].minus(paidTakerFee)); - expect(newBalances[feeRecipient][zrx.address]) - .to.be.bignumber.equal(balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee))); + .times(cancelMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(cancelMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(cancelTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(cancelTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(cancelMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); }); it('should log 1 event with correct arguments', async () => { const divisor = 2; - const res = await exWrapper.cancelOrderAsync(order, maker, - {cancelTakerTokenAmount: order.params.takerTokenAmount.div(divisor)}); + const res = await exWrapper.cancelOrderAsync(order, maker, { + cancelTakerTokenAmount: order.params.takerTokenAmount.div(divisor), + }); expect(res.logs).to.have.length(1); const logArgs = res.logs[0].args; @@ -747,7 +834,6 @@ contract('Exchange', (accounts: string[]) => { const res = await exWrapper.cancelOrderAsync(order, maker); expect(res.logs).to.have.length(1); - const errId = res.logs[0].args.errorId.toNumber(); const errCode = res.logs[0].args.errorId.toNumber(); expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); }); |