diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-07 20:41:30 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-07 20:41:30 +0800 |
commit | 568e4d33f29db2f5c0195585438e18ab4b2aea18 (patch) | |
tree | 4f2372dc040f4354d51cfb2229391cfd7c033264 /packages | |
parent | fd004032cb23998184a78ac4a0a486ef1bd04c25 (diff) | |
download | dexon-sol-tools-568e4d33f29db2f5c0195585438e18ab4b2aea18.tar.gz dexon-sol-tools-568e4d33f29db2f5c0195585438e18ab4b2aea18.tar.zst dexon-sol-tools-568e4d33f29db2f5c0195585438e18ab4b2aea18.zip |
Use Order and SignedOrder type from 0x.js
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/test/exchange/core.ts | 301 | ||||
-rw-r--r-- | packages/contracts/test/exchange/helpers.ts | 19 | ||||
-rw-r--r-- | packages/contracts/test/exchange/wrapper.ts | 154 | ||||
-rw-r--r-- | packages/contracts/util/exchange_wrapper.ts | 30 | ||||
-rw-r--r-- | packages/contracts/util/formatters.ts | 100 | ||||
-rw-r--r-- | packages/contracts/util/order.ts | 47 | ||||
-rw-r--r-- | packages/contracts/util/order_factory.ts | 44 | ||||
-rw-r--r-- | packages/contracts/util/signed_order.ts | 92 | ||||
-rw-r--r-- | packages/contracts/util/signed_order_utils.ts | 49 | ||||
-rw-r--r-- | packages/contracts/util/types.ts | 35 |
10 files changed, 375 insertions, 496 deletions
diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 4c3c1dd93..9cfc68418 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -3,6 +3,7 @@ import { LogErrorContractEventArgs, LogFillContractEventArgs, LogWithDecodedArgs, + SignedOrder, TransactionReceiptWithDecodedLogs, ZeroEx, } from '0x.js'; @@ -20,9 +21,7 @@ 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 { SignedOrder } from '../../util/signed_order'; import { BalancesByOwner, ContractName, ExchangeContractErrs } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -85,14 +84,14 @@ describe('Exchange', () => { exchangeContractAddress: exchange.address, maker, feeRecipient, - makerToken: rep.address, - takerToken: dgd.address, + makerTokenAddress: rep.address, + takerTokenAddress: dgd.address, makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; - orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams); + orderFactory = new OrderFactory(zeroEx, defaultOrderParams); dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); await Promise.all([ rep.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { @@ -158,7 +157,7 @@ describe('Exchange', () => { }); const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); @@ -168,7 +167,7 @@ describe('Exchange', () => { }); const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1); @@ -178,7 +177,7 @@ describe('Exchange', () => { }); const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1); }); @@ -190,43 +189,43 @@ describe('Exchange', () => { }); const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount }); const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(signedOrder.params.makerTokenAmount) - .dividedToIntegerBy(signedOrder.params.takerTokenAmount); - const paidMakerFee = signedOrder.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const paidMakerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - const paidTakerFee = signedOrder.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const paidTakerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount), + .dividedToIntegerBy(signedOrder.makerTokenAmount); + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].add(fillTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( balances[maker][zrx.address].minus(paidMakerFee), ); - expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount), + expect(newBalances[taker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(paidTakerFee), @@ -243,43 +242,43 @@ describe('Exchange', () => { }); const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount }); const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(signedOrder.params.makerTokenAmount) - .dividedToIntegerBy(signedOrder.params.takerTokenAmount); - const paidMakerFee = signedOrder.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const paidMakerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - const paidTakerFee = signedOrder.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const paidTakerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount), + .dividedToIntegerBy(signedOrder.makerTokenAmount); + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].add(fillTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( balances[maker][zrx.address].minus(paidMakerFee), ); - expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount), + expect(newBalances[taker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(paidTakerFee), @@ -296,43 +295,43 @@ describe('Exchange', () => { }); const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount }); const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(signedOrder.params.makerTokenAmount) - .dividedToIntegerBy(signedOrder.params.takerTokenAmount); - const paidMakerFee = signedOrder.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const paidMakerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - const paidTakerFee = signedOrder.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const paidTakerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount), + .dividedToIntegerBy(signedOrder.makerTokenAmount); + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].add(fillTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( balances[maker][zrx.address].minus(paidMakerFee), ); - expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount), + expect(newBalances[taker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(paidTakerFee), @@ -350,15 +349,15 @@ describe('Exchange', () => { }); const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount }); const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - signedOrder.getOrderHashHex(), + ZeroEx.getOrderHashHex(signedOrder), ); const expectedFillAmountTAfter = fillTakerTokenAmount.add(filledTakerTokenAmountBefore); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(expectedFillAmountTAfter); @@ -366,28 +365,28 @@ describe('Exchange', () => { const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(signedOrder.params.makerTokenAmount) - .dividedToIntegerBy(signedOrder.params.takerTokenAmount); - const paidMakerFee = signedOrder.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const paidMakerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - const paidTakerFee = signedOrder.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const paidTakerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount), + .dividedToIntegerBy(signedOrder.makerTokenAmount); + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].add(fillTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( balances[maker][zrx.address].minus(paidMakerFee), ); - expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount), + expect(newBalances[taker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(paidTakerFee), @@ -398,67 +397,67 @@ describe('Exchange', () => { }); it('should fill remaining value if fillTakerTokenAmount > remaining takerTokenAmount', async () => { - const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount }); const res = await exWrapper.fillOrderAsync(signedOrder, taker, { - fillTakerTokenAmount: signedOrder.params.takerTokenAmount, + fillTakerTokenAmount: signedOrder.takerTokenAmount, }); const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>; expect(log.args.filledTakerTokenAmount).to.be.bignumber.equal( - signedOrder.params.takerTokenAmount.minus(fillTakerTokenAmount), + signedOrder.takerTokenAmount.minus(fillTakerTokenAmount), ); const newBalances = await dmyBalances.getAsync(); - expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.makerToken].minus(signedOrder.params.makerTokenAmount), + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(signedOrder.makerTokenAmount), ); - expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.takerToken].add(signedOrder.params.takerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].add(signedOrder.takerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(signedOrder.params.makerFee), + balances[maker][zrx.address].minus(signedOrder.makerFee), ); - expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.takerToken].minus(signedOrder.params.takerTokenAmount), + expect(newBalances[taker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(signedOrder.takerTokenAmount), ); - expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.makerToken].add(signedOrder.params.makerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].add(signedOrder.makerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(signedOrder.params.takerFee), + balances[taker][zrx.address].minus(signedOrder.takerFee), ); expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(signedOrder.params.makerFee.add(signedOrder.params.takerFee)), + balances[feeRecipient][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), ); }); it('should log 1 event with the correct arguments when order has a feeRecipient', async () => { const divisor = 2; const res = await exWrapper.fillOrderAsync(signedOrder, taker, { - fillTakerTokenAmount: signedOrder.params.takerTokenAmount.div(divisor), + fillTakerTokenAmount: signedOrder.takerTokenAmount.div(divisor), }); expect(res.logs).to.have.length(1); const logArgs = (res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>).args; - const expectedFilledMakerTokenAmount = signedOrder.params.makerTokenAmount.div(divisor); - const expectedFilledTakerTokenAmount = signedOrder.params.takerTokenAmount.div(divisor); - const expectedFeeMPaid = signedOrder.params.makerFee.div(divisor); - const expectedFeeTPaid = signedOrder.params.takerFee.div(divisor); - const tokensHashBuff = crypto.solSHA3([signedOrder.params.makerToken, signedOrder.params.takerToken]); + const expectedFilledMakerTokenAmount = signedOrder.makerTokenAmount.div(divisor); + const expectedFilledTakerTokenAmount = signedOrder.takerTokenAmount.div(divisor); + const expectedFeeMPaid = signedOrder.makerFee.div(divisor); + const expectedFeeTPaid = signedOrder.takerFee.div(divisor); + const tokensHashBuff = crypto.solSHA3([signedOrder.makerTokenAddress, signedOrder.takerTokenAddress]); const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); - expect(signedOrder.params.maker).to.be.equal(logArgs.maker); + expect(signedOrder.maker).to.be.equal(logArgs.maker); expect(taker).to.be.equal(logArgs.taker); - expect(signedOrder.params.feeRecipient).to.be.equal(logArgs.feeRecipient); - expect(signedOrder.params.makerToken).to.be.equal(logArgs.makerToken); - expect(signedOrder.params.takerToken).to.be.equal(logArgs.takerToken); + expect(signedOrder.feeRecipient).to.be.equal(logArgs.feeRecipient); + expect(signedOrder.makerTokenAddress).to.be.equal(logArgs.makerToken); + expect(signedOrder.takerTokenAddress).to.be.equal(logArgs.takerToken); expect(expectedFilledMakerTokenAmount).to.be.bignumber.equal(logArgs.filledMakerTokenAmount); expect(expectedFilledTakerTokenAmount).to.be.bignumber.equal(logArgs.filledTakerTokenAmount); expect(expectedFeeMPaid).to.be.bignumber.equal(logArgs.paidMakerFee); expect(expectedFeeTPaid).to.be.bignumber.equal(logArgs.paidTakerFee); expect(expectedTokens).to.be.equal(logArgs.tokens); - expect(signedOrder.getOrderHashHex()).to.be.equal(logArgs.orderHash); + expect(ZeroEx.getOrderHashHex(signedOrder)).to.be.equal(logArgs.orderHash); }); it('should log 1 event with the correct arguments when order has no feeRecipient', async () => { @@ -467,29 +466,29 @@ describe('Exchange', () => { }); const divisor = 2; const res = await exWrapper.fillOrderAsync(signedOrder, taker, { - fillTakerTokenAmount: signedOrder.params.takerTokenAmount.div(divisor), + fillTakerTokenAmount: signedOrder.takerTokenAmount.div(divisor), }); expect(res.logs).to.have.length(1); const logArgs = (res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>).args; - const expectedFilledMakerTokenAmount = signedOrder.params.makerTokenAmount.div(divisor); - const expectedFilledTakerTokenAmount = signedOrder.params.takerTokenAmount.div(divisor); + const expectedFilledMakerTokenAmount = signedOrder.makerTokenAmount.div(divisor); + const expectedFilledTakerTokenAmount = signedOrder.takerTokenAmount.div(divisor); const expectedFeeMPaid = new BigNumber(0); const expectedFeeTPaid = new BigNumber(0); - const tokensHashBuff = crypto.solSHA3([signedOrder.params.makerToken, signedOrder.params.takerToken]); + const tokensHashBuff = crypto.solSHA3([signedOrder.makerTokenAddress, signedOrder.takerTokenAddress]); const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); - expect(signedOrder.params.maker).to.be.equal(logArgs.maker); + expect(signedOrder.maker).to.be.equal(logArgs.maker); expect(taker).to.be.equal(logArgs.taker); - expect(signedOrder.params.feeRecipient).to.be.equal(logArgs.feeRecipient); - expect(signedOrder.params.makerToken).to.be.equal(logArgs.makerToken); - expect(signedOrder.params.takerToken).to.be.equal(logArgs.takerToken); + expect(signedOrder.feeRecipient).to.be.equal(logArgs.feeRecipient); + expect(signedOrder.makerTokenAddress).to.be.equal(logArgs.makerToken); + expect(signedOrder.takerTokenAddress).to.be.equal(logArgs.takerToken); expect(expectedFilledMakerTokenAmount).to.be.bignumber.equal(logArgs.filledMakerTokenAmount); expect(expectedFilledTakerTokenAmount).to.be.bignumber.equal(logArgs.filledTakerTokenAmount); expect(expectedFeeMPaid).to.be.bignumber.equal(logArgs.paidMakerFee); expect(expectedFeeTPaid).to.be.bignumber.equal(logArgs.paidTakerFee); expect(expectedTokens).to.be.equal(logArgs.tokens); - expect(signedOrder.getOrderHashHex()).to.be.equal(logArgs.orderHash); + expect(ZeroEx.getOrderHashHex(signedOrder)).to.be.equal(logArgs.orderHash); }); it('should throw when taker is specified and order is claimed by other', async () => { @@ -507,8 +506,8 @@ describe('Exchange', () => { makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(10), 18), }); - signedOrder.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); - signedOrder.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); + signedOrder.ecSignature.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); + signedOrder.ecSignature.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT); }); @@ -636,11 +635,11 @@ describe('Exchange', () => { }); }); - it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker balance, \ + it('should not change balances if makerTokenAddress is ZRX, makerTokenAmount + makerFee > maker balance, \ and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const makerZRXBalance = new BigNumber(balances[maker][zrx.address]); signedOrder = await orderFactory.newSignedOrderAsync({ - makerToken: zrx.address, + makerTokenAddress: zrx.address, makerTokenAmount: makerZRXBalance, makerFee: new BigNumber(1), }); @@ -649,11 +648,11 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker allowance, \ + it('should not change balances if makerTokenAddress is ZRX, makerTokenAmount + makerFee > maker allowance, \ and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const makerZRXAllowance = await zrx.allowance(maker, tokenTransferProxy.address); signedOrder = await orderFactory.newSignedOrderAsync({ - makerToken: zrx.address, + makerTokenAddress: zrx.address, makerTokenAmount: new BigNumber(makerZRXAllowance), makerFee: new BigNumber(1), }); @@ -662,11 +661,11 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker balance, \ + it('should not change balances if takerTokenAddress is ZRX, takerTokenAmount + takerFee > taker balance, \ and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const takerZRXBalance = new BigNumber(balances[taker][zrx.address]); signedOrder = await orderFactory.newSignedOrderAsync({ - takerToken: zrx.address, + takerTokenAddress: zrx.address, takerTokenAmount: takerZRXBalance, takerFee: new BigNumber(1), }); @@ -675,11 +674,11 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker allowance, \ + it('should not change balances if takerTokenAddress is ZRX, takerTokenAmount + takerFee > taker allowance, \ and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const takerZRXAllowance = await zrx.allowance(taker, tokenTransferProxy.address); signedOrder = await orderFactory.newSignedOrderAsync({ - takerToken: zrx.address, + takerTokenAddress: zrx.address, takerTokenAmount: new BigNumber(takerZRXAllowance), takerFee: new BigNumber(1), }); @@ -696,7 +695,7 @@ describe('Exchange', () => { }); signedOrder = await orderFactory.newSignedOrderAsync({ - takerToken: maliciousToken.address, + takerTokenAddress: maliciousToken.address, }); return expect( @@ -708,7 +707,7 @@ describe('Exchange', () => { it('should not change balances if an order is expired', async () => { signedOrder = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + expirationUnixTimestampSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); await exWrapper.fillOrderAsync(signedOrder, taker); @@ -718,7 +717,7 @@ describe('Exchange', () => { it('should log an error event if an order is expired', async () => { signedOrder = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + expirationUnixTimestampSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); const res = await exWrapper.fillOrderAsync(signedOrder, taker); @@ -779,7 +778,7 @@ describe('Exchange', () => { it('should be able to cancel a full order', async () => { await exWrapper.cancelOrderAsync(signedOrder, maker); await exWrapper.fillOrderAsync(signedOrder, taker, { - fillTakerTokenAmount: signedOrder.params.takerTokenAmount.div(2), + fillTakerTokenAmount: signedOrder.takerTokenAmount.div(2), }); const newBalances = await dmyBalances.getAsync(); @@ -787,43 +786,43 @@ describe('Exchange', () => { }); it('should be able to cancel part of an order', async () => { - const cancelTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2); + const cancelTakerTokenAmount = signedOrder.takerTokenAmount.div(2); await exWrapper.cancelOrderAsync(signedOrder, maker, { cancelTakerTokenAmount, }); const res = await exWrapper.fillOrderAsync(signedOrder, taker, { - fillTakerTokenAmount: signedOrder.params.takerTokenAmount, + fillTakerTokenAmount: signedOrder.takerTokenAmount, }); const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>; expect(log.args.filledTakerTokenAmount).to.be.bignumber.equal( - signedOrder.params.takerTokenAmount.minus(cancelTakerTokenAmount), + signedOrder.takerTokenAmount.minus(cancelTakerTokenAmount), ); const newBalances = await dmyBalances.getAsync(); const cancelMakerTokenAmount = cancelTakerTokenAmount - .times(signedOrder.params.makerTokenAmount) - .dividedToIntegerBy(signedOrder.params.takerTokenAmount); - const paidMakerFee = signedOrder.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const paidMakerFee = signedOrder.makerFee .times(cancelMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - const paidTakerFee = signedOrder.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const paidTakerFee = signedOrder.takerFee .times(cancelMakerTokenAmount) - .dividedToIntegerBy(signedOrder.params.makerTokenAmount); - expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.makerToken].minus(cancelMakerTokenAmount), + .dividedToIntegerBy(signedOrder.makerTokenAmount); + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(cancelMakerTokenAmount), ); - expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[maker][signedOrder.params.takerToken].add(cancelTakerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].add(cancelTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( balances[maker][zrx.address].minus(paidMakerFee), ); - expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.takerToken].minus(cancelTakerTokenAmount), + expect(newBalances[taker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(cancelTakerTokenAmount), ); - expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal( - balances[taker][signedOrder.params.makerToken].add(cancelMakerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].add(cancelMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(paidTakerFee), @@ -836,25 +835,25 @@ describe('Exchange', () => { it('should log 1 event with correct arguments', async () => { const divisor = 2; const res = await exWrapper.cancelOrderAsync(signedOrder, maker, { - cancelTakerTokenAmount: signedOrder.params.takerTokenAmount.div(divisor), + cancelTakerTokenAmount: signedOrder.takerTokenAmount.div(divisor), }); expect(res.logs).to.have.length(1); const log = res.logs[0] as LogWithDecodedArgs<LogCancelContractEventArgs>; const logArgs = log.args; - const expectedCancelledMakerTokenAmount = signedOrder.params.makerTokenAmount.div(divisor); - const expectedCancelledTakerTokenAmount = signedOrder.params.takerTokenAmount.div(divisor); - const tokensHashBuff = crypto.solSHA3([signedOrder.params.makerToken, signedOrder.params.takerToken]); + const expectedCancelledMakerTokenAmount = signedOrder.makerTokenAmount.div(divisor); + const expectedCancelledTakerTokenAmount = signedOrder.takerTokenAmount.div(divisor); + const tokensHashBuff = crypto.solSHA3([signedOrder.makerTokenAddress, signedOrder.takerTokenAddress]); const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); - expect(signedOrder.params.maker).to.be.equal(logArgs.maker); - expect(signedOrder.params.feeRecipient).to.be.equal(logArgs.feeRecipient); - expect(signedOrder.params.makerToken).to.be.equal(logArgs.makerToken); - expect(signedOrder.params.takerToken).to.be.equal(logArgs.takerToken); + expect(signedOrder.maker).to.be.equal(logArgs.maker); + expect(signedOrder.feeRecipient).to.be.equal(logArgs.feeRecipient); + expect(signedOrder.makerTokenAddress).to.be.equal(logArgs.makerToken); + expect(signedOrder.takerTokenAddress).to.be.equal(logArgs.takerToken); expect(expectedCancelledMakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledMakerTokenAmount); expect(expectedCancelledTakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledTakerTokenAmount); expect(expectedTokens).to.be.equal(logArgs.tokens); - expect(signedOrder.getOrderHashHex()).to.be.equal(logArgs.orderHash); + expect(ZeroEx.getOrderHashHex(signedOrder)).to.be.equal(logArgs.orderHash); }); it('should not log events if no value is cancelled', async () => { @@ -869,7 +868,7 @@ describe('Exchange', () => { it('should not log events if order is expired', async () => { signedOrder = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + expirationUnixTimestampSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); const res = await exWrapper.cancelOrderAsync(signedOrder, maker); diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index 263a74911..5fe00225e 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -1,4 +1,4 @@ -import { ZeroEx } from '0x.js'; +import { SignedOrder, ZeroEx } from '0x.js'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -8,9 +8,7 @@ import ethUtil = require('ethereumjs-util'); import { ExchangeContract } from '../../src/contract_wrappers/generated/exchange'; import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; -import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; -import { SignedOrder } from '../../util/signed_order'; import { ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -59,7 +57,7 @@ describe('Exchange', () => { makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; - orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams); + orderFactory = new OrderFactory(zeroEx, defaultOrderParams); signedOrder = await orderFactory.newSignedOrderAsync(); }); @@ -72,7 +70,7 @@ describe('Exchange', () => { describe('getOrderHash', () => { it('should output the correct orderHash', async () => { const orderHashHex = await exchangeWrapper.getOrderHashAsync(signedOrder); - expect(signedOrder.getOrderHashHex()).to.be.equal(orderHashHex); + expect(ZeroEx.getOrderHashHex(signedOrder)).to.be.equal(orderHashHex); }); }); @@ -83,16 +81,19 @@ describe('Exchange', () => { it('should return true with a valid signature', async () => { const success = await exchangeWrapper.isValidSignatureAsync(signedOrder); - const isValidSignature = signedOrder.isValidSignature(); + const orderHashHex = ZeroEx.getOrderHashHex(signedOrder); + const isValidSignature = ZeroEx.isValidSignature(orderHashHex, signedOrder.ecSignature, signedOrder.maker); expect(isValidSignature).to.be.true(); expect(success).to.be.true(); }); it('should return false with an invalid signature', async () => { - signedOrder.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); - signedOrder.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); + signedOrder.ecSignature.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); + signedOrder.ecSignature.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); const success = await exchangeWrapper.isValidSignatureAsync(signedOrder); - expect(signedOrder.isValidSignature()).to.be.false(); + const orderHashHex = ZeroEx.getOrderHashHex(signedOrder); + const isValidSignature = ZeroEx.isValidSignature(orderHashHex, signedOrder.ecSignature, signedOrder.maker); + expect(isValidSignature).to.be.false(); expect(success).to.be.false(); }); }); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 02d722ed5..b4ac12730 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -1,4 +1,4 @@ -import { ZeroEx } from '0x.js'; +import { SignedOrder, ZeroEx } from '0x.js'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -13,9 +13,7 @@ import { TokenTransferProxyContract } from '../../src/contract_wrappers/generate import { Balances } from '../../util/balances'; import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; -import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; -import { SignedOrder } from '../../util/signed_order'; import { BalancesByOwner, ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -77,15 +75,15 @@ describe('Exchange', () => { exchangeContractAddress: exchange.address, maker, feeRecipient, - makerToken: rep.address, - takerToken: dgd.address, + makerTokenAddress: rep.address, + takerTokenAddress: dgd.address, makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; - orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams); + orderFactory = new OrderFactory(zeroEx, defaultOrderParams); dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); await Promise.all([ rep.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: maker }), @@ -118,7 +116,7 @@ describe('Exchange', () => { makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = order.takerTokenAmount.div(2); await exWrapper.fillOrKillOrderAsync(order, taker, { fillTakerTokenAmount, }); @@ -126,26 +124,22 @@ describe('Exchange', () => { const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = 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), + .times(order.makerTokenAmount) + .dividedToIntegerBy(order.takerTokenAmount); + const makerFee = order.makerFee.times(fillMakerTokenAmount).dividedToIntegerBy(order.makerTokenAmount); + const takerFee = order.takerFee.times(fillMakerTokenAmount).dividedToIntegerBy(order.makerTokenAmount); + expect(newBalances[maker][order.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][order.makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][order.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][order.takerTokenAddress].add(fillTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal(balances[maker][zrx.address].minus(makerFee)); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + expect(newBalances[taker][order.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][order.takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][order.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][order.makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(balances[taker][zrx.address].minus(takerFee)); expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( @@ -155,7 +149,7 @@ describe('Exchange', () => { it('should throw if an order is expired', async () => { const order = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + expirationUnixTimestampSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); @@ -166,7 +160,7 @@ describe('Exchange', () => { const from = taker; await exWrapper.fillOrderAsync(order, from, { - fillTakerTokenAmount: order.params.takerTokenAmount.div(2), + fillTakerTokenAmount: order.takerTokenAmount.div(2), }); return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); @@ -187,25 +181,25 @@ describe('Exchange', () => { describe('batchFillOrders', () => { it('should transfer the correct amounts', async () => { const fillTakerTokenAmounts: BigNumber[] = []; - const makerToken = rep.address; - const takerToken = dgd.address; + const makerTokenAddress = rep.address; + const takerTokenAddress = dgd.address; orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = order.takerTokenAmount.div(2); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee + .times(order.makerTokenAmount) + .dividedToIntegerBy(order.takerTokenAmount); + const makerFee = order.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee + .dividedToIntegerBy(order.makerTokenAmount); + const takerFee = order.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .dividedToIntegerBy(order.makerTokenAmount); fillTakerTokenAmounts.push(fillTakerTokenAmount); - balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); - balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); + balances[maker][makerTokenAddress] = balances[maker][makerTokenAddress].minus(fillMakerTokenAmount); + balances[maker][takerTokenAddress] = balances[maker][takerTokenAddress].add(fillTakerTokenAmount); balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee); - balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount); - balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount); + balances[taker][makerTokenAddress] = balances[taker][makerTokenAddress].add(fillMakerTokenAmount); + balances[taker][takerTokenAddress] = balances[taker][takerTokenAddress].minus(fillTakerTokenAmount); balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee); balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( makerFee.add(takerFee), @@ -224,25 +218,25 @@ describe('Exchange', () => { describe('batchFillOrKillOrders', () => { it('should transfer the correct amounts', async () => { const fillTakerTokenAmounts: BigNumber[] = []; - const makerToken = rep.address; - const takerToken = dgd.address; + const makerTokenAddress = rep.address; + const takerTokenAddress = dgd.address; orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = order.takerTokenAmount.div(2); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee + .times(order.makerTokenAmount) + .dividedToIntegerBy(order.takerTokenAmount); + const makerFee = order.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee + .dividedToIntegerBy(order.makerTokenAmount); + const takerFee = order.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .dividedToIntegerBy(order.makerTokenAmount); fillTakerTokenAmounts.push(fillTakerTokenAmount); - balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); - balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); + balances[maker][makerTokenAddress] = balances[maker][makerTokenAddress].minus(fillMakerTokenAmount); + balances[maker][takerTokenAddress] = balances[maker][takerTokenAddress].add(fillTakerTokenAmount); balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee); - balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount); - balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount); + balances[taker][makerTokenAddress] = balances[taker][makerTokenAddress].add(fillMakerTokenAmount); + balances[taker][takerTokenAddress] = balances[taker][takerTokenAddress].minus(fillTakerTokenAmount); balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee); balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( makerFee.add(takerFee), @@ -260,7 +254,7 @@ describe('Exchange', () => { it('should throw if a single order does not fill the expected amount', async () => { const fillTakerTokenAmounts: BigNumber[] = []; orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + const fillTakerTokenAmount = order.takerTokenAmount.div(2); fillTakerTokenAmounts.push(fillTakerTokenAmount); }); @@ -276,34 +270,32 @@ describe('Exchange', () => { describe('fillOrdersUpTo', () => { it('should stop when the entire fillTakerTokenAmount is filled', async () => { - const fillTakerTokenAmount = orders[0].params.takerTokenAmount.plus( - orders[1].params.takerTokenAmount.div(2), - ); + const fillTakerTokenAmount = orders[0].takerTokenAmount.plus(orders[1].takerTokenAmount.div(2)); await exWrapper.fillOrdersUpToAsync(orders, taker, { fillTakerTokenAmount, }); const newBalances = await dmyBalances.getAsync(); - const fillMakerTokenAmount = orders[0].params.makerTokenAmount.add( - orders[1].params.makerTokenAmount.dividedToIntegerBy(2), + const fillMakerTokenAmount = orders[0].makerTokenAmount.add( + orders[1].makerTokenAmount.dividedToIntegerBy(2), ); - const makerFee = orders[0].params.makerFee.add(orders[1].params.makerFee.dividedToIntegerBy(2)); - const takerFee = orders[0].params.takerFee.add(orders[1].params.takerFee.dividedToIntegerBy(2)); - expect(newBalances[maker][orders[0].params.makerToken]).to.be.bignumber.equal( - balances[maker][orders[0].params.makerToken].minus(fillMakerTokenAmount), + const makerFee = orders[0].makerFee.add(orders[1].makerFee.dividedToIntegerBy(2)); + const takerFee = orders[0].takerFee.add(orders[1].takerFee.dividedToIntegerBy(2)); + expect(newBalances[maker][orders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[maker][orders[0].makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][orders[0].params.takerToken]).to.be.bignumber.equal( - balances[maker][orders[0].params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][orders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[maker][orders[0].takerTokenAddress].add(fillTakerTokenAmount), ); expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( balances[maker][zrx.address].minus(makerFee), ); - expect(newBalances[taker][orders[0].params.takerToken]).to.be.bignumber.equal( - balances[taker][orders[0].params.takerToken].minus(fillTakerTokenAmount), + expect(newBalances[taker][orders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[taker][orders[0].takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][orders[0].params.makerToken]).to.be.bignumber.equal( - balances[taker][orders[0].params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][orders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[taker][orders[0].makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(takerFee), @@ -316,22 +308,22 @@ describe('Exchange', () => { it('should fill all orders if cannot fill entire fillTakerTokenAmount', async () => { const fillTakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); orders.forEach(order => { - balances[maker][order.params.makerToken] = balances[maker][order.params.makerToken].minus( - order.params.makerTokenAmount, + balances[maker][order.makerTokenAddress] = balances[maker][order.makerTokenAddress].minus( + order.makerTokenAmount, ); - balances[maker][order.params.takerToken] = balances[maker][order.params.takerToken].add( - order.params.takerTokenAmount, + balances[maker][order.takerTokenAddress] = balances[maker][order.takerTokenAddress].add( + order.takerTokenAmount, ); - balances[maker][zrx.address] = balances[maker][zrx.address].minus(order.params.makerFee); - balances[taker][order.params.makerToken] = balances[taker][order.params.makerToken].add( - order.params.makerTokenAmount, + balances[maker][zrx.address] = balances[maker][zrx.address].minus(order.makerFee); + balances[taker][order.makerTokenAddress] = balances[taker][order.makerTokenAddress].add( + order.makerTokenAmount, ); - balances[taker][order.params.takerToken] = balances[taker][order.params.takerToken].minus( - order.params.takerTokenAmount, + balances[taker][order.takerTokenAddress] = balances[taker][order.takerTokenAddress].minus( + order.takerTokenAmount, ); - balances[taker][zrx.address] = balances[taker][zrx.address].minus(order.params.takerFee); + balances[taker][zrx.address] = balances[taker][zrx.address].minus(order.takerFee); balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( - order.params.makerFee.add(order.params.takerFee), + order.makerFee.add(order.takerFee), ); }); await exWrapper.fillOrdersUpToAsync(orders, taker, { @@ -342,10 +334,10 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should throw when an order does not use the same takerToken', async () => { + it('should throw when an order does not use the same takerTokenAddress', async () => { orders = await Promise.all([ orderFactory.newSignedOrderAsync(), - orderFactory.newSignedOrderAsync({ takerToken: zrx.address }), + orderFactory.newSignedOrderAsync({ takerTokenAddress: zrx.address }), orderFactory.newSignedOrderAsync(), ]); @@ -359,7 +351,7 @@ describe('Exchange', () => { describe('batchCancelOrders', () => { it('should be able to cancel multiple orders', async () => { - const cancelTakerTokenAmounts = _.map(orders, order => order.params.takerTokenAmount); + const cancelTakerTokenAmounts = _.map(orders, order => order.takerTokenAmount); await exWrapper.batchCancelOrdersAsync(orders, maker, { cancelTakerTokenAmounts, }); diff --git a/packages/contracts/util/exchange_wrapper.ts b/packages/contracts/util/exchange_wrapper.ts index ff263246f..03d04629d 100644 --- a/packages/contracts/util/exchange_wrapper.ts +++ b/packages/contracts/util/exchange_wrapper.ts @@ -1,4 +1,4 @@ -import { TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; +import { SignedOrder, TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -6,7 +6,7 @@ import * as Web3 from 'web3'; import { ExchangeContract } from '../src/contract_wrappers/generated/exchange'; import { formatters } from './formatters'; -import { SignedOrder } from './signed_order'; +import { signedOrderUtils } from './signed_order_utils'; export class ExchangeWrapper { private _exchange: ExchangeContract; @@ -24,7 +24,11 @@ export class ExchangeWrapper { } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance; - const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); + const params = signedOrderUtils.createFill( + signedOrder, + shouldThrowOnInsufficientBalanceOrAllowance, + opts.fillTakerTokenAmount, + ); const txHash = await this._exchange.fillOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, @@ -45,7 +49,7 @@ export class ExchangeWrapper { from: string, opts: { cancelTakerTokenAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = signedOrder.createCancel(opts.cancelTakerTokenAmount); + const params = signedOrderUtils.createCancel(signedOrder, opts.cancelTakerTokenAmount); const txHash = await this._exchange.cancelOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, @@ -63,7 +67,11 @@ export class ExchangeWrapper { opts: { fillTakerTokenAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = true; - const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); + const params = signedOrderUtils.createFill( + signedOrder, + shouldThrowOnInsufficientBalanceOrAllowance, + opts.fillTakerTokenAmount, + ); const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, @@ -177,17 +185,17 @@ export class ExchangeWrapper { } public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> { const shouldThrowOnInsufficientBalanceOrAllowance = false; - const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance); + const params = signedOrderUtils.getOrderAddressesAndValues(signedOrder); const orderHash = await this._exchange.getOrderHash(params.orderAddresses, params.orderValues); return orderHash; } public async isValidSignatureAsync(signedOrder: SignedOrder): Promise<boolean> { const isValidSignature = await this._exchange.isValidSignature( - signedOrder.params.maker, - signedOrder.getOrderHashHex(), - signedOrder.params.v, - signedOrder.params.r, - signedOrder.params.s, + signedOrder.maker, + ZeroEx.getOrderHashHex(signedOrder), + signedOrder.ecSignature.v, + signedOrder.ecSignature.r, + signedOrder.ecSignature.s, ); return isValidSignature; } diff --git a/packages/contracts/util/formatters.ts b/packages/contracts/util/formatters.ts index 6c225268e..8e0dfb09e 100644 --- a/packages/contracts/util/formatters.ts +++ b/packages/contracts/util/formatters.ts @@ -1,12 +1,12 @@ +import { SignedOrder } from '0x.js'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; -import { SignedOrder } from './signed_order'; import { BatchCancelOrders, BatchFillOrders, FillOrdersUpTo } from './types'; export const formatters = { createBatchFill( - orders: SignedOrder[], + signedOrders: SignedOrder[], shouldThrowOnInsufficientBalanceOrAllowance: boolean, fillTakerTokenAmounts: BigNumber[] = [], ) { @@ -19,33 +19,33 @@ export const formatters = { r: [], s: [], }; - _.forEach(orders, order => { + _.forEach(signedOrders, signedOrder => { batchFill.orderAddresses.push([ - order.params.maker, - order.params.taker, - order.params.makerToken, - order.params.takerToken, - order.params.feeRecipient, + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, ]); batchFill.orderValues.push([ - order.params.makerTokenAmount, - order.params.takerTokenAmount, - order.params.makerFee, - order.params.takerFee, - order.params.expirationTimestampInSec, - order.params.salt, + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, ]); - batchFill.v.push(order.params.v); - batchFill.r.push(order.params.r); - batchFill.s.push(order.params.s); - if (fillTakerTokenAmounts.length < orders.length) { - batchFill.fillTakerTokenAmounts.push(order.params.takerTokenAmount); + batchFill.v.push(signedOrder.ecSignature.v); + batchFill.r.push(signedOrder.ecSignature.r); + batchFill.s.push(signedOrder.ecSignature.s); + if (fillTakerTokenAmounts.length < signedOrders.length) { + batchFill.fillTakerTokenAmounts.push(signedOrder.takerTokenAmount); } }); return batchFill; }, createFillUpTo( - orders: SignedOrder[], + signedOrders: SignedOrder[], shouldThrowOnInsufficientBalanceOrAllowance: boolean, fillTakerTokenAmount: BigNumber, ) { @@ -58,52 +58,52 @@ export const formatters = { r: [], s: [], }; - orders.forEach(order => { + signedOrders.forEach(signedOrder => { fillUpTo.orderAddresses.push([ - order.params.maker, - order.params.taker, - order.params.makerToken, - order.params.takerToken, - order.params.feeRecipient, + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, ]); fillUpTo.orderValues.push([ - order.params.makerTokenAmount, - order.params.takerTokenAmount, - order.params.makerFee, - order.params.takerFee, - order.params.expirationTimestampInSec, - order.params.salt, + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, ]); - fillUpTo.v.push(order.params.v); - fillUpTo.r.push(order.params.r); - fillUpTo.s.push(order.params.s); + fillUpTo.v.push(signedOrder.ecSignature.v); + fillUpTo.r.push(signedOrder.ecSignature.r); + fillUpTo.s.push(signedOrder.ecSignature.s); }); return fillUpTo; }, - createBatchCancel(orders: SignedOrder[], cancelTakerTokenAmounts: BigNumber[] = []) { + createBatchCancel(signedOrders: SignedOrder[], cancelTakerTokenAmounts: BigNumber[] = []) { const batchCancel: BatchCancelOrders = { orderAddresses: [], orderValues: [], cancelTakerTokenAmounts, }; - orders.forEach(order => { + signedOrders.forEach(signedOrder => { batchCancel.orderAddresses.push([ - order.params.maker, - order.params.taker, - order.params.makerToken, - order.params.takerToken, - order.params.feeRecipient, + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, ]); batchCancel.orderValues.push([ - order.params.makerTokenAmount, - order.params.takerTokenAmount, - order.params.makerFee, - order.params.takerFee, - order.params.expirationTimestampInSec, - order.params.salt, + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, ]); - if (cancelTakerTokenAmounts.length < orders.length) { - batchCancel.cancelTakerTokenAmounts.push(order.params.takerTokenAmount); + if (cancelTakerTokenAmounts.length < signedOrders.length) { + batchCancel.cancelTakerTokenAmounts.push(signedOrder.takerTokenAmount); } }); return batchCancel; diff --git a/packages/contracts/util/order.ts b/packages/contracts/util/order.ts deleted file mode 100644 index 6d412e2af..000000000 --- a/packages/contracts/util/order.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import ethUtil = require('ethereumjs-util'); -import * as _ from 'lodash'; - -import { crypto } from './crypto'; -import { SignedOrder } from './signed_order'; -import { OrderParams } from './types'; - -export class Order { - public params: OrderParams; - private _web3Wrapper: Web3Wrapper; - constructor(web3Wrapper: Web3Wrapper, params: OrderParams) { - this.params = params; - this._web3Wrapper = web3Wrapper; - } - public async signAsync(): Promise<SignedOrder> { - const orderHash = this.getOrderHashHex(); - const signature = await this._web3Wrapper.signTransactionAsync(this.params.maker, orderHash); - const { v, r, s } = ethUtil.fromRpcSig(signature); - const signedOrderParams = _.assign(this.params, { - v, - r: ethUtil.bufferToHex(r), - s: ethUtil.bufferToHex(s), - }); - const signedOrder = new SignedOrder(this._web3Wrapper, signedOrderParams); - return signedOrder; - } - public getOrderHashHex(): string { - const orderHash = crypto.solSHA3([ - this.params.exchangeContractAddress, - this.params.maker, - this.params.taker, - this.params.makerToken, - this.params.takerToken, - this.params.feeRecipient, - this.params.makerTokenAmount, - this.params.takerTokenAmount, - this.params.makerFee, - this.params.takerFee, - this.params.expirationTimestampInSec, - this.params.salt, - ]); - const orderHashHex = ethUtil.bufferToHex(orderHash); - return orderHashHex; - } -} diff --git a/packages/contracts/util/order_factory.ts b/packages/contracts/util/order_factory.ts index 788f60a88..8ba5df24a 100644 --- a/packages/contracts/util/order_factory.ts +++ b/packages/contracts/util/order_factory.ts @@ -1,33 +1,37 @@ -import { ZeroEx } from '0x.js'; +import { Order, SignedOrder, ZeroEx } from '0x.js'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; -import { Order } from './order'; -import { SignedOrder } from './signed_order'; -import { DefaultOrderParams, OptionalOrderParams, OrderParams } from './types'; +import { DefaultOrderParams } from './types'; export class OrderFactory { - private _defaultOrderParams: DefaultOrderParams; - private _web3Wrapper: Web3Wrapper; - constructor(web3Wrapper: Web3Wrapper, defaultOrderParams: DefaultOrderParams) { + private _defaultOrderParams: Partial<Order>; + private _zeroEx: ZeroEx; + constructor(zeroEx: ZeroEx, defaultOrderParams: Partial<Order>) { this._defaultOrderParams = defaultOrderParams; - this._web3Wrapper = web3Wrapper; + this._zeroEx = zeroEx; } - public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<SignedOrder> { + public async newSignedOrderAsync(customOrderParams: Partial<Order> = {}): Promise<SignedOrder> { const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000)); - const orderParams: OrderParams = _.assign( - {}, - { - expirationTimestampInSec: randomExpiration, - salt: ZeroEx.generatePseudoRandomSalt(), - taker: ZeroEx.NULL_ADDRESS, - }, - this._defaultOrderParams, - customOrderParams, + const order = ({ + expirationUnixTimestampSec: randomExpiration, + salt: ZeroEx.generatePseudoRandomSalt(), + taker: ZeroEx.NULL_ADDRESS, + ...this._defaultOrderParams, + ...customOrderParams, + } as any) as Order; + const orderHashHex = ZeroEx.getOrderHashHex(order); + const shouldAddPersonalMessagePrefix = false; + const ecSignature = await this._zeroEx.signOrderHashAsync( + orderHashHex, + order.maker, + shouldAddPersonalMessagePrefix, ); - const order = new Order(this._web3Wrapper, orderParams); - const signedOrder = await order.signAsync(); + const signedOrder = { + ...order, + ecSignature, + }; return signedOrder; } } diff --git a/packages/contracts/util/signed_order.ts b/packages/contracts/util/signed_order.ts deleted file mode 100644 index a84e0686c..000000000 --- a/packages/contracts/util/signed_order.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import ethUtil = require('ethereumjs-util'); -import * as _ from 'lodash'; - -import { crypto } from './crypto'; -import { SignedOrderParams } from './types'; - -export class SignedOrder { - public params: SignedOrderParams; - private _web3Wrapper: Web3Wrapper; - constructor(web3Wrapper: Web3Wrapper, params: SignedOrderParams) { - this.params = params; - this._web3Wrapper = web3Wrapper; - } - public isValidSignature() { - const { v, r, s } = this.params; - const orderHash = this.getOrderHashHex(); - const msgHash = ethUtil.hashPersonalMessage(ethUtil.toBuffer(orderHash)); - try { - const pubKey = ethUtil.ecrecover(msgHash, v, ethUtil.toBuffer(r), ethUtil.toBuffer(s)); - const recoveredAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return recoveredAddress === this.params.maker; - } catch (err) { - return false; - } - } - public createFill(shouldThrowOnInsufficientBalanceOrAllowance?: boolean, fillTakerTokenAmount?: BigNumber) { - const fill = { - orderAddresses: [ - this.params.maker, - this.params.taker, - this.params.makerToken, - this.params.takerToken, - this.params.feeRecipient, - ], - orderValues: [ - this.params.makerTokenAmount, - this.params.takerTokenAmount, - this.params.makerFee, - this.params.takerFee, - this.params.expirationTimestampInSec, - this.params.salt, - ], - fillTakerTokenAmount: fillTakerTokenAmount || this.params.takerTokenAmount, - shouldThrowOnInsufficientBalanceOrAllowance: !!shouldThrowOnInsufficientBalanceOrAllowance, - v: this.params.v, - r: this.params.r, - s: this.params.s, - }; - return fill; - } - public createCancel(cancelTakerTokenAmount?: BigNumber) { - const cancel = { - orderAddresses: [ - this.params.maker, - this.params.taker, - this.params.makerToken, - this.params.takerToken, - this.params.feeRecipient, - ], - orderValues: [ - this.params.makerTokenAmount, - this.params.takerTokenAmount, - this.params.makerFee, - this.params.takerFee, - this.params.expirationTimestampInSec, - this.params.salt, - ], - cancelTakerTokenAmount: cancelTakerTokenAmount || this.params.takerTokenAmount, - }; - return cancel; - } - public getOrderHashHex(): string { - const orderHash = crypto.solSHA3([ - this.params.exchangeContractAddress, - this.params.maker, - this.params.taker, - this.params.makerToken, - this.params.takerToken, - this.params.feeRecipient, - this.params.makerTokenAmount, - this.params.takerTokenAmount, - this.params.makerFee, - this.params.takerFee, - this.params.expirationTimestampInSec, - this.params.salt, - ]); - const orderHashHex = ethUtil.bufferToHex(orderHash); - return orderHashHex; - } -} diff --git a/packages/contracts/util/signed_order_utils.ts b/packages/contracts/util/signed_order_utils.ts new file mode 100644 index 000000000..fc2f800cd --- /dev/null +++ b/packages/contracts/util/signed_order_utils.ts @@ -0,0 +1,49 @@ +import { SignedOrder } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import ethUtil = require('ethereumjs-util'); +import * as _ from 'lodash'; + +import { crypto } from './crypto'; + +export const signedOrderUtils = { + createFill: ( + signedOrder: SignedOrder, + shouldThrowOnInsufficientBalanceOrAllowance?: boolean, + fillTakerTokenAmount?: BigNumber, + ) => { + const fill = { + ...signedOrderUtils.getOrderAddressesAndValues(signedOrder), + fillTakerTokenAmount: fillTakerTokenAmount || signedOrder.takerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance: !!shouldThrowOnInsufficientBalanceOrAllowance, + ...signedOrder.ecSignature, + }; + return fill; + }, + createCancel(signedOrder: SignedOrder, cancelTakerTokenAmount?: BigNumber) { + const cancel = { + ...signedOrderUtils.getOrderAddressesAndValues(signedOrder), + cancelTakerTokenAmount: cancelTakerTokenAmount || signedOrder.takerTokenAmount, + }; + return cancel; + }, + getOrderAddressesAndValues(signedOrder: SignedOrder) { + return { + orderAddresses: [ + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, + ], + orderValues: [ + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, + ], + }; + }, +}; diff --git a/packages/contracts/util/types.ts b/packages/contracts/util/types.ts index 1bd8940ad..65bc26f79 100644 --- a/packages/contracts/util/types.ts +++ b/packages/contracts/util/types.ts @@ -49,41 +49,6 @@ export interface DefaultOrderParams { takerFee: BigNumber; } -export interface OptionalOrderParams { - exchangeContractAddress?: string; - maker?: string; - taker?: string; - feeRecipient?: string; - makerToken?: string; - takerToken?: string; - makerTokenAmount?: BigNumber; - takerTokenAmount?: BigNumber; - makerFee?: BigNumber; - takerFee?: BigNumber; - expirationTimestampInSec?: BigNumber; -} - -export interface OrderParams { - exchangeContractAddress: string; - maker: string; - taker: string; - feeRecipient: string; - makerToken: string; - takerToken: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFee: BigNumber; - takerFee: BigNumber; - expirationTimestampInSec: BigNumber; - salt: BigNumber; -} - -export interface SignedOrderParams extends OrderParams { - v: number; - r: string; - s: string; -} - export interface TransactionDataParams { name: string; abi: Web3.AbiDefinition[]; |