From edf837ed622d782e43b0d25dd46a27469b9b0381 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Fri, 9 Feb 2018 13:20:15 -0800 Subject: Add logDecoder, remove outdated tests --- packages/contracts/src/utils/exchange_wrapper.ts | 10 +---- packages/contracts/src/utils/formatters.ts | 10 +---- packages/contracts/src/utils/log_decoder.ts | 29 ++++++++++++++ packages/contracts/src/utils/signed_order_utils.ts | 44 ++++++++++++++++++++++ 4 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 packages/contracts/src/utils/log_decoder.ts create mode 100644 packages/contracts/src/utils/signed_order_utils.ts (limited to 'packages/contracts/src/utils') diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index 75b452b04..daab002b3 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -20,10 +20,7 @@ export class ExchangeWrapper { from: string, opts: { takerTokenFillAmount?: BigNumber } = {}, ): Promise { - const params = signedOrderUtils.createFill( - signedOrder, - opts.takerTokenFillAmount, - ); + const params = signedOrderUtils.createFill(signedOrder, opts.takerTokenFillAmount); const txHash = await this._exchange.fillOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, @@ -60,10 +57,7 @@ export class ExchangeWrapper { from: string, opts: { takerTokenFillAmount?: BigNumber } = {}, ): Promise { - const params = signedOrderUtils.createFill( - signedOrder, - opts.takerTokenFillAmount, - ); + const params = signedOrderUtils.createFill(signedOrder, opts.takerTokenFillAmount); const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync( params.orderAddresses, params.orderValues, diff --git a/packages/contracts/src/utils/formatters.ts b/packages/contracts/src/utils/formatters.ts index 88e12a6b4..48c77fea1 100644 --- a/packages/contracts/src/utils/formatters.ts +++ b/packages/contracts/src/utils/formatters.ts @@ -5,10 +5,7 @@ import * as _ from 'lodash'; import { BatchCancelOrders, BatchFillOrders, MarketFillOrders } from './types'; export const formatters = { - createBatchFill( - signedOrders: SignedOrder[], - takerTokenFillAmounts: BigNumber[] = [], - ) { + createBatchFill(signedOrders: SignedOrder[], takerTokenFillAmounts: BigNumber[] = []) { const batchFill: BatchFillOrders = { orderAddresses: [], orderValues: [], @@ -42,10 +39,7 @@ export const formatters = { }); return batchFill; }, - createMarketFillOrders( - signedOrders: SignedOrder[], - takerTokenFillAmount: BigNumber, - ) { + createMarketFillOrders(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber) { const marketFillOrders: MarketFillOrders = { orderAddresses: [], orderValues: [], diff --git a/packages/contracts/src/utils/log_decoder.ts b/packages/contracts/src/utils/log_decoder.ts new file mode 100644 index 000000000..98dd8eab6 --- /dev/null +++ b/packages/contracts/src/utils/log_decoder.ts @@ -0,0 +1,29 @@ +import { LogWithDecodedArgs, RawLog } from '@0xproject/types'; +import { AbiDecoder } from '@0xproject/utils'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + +import { artifacts } from './artifacts'; +import { Artifact } from './types'; + +export class LogDecoder { + private _abiDecoder: AbiDecoder; + constructor(networkId: number) { + if (_.isUndefined(networkId)) { + throw new Error('networkId not specified'); + } + const abiArrays: Web3.AbiDefinition[][] = []; + _.forEach(artifacts, (artifact: Artifact) => { + const network = artifact.networks[networkId]; + if (_.isUndefined(network)) { + throw new Error(`Artifact does not exist on network ${networkId}`); + } + abiArrays.push(network.abi); + }); + this._abiDecoder = new AbiDecoder(abiArrays); + } + public tryToDecodeLogOrNoop(log: Web3.LogEntry): LogWithDecodedArgs | RawLog { + const logWithDecodedArgs = this._abiDecoder.tryToDecodeLogOrNoop(log); + return logWithDecodedArgs; + } +} diff --git a/packages/contracts/src/utils/signed_order_utils.ts b/packages/contracts/src/utils/signed_order_utils.ts new file mode 100644 index 000000000..6122748b4 --- /dev/null +++ b/packages/contracts/src/utils/signed_order_utils.ts @@ -0,0 +1,44 @@ +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, takerTokenFillAmount?: BigNumber) => { + const fill = { + ...signedOrderUtils.getOrderAddressesAndValues(signedOrder), + takerTokenFillAmount: takerTokenFillAmount || signedOrder.takerTokenAmount, + ...signedOrder.ecSignature, + }; + return fill; + }, + createCancel(signedOrder: SignedOrder, takerTokenCancelAmount?: BigNumber) { + const cancel = { + ...signedOrderUtils.getOrderAddressesAndValues(signedOrder), + takerTokenCancelAmount: takerTokenCancelAmount || 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, + ], + }; + }, +}; -- cgit v1.2.3