diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-21 05:40:46 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | fee67326adbbf745966803c935d5663c3ca7f52c (patch) | |
tree | 752d17d251026abf2a774992a79b2e9030957161 /packages/utils/src/abi_encoder/abstract_data_types/data_type.ts | |
parent | a47901370bc69d6247e941c569bc9fe824516db9 (diff) | |
download | dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.tar dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.tar.gz dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.tar.bz2 dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.tar.lz dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.tar.xz dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.tar.zst dexon-0x-contracts-fee67326adbbf745966803c935d5663c3ca7f52c.zip |
Merge above
Diffstat (limited to 'packages/utils/src/abi_encoder/abstract_data_types/data_type.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/abstract_data_types/data_type.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts new file mode 100644 index 000000000..a6adeb23b --- /dev/null +++ b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts @@ -0,0 +1,51 @@ +import { DataItem } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { Calldata, CalldataBlock, RawCalldata } from '../calldata'; +import { DecodingRules, EncodingRules } from '../utils/rules'; + +import { DataTypeFactory } from './interfaces'; + +export abstract class DataType { + private static readonly _DEFAULT_ENCODING_RULES: EncodingRules = { optimize: false, annotate: false }; + private static readonly _DEFAULT_DECODING_RULES: DecodingRules = { structsAsObjects: false }; + private readonly _dataItem: DataItem; + private readonly _factory: DataTypeFactory; + + constructor(dataItem: DataItem, factory: DataTypeFactory) { + this._dataItem = dataItem; + this._factory = factory; + } + + public getDataItem(): DataItem { + return this._dataItem; + } + + public getFactory(): DataTypeFactory { + return this._factory; + } + + public encode(value: any, rules?: EncodingRules, selector?: string): string { + const rules_ = rules ? rules : DataType._DEFAULT_ENCODING_RULES; + const calldata = new Calldata(rules_); + if (selector) { + calldata.setSelector(selector); + } + const block = this.generateCalldataBlock(value); + calldata.setRoot(block); // @TODO CHANGE + const calldataHex = calldata.toHexString(); + return calldataHex; + } + + public decode(calldata: string, rules?: DecodingRules, hasSelector: boolean = false): any { + const rawCalldata = new RawCalldata(calldata, hasSelector); + const rules_ = rules ? rules : DataType._DEFAULT_DECODING_RULES; + const value = this.generateValue(rawCalldata, rules_); + return value; + } + + public abstract generateCalldataBlock(value: any, parentBlock?: CalldataBlock): CalldataBlock; + public abstract generateValue(calldata: RawCalldata, rules: DecodingRules): any; + public abstract getSignature(): string; + public abstract isStatic(): boolean; +} |