diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-12-04 06:48:15 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-12-04 06:48:15 +0800 |
commit | 7086bd32ee23f0931be26194eb28af8178f858eb (patch) | |
tree | 53764373a8228b00d88939fdecc6f6a1ae633c9e /packages/utils/src/abi_encoder/evm_data_types/array.ts | |
parent | 50bfbda79a312651581f03614c1b4f4cbbe49cf1 (diff) | |
parent | 50df67e7511460f051f91785bb4384485077ef60 (diff) | |
download | dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.gz dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.bz2 dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.lz dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.xz dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.zst dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/website/instant-configurator
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_types/array.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/array.ts | 64 |
1 files changed, 64 insertions, 0 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 new file mode 100644 index 000000000..7595cb667 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -0,0 +1,64 @@ +import { DataItem } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { DataTypeFactory } from '../abstract_data_types/interfaces'; +import { AbstractSetDataType } from '../abstract_data_types/types/set'; +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 { + return ArrayDataType._MATCHER.test(type); + } + + private static _decodeElementTypeAndLengthFromType(type: string): [string, undefined | number] { + const matches = ArrayDataType._MATCHER.exec(type); + if (_.isNull(matches) || matches.length !== 3) { + throw new Error(`Could not parse array: ${type}`); + } else if (_.isUndefined(matches[1])) { + throw new Error(`Could not parse array type: ${type}`); + } else if (_.isUndefined(matches[2])) { + throw new Error(`Could not parse array length: ${type}`); + } + const arrayElementType = matches[1]; + const arrayLength = _.isEmpty(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] = ArrayDataType._decodeElementTypeAndLengthFromType(dataItem.type); + super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); + // Set array properties + this._elementType = arrayElementType; + this._arraySignature = this._computeSignature(); + } + + public getSignature(): string { + return this._arraySignature; + } + + private _computeSignature(): string { + // Compute signature for a single array element + const elementDataItem: DataItem = { + type: this._elementType, + name: 'N/A', + }; + const elementComponents = this.getDataItem().components; + if (!_.isUndefined(elementComponents)) { + elementDataItem.components = elementComponents; + } + const elementDataType = this.getFactory().create(elementDataItem); + const elementSignature = elementDataType.getSignature(); + // Construct signature for array of type `element` + if (_.isUndefined(this._arrayLength)) { + return `${elementSignature}[]`; + } else { + return `${elementSignature}[${this._arrayLength}]`; + } + } +} |