diff options
Diffstat (limited to 'packages/utils')
-rw-r--r-- | packages/utils/package.json | 5 | ||||
-rw-r--r-- | packages/utils/src/abi_decoder.ts | 70 | ||||
-rw-r--r-- | packages/utils/src/globals.d.ts | 3 | ||||
-rw-r--r-- | packages/utils/src/index.ts | 1 | ||||
-rw-r--r-- | packages/utils/src/transaction_utils.ts | 52 | ||||
-rw-r--r-- | packages/utils/src/types.ts | 3 |
6 files changed, 133 insertions, 1 deletions
diff --git a/packages/utils/package.json b/packages/utils/package.json index a1cf4c24a..959dd8d21 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -20,15 +20,18 @@ "homepage": "https://github.com/0xProject/0x.js/packages/utils/README.md", "devDependencies": { "@0xproject/tslint-config": "^0.4.5", + "@0xproject/types": "^0.1.4", "@types/lodash": "^4.14.86", "npm-run-all": "^4.1.2", "shx": "^0.2.2", "tslint": "5.8.0", + "web3-typescript-typings": "^0.9.4", "typescript": "~2.6.1" }, "dependencies": { "bignumber.js": "~4.1.0", "js-sha3": "^0.7.0", - "lodash": "^4.17.4" + "lodash": "^4.17.4", + "web3": "^0.20.0" } } diff --git a/packages/utils/src/abi_decoder.ts b/packages/utils/src/abi_decoder.ts new file mode 100644 index 000000000..574902de6 --- /dev/null +++ b/packages/utils/src/abi_decoder.ts @@ -0,0 +1,70 @@ +import { AbiType, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes } from '@0xproject/types'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; +import * as SolidityCoder from 'web3/lib/solidity/coder'; + +import { BigNumber } from './configured_bignumber'; + +export class AbiDecoder { + private _savedABIs: Web3.AbiDefinition[] = []; + private _methodIds: { [signatureHash: string]: Web3.EventAbi } = {}; + private static _padZeros(address: string) { + let formatted = address; + if (_.startsWith(formatted, '0x')) { + formatted = formatted.slice(2); + } + + formatted = _.padStart(formatted, 40, '0'); + return `0x${formatted}`; + } + constructor(abiArrays: Web3.AbiDefinition[][]) { + _.map(abiArrays, this._addABI.bind(this)); + } + // This method can only decode logs from the 0x & ERC20 smart contracts + public tryToDecodeLogOrNoop<ArgsType>(log: Web3.LogEntry): LogWithDecodedArgs<ArgsType> | RawLog { + const methodId = log.topics[0]; + const event = this._methodIds[methodId]; + if (_.isUndefined(event)) { + return log; + } + const logData = log.data; + const decodedParams: DecodedLogArgs = {}; + let dataIndex = 0; + let topicsIndex = 1; + + const nonIndexedInputs = _.filter(event.inputs, input => !input.indexed); + const dataTypes = _.map(nonIndexedInputs, input => input.type); + const decodedData = SolidityCoder.decodeParams(dataTypes, logData.slice('0x'.length)); + + _.map(event.inputs, (param: Web3.EventParameter) => { + // Indexed parameters are stored in topics. Non-indexed ones in decodedData + let value = param.indexed ? log.topics[topicsIndex++] : decodedData[dataIndex++]; + if (param.type === SolidityTypes.Address) { + value = AbiDecoder._padZeros(new BigNumber(value).toString(16)); + } else if ( + param.type === SolidityTypes.Uint256 || + param.type === SolidityTypes.Uint8 || + param.type === SolidityTypes.Uint + ) { + value = new BigNumber(value); + } + decodedParams[param.name] = value; + }); + + return { + ...log, + event: event.name, + args: decodedParams, + }; + } + private _addABI(abiArray: Web3.AbiDefinition[]): void { + _.map(abiArray, (abi: Web3.AbiDefinition) => { + if (abi.type === AbiType.Event) { + const signature = `${abi.name}(${_.map(abi.inputs, input => input.type).join(',')})`; + const signatureHash = new Web3().sha3(signature); + this._methodIds[signatureHash] = abi; + } + }); + this._savedABIs = this._savedABIs.concat(abiArray); + } +} diff --git a/packages/utils/src/globals.d.ts b/packages/utils/src/globals.d.ts new file mode 100644 index 000000000..ade9e59db --- /dev/null +++ b/packages/utils/src/globals.d.ts @@ -0,0 +1,3 @@ +declare module 'web3/lib/solidity/coder' { + const decodeParams: (types: string[], data: string) => any[]; +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 2768e49ab..39dede41f 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -3,3 +3,4 @@ export { addressUtils } from './address_utils'; export { classUtils } from './class_utils'; export { intervalUtils } from './interval_utils'; export { BigNumber } from './configured_bignumber'; +export { AbiDecoder } from './abi_decoder'; diff --git a/packages/utils/src/transaction_utils.ts b/packages/utils/src/transaction_utils.ts new file mode 100644 index 000000000..a1db90817 --- /dev/null +++ b/packages/utils/src/transaction_utils.ts @@ -0,0 +1,52 @@ +import { AbiDecoder } from '@0xproject/abi-decoder'; +import { TransactionReceiptWithDecodedLogs } from '@0xproject/types'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as _ from 'lodash'; + +import { intervalUtils } from './interval_utils'; +import { TransactionError } from './types'; + +export const awaitTransactionMinedAsync = async ( + web3Wrapper: Web3Wrapper, + abiDecoder: AbiDecoder, + txHash: string, + pollingIntervalMs = 1000, + timeoutMs?: number, +) => { + let timeoutExceeded = false; + if (timeoutMs) { + setTimeout(() => (timeoutExceeded = true), timeoutMs); + } + + const txReceiptPromise = new Promise((resolve: (receipt: TransactionReceiptWithDecodedLogs) => void, reject) => { + const intervalId = intervalUtils.setAsyncExcludingInterval( + async () => { + if (timeoutExceeded) { + intervalUtils.clearAsyncExcludingInterval(intervalId); + return reject(TransactionError.TransactionMiningTimeout); + } + + const transactionReceipt = await web3Wrapper.getTransactionReceiptAsync(txHash); + if (!_.isNull(transactionReceipt)) { + intervalUtils.clearAsyncExcludingInterval(intervalId); + const logsWithDecodedArgs = _.map( + transactionReceipt.logs, + abiDecoder.tryToDecodeLogOrNoop.bind(abiDecoder), + ); + const transactionReceiptWithDecodedLogArgs: TransactionReceiptWithDecodedLogs = { + ...transactionReceipt, + logs: logsWithDecodedArgs, + }; + resolve(transactionReceiptWithDecodedLogArgs); + } + }, + pollingIntervalMs, + (err: Error) => { + intervalUtils.clearAsyncExcludingInterval(intervalId); + reject(err); + }, + ); + }); + + return txReceiptPromise; +}; diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts new file mode 100644 index 000000000..936256b61 --- /dev/null +++ b/packages/utils/src/types.ts @@ -0,0 +1,3 @@ +export enum TransactionError { + TransactionMiningTimeout = 'TRANSACTION_MINING_TIMEOUT', +} |