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_type_factory.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_type_factory.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_type_factory.ts | 124 |
1 files changed, 124 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..0f8dfb4a3 --- /dev/null +++ b/packages/utils/src/abi_encoder/evm_data_type_factory.ts @@ -0,0 +1,124 @@ +/* tslint:disable prefer-function-over-method */ +/* tslint:disable max-classes-per-file */ +/* tslint:disable no-construct */ +import { DataItem, MethodAbi } from 'ethereum-types'; + +import { DataType, DataTypeFactory } from './data_type'; +import * as Impl from './evm_data_types'; + +export class Address extends Impl.Address { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Bool extends Impl.Bool { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Int extends Impl.Int { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class UInt extends Impl.UInt { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class StaticBytes extends Impl.StaticBytes { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class DynamicBytes extends Impl.DynamicBytes { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class String extends Impl.String { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Pointer extends Impl.Pointer { + public constructor(destDataType: DataType, parentDataType: DataType) { + super(destDataType, parentDataType, EvmDataTypeFactory.getInstance()); + } +} + +export class Tuple extends Impl.Tuple { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Array extends Impl.Array { + public constructor(dataItem: DataItem) { + super(dataItem, EvmDataTypeFactory.getInstance()); + } +} + +export class Method extends Impl.Method { + public constructor(abi: MethodAbi) { + super(abi, EvmDataTypeFactory.getInstance()); + } +} + +export class EvmDataTypeFactory implements DataTypeFactory { + private static _instance: DataTypeFactory; + + public static getInstance(): DataTypeFactory { + if (!EvmDataTypeFactory._instance) { + EvmDataTypeFactory._instance = new EvmDataTypeFactory(); + } + return EvmDataTypeFactory._instance; + } + + public mapDataItemToDataType(dataItem: DataItem): DataType { + if (Array.matchType(dataItem.type)) { + return new Array(dataItem); + } else if (Address.matchType(dataItem.type)) { + return new Address(dataItem); + } else if (Bool.matchType(dataItem.type)) { + return new Bool(dataItem); + } else if (Int.matchType(dataItem.type)) { + return new Int(dataItem); + } else if (UInt.matchType(dataItem.type)) { + return new UInt(dataItem); + } else if (StaticBytes.matchType(dataItem.type)) { + return new StaticBytes(dataItem); + } else if (Tuple.matchType(dataItem.type)) { + return new Tuple(dataItem); + } else if (DynamicBytes.matchType(dataItem.type)) { + return new DynamicBytes(dataItem); + } else if (String.matchType(dataItem.type)) { + return new String(dataItem); + } + // @TODO: Implement Fixed/UFixed types + throw new Error(`Unrecognized data type: '${dataItem.type}'`); + } + + public create(dataItem: DataItem, parentDataType?: DataType): DataType { + const dataType = this.mapDataItemToDataType(dataItem); + if (dataType.isStatic()) { + return dataType; + } + + if (parentDataType === undefined) { + // @Todo -- will this work for return values? + throw new Error(`Trying to create a pointer`); + } + const pointer = new Pointer(dataType, parentDataType); + return pointer; + } + + private constructor() { } +} |