Move log decoding logic to exchange wrapper
This commit is contained in:
@@ -5,11 +5,14 @@ import * as Web3 from 'web3';
|
||||
|
||||
import { ExchangeContract } from '../contract_wrappers/generated/exchange';
|
||||
|
||||
import { constants } from './constants';
|
||||
import { formatters } from './formatters';
|
||||
import { LogDecoder } from './log_decoder';
|
||||
import { signedOrderUtils } from './signed_order_utils';
|
||||
|
||||
export class ExchangeWrapper {
|
||||
private _exchange: ExchangeContract;
|
||||
private _logDecoder: LogDecoder = new LogDecoder(constants.TESTRPC_NETWORK_ID);
|
||||
private _zeroEx: ZeroEx;
|
||||
constructor(exchangeContract: ExchangeContract, zeroEx: ZeroEx) {
|
||||
this._exchange = exchangeContract;
|
||||
@@ -32,7 +35,11 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async cancelOrderAsync(
|
||||
@@ -49,7 +56,11 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async fillOrKillOrderAsync(
|
||||
@@ -69,16 +80,17 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async batchFillOrdersAsync(
|
||||
orders: SignedOrder[],
|
||||
from: string,
|
||||
opts: {
|
||||
takerTokenFillAmounts?: BigNumber[];
|
||||
shouldThrowOnInsufficientBalanceOrAllowance?: boolean;
|
||||
} = {},
|
||||
opts: { takerTokenFillAmounts?: BigNumber[] } = {},
|
||||
): Promise<TransactionReceiptWithDecodedLogs> {
|
||||
const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts);
|
||||
const txHash = await this._exchange.batchFillOrders.sendTransactionAsync(
|
||||
@@ -92,7 +104,11 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async batchFillOrKillOrdersAsync(
|
||||
@@ -112,7 +128,11 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async marketFillOrdersAsync(
|
||||
@@ -132,7 +152,11 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async batchCancelOrdersAsync(
|
||||
@@ -149,7 +173,11 @@ export class ExchangeWrapper {
|
||||
);
|
||||
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
|
||||
tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
|
||||
_.each(tx.logs, log => wrapLogBigNumbers(log));
|
||||
tx.logs = _.map(tx.logs, log => {
|
||||
const decodedLog = this._logDecoder.tryToDecodeLogOrNoop(log);
|
||||
wrapLogBigNumbers(decodedLog);
|
||||
return decodedLog;
|
||||
});
|
||||
return tx;
|
||||
}
|
||||
public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> {
|
||||
|
||||
@@ -19,7 +19,6 @@ import { Balances } from '../../src/utils/balances';
|
||||
import { constants } from '../../src/utils/constants';
|
||||
import { crypto } from '../../src/utils/crypto';
|
||||
import { ExchangeWrapper } from '../../src/utils/exchange_wrapper';
|
||||
import { LogDecoder } from '../../src/utils/log_decoder';
|
||||
import { OrderFactory } from '../../src/utils/order_factory';
|
||||
import { BalancesByOwner, ContractName, ExchangeContractErrs } from '../../src/utils/types';
|
||||
import { chaiSetup } from '../utils/chai_setup';
|
||||
@@ -394,7 +393,7 @@ describe('Exchange', () => {
|
||||
const res = await exWrapper.fillOrderAsync(signedOrder, taker, {
|
||||
takerTokenFillAmount: signedOrder.takerTokenAmount,
|
||||
});
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
expect(log.args.takerTokenFilledAmount).to.be.bignumber.equal(
|
||||
signedOrder.takerTokenAmount.minus(takerTokenFillAmount),
|
||||
);
|
||||
@@ -430,7 +429,7 @@ describe('Exchange', () => {
|
||||
});
|
||||
expect(res.logs).to.have.length(1);
|
||||
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
const logArgs = log.args;
|
||||
const expectedFilledMakerTokenAmount = signedOrder.makerTokenAmount.div(divisor);
|
||||
const expectedFilledTakerTokenAmount = signedOrder.takerTokenAmount.div(divisor);
|
||||
@@ -461,7 +460,7 @@ describe('Exchange', () => {
|
||||
});
|
||||
expect(res.logs).to.have.length(1);
|
||||
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
const logArgs = log.args;
|
||||
const expectedFilledMakerTokenAmount = signedOrder.makerTokenAmount.div(divisor);
|
||||
const expectedFilledTakerTokenAmount = signedOrder.takerTokenAmount.div(divisor);
|
||||
@@ -689,7 +688,7 @@ describe('Exchange', () => {
|
||||
|
||||
const res = await exWrapper.fillOrderAsync(signedOrder, taker);
|
||||
expect(res.logs).to.have.length(1);
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const errCode = log.args.errorId.toNumber();
|
||||
expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED);
|
||||
});
|
||||
@@ -700,7 +699,7 @@ describe('Exchange', () => {
|
||||
|
||||
const res = await exWrapper.fillOrderAsync(signedOrder, taker);
|
||||
expect(res.logs).to.have.length(1);
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const errCode = log.args.errorId.toNumber();
|
||||
expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED);
|
||||
});
|
||||
@@ -761,7 +760,7 @@ describe('Exchange', () => {
|
||||
const res = await exWrapper.fillOrderAsync(signedOrder, taker, {
|
||||
takerTokenFillAmount: signedOrder.takerTokenAmount,
|
||||
});
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogFillContractEventArgs>;
|
||||
expect(log.args.takerTokenFilledAmount).to.be.bignumber.equal(
|
||||
signedOrder.takerTokenAmount.minus(takerTokenCancelAmount),
|
||||
);
|
||||
@@ -806,7 +805,7 @@ describe('Exchange', () => {
|
||||
});
|
||||
expect(res.logs).to.have.length(1);
|
||||
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogCancelContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogCancelContractEventArgs>;
|
||||
const logArgs = log.args;
|
||||
const expectedCancelledMakerTokenAmount = signedOrder.makerTokenAmount.div(divisor);
|
||||
const expectedCancelledTakerTokenAmount = signedOrder.takerTokenAmount.div(divisor);
|
||||
@@ -827,7 +826,7 @@ describe('Exchange', () => {
|
||||
|
||||
const res = await exWrapper.cancelOrderAsync(signedOrder, maker);
|
||||
expect(res.logs).to.have.length(1);
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const errCode = log.args.errorId.toNumber();
|
||||
expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED);
|
||||
});
|
||||
@@ -839,7 +838,7 @@ describe('Exchange', () => {
|
||||
|
||||
const res = await exWrapper.cancelOrderAsync(signedOrder, maker);
|
||||
expect(res.logs).to.have.length(1);
|
||||
const log = logDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const log = res.logs[0] as LogWithDecodedArgs<LogErrorContractEventArgs>;
|
||||
const errCode = log.args.errorId.toNumber();
|
||||
expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user