aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-12-24 12:50:02 +0800
committerGreg Hysen <greg.hysen@gmail.com>2019-01-15 02:49:44 +0800
commit86caa4a0bb82e3ba7d362cca9b75565bf8944952 (patch)
tree1cddd589130d6844dcb6824d1b4bcf588a8719b5 /packages/utils/src
parent6d832deb62cdef38c5fa69169986ef00dbc6e1eb (diff)
downloaddexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.tar
dexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.tar.gz
dexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.tar.bz2
dexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.tar.lz
dexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.tar.xz
dexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.tar.zst
dexon-0x-contracts-86caa4a0bb82e3ba7d362cca9b75565bf8944952.zip
Ran prettier and linter
Diffstat (limited to 'packages/utils/src')
-rw-r--r--packages/utils/src/abi_encoder/abstract_data_types/data_type.ts8
-rw-r--r--packages/utils/src/abi_encoder/abstract_data_types/types/blob.ts2
-rw-r--r--packages/utils/src/abi_encoder/abstract_data_types/types/set.ts1
-rw-r--r--packages/utils/src/abi_encoder/evm_data_type_factory.ts14
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/array.ts10
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/int.ts3
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/method.ts1
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/tuple.ts10
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types/uint.ts3
-rw-r--r--packages/utils/src/abi_encoder/utils/signatureParser.ts42
10 files changed, 50 insertions, 44 deletions
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 '<type>' is interpreted as a `DataItem`
+ * For example, 'string' is interpreted as {type: 'string'}
+ * A signature in the form '(<type1>, <type2>, ..., <typen>)' 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: '<DataItem.type>' or '(<DataItem1.type>, <DataItem2.type>, ...)
+ * 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 {