diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-26 09:46:45 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | 22ce3e2e29fb50d9b9244c9ee567c124586be9ae (patch) | |
tree | 780740b35250e2d19eea17090c858a4cf2812109 /packages | |
parent | acd364b71c8b3ddb6d4d75d8667cc7f50b18694d (diff) | |
download | dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.tar dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.tar.gz dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.tar.bz2 dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.tar.lz dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.tar.xz dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.tar.zst dexon-0x-contracts-22ce3e2e29fb50d9b9244c9ee567c124586be9ae.zip |
Comments for Array
Diffstat (limited to 'packages')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/array.ts | 39 |
1 files changed, 23 insertions, 16 deletions
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 527cdadfe..77e38ebd7 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -12,21 +12,26 @@ export class Array extends MemberDataType { return Array._MATCHER.test(type); } - public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - // Sanity check - const matches = Array._MATCHER.exec(dataItem.type); + private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined|number] { + const matches = Array._MATCHER.exec(type); if (matches === null || matches.length !== 3) { - throw new Error(`Could not parse array: ${dataItem.type}`); + throw new Error(`Could not parse array: ${type}`); } else if (matches[1] === undefined) { - throw new Error(`Could not parse array type: ${dataItem.type}`); + throw new Error(`Could not parse array type: ${type}`); } else if (matches[2] === undefined) { - throw new Error(`Could not parse array length: ${dataItem.type}`); + throw new Error(`Could not parse array length: ${type}`); } - - const isArray = true; const arrayElementType = matches[1]; const arrayLength = matches[2] === '' ? undefined : parseInt(matches[2], Constants.DEC_BASE); + return [arrayElementType, arrayLength]; + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + // Construct parent + const isArray = true; + const [arrayElementType, arrayLength] = Array._decodeElementTypeAndLengthFromType(dataItem.type); super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); + // Set array properties this._elementType = arrayElementType; this._arraySignature = this._computeSignature(); } @@ -36,20 +41,22 @@ export class Array extends MemberDataType { } private _computeSignature(): string { - const dataItem: DataItem = { + // Compute signature for a single array element + const elementDataItem: DataItem = { type: this._elementType, name: 'N/A', }; - const components = this.getDataItem().components; - if (components !== undefined) { - dataItem.components = components; + const elementComponents = this.getDataItem().components; + if (elementComponents !== undefined) { + elementDataItem.components = elementComponents; } - const elementDataType = this.getFactory().create(dataItem); - const type = elementDataType.getSignature(); + const elementDataType = this.getFactory().create(elementDataItem); + const elementSignature = elementDataType.getSignature(); + // Construct signature for array of type `element` if (this._arrayLength === undefined) { - return `${type}[]`; + return `${elementSignature}[]`; } else { - return `${type}[${this._arrayLength}]`; + return `${elementSignature}[${this._arrayLength}]`; } } } |