diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-03-10 07:47:21 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:16 +0800 |
commit | 6db0b2e39821f27f6f31a8247159864dacd1d2bd (patch) | |
tree | be4b6b6cc5cb098446a3a82016931173642faf5b /packages/contracts/src/utils | |
parent | 2bd1ddd1296b4b3706bf91934e00c02508ca7c6f (diff) | |
download | dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.tar dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.tar.gz dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.tar.bz2 dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.tar.lz dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.tar.xz dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.tar.zst dexon-sol-tools-6db0b2e39821f27f6f31a8247159864dacd1d2bd.zip |
Address feedback and lint
Diffstat (limited to 'packages/contracts/src/utils')
-rw-r--r-- | packages/contracts/src/utils/crypto.ts | 2 | ||||
-rw-r--r-- | packages/contracts/src/utils/exchange_wrapper.ts | 94 | ||||
-rw-r--r-- | packages/contracts/src/utils/log_decoder.ts | 13 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_factory.ts | 8 | ||||
-rw-r--r-- | packages/contracts/src/utils/signing_utils.ts | 6 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 27 | ||||
-rw-r--r-- | packages/contracts/src/utils/utils.ts | 7 |
7 files changed, 36 insertions, 121 deletions
diff --git a/packages/contracts/src/utils/crypto.ts b/packages/contracts/src/utils/crypto.ts index 4126b8ff4..810072d2f 100644 --- a/packages/contracts/src/utils/crypto.ts +++ b/packages/contracts/src/utils/crypto.ts @@ -3,8 +3,6 @@ import ABI = require('ethereumjs-abi'); import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; -import { SignedOrder } from './types'; - export const crypto = { /** * We convert types from JS to Solidity as follows: diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index b53519871..5996867cb 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -31,13 +31,7 @@ export class ExchangeWrapper { params.signature, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async cancelOrderAsync( @@ -51,13 +45,7 @@ export class ExchangeWrapper { params.takerTokenCancelAmount, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async fillOrKillOrderAsync( @@ -72,13 +60,7 @@ export class ExchangeWrapper { params.signature, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async fillOrderNoThrowAsync( @@ -93,13 +75,7 @@ export class ExchangeWrapper { params.signature, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async batchFillOrdersAsync( @@ -114,13 +90,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async batchFillOrKillOrdersAsync( @@ -135,13 +105,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async batchFillOrdersNoThrowAsync( @@ -156,13 +120,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async marketFillOrdersAsync( @@ -177,13 +135,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async marketFillOrdersNoThrowAsync( @@ -198,13 +150,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async batchCancelOrdersAsync( @@ -218,13 +164,7 @@ export class ExchangeWrapper { params.takerTokenCancelAmounts, { from }, ); - const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => { - const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); - wrapLogBigNumbers(logWithDecodedArgs); - return logWithDecodedArgs; - }); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> { @@ -262,14 +202,10 @@ export class ExchangeWrapper { const filledAmount = new BigNumber(await this._exchange.filled.callAsync(orderHashHex)); return filledAmount; } -} - -function wrapLogBigNumbers(log: any): any { - const argNames = _.keys(log.args); - for (const argName of argNames) { - const isWeb3BigNumber = _.startsWith(log.args[argName].constructor.toString(), 'function BigNumber('); - if (isWeb3BigNumber) { - log.args[argName] = new BigNumber(log.args[argName]); - } + private async _getTxWithDecodedExchangeLogsAsync(txHash: string) { + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); + tx.logs = _.map(tx.logs, log => this._logDecoder.decodeLogOrThrow(log)); + return tx; } } diff --git a/packages/contracts/src/utils/log_decoder.ts b/packages/contracts/src/utils/log_decoder.ts index d8685e3d7..845e025fe 100644 --- a/packages/contracts/src/utils/log_decoder.ts +++ b/packages/contracts/src/utils/log_decoder.ts @@ -1,5 +1,5 @@ import { LogWithDecodedArgs, RawLog } from '@0xproject/types'; -import { AbiDecoder } from '@0xproject/utils'; +import { AbiDecoder, BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -27,6 +27,17 @@ export class LogDecoder { if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs<ArgsType>).args)) { throw new Error(`Unable to decode log: ${JSON.stringify(log)}`); } + wrapLogBigNumbers(logWithDecodedArgsOrLog); return logWithDecodedArgsOrLog; } } + +function wrapLogBigNumbers(log: any): any { + const argNames = _.keys(log.args); + for (const argName of argNames) { + const isWeb3BigNumber = _.startsWith(log.args[argName].constructor.toString(), 'function BigNumber('); + if (isWeb3BigNumber) { + log.args[argName] = new BigNumber(log.args[argName]); + } + } +} diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts index 3f09cedf3..bdf5f9abe 100644 --- a/packages/contracts/src/utils/order_factory.ts +++ b/packages/contracts/src/utils/order_factory.ts @@ -8,10 +8,10 @@ import { DefaultOrderParams, SignatureType, SignedOrder, UnsignedOrder } from '. export class OrderFactory { private _defaultOrderParams: Partial<UnsignedOrder>; - private _secretKey: Buffer; - constructor(secretKey: Buffer, defaultOrderParams: Partial<UnsignedOrder>) { + private _privateKey: Buffer; + constructor(privateKey: Buffer, defaultOrderParams: Partial<UnsignedOrder>) { this._defaultOrderParams = defaultOrderParams; - this._secretKey = secretKey; + this._privateKey = privateKey; } public newSignedOrder( customOrderParams: Partial<UnsignedOrder> = {}, @@ -26,7 +26,7 @@ export class OrderFactory { ...customOrderParams, } as any) as UnsignedOrder; const orderHashBuff = orderUtils.getOrderHashBuff(order); - const signature = signingUtils.signMessage(orderHashBuff, this._secretKey, signatureType); + const signature = signingUtils.signMessage(orderHashBuff, this._privateKey, signatureType); const signedOrder = { ...order, signature: `0x${signature.toString('hex')}`, diff --git a/packages/contracts/src/utils/signing_utils.ts b/packages/contracts/src/utils/signing_utils.ts index 21b69619c..61ab1f138 100644 --- a/packages/contracts/src/utils/signing_utils.ts +++ b/packages/contracts/src/utils/signing_utils.ts @@ -3,10 +3,10 @@ import * as ethUtil from 'ethereumjs-util'; import { SignatureType } from './types'; export const signingUtils = { - signMessage(message: Buffer, secretKey: Buffer, signatureType: SignatureType): Buffer { + signMessage(message: Buffer, privateKey: Buffer, signatureType: SignatureType): Buffer { if (signatureType === SignatureType.Ecrecover) { const prefixedMessage = ethUtil.hashPersonalMessage(message); - const ecSignature = ethUtil.ecsign(prefixedMessage, secretKey); + const ecSignature = ethUtil.ecsign(prefixedMessage, privateKey); const signature = Buffer.concat([ ethUtil.toBuffer(signatureType), ethUtil.toBuffer(ecSignature.v), @@ -15,7 +15,7 @@ export const signingUtils = { ]); return signature; } else if (signatureType === SignatureType.EIP712) { - const ecSignature = ethUtil.ecsign(message, secretKey); + const ecSignature = ethUtil.ecsign(message, privateKey); const signature = Buffer.concat([ ethUtil.toBuffer(signatureType), ethUtil.toBuffer(ecSignature.v), diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index b16925825..9f874d9ec 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -108,19 +108,7 @@ export interface Artifact { }; } -export interface SignedOrder { - exchangeAddress: string; - makerAddress: string; - takerAddress: string; - makerTokenAddress: string; - takerTokenAddress: string; - feeRecipientAddress: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFeeAmount: BigNumber; - takerFeeAmount: BigNumber; - expirationTimeSeconds: BigNumber; - salt: BigNumber; +export interface SignedOrder extends UnsignedOrder { signature: string; } @@ -138,19 +126,8 @@ export interface OrderStruct { salt: BigNumber; } -export interface UnsignedOrder { +export interface UnsignedOrder extends OrderStruct { exchangeAddress: string; - makerAddress: string; - takerAddress: string; - makerTokenAddress: string; - takerTokenAddress: string; - feeRecipientAddress: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFeeAmount: BigNumber; - takerFeeAmount: BigNumber; - expirationTimeSeconds: BigNumber; - salt: BigNumber; } export enum SignatureType { diff --git a/packages/contracts/src/utils/utils.ts b/packages/contracts/src/utils/utils.ts deleted file mode 100644 index 04ac36b54..000000000 --- a/packages/contracts/src/utils/utils.ts +++ /dev/null @@ -1,7 +0,0 @@ -export const utils = { - consoleLog(message: string): void { - /* tslint:disable */ - console.log(message); - /* tslint:enable */ - }, -}; |