diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-21 04:58:49 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | e6ab6f38bacdec90c960ff1db4781d161b1f4103 (patch) | |
tree | 7edec03e3f249edf49b9deafcc44416bf25f7dc0 /packages/utils/src/abi_encoder/evm_data_types/array.ts | |
parent | 5934e5a57b5897594e22efc9e6ff28841c6951bf (diff) | |
download | dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.gz dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.bz2 dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.lz dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.xz dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.tar.zst dexon-0x-contracts-e6ab6f38bacdec90c960ff1db4781d161b1f4103.zip |
Split EVM data types and factory into separate files
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 | 55 |
1 files changed, 55 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..707af7c7e --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -0,0 +1,55 @@ +import { DataItem } from 'ethereum-types'; + +import * as Constants from '../constants'; +import { DataTypeFactory, MemberDataType } from '../data_type'; + +export class Array extends MemberDataType { + private static readonly _matcher = RegExp('^(.+)\\[([0-9]*)\\]$'); + private readonly _arraySignature: string; + private readonly _elementType: string; + + public static matchType(type: string): boolean { + return Array._matcher.test(type); + } + + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { + // Sanity check + const matches = Array._matcher.exec(dataItem.type); + if (matches === null || matches.length !== 3) { + throw new Error(`Could not parse array: ${dataItem.type}`); + } else if (matches[1] === undefined) { + throw new Error(`Could not parse array type: ${dataItem.type}`); + } else if (matches[2] === undefined) { + throw new Error(`Could not parse array length: ${dataItem.type}`); + } + + const isArray = true; + const arrayElementType = matches[1]; + const arrayLength = matches[2] === '' ? undefined : parseInt(matches[2], Constants.DEC_BASE); + super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); + this._elementType = arrayElementType; + this._arraySignature = this._computeSignature(); + } + + public getSignature(): string { + return this._arraySignature; + } + + private _computeSignature(): string { + const dataItem: DataItem = { + type: this._elementType, + name: 'N/A', + }; + const components = this.getDataItem().components; + if (components !== undefined) { + dataItem.components = components; + } + const elementDataType = this.getFactory().mapDataItemToDataType(dataItem); + const type = elementDataType.getSignature(); + if (this._arrayLength === undefined) { + return `${type}[]`; + } else { + return `${type}[${this._arrayLength}]`; + } + } +} |