From 387363283ca03ac1d6c9be5b7be2107790bbf79d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 22 Jan 2018 21:53:32 +0100 Subject: Remove truffle from tests --- packages/utils/package.json | 5 ++- packages/utils/src/abi_decoder.ts | 70 +++++++++++++++++++++++++++++++++ packages/utils/src/globals.d.ts | 3 ++ packages/utils/src/index.ts | 1 + packages/utils/src/transaction_utils.ts | 52 ++++++++++++++++++++++++ packages/utils/src/types.ts | 3 ++ 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 packages/utils/src/abi_decoder.ts create mode 100644 packages/utils/src/globals.d.ts create mode 100644 packages/utils/src/transaction_utils.ts create mode 100644 packages/utils/src/types.ts (limited to 'packages/utils') 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(log: Web3.LogEntry): LogWithDecodedArgs | 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', +} -- cgit From bc37cc8a91526b919c97eef06d9499ed285e5cfe Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 22 Jan 2018 22:05:30 +0100 Subject: Remove duplicate code --- packages/utils/src/transaction_utils.ts | 52 --------------------------------- packages/utils/src/types.ts | 3 -- 2 files changed, 55 deletions(-) delete mode 100644 packages/utils/src/transaction_utils.ts delete mode 100644 packages/utils/src/types.ts (limited to 'packages/utils') diff --git a/packages/utils/src/transaction_utils.ts b/packages/utils/src/transaction_utils.ts deleted file mode 100644 index a1db90817..000000000 --- a/packages/utils/src/transaction_utils.ts +++ /dev/null @@ -1,52 +0,0 @@ -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 deleted file mode 100644 index 936256b61..000000000 --- a/packages/utils/src/types.ts +++ /dev/null @@ -1,3 +0,0 @@ -export enum TransactionError { - TransactionMiningTimeout = 'TRANSACTION_MINING_TIMEOUT', -} -- cgit From 1e9147b8bb45a251eefdead23573a9e8d68fc34b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 Jan 2018 15:12:46 +0100 Subject: Normalize the dependencies --- packages/utils/package.json | 4 ++-- packages/utils/src/abi_decoder.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'packages/utils') diff --git a/packages/utils/package.json b/packages/utils/package.json index 959dd8d21..db82f8040 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -20,12 +20,12 @@ "homepage": "https://github.com/0xProject/0x.js/packages/utils/README.md", "devDependencies": { "@0xproject/tslint-config": "^0.4.5", - "@0xproject/types": "^0.1.4", + "@0xproject/types": "^0.1.5", "@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", + "web3-typescript-typings": "^0.9.5", "typescript": "~2.6.1" }, "dependencies": { diff --git a/packages/utils/src/abi_decoder.ts b/packages/utils/src/abi_decoder.ts index 574902de6..d6584d44d 100644 --- a/packages/utils/src/abi_decoder.ts +++ b/packages/utils/src/abi_decoder.ts @@ -3,6 +3,7 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; import * as SolidityCoder from 'web3/lib/solidity/coder'; +// tslint:disable-next-line:no-unused-variable import { BigNumber } from './configured_bignumber'; export class AbiDecoder { -- cgit From c02dfc4fb1bf39ceaaf80ec68fb8907a4a11e0df Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 26 Jan 2018 12:57:51 +0100 Subject: Fix tslint issues --- packages/utils/src/abi_decoder.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'packages/utils') diff --git a/packages/utils/src/abi_decoder.ts b/packages/utils/src/abi_decoder.ts index d6584d44d..f96ee2edb 100644 --- a/packages/utils/src/abi_decoder.ts +++ b/packages/utils/src/abi_decoder.ts @@ -3,7 +3,6 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; import * as SolidityCoder from 'web3/lib/solidity/coder'; -// tslint:disable-next-line:no-unused-variable import { BigNumber } from './configured_bignumber'; export class AbiDecoder { @@ -39,7 +38,7 @@ export class AbiDecoder { _.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++]; + let value: BigNumber | string = param.indexed ? log.topics[topicsIndex++] : decodedData[dataIndex++]; if (param.type === SolidityTypes.Address) { value = AbiDecoder._padZeros(new BigNumber(value).toString(16)); } else if ( -- cgit From 09659cc3041e8662e64beab2728477f3437c1ced Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Feb 2018 12:30:41 +0100 Subject: Add build:watch command to all TS packages --- packages/utils/package.json | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/utils') diff --git a/packages/utils/package.json b/packages/utils/package.json index db82f8040..65ee69805 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -5,6 +5,7 @@ "main": "lib/index.js", "types": "lib/index.d.ts", "scripts": { + "build:watch": "tsc -w", "build": "tsc", "clean": "shx rm -rf lib", "lint": "tslint --project . 'src/**/*.ts'" -- cgit From 39e3733be4b2a8bb6f163c0aa02fbe57db213e61 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Feb 2018 12:50:55 +0100 Subject: Upgrade TS to the newest version --- packages/utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils') diff --git a/packages/utils/package.json b/packages/utils/package.json index 65ee69805..99d9fbd2b 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -27,7 +27,7 @@ "shx": "^0.2.2", "tslint": "5.8.0", "web3-typescript-typings": "^0.9.5", - "typescript": "~2.6.1" + "typescript": "2.7.1" }, "dependencies": { "bignumber.js": "~4.1.0", -- cgit From d4631e14b2203bfd95b995d25819d8d9cb834336 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Feb 2018 16:39:06 +0100 Subject: Publish - 0x.js@0.31.1 - @0xproject/abi-gen@0.1.6 - @0xproject/assert@0.0.15 - chai-as-promised-typescript-typings@0.0.8 - @0xproject/connect@0.5.4 - contracts@2.1.8 - @0xproject/deployer@0.0.5 - @0xproject/dev-utils@0.0.9 - @0xproject/json-schemas@0.7.7 - @0xproject/monorepo-scripts@0.1.8 - @0xproject/subproviders@0.3.5 - @0xproject/testnet-faucets@1.0.9 - @0xproject/tslint-config@0.4.6 - @0xproject/types@0.1.8 - @0xproject/utils@0.2.4 - web3-typescript-typings@0.9.8 - @0xproject/web3-wrapper@0.1.9 - @0xproject/website@0.0.11 --- packages/utils/package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'packages/utils') diff --git a/packages/utils/package.json b/packages/utils/package.json index db82f8040..5c03826ba 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@0xproject/utils", - "version": "0.2.3", + "version": "0.2.4", "description": "0x TS utils", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -19,14 +19,14 @@ }, "homepage": "https://github.com/0xProject/0x.js/packages/utils/README.md", "devDependencies": { - "@0xproject/tslint-config": "^0.4.5", - "@0xproject/types": "^0.1.5", + "@0xproject/tslint-config": "^0.4.6", + "@0xproject/types": "^0.1.8", "@types/lodash": "^4.14.86", "npm-run-all": "^4.1.2", "shx": "^0.2.2", "tslint": "5.8.0", - "web3-typescript-typings": "^0.9.5", - "typescript": "~2.6.1" + "typescript": "~2.6.1", + "web3-typescript-typings": "^0.9.8" }, "dependencies": { "bignumber.js": "~4.1.0", -- cgit From 45ac9603083c38dbab6fb6eff26c744eea2b4429 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Feb 2018 12:42:19 +0100 Subject: Add build:watch to README's --- packages/utils/README.md | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'packages/utils') diff --git a/packages/utils/README.md b/packages/utils/README.md index d6cacfa11..ffb0d0190 100644 --- a/packages/utils/README.md +++ b/packages/utils/README.md @@ -40,6 +40,12 @@ yarn install yarn build ``` +or + +```bash +yarn build:watch +``` + ### Lint ```bash -- cgit From 85b4a82a4bc1059b3321763e20b8e2c4d8ff0e73 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 5 Feb 2018 12:03:13 +0100 Subject: Add missing comas --- packages/utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils') diff --git a/packages/utils/package.json b/packages/utils/package.json index dc0a79f0a..e276d5e5a 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -26,7 +26,7 @@ "npm-run-all": "^4.1.2", "shx": "^0.2.2", "tslint": "5.8.0", - "typescript": "2.7.1" + "typescript": "2.7.1", "web3-typescript-typings": "^0.9.8" }, "dependencies": { -- cgit