diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-26 07:38:29 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | dd8bb6d08b6e837304a76e9707b79e070f951e4e (patch) | |
tree | 959c99e619d9ca8bd20e89d7fbfcf03c132b8b0e /packages/utils | |
parent | dc7092e1eb11ff9844efe02e367ef37592f38c55 (diff) | |
download | dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.tar dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.tar.gz dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.tar.bz2 dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.tar.lz dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.tar.xz dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.tar.zst dexon-sol-tools-dd8bb6d08b6e837304a76e9707b79e070f951e4e.zip |
Made default encoding/decoding rules global to all modules in encoder
Diffstat (limited to 'packages/utils')
3 files changed, 18 insertions, 16 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 index c83f2085e..dd166b19c 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts @@ -2,13 +2,12 @@ import { DataItem } from 'ethereum-types'; import * as _ from 'lodash'; import { Calldata, CalldataBlock, RawCalldata } from '../calldata'; +import * as Constants from '../utils/constants'; 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; @@ -26,7 +25,7 @@ export abstract class DataType { } public encode(value: any, rules?: EncodingRules, selector?: string): string { - const rules_ = rules ? rules : DataType._DEFAULT_ENCODING_RULES; + const rules_ = rules ? rules : Constants.DEFAULT_ENCODING_RULES; const calldata = new Calldata(rules_); if (selector) { calldata.setSelector(selector); @@ -39,7 +38,7 @@ export abstract class DataType { 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 rules_ = rules ? rules : Constants.DEFAULT_DECODING_RULES; const value = this.generateValue(rawCalldata, rules_); return value; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 671b80890..2faffd44e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -10,30 +10,28 @@ import { DecodingRules, EncodingRules } from '../utils/rules'; import { Tuple } from './tuple'; export class Method extends MemberDataType { - // TMP - public selector: string; - private readonly _methodSignature: string; private readonly _methodSelector: string; private readonly _returnDataType: DataType; public constructor(abi: MethodAbi, dataTypeFactory: DataTypeFactory) { - super({ type: 'method', name: abi.name, components: abi.inputs }, dataTypeFactory); + const methodDataItem = { type: 'method', name: abi.name, components: abi.inputs }; + super(methodDataItem, dataTypeFactory); this._methodSignature = this._computeSignature(); - this.selector = this._methodSelector = this._computeSelector(); + this._methodSelector = this._computeSelector(); const returnDataItem: DataItem = { type: 'tuple', name: abi.name, components: abi.outputs }; this._returnDataType = new Tuple(returnDataItem, this.getFactory()); } public encode(value: any, rules?: EncodingRules): string { - const calldata = super.encode(value, rules, this.selector); + const calldata = super.encode(value, rules, this._methodSelector); return calldata; } public decode(calldata: string, rules?: DecodingRules): any[] | object { - if (!calldata.startsWith(this.selector)) { + if (!calldata.startsWith(this._methodSelector)) { throw new Error( - `Tried to decode calldata, but it was missing the function selector. Expected '${this.selector}'.`, + `Tried to decode calldata, but it was missing the function selector. Expected '${this._methodSelector}'.`, ); } const hasSelector = true; @@ -46,10 +44,11 @@ export class Method extends MemberDataType { return returnData; } - public decodeReturnValues(returndata: string, rules?: DecodingRules): any { - const rules_: DecodingRules = rules ? rules : { structsAsObjects: false }; - const rawReturnData = new RawCalldata(returndata, false); - const returnValues = this._returnDataType.generateValue(rawReturnData, rules_); + public decodeReturnValues(returndata: string, rules_?: DecodingRules): any { + const rules: DecodingRules = rules_ ? rules_ : Constants.DEFAULT_DECODING_RULES; + const returnDataHasSelector = false; + const rawReturnData = new RawCalldata(returndata, returnDataHasSelector); + const returnValues = this._returnDataType.generateValue(rawReturnData, rules); return returnValues; } diff --git a/packages/utils/src/abi_encoder/utils/constants.ts b/packages/utils/src/abi_encoder/utils/constants.ts index 3d85fbdb8..05c6783e7 100644 --- a/packages/utils/src/abi_encoder/utils/constants.ts +++ b/packages/utils/src/abi_encoder/utils/constants.ts @@ -1,3 +1,5 @@ +import { DecodingRules, EncodingRules } from './rules'; + export const EVM_WORD_WIDTH_IN_BYTES = 32; export const EVM_WORD_WIDTH_IN_BITS = 256; export const HEX_BASE = 16; @@ -6,3 +8,5 @@ export const BIN_BASE = 2; export const HEX_SELECTOR_LENGTH_IN_CHARS = 10; export const HEX_SELECTOR_LENGTH_IN_BYTES = 4; export const HEX_SELECTOR_BYTE_OFFSET_IN_CALLDATA = 0; +export const DEFAULT_DECODING_RULES: DecodingRules = { structsAsObjects: false }; +export const DEFAULT_ENCODING_RULES: EncodingRules = { optimize: false, annotate: false }; |