diff options
Diffstat (limited to 'packages/contracts/src')
-rw-r--r-- | packages/contracts/src/utils/exchange_wrapper.ts | 42 | ||||
-rw-r--r-- | packages/contracts/src/utils/log_decoder.ts | 52 | ||||
-rw-r--r-- | packages/contracts/src/utils/multi_sig_wrapper.ts | 18 |
3 files changed, 62 insertions, 50 deletions
diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index 0446f35d1..ca587f220 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -7,16 +7,18 @@ import { ExchangeContract } from '../contract_wrappers/generated/exchange'; import { constants } from './constants'; import { formatters } from './formatters'; -import { logDecoder } from './log_decoder'; +import { LogDecoder } from './log_decoder'; import { orderUtils } from './order_utils'; import { AssetProxyId, OrderInfo, SignedTransaction } from './types'; export class ExchangeWrapper { private _exchange: ExchangeContract; private _web3Wrapper: Web3Wrapper; + private _logDecoder: LogDecoder; constructor(exchangeContract: ExchangeContract, provider: Provider) { this._exchange = exchangeContract; this._web3Wrapper = new Web3Wrapper(provider); + this._logDecoder = new LogDecoder(this._web3Wrapper, this._exchange.address); } public async fillOrderAsync( signedOrder: SignedOrder, @@ -30,13 +32,13 @@ export class ExchangeWrapper { params.signature, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async cancelOrderAsync(signedOrder: SignedOrder, from: string): Promise<TransactionReceiptWithDecodedLogs> { const params = orderUtils.createCancel(signedOrder); const txHash = await this._exchange.cancelOrder.sendTransactionAsync(params.order, { from }); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async fillOrKillOrderAsync( @@ -51,7 +53,7 @@ export class ExchangeWrapper { params.signature, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async fillOrderNoThrowAsync( @@ -66,7 +68,7 @@ export class ExchangeWrapper { params.signature, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async batchFillOrdersAsync( @@ -81,7 +83,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async batchFillOrKillOrdersAsync( @@ -96,7 +98,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async batchFillOrdersNoThrowAsync( @@ -111,7 +113,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async marketSellOrdersAsync( @@ -126,7 +128,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async marketSellOrdersNoThrowAsync( @@ -141,7 +143,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async marketBuyOrdersAsync( @@ -156,7 +158,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async marketBuyOrdersNoThrowAsync( @@ -171,7 +173,7 @@ export class ExchangeWrapper { params.signatures, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async batchCancelOrdersAsync( @@ -180,12 +182,12 @@ export class ExchangeWrapper { ): Promise<TransactionReceiptWithDecodedLogs> { const params = formatters.createBatchCancel(orders); const txHash = await this._exchange.batchCancelOrders.sendTransactionAsync(params.orders, { from }); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async cancelOrdersUpToAsync(salt: BigNumber, from: string): Promise<TransactionReceiptWithDecodedLogs> { const txHash = await this._exchange.cancelOrdersUpTo.sendTransactionAsync(salt, { from }); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async registerAssetProxyAsync( @@ -203,7 +205,7 @@ export class ExchangeWrapper { oldAssetProxyAddress, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async executeTransactionAsync( @@ -217,7 +219,7 @@ export class ExchangeWrapper { signedTx.signature, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async getTakerAssetFilledAmountAsync(orderHashHex: string): Promise<BigNumber> { @@ -241,13 +243,7 @@ export class ExchangeWrapper { params.rightSignature, { from }, ); - const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); - return tx; - } - private async _getTxWithDecodedExchangeLogsAsync(txHash: string): Promise<TransactionReceiptWithDecodedLogs> { - const tx = await this._web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); - tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); - tx.logs = _.map(tx.logs, log => logDecoder.decodeLogOrThrow(log)); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } } diff --git a/packages/contracts/src/utils/log_decoder.ts b/packages/contracts/src/utils/log_decoder.ts index d2e65d176..32819b657 100644 --- a/packages/contracts/src/utils/log_decoder.ts +++ b/packages/contracts/src/utils/log_decoder.ts @@ -1,19 +1,23 @@ import { ContractArtifact } from '@0xproject/sol-compiler'; -import { AbiDefinition, LogEntry, LogWithDecodedArgs, RawLog } from '@0xproject/types'; +import { + AbiDefinition, + LogEntry, + LogWithDecodedArgs, + RawLog, + TransactionReceiptWithDecodedLogs, +} from '@0xproject/types'; import { AbiDecoder, BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from './artifacts'; +import { constants } from './constants'; -const abiArrays: AbiDefinition[][] = []; -_.forEach(artifacts, (artifact: ContractArtifact) => { - const compilerOutput = artifact.compilerOutput; - abiArrays.push(compilerOutput.abi); -}); -const abiDecoder = new AbiDecoder(abiArrays); - -export const logDecoder = { - wrapLogBigNumbers(log: any): any { +export class LogDecoder { + private _web3Wrapper: Web3Wrapper; + private _contractAddress: string; + private _abiDecoder: AbiDecoder; + public static wrapLogBigNumbers(log: any): any { const argNames = _.keys(log.args); for (const argName of argNames) { const isWeb3BigNumber = _.startsWith(log.args[argName].constructor.toString(), 'function BigNumber('); @@ -21,13 +25,29 @@ export const logDecoder = { log.args[argName] = new BigNumber(log.args[argName]); } } - }, - decodeLogOrThrow<ArgsType>(log: LogEntry): LogWithDecodedArgs<ArgsType> | RawLog { - const logWithDecodedArgsOrLog = abiDecoder.tryToDecodeLogOrNoop(log); + } + constructor(web3Wrapper: Web3Wrapper, contractAddress: string) { + this._web3Wrapper = web3Wrapper; + this._contractAddress = contractAddress; + const abiArrays: AbiDefinition[][] = []; + _.forEach(artifacts, (artifact: ContractArtifact) => { + const compilerOutput = artifact.compilerOutput; + abiArrays.push(compilerOutput.abi); + }); + this._abiDecoder = new AbiDecoder(abiArrays); + } + public decodeLogOrThrow<ArgsType>(log: LogEntry): LogWithDecodedArgs<ArgsType> | RawLog { + const logWithDecodedArgsOrLog = this._abiDecoder.tryToDecodeLogOrNoop(log); if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs<ArgsType>).args)) { throw new Error(`Unable to decode log: ${JSON.stringify(log)}`); } - logDecoder.wrapLogBigNumbers(logWithDecodedArgsOrLog); + LogDecoder.wrapLogBigNumbers(logWithDecodedArgsOrLog); return logWithDecodedArgsOrLog; - }, -}; + } + public async getTxWithDecodedLogsAsync(txHash: string): Promise<TransactionReceiptWithDecodedLogs> { + const tx = await this._web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); + tx.logs = _.filter(tx.logs, log => log.address === this._contractAddress); + tx.logs = _.map(tx.logs, log => this.decodeLogOrThrow(log)); + return tx; + } +} diff --git a/packages/contracts/src/utils/multi_sig_wrapper.ts b/packages/contracts/src/utils/multi_sig_wrapper.ts index 5c73cdf5a..d67692194 100644 --- a/packages/contracts/src/utils/multi_sig_wrapper.ts +++ b/packages/contracts/src/utils/multi_sig_wrapper.ts @@ -7,14 +7,16 @@ import { AssetProxyOwnerContract } from '../contract_wrappers/generated/asset_pr import { MultiSigWalletContract } from '../contract_wrappers/generated/multi_sig_wallet'; import { constants } from './constants'; -import { logDecoder } from './log_decoder'; +import { LogDecoder } from './log_decoder'; export class MultiSigWrapper { private _multiSig: MultiSigWalletContract; private _web3Wrapper: Web3Wrapper; + private _logDecoder: LogDecoder; constructor(multiSigContract: MultiSigWalletContract, provider: Provider) { this._multiSig = multiSigContract; this._web3Wrapper = new Web3Wrapper(provider); + this._logDecoder = new LogDecoder(this._web3Wrapper, this._multiSig.address); } public async submitTransactionAsync( destination: string, @@ -26,17 +28,17 @@ export class MultiSigWrapper { const txHash = await this._multiSig.submitTransaction.sendTransactionAsync(destination, value, data, { from, }); - const tx = await this._getTxWithDecodedMultiSigLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async confirmTransactionAsync(txId: BigNumber, from: string): Promise<TransactionReceiptWithDecodedLogs> { const txHash = await this._multiSig.confirmTransaction.sendTransactionAsync(txId, { from }); - const tx = await this._getTxWithDecodedMultiSigLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async executeTransactionAsync(txId: BigNumber, from: string): Promise<TransactionReceiptWithDecodedLogs> { const txHash = await this._multiSig.executeTransaction.sendTransactionAsync(txId, { from }); - const tx = await this._getTxWithDecodedMultiSigLogsAsync(txHash); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } public async executeRemoveAuthorizedAddressAsync( @@ -45,13 +47,7 @@ export class MultiSigWrapper { ): Promise<TransactionReceiptWithDecodedLogs> { const txHash = await (this ._multiSig as AssetProxyOwnerContract).executeRemoveAuthorizedAddress.sendTransactionAsync(txId, { from }); - const tx = await this._getTxWithDecodedMultiSigLogsAsync(txHash); - return tx; - } - private async _getTxWithDecodedMultiSigLogsAsync(txHash: string): Promise<TransactionReceiptWithDecodedLogs> { - const tx = await this._web3Wrapper.awaitTransactionMinedAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); - tx.logs = _.filter(tx.logs, log => log.address === this._multiSig.address); - tx.logs = _.map(tx.logs, log => logDecoder.decodeLogOrThrow(log)); + const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } } |