From 86caa4a0bb82e3ba7d362cca9b75565bf8944952 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sun, 23 Dec 2018 20:50:02 -0800 Subject: Ran prettier and linter --- .../abi_encoder/abstract_data_types/data_type.ts | 8 +++-- .../abi_encoder/abstract_data_types/types/blob.ts | 2 -- .../abi_encoder/abstract_data_types/types/set.ts | 1 - .../utils/src/abi_encoder/evm_data_type_factory.ts | 14 ++++++-- .../utils/src/abi_encoder/evm_data_types/array.ts | 10 +++--- .../utils/src/abi_encoder/evm_data_types/int.ts | 3 +- .../utils/src/abi_encoder/evm_data_types/method.ts | 1 - .../utils/src/abi_encoder/evm_data_types/tuple.ts | 10 +++--- .../utils/src/abi_encoder/evm_data_types/uint.ts | 3 +- .../utils/src/abi_encoder/utils/signatureParser.ts | 42 +++++++++------------- 10 files changed, 50 insertions(+), 44 deletions(-) (limited to 'packages/utils/src/abi_encoder') diff --git a/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts index dc897d810..d9e854f51 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts @@ -58,9 +58,13 @@ export abstract class DataType { } public getSignature(detailed?: boolean): string { - if (_.isEmpty(this._dataItem.name) || !detailed) return this.getSignatureType(); + if (_.isEmpty(this._dataItem.name) || !detailed) { + return this.getSignatureType(); + } const name = this.getDataItem().name; - const shortName = name.indexOf('.') > 0 ? name.substr(name.lastIndexOf('.') + 1) : name; + const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); + const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; + const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; const detailedSignature = `${shortName} ${this.getSignatureType()}`; return detailedSignature; } diff --git a/packages/utils/src/abi_encoder/abstract_data_types/types/blob.ts b/packages/utils/src/abi_encoder/abstract_data_types/types/blob.ts index 4252b782c..a091e55b9 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/types/blob.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/types/blob.ts @@ -9,8 +9,6 @@ import { DecodingRules } from '../../utils/rules'; import { DataType } from '../data_type'; import { DataTypeFactory } from '../interfaces'; -import * as ethUtil from 'ethereumjs-util'; - export abstract class AbstractBlobDataType extends DataType { protected _sizeKnownAtCompileTime: boolean; diff --git a/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts b/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts index 28d11c4b5..e4be96235 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts @@ -13,7 +13,6 @@ import { DataType } from '../data_type'; import { DataTypeFactory, MemberIndexByName } from '../interfaces'; import { AbstractPointerDataType } from './pointer'; -import { logUtils } from '../../../log_utils'; export abstract class AbstractSetDataType extends DataType { protected readonly _arrayLength: number | undefined; diff --git a/packages/utils/src/abi_encoder/evm_data_type_factory.ts b/packages/utils/src/abi_encoder/evm_data_type_factory.ts index dcfcc9c5a..6d59ec01a 100644 --- a/packages/utils/src/abi_encoder/evm_data_type_factory.ts +++ b/packages/utils/src/abi_encoder/evm_data_type_factory.ts @@ -132,15 +132,23 @@ export class EvmDataTypeFactory implements DataTypeFactory { private constructor() {} } -// Convenience function +/** + * Convenience function for creating a DataType from different inputs. + * @param input A single or set of DataItem or a DataType signature. + * A signature in the form of '' is interpreted as a `DataItem` + * For example, 'string' is interpreted as {type: 'string'} + * A signature in the form '(, , ..., )' is interpreted as `DataItem[]` + * For eaxmple, '(string, uint256)' is interpreted as [{type: 'string'}, {type: 'uint256'}] + * @return DataType corresponding to input. + */ export function create(input: DataItem | DataItem[] | string): DataType { // Handle different types of input const isSignature = typeof input === 'string'; const isTupleSignature = isSignature && (input as string).startsWith('('); - const parseAsTuple = isTupleSignature || _.isArray(input); + const shouldParseAsTuple = isTupleSignature || _.isArray(input); // Create input `dataItem` let dataItem: DataItem; - if (parseAsTuple) { + if (shouldParseAsTuple) { const dataItems = isSignature ? generateDataItemsFromSignature(input as string) : (input as DataItem[]); dataItem = { name: '', diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 8b71dc913..449de0568 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -7,7 +7,6 @@ import { constants } from '../utils/constants'; export class ArrayDataType extends AbstractSetDataType { private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); - private readonly _arraySignature: string; private readonly _elementType: string; public static matchType(type: string): boolean { @@ -35,7 +34,6 @@ export class ArrayDataType extends AbstractSetDataType { super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); // Set array properties this._elementType = arrayElementType; - this._arraySignature = this._computeSignature(); } public getSignatureType(): string { @@ -43,9 +41,13 @@ export class ArrayDataType extends AbstractSetDataType { } public getSignature(detailed?: boolean): string { - if (_.isEmpty(this.getDataItem().name) || !detailed) return this.getSignatureType(); + if (_.isEmpty(this.getDataItem().name) || !detailed) { + return this.getSignatureType(); + } const name = this.getDataItem().name; - const shortName = name.indexOf('.') > 0 ? name.substr(name.lastIndexOf('.') + 1) : name; + const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); + const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; + const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; const detailedSignature = `${shortName} ${this._computeSignature(detailed)}`; return detailedSignature; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index 144a0eb6a..8d98e195b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -50,7 +50,8 @@ export class IntDataType extends AbstractBlobDataType { public decodeValue(calldata: RawCalldata): BigNumber | number { const valueBuf = calldata.popWord(); const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); - if (this._width === 8) { + const numberOfBytesInUint8 = 8; + if (this._width === numberOfBytesInUint8) { return value.toNumber(); } return value; diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 44456cd0a..6e3fdcf6d 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -8,7 +8,6 @@ import { AbstractSetDataType } from '../abstract_data_types/types/set'; import { constants } from '../utils/constants'; import { DecodingRules, EncodingRules } from '../utils/rules'; -import { ArrayDataType } from './array'; import { TupleDataType } from './tuple'; export class MethodDataType extends AbstractSetDataType { diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 54964235c..3d1cb5a92 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -5,7 +5,6 @@ import { DataTypeFactory } from '../abstract_data_types/interfaces'; import { AbstractSetDataType } from '../abstract_data_types/types/set'; export class TupleDataType extends AbstractSetDataType { - //private readonly _signature: string; public static matchType(type: string): boolean { return type === SolidityTypes.Tuple; @@ -16,7 +15,6 @@ export class TupleDataType extends AbstractSetDataType { if (!TupleDataType.matchType(dataItem.type)) { throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); } - //this._signature = } public getSignatureType(): string { @@ -24,9 +22,13 @@ export class TupleDataType extends AbstractSetDataType { } public getSignature(detailed?: boolean): string { - if (_.isEmpty(this.getDataItem().name) || !detailed) return this.getSignatureType(); + if (_.isEmpty(this.getDataItem().name) || !detailed) { + return this.getSignatureType(); + } const name = this.getDataItem().name; - const shortName = name.indexOf('.') > 0 ? name.substr(name.lastIndexOf('.') + 1) : name; + const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); + const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; + const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; const detailedSignature = `${shortName} ${this._computeSignatureOfMembers(detailed)}`; return detailedSignature; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 940fda072..8e382e8dc 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -49,7 +49,8 @@ export class UIntDataType extends AbstractBlobDataType { public decodeValue(calldata: RawCalldata): BigNumber | number { const valueBuf = calldata.popWord(); const value = EncoderMath.safeDecodeNumericValue(valueBuf, UIntDataType._MIN_VALUE, this._maxValue); - if (this._width === 8) { + const numberOfBytesInUint8 = 8; + if (this._width === numberOfBytesInUint8) { return value.toNumber(); } return value; diff --git a/packages/utils/src/abi_encoder/utils/signatureParser.ts b/packages/utils/src/abi_encoder/utils/signatureParser.ts index c4796e24e..315784cea 100644 --- a/packages/utils/src/abi_encoder/utils/signatureParser.ts +++ b/packages/utils/src/abi_encoder/utils/signatureParser.ts @@ -1,21 +1,15 @@ +import { DataItem } from 'ethereum-types'; import * as _ from 'lodash'; -import { DataItem } from 'ethereum-protocol'; - -/* -export function generateDataItemFromSignature(signature: string): DataItem { - const dataItems = generateDataItemsFromSignature(signature); - if (dataItems.length === 1) { - return dataItems[0]; - } - // signature represents a tuple - return { - name: '', - type: 'tuple', - components: dataItems - }; -}*/ - +/** + * Returns an array of DataItem's corresponding to the input signature. + * A signature can be in two forms: '' or '(, , ...) + * An example of the first form would be 'address' or 'uint256' + * An example of the second form would be '(address, uint256)' + * Signatures can also include a name field, for example: 'foo address' or '(foo address, bar uint256)' + * @param signature of input DataItems + * @return DataItems derived from input signature + */ export function generateDataItemsFromSignature(signature: string): DataItem[] { let trimmedSignature = signature; if (signature.startsWith('(')) { @@ -25,7 +19,7 @@ export function generateDataItemsFromSignature(signature: string): DataItem[] { trimmedSignature = signature.substr(1, signature.length - 2); } trimmedSignature += ','; - let currTokenIsArray = false; + let isCurrTokenArray = false; let currTokenArrayModifier = ''; let isParsingArrayModifier = false; let currToken = ''; @@ -46,7 +40,7 @@ export function generateDataItemsFromSignature(signature: string): DataItem[] { case '[': if (parenCount === 0) { isParsingArrayModifier = true; - currTokenIsArray = true; + isCurrTokenArray = true; currTokenArrayModifier += '['; } else { currToken += char; @@ -70,26 +64,24 @@ export function generateDataItemsFromSignature(signature: string): DataItem[] { break; case ',': if (parenCount === 0) { - //throw new Error(`Generating Data Items`); + // Generate new DataItem from token const components = currToken.startsWith('(') ? generateDataItemsFromSignature(currToken) : []; const isTuple = !_.isEmpty(components); - const isArray = currTokenIsArray; - let dataItem: DataItem = { name: currTokenName, type: '' }; + const dataItem: DataItem = { name: currTokenName, type: '' }; if (isTuple) { dataItem.type = 'tuple'; dataItem.components = components; } else { dataItem.type = currToken; } - if (isArray) { + if (isCurrTokenArray) { dataItem.type += currTokenArrayModifier; } - dataItems.push(dataItem); - + // reset token state currTokenName = ''; currToken = ''; - currTokenIsArray = false; + isCurrTokenArray = false; currTokenArrayModifier = ''; break; } else { -- cgit v1.2.3