diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-30 07:45:35 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-30 07:45:35 +0800 |
commit | 702dbbae54ae2fba3330c7dcda68ad6ffe64abc0 (patch) | |
tree | e5f6da99570de8d0f49695f944e92b2423de0b85 /packages/utils/src/abi_encoder/evm_data_type_factory.ts | |
parent | 856f4b473b54ecb3dc707ea334890397c98606bf (diff) | |
parent | fc3641b49938ea9dc5e77e633e798ca19aa812ff (diff) | |
download | dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.tar dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.tar.gz dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.tar.bz2 dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.tar.lz dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.tar.xz dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.tar.zst dexon-sol-tools-702dbbae54ae2fba3330c7dcda68ad6ffe64abc0.zip |
Merge branch 'development' into feature/instant/misc-close-analytics
Diffstat (limited to 'packages/utils/src/abi_encoder/evm_data_type_factory.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_type_factory.ts | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_type_factory.ts b/packages/utils/src/abi_encoder/evm_data_type_factory.ts new file mode 100644 index 000000000..4cc124e0a --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_type_factory.ts @@ -0,0 +1,132 @@ +/* tslint:disable max-classes-per-file */ +import { DataItem, MethodAbi } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { DataType } from './abstract_data_types/data_type'; +import { DataTypeFactory } from './abstract_data_types/interfaces'; +import { AddressDataType } from './evm_data_types/address'; +import { ArrayDataType } from './evm_data_types/array'; +import { BoolDataType } from './evm_data_types/bool'; +import { DynamicBytesDataType } from './evm_data_types/dynamic_bytes'; +import { IntDataType } from './evm_data_types/int'; +import { MethodDataType } from './evm_data_types/method'; +import { PointerDataType } from './evm_data_types/pointer'; +import { StaticBytesDataType } from './evm_data_types/static_bytes'; +import { StringDataType } from './evm_data_types/string'; +import { TupleDataType } from './evm_data_types/tuple'; +import { UIntDataType } from './evm_data_types/uint'; + +export class Address extends AddressDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Bool extends BoolDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Int extends IntDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class UInt extends UIntDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class StaticBytes extends StaticBytesDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class DynamicBytes extends DynamicBytesDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class String extends StringDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Pointer extends PointerDataType { + public constructor(destDataType: DataType, parentDataType: DataType) { + super(destDataType, parentDataType, EvmDataTypeFactory.getInstance()); + } +} + +export class Tuple extends TupleDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Array extends ArrayDataType { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Method extends MethodDataType { + public constructor(abi: MethodAbi) { + super(abi, EvmDataTypeFactory.getInstance()); + } +} + +/* tslint:disable no-construct */ +export class EvmDataTypeFactory implements DataTypeFactory { + private static _instance: DataTypeFactory; + + public static getInstance(): DataTypeFactory { + if (!EvmDataTypeFactory._instance) { + EvmDataTypeFactory._instance = new EvmDataTypeFactory(); + } + return EvmDataTypeFactory._instance; + } + + /* tslint:disable prefer-function-over-method */ + public create(dataItem: DataItem, parentDataType?: DataType): DataType { + // Create data type + let dataType: undefined | DataType; + if (Array.matchType(dataItem.type)) { + dataType = new Array(dataItem); + } else if (Address.matchType(dataItem.type)) { + dataType = new Address(dataItem); + } else if (Bool.matchType(dataItem.type)) { + dataType = new Bool(dataItem); + } else if (Int.matchType(dataItem.type)) { + dataType = new Int(dataItem); + } else if (UInt.matchType(dataItem.type)) { + dataType = new UInt(dataItem); + } else if (StaticBytes.matchType(dataItem.type)) { + dataType = new StaticBytes(dataItem); + } else if (Tuple.matchType(dataItem.type)) { + dataType = new Tuple(dataItem); + } else if (DynamicBytes.matchType(dataItem.type)) { + dataType = new DynamicBytes(dataItem); + } else if (String.matchType(dataItem.type)) { + dataType = new String(dataItem); + } + // @TODO: DataTypeement Fixed/UFixed types + if (_.isUndefined(dataType)) { + throw new Error(`Unrecognized data type: '${dataItem.type}'`); + } else if (!_.isUndefined(parentDataType) && !dataType.isStatic()) { + const pointerToDataType = new Pointer(dataType, parentDataType); + return pointerToDataType; + } + return dataType; + } + /* tslint:enable prefer-function-over-method */ + + private constructor() {} +} +/* tslint:enable no-construct */ |