Introduce SignedOrder class and remove type assertions

This commit is contained in:
Leonid Logvinov
2018-02-07 12:22:22 +01:00
parent 4b6324050d
commit fd004032cb
9 changed files with 365 additions and 324 deletions

View File

@@ -22,6 +22,7 @@ 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';
@@ -46,7 +47,7 @@ describe('Exchange', () => {
let exchange: ExchangeContract;
let tokenTransferProxy: TokenTransferProxyContract;
let order: Order;
let signedOrder: SignedOrder;
let balances: BalancesByOwner;
let exWrapper: ExchangeWrapper;
let dmyBalances: Balances;
@@ -147,85 +148,85 @@ describe('Exchange', () => {
describe('fillOrder', () => {
beforeEach(async () => {
balances = await dmyBalances.getAsync();
order = await orderFactory.newSignedOrderAsync();
signedOrder = await orderFactory.newSignedOrderAsync();
});
it('should create an unfillable order', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: new BigNumber(1001),
takerTokenAmount: new BigNumber(3),
});
const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0);
const fillTakerTokenAmount1 = new BigNumber(2);
await exWrapper.fillOrderAsync(order, taker, {
await exWrapper.fillOrderAsync(signedOrder, taker, {
fillTakerTokenAmount: fillTakerTokenAmount1,
});
const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1);
const fillTakerTokenAmount2 = new BigNumber(1);
await exWrapper.fillOrderAsync(order, taker, {
await exWrapper.fillOrderAsync(signedOrder, taker, {
fillTakerTokenAmount: fillTakerTokenAmount2,
});
const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1);
});
it('should transfer the correct amounts when makerTokenAmount === takerTokenAmount', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
});
const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0);
const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount });
const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount });
const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount);
const newBalances = await dmyBalances.getAsync();
const fillMakerTokenAmount = fillTakerTokenAmount
.times(order.params.makerTokenAmount)
.dividedToIntegerBy(order.params.takerTokenAmount);
const paidMakerFee = order.params.makerFee
.times(signedOrder.params.makerTokenAmount)
.dividedToIntegerBy(signedOrder.params.takerTokenAmount);
const paidMakerFee = signedOrder.params.makerFee
.times(fillMakerTokenAmount)
.dividedToIntegerBy(order.params.makerTokenAmount);
const paidTakerFee = order.params.takerFee
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
const paidTakerFee = signedOrder.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),
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount),
);
expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
balances[maker][order.params.takerToken].add(fillTakerTokenAmount),
expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.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][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount),
);
expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
balances[taker][order.params.makerToken].add(fillMakerTokenAmount),
expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount),
);
expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
balances[taker][zrx.address].minus(paidTakerFee),
@@ -236,49 +237,49 @@ describe('Exchange', () => {
});
it('should transfer the correct amounts when makerTokenAmount > takerTokenAmount', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
});
const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0);
const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount });
const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount });
const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount);
const newBalances = await dmyBalances.getAsync();
const fillMakerTokenAmount = fillTakerTokenAmount
.times(order.params.makerTokenAmount)
.dividedToIntegerBy(order.params.takerTokenAmount);
const paidMakerFee = order.params.makerFee
.times(signedOrder.params.makerTokenAmount)
.dividedToIntegerBy(signedOrder.params.takerTokenAmount);
const paidMakerFee = signedOrder.params.makerFee
.times(fillMakerTokenAmount)
.dividedToIntegerBy(order.params.makerTokenAmount);
const paidTakerFee = order.params.takerFee
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
const paidTakerFee = signedOrder.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),
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount),
);
expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
balances[maker][order.params.takerToken].add(fillTakerTokenAmount),
expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.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][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount),
);
expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
balances[taker][order.params.makerToken].add(fillMakerTokenAmount),
expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount),
);
expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
balances[taker][zrx.address].minus(paidTakerFee),
@@ -289,49 +290,49 @@ describe('Exchange', () => {
});
it('should transfer the correct amounts when makerTokenAmount < takerTokenAmount', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
});
const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0);
const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount });
const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount });
const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount);
const newBalances = await dmyBalances.getAsync();
const fillMakerTokenAmount = fillTakerTokenAmount
.times(order.params.makerTokenAmount)
.dividedToIntegerBy(order.params.takerTokenAmount);
const paidMakerFee = order.params.makerFee
.times(signedOrder.params.makerTokenAmount)
.dividedToIntegerBy(signedOrder.params.takerTokenAmount);
const paidMakerFee = signedOrder.params.makerFee
.times(fillMakerTokenAmount)
.dividedToIntegerBy(order.params.makerTokenAmount);
const paidTakerFee = order.params.takerFee
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
const paidTakerFee = signedOrder.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),
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount),
);
expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
balances[maker][order.params.takerToken].add(fillTakerTokenAmount),
expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.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][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount),
);
expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
balances[taker][order.params.makerToken].add(fillMakerTokenAmount),
expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount),
);
expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
balances[taker][zrx.address].minus(paidTakerFee),
@@ -342,22 +343,22 @@ describe('Exchange', () => {
});
it('should transfer the correct amounts when taker is specified and order is claimed by taker', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
taker,
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
});
const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0);
const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount });
const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount });
const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(
order.getOrderHashHex(),
signedOrder.getOrderHashHex(),
);
const expectedFillAmountTAfter = fillTakerTokenAmount.add(filledTakerTokenAmountBefore);
expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(expectedFillAmountTAfter);
@@ -365,28 +366,28 @@ describe('Exchange', () => {
const newBalances = await dmyBalances.getAsync();
const fillMakerTokenAmount = fillTakerTokenAmount
.times(order.params.makerTokenAmount)
.dividedToIntegerBy(order.params.takerTokenAmount);
const paidMakerFee = order.params.makerFee
.times(signedOrder.params.makerTokenAmount)
.dividedToIntegerBy(signedOrder.params.takerTokenAmount);
const paidMakerFee = signedOrder.params.makerFee
.times(fillMakerTokenAmount)
.dividedToIntegerBy(order.params.makerTokenAmount);
const paidTakerFee = order.params.takerFee
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
const paidTakerFee = signedOrder.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),
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.makerToken].minus(fillMakerTokenAmount),
);
expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
balances[maker][order.params.takerToken].add(fillTakerTokenAmount),
expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.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][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.takerToken].minus(fillTakerTokenAmount),
);
expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
balances[taker][order.params.makerToken].add(fillMakerTokenAmount),
expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.makerToken].add(fillMakerTokenAmount),
);
expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
balances[taker][zrx.address].minus(paidTakerFee),
@@ -397,141 +398,141 @@ describe('Exchange', () => {
});
it('should fill remaining value if fillTakerTokenAmount > remaining takerTokenAmount', async () => {
const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount });
const fillTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2);
await exWrapper.fillOrderAsync(signedOrder, taker, { fillTakerTokenAmount });
const res = await exWrapper.fillOrderAsync(order, taker, {
fillTakerTokenAmount: order.params.takerTokenAmount,
const res = await exWrapper.fillOrderAsync(signedOrder, taker, {
fillTakerTokenAmount: signedOrder.params.takerTokenAmount,
});
const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>;
expect(log.args.filledTakerTokenAmount).to.be.bignumber.equal(
order.params.takerTokenAmount.minus(fillTakerTokenAmount),
signedOrder.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][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.makerToken].minus(signedOrder.params.makerTokenAmount),
);
expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
balances[maker][order.params.takerToken].add(order.params.takerTokenAmount),
expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.takerToken].add(signedOrder.params.takerTokenAmount),
);
expect(newBalances[maker][zrx.address]).to.be.bignumber.equal(
balances[maker][zrx.address].minus(order.params.makerFee),
balances[maker][zrx.address].minus(signedOrder.params.makerFee),
);
expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal(
balances[taker][order.params.takerToken].minus(order.params.takerTokenAmount),
expect(newBalances[taker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.takerToken].minus(signedOrder.params.takerTokenAmount),
);
expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
balances[taker][order.params.makerToken].add(order.params.makerTokenAmount),
expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.makerToken].add(signedOrder.params.makerTokenAmount),
);
expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
balances[taker][zrx.address].minus(order.params.takerFee),
balances[taker][zrx.address].minus(signedOrder.params.takerFee),
);
expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal(
balances[feeRecipient][zrx.address].add(order.params.makerFee.add(order.params.takerFee)),
balances[feeRecipient][zrx.address].add(signedOrder.params.makerFee.add(signedOrder.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(signedOrder, taker, {
fillTakerTokenAmount: signedOrder.params.takerTokenAmount.div(divisor),
});
expect(res.logs).to.have.length(1);
const logArgs = (res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>).args;
const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor);
const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor);
const expectedFeeMPaid = order.params.makerFee.div(divisor);
const expectedFeeTPaid = order.params.takerFee.div(divisor);
const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]);
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 expectedTokens = ethUtil.bufferToHex(tokensHashBuff);
expect(order.params.maker).to.be.equal(logArgs.maker);
expect(signedOrder.params.maker).to.be.equal(logArgs.maker);
expect(taker).to.be.equal(logArgs.taker);
expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient);
expect(order.params.makerToken).to.be.equal(logArgs.makerToken);
expect(order.params.takerToken).to.be.equal(logArgs.takerToken);
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(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(order.getOrderHashHex()).to.be.equal(logArgs.orderHash);
expect(signedOrder.getOrderHashHex()).to.be.equal(logArgs.orderHash);
});
it('should log 1 event with the correct arguments when order has no feeRecipient', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
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(signedOrder, taker, {
fillTakerTokenAmount: signedOrder.params.takerTokenAmount.div(divisor),
});
expect(res.logs).to.have.length(1);
const logArgs = (res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>).args;
const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor);
const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor);
const expectedFilledMakerTokenAmount = signedOrder.params.makerTokenAmount.div(divisor);
const expectedFilledTakerTokenAmount = signedOrder.params.takerTokenAmount.div(divisor);
const expectedFeeMPaid = new BigNumber(0);
const expectedFeeTPaid = new BigNumber(0);
const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]);
const tokensHashBuff = crypto.solSHA3([signedOrder.params.makerToken, signedOrder.params.takerToken]);
const expectedTokens = ethUtil.bufferToHex(tokensHashBuff);
expect(order.params.maker).to.be.equal(logArgs.maker);
expect(signedOrder.params.maker).to.be.equal(logArgs.maker);
expect(taker).to.be.equal(logArgs.taker);
expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient);
expect(order.params.makerToken).to.be.equal(logArgs.makerToken);
expect(order.params.takerToken).to.be.equal(logArgs.takerToken);
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(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(order.getOrderHashHex()).to.be.equal(logArgs.orderHash);
expect(signedOrder.getOrderHashHex()).to.be.equal(logArgs.orderHash);
});
it('should throw when taker is specified and order is claimed by other', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
taker: feeRecipient,
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
});
return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT);
return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if signature is invalid', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(10), 18),
});
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.REVERT);
signedOrder.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR'));
signedOrder.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS'));
return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if makerTokenAmount is 0', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: new BigNumber(0),
});
return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT);
return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if takerTokenAmount is 0', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerTokenAmount: new BigNumber(0),
});
return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT);
return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if fillTakerTokenAmount is 0', async () => {
order = await orderFactory.newSignedOrderAsync();
signedOrder = await orderFactory.newSignedOrderAsync();
return expect(
exWrapper.fillOrderAsync(order, taker, {
exWrapper.fillOrderAsync(signedOrder, taker, {
fillTakerTokenAmount: new BigNumber(0),
}),
).to.be.rejectedWith(constants.REVERT);
@@ -539,23 +540,23 @@ describe('Exchange', () => {
it('should not change balances if maker balances are too low to fill order and \
shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
it('should throw if maker balances are too low to fill order and \
shouldThrowOnInsufficientBalanceOrAllowance = true', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18),
});
return expect(
exWrapper.fillOrderAsync(order, taker, {
exWrapper.fillOrderAsync(signedOrder, taker, {
shouldThrowOnInsufficientBalanceOrAllowance: true,
}),
).to.be.rejectedWith(constants.REVERT);
@@ -563,23 +564,23 @@ describe('Exchange', () => {
it('should not change balances if taker balances are too low to fill order and \
shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
it('should throw if taker balances are too low to fill order and \
shouldThrowOnInsufficientBalanceOrAllowance = true', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18),
});
return expect(
exWrapper.fillOrderAsync(order, taker, {
exWrapper.fillOrderAsync(signedOrder, taker, {
shouldThrowOnInsufficientBalanceOrAllowance: true,
}),
).to.be.rejectedWith(constants.REVERT);
@@ -588,7 +589,7 @@ describe('Exchange', () => {
it('should not change balances if maker allowances are too low to fill order and \
shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
await rep.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: maker });
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
await rep.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, {
from: maker,
});
@@ -601,7 +602,7 @@ describe('Exchange', () => {
shouldThrowOnInsufficientBalanceOrAllowance = true', async () => {
await rep.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: maker });
expect(
exWrapper.fillOrderAsync(order, taker, {
exWrapper.fillOrderAsync(signedOrder, taker, {
shouldThrowOnInsufficientBalanceOrAllowance: true,
}),
).to.be.rejectedWith(constants.REVERT);
@@ -613,7 +614,7 @@ describe('Exchange', () => {
it('should not change balances if taker allowances are too low to fill order and \
shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: taker });
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, {
from: taker,
});
@@ -626,7 +627,7 @@ describe('Exchange', () => {
shouldThrowOnInsufficientBalanceOrAllowance = true', async () => {
await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: taker });
expect(
exWrapper.fillOrderAsync(order, taker, {
exWrapper.fillOrderAsync(signedOrder, taker, {
shouldThrowOnInsufficientBalanceOrAllowance: true,
}),
).to.be.rejectedWith(constants.REVERT);
@@ -638,12 +639,12 @@ describe('Exchange', () => {
it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker balance, \
and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
const makerZRXBalance = new BigNumber(balances[maker][zrx.address]);
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerToken: zrx.address,
makerTokenAmount: makerZRXBalance,
makerFee: new BigNumber(1),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
@@ -651,12 +652,12 @@ describe('Exchange', () => {
it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker allowance, \
and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
const makerZRXAllowance = await zrx.allowance(maker, tokenTransferProxy.address);
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerToken: zrx.address,
makerTokenAmount: new BigNumber(makerZRXAllowance),
makerFee: new BigNumber(1),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
@@ -664,12 +665,12 @@ describe('Exchange', () => {
it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker balance, \
and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
const takerZRXBalance = new BigNumber(balances[taker][zrx.address]);
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerToken: zrx.address,
takerTokenAmount: takerZRXBalance,
takerFee: new BigNumber(1),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
@@ -677,12 +678,12 @@ describe('Exchange', () => {
it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker allowance, \
and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => {
const takerZRXAllowance = await zrx.allowance(taker, tokenTransferProxy.address);
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerToken: zrx.address,
takerTokenAmount: new BigNumber(takerZRXAllowance),
takerFee: new BigNumber(1),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
@@ -694,33 +695,33 @@ describe('Exchange', () => {
from: taker,
});
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerToken: maliciousToken.address,
});
return expect(
exWrapper.fillOrderAsync(order, taker, {
exWrapper.fillOrderAsync(signedOrder, taker, {
shouldThrowOnInsufficientBalanceOrAllowance: false,
}),
).to.be.rejectedWith(constants.REVERT);
});
it('should not change balances if an order is expired', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)),
});
await exWrapper.fillOrderAsync(order, taker);
await exWrapper.fillOrderAsync(signedOrder, taker);
const newBalances = await dmyBalances.getAsync();
expect(newBalances).to.be.deep.equal(balances);
});
it('should log an error event if an order is expired', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)),
});
const res = await exWrapper.fillOrderAsync(order, taker);
const res = await exWrapper.fillOrderAsync(signedOrder, taker);
expect(res.logs).to.have.length(1);
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
const errCode = log.args.errorId.toNumber();
@@ -728,10 +729,10 @@ describe('Exchange', () => {
});
it('should log an error event if no value is filled', async () => {
order = await orderFactory.newSignedOrderAsync({});
await exWrapper.fillOrderAsync(order, taker);
signedOrder = await orderFactory.newSignedOrderAsync({});
await exWrapper.fillOrderAsync(signedOrder, taker);
const res = await exWrapper.fillOrderAsync(order, taker);
const res = await exWrapper.fillOrderAsync(signedOrder, taker);
expect(res.logs).to.have.length(1);
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
const errCode = log.args.errorId.toNumber();
@@ -742,43 +743,43 @@ describe('Exchange', () => {
describe('cancelOrder', () => {
beforeEach(async () => {
balances = await dmyBalances.getAsync();
order = await orderFactory.newSignedOrderAsync();
signedOrder = await orderFactory.newSignedOrderAsync();
});
it('should throw if not sent by maker', async () => {
return expect(exWrapper.cancelOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT);
return expect(exWrapper.cancelOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if makerTokenAmount is 0', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
makerTokenAmount: new BigNumber(0),
});
return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT);
return expect(exWrapper.cancelOrderAsync(signedOrder, maker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if takerTokenAmount is 0', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
takerTokenAmount: new BigNumber(0),
});
return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT);
return expect(exWrapper.cancelOrderAsync(signedOrder, maker)).to.be.rejectedWith(constants.REVERT);
});
it('should throw if cancelTakerTokenAmount is 0', async () => {
order = await orderFactory.newSignedOrderAsync();
signedOrder = await orderFactory.newSignedOrderAsync();
return expect(
exWrapper.cancelOrderAsync(order, maker, {
exWrapper.cancelOrderAsync(signedOrder, 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.cancelOrderAsync(signedOrder, maker);
await exWrapper.fillOrderAsync(signedOrder, taker, {
fillTakerTokenAmount: signedOrder.params.takerTokenAmount.div(2),
});
const newBalances = await dmyBalances.getAsync();
@@ -786,43 +787,43 @@ describe('Exchange', () => {
});
it('should be able to cancel part of an order', async () => {
const cancelTakerTokenAmount = order.params.takerTokenAmount.div(2);
await exWrapper.cancelOrderAsync(order, maker, {
const cancelTakerTokenAmount = signedOrder.params.takerTokenAmount.div(2);
await exWrapper.cancelOrderAsync(signedOrder, maker, {
cancelTakerTokenAmount,
});
const res = await exWrapper.fillOrderAsync(order, taker, {
fillTakerTokenAmount: order.params.takerTokenAmount,
const res = await exWrapper.fillOrderAsync(signedOrder, taker, {
fillTakerTokenAmount: signedOrder.params.takerTokenAmount,
});
const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>;
expect(log.args.filledTakerTokenAmount).to.be.bignumber.equal(
order.params.takerTokenAmount.minus(cancelTakerTokenAmount),
signedOrder.params.takerTokenAmount.minus(cancelTakerTokenAmount),
);
const newBalances = await dmyBalances.getAsync();
const cancelMakerTokenAmount = cancelTakerTokenAmount
.times(order.params.makerTokenAmount)
.dividedToIntegerBy(order.params.takerTokenAmount);
const paidMakerFee = order.params.makerFee
.times(signedOrder.params.makerTokenAmount)
.dividedToIntegerBy(signedOrder.params.takerTokenAmount);
const paidMakerFee = signedOrder.params.makerFee
.times(cancelMakerTokenAmount)
.dividedToIntegerBy(order.params.makerTokenAmount);
const paidTakerFee = order.params.takerFee
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
const paidTakerFee = signedOrder.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),
.dividedToIntegerBy(signedOrder.params.makerTokenAmount);
expect(newBalances[maker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.params.makerToken].minus(cancelMakerTokenAmount),
);
expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
balances[maker][order.params.takerToken].add(cancelTakerTokenAmount),
expect(newBalances[maker][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[maker][signedOrder.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][signedOrder.params.takerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.takerToken].minus(cancelTakerTokenAmount),
);
expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
balances[taker][order.params.makerToken].add(cancelMakerTokenAmount),
expect(newBalances[taker][signedOrder.params.makerToken]).to.be.bignumber.equal(
balances[taker][signedOrder.params.makerToken].add(cancelMakerTokenAmount),
);
expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
balances[taker][zrx.address].minus(paidTakerFee),
@@ -834,32 +835,32 @@ describe('Exchange', () => {
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(signedOrder, maker, {
cancelTakerTokenAmount: signedOrder.params.takerTokenAmount.div(divisor),
});
expect(res.logs).to.have.length(1);
const log = res.logs[0] as LogWithDecodedArgs<LogCancelContractEventArgs>;
const logArgs = log.args;
const expectedCancelledMakerTokenAmount = order.params.makerTokenAmount.div(divisor);
const expectedCancelledTakerTokenAmount = order.params.takerTokenAmount.div(divisor);
const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]);
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 expectedTokens = ethUtil.bufferToHex(tokensHashBuff);
expect(order.params.maker).to.be.equal(logArgs.maker);
expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient);
expect(order.params.makerToken).to.be.equal(logArgs.makerToken);
expect(order.params.takerToken).to.be.equal(logArgs.takerToken);
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(expectedCancelledMakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledMakerTokenAmount);
expect(expectedCancelledTakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledTakerTokenAmount);
expect(expectedTokens).to.be.equal(logArgs.tokens);
expect(order.getOrderHashHex()).to.be.equal(logArgs.orderHash);
expect(signedOrder.getOrderHashHex()).to.be.equal(logArgs.orderHash);
});
it('should not log events if no value is cancelled', async () => {
await exWrapper.cancelOrderAsync(order, maker);
await exWrapper.cancelOrderAsync(signedOrder, maker);
const res = await exWrapper.cancelOrderAsync(order, maker);
const res = await exWrapper.cancelOrderAsync(signedOrder, maker);
expect(res.logs).to.have.length(1);
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
const errCode = log.args.errorId.toNumber();
@@ -867,11 +868,11 @@ describe('Exchange', () => {
});
it('should not log events if order is expired', async () => {
order = await orderFactory.newSignedOrderAsync({
signedOrder = await orderFactory.newSignedOrderAsync({
expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)),
});
const res = await exWrapper.cancelOrderAsync(order, maker);
const res = await exWrapper.cancelOrderAsync(signedOrder, maker);
expect(res.logs).to.have.length(1);
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
const errCode = log.args.errorId.toNumber();

View File

@@ -10,6 +10,7 @@ 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';
@@ -25,7 +26,7 @@ describe('Exchange', () => {
let maker: string;
let feeRecipient: string;
let order: Order;
let signedOrder: SignedOrder;
let exchangeWrapper: ExchangeWrapper;
let orderFactory: OrderFactory;
@@ -59,7 +60,7 @@ describe('Exchange', () => {
takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
};
orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams);
order = await orderFactory.newSignedOrderAsync();
signedOrder = await orderFactory.newSignedOrderAsync();
});
beforeEach(async () => {
@@ -70,28 +71,28 @@ describe('Exchange', () => {
});
describe('getOrderHash', () => {
it('should output the correct orderHash', async () => {
const orderHashHex = await exchangeWrapper.getOrderHashAsync(order);
expect(order.getOrderHashHex()).to.be.equal(orderHashHex);
const orderHashHex = await exchangeWrapper.getOrderHashAsync(signedOrder);
expect(signedOrder.getOrderHashHex()).to.be.equal(orderHashHex);
});
});
describe('isValidSignature', () => {
beforeEach(async () => {
order = await orderFactory.newSignedOrderAsync();
signedOrder = await orderFactory.newSignedOrderAsync();
});
it('should return true with a valid signature', async () => {
const success = await exchangeWrapper.isValidSignatureAsync(order);
const isValidSignature = order.isValidSignature();
const success = await exchangeWrapper.isValidSignatureAsync(signedOrder);
const isValidSignature = signedOrder.isValidSignature();
expect(isValidSignature).to.be.true();
expect(success).to.be.true();
});
it('should return false with an invalid signature', async () => {
order.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR'));
order.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS'));
const success = await exchangeWrapper.isValidSignatureAsync(order);
expect(order.isValidSignature()).to.be.false();
signedOrder.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR'));
signedOrder.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS'));
const success = await exchangeWrapper.isValidSignatureAsync(signedOrder);
expect(signedOrder.isValidSignature()).to.be.false();
expect(success).to.be.false();
});
});

View File

@@ -15,6 +15,7 @@ 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';
@@ -173,7 +174,7 @@ describe('Exchange', () => {
});
describe('batch functions', () => {
let orders: Order[];
let orders: SignedOrder[];
beforeEach(async () => {
orders = await Promise.all([
orderFactory.newSignedOrderAsync(),

View File

@@ -6,7 +6,7 @@ import * as Web3 from 'web3';
import { ExchangeContract } from '../src/contract_wrappers/generated/exchange';
import { formatters } from './formatters';
import { Order } from './order';
import { SignedOrder } from './signed_order';
export class ExchangeWrapper {
private _exchange: ExchangeContract;
@@ -16,7 +16,7 @@ export class ExchangeWrapper {
this._zeroEx = zeroEx;
}
public async fillOrderAsync(
order: Order,
signedOrder: SignedOrder,
from: string,
opts: {
fillTakerTokenAmount?: BigNumber;
@@ -24,15 +24,15 @@ export class ExchangeWrapper {
} = {},
): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance;
const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
const txHash = await this._exchange.fillOrder.sendTransactionAsync(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
params.shouldThrowOnInsufficientBalanceOrAllowance,
params.v as number,
params.r as string,
params.s as string,
params.v,
params.r,
params.s,
{ from },
);
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
@@ -41,11 +41,11 @@ export class ExchangeWrapper {
return tx;
}
public async cancelOrderAsync(
order: Order,
signedOrder: SignedOrder,
from: string,
opts: { cancelTakerTokenAmount?: BigNumber } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
const params = order.createCancel(opts.cancelTakerTokenAmount);
const params = signedOrder.createCancel(opts.cancelTakerTokenAmount);
const txHash = await this._exchange.cancelOrder.sendTransactionAsync(
params.orderAddresses,
params.orderValues,
@@ -58,19 +58,19 @@ export class ExchangeWrapper {
return tx;
}
public async fillOrKillOrderAsync(
order: Order,
signedOrder: SignedOrder,
from: string,
opts: { fillTakerTokenAmount?: BigNumber } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = true;
const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
params.v as number,
params.r as string,
params.s as string,
params.v,
params.r,
params.s,
{ from },
);
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
@@ -79,7 +79,7 @@ export class ExchangeWrapper {
return tx;
}
public async batchFillOrdersAsync(
orders: Order[],
orders: SignedOrder[],
from: string,
opts: {
fillTakerTokenAmounts?: BigNumber[];
@@ -108,7 +108,7 @@ export class ExchangeWrapper {
return tx;
}
public async batchFillOrKillOrdersAsync(
orders: Order[],
orders: SignedOrder[],
from: string,
opts: { fillTakerTokenAmounts?: BigNumber[]; shouldThrowOnInsufficientBalanceOrAllowance?: boolean } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
@@ -133,7 +133,7 @@ export class ExchangeWrapper {
return tx;
}
public async fillOrdersUpToAsync(
orders: Order[],
orders: SignedOrder[],
from: string,
opts: { fillTakerTokenAmount: BigNumber; shouldThrowOnInsufficientBalanceOrAllowance?: boolean },
): Promise<TransactionReceiptWithDecodedLogs> {
@@ -159,7 +159,7 @@ export class ExchangeWrapper {
return tx;
}
public async batchCancelOrdersAsync(
orders: Order[],
orders: SignedOrder[],
from: string,
opts: { cancelTakerTokenAmounts?: BigNumber[] } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
@@ -175,19 +175,19 @@ export class ExchangeWrapper {
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
public async getOrderHashAsync(order: Order): Promise<string> {
public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> {
const shouldThrowOnInsufficientBalanceOrAllowance = false;
const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance);
const params = signedOrder.createFill(shouldThrowOnInsufficientBalanceOrAllowance);
const orderHash = await this._exchange.getOrderHash(params.orderAddresses, params.orderValues);
return orderHash;
}
public async isValidSignatureAsync(order: Order): Promise<boolean> {
public async isValidSignatureAsync(signedOrder: SignedOrder): Promise<boolean> {
const isValidSignature = await this._exchange.isValidSignature(
order.params.maker,
order.getOrderHashHex(),
order.params.v as number,
order.params.r as string,
order.params.s as string,
signedOrder.params.maker,
signedOrder.getOrderHashHex(),
signedOrder.params.v,
signedOrder.params.r,
signedOrder.params.s,
);
return isValidSignature;
}

View File

@@ -1,12 +1,12 @@
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
import { Order } from './order';
import { SignedOrder } from './signed_order';
import { BatchCancelOrders, BatchFillOrders, FillOrdersUpTo } from './types';
export const formatters = {
createBatchFill(
orders: Order[],
orders: SignedOrder[],
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
fillTakerTokenAmounts: BigNumber[] = [],
) {
@@ -35,9 +35,9 @@ export const formatters = {
order.params.expirationTimestampInSec,
order.params.salt,
]);
batchFill.v.push(order.params.v as number);
batchFill.r.push(order.params.r as string);
batchFill.s.push(order.params.s as string);
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);
}
@@ -45,7 +45,7 @@ export const formatters = {
return batchFill;
},
createFillUpTo(
orders: Order[],
orders: SignedOrder[],
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
fillTakerTokenAmount: BigNumber,
) {
@@ -74,13 +74,13 @@ export const formatters = {
order.params.expirationTimestampInSec,
order.params.salt,
]);
fillUpTo.v.push(order.params.v as number);
fillUpTo.r.push(order.params.r as string);
fillUpTo.s.push(order.params.s as string);
fillUpTo.v.push(order.params.v);
fillUpTo.r.push(order.params.r);
fillUpTo.s.push(order.params.s);
});
return fillUpTo;
},
createBatchCancel(orders: Order[], cancelTakerTokenAmounts: BigNumber[] = []) {
createBatchCancel(orders: SignedOrder[], cancelTakerTokenAmounts: BigNumber[] = []) {
const batchCancel: BatchCancelOrders = {
orderAddresses: [],
orderValues: [],

View File

@@ -4,6 +4,7 @@ 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 {
@@ -13,76 +14,17 @@ export class Order {
this.params = params;
this._web3Wrapper = web3Wrapper;
}
public isValidSignature() {
const { v, r, s } = this.params;
if (_.isUndefined(v) || _.isUndefined(r) || _.isUndefined(s)) {
throw new Error('Cannot call isValidSignature on unsigned order');
}
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 async signAsync() {
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);
this.params = _.assign(this.params, {
const signedOrderParams = _.assign(this.params, {
v,
r: ethUtil.bufferToHex(r),
s: ethUtil.bufferToHex(s),
});
}
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;
const signedOrder = new SignedOrder(this._web3Wrapper, signedOrderParams);
return signedOrder;
}
public getOrderHashHex(): string {
const orderHash = crypto.solSHA3([

View File

@@ -4,6 +4,7 @@ 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';
export class OrderFactory {
@@ -13,7 +14,7 @@ export class OrderFactory {
this._defaultOrderParams = defaultOrderParams;
this._web3Wrapper = web3Wrapper;
}
public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<Order> {
public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<SignedOrder> {
const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000));
const orderParams: OrderParams = _.assign(
{},
@@ -26,7 +27,7 @@ export class OrderFactory {
customOrderParams,
);
const order = new Order(this._web3Wrapper, orderParams);
await order.signAsync();
return order;
const signedOrder = await order.signAsync();
return signedOrder;
}
}

View File

@@ -0,0 +1,92 @@
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;
}
}

View File

@@ -76,9 +76,12 @@ export interface OrderParams {
takerFee: BigNumber;
expirationTimestampInSec: BigNumber;
salt: BigNumber;
v?: number;
r?: string;
s?: string;
}
export interface SignedOrderParams extends OrderParams {
v: number;
r: string;
s: string;
}
export interface TransactionDataParams {