diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-21 08:51:56 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | c638151b73289fc936bb7d4323711d1954cc4fcb (patch) | |
tree | 72a58a5ce345852c4728b852e723e426caec489b /packages/utils/src/abi_encoder/abstract_data_types | |
parent | 978a58105cd2d2f5d6ee3bcd870218fd357fb010 (diff) | |
download | dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.tar dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.tar.gz dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.tar.bz2 dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.tar.lz dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.tar.xz dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.tar.zst dexon-sol-tools-c638151b73289fc936bb7d4323711d1954cc4fcb.zip |
Some minor cleanup in ABI Encoder
Diffstat (limited to 'packages/utils/src/abi_encoder/abstract_data_types')
5 files changed, 26 insertions, 25 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 a6adeb23b..c83f2085e 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 @@ -32,7 +32,7 @@ export abstract class DataType { calldata.setSelector(selector); } const block = this.generateCalldataBlock(value); - calldata.setRoot(block); // @TODO CHANGE + calldata.setRoot(block); const calldataHex = calldata.toHexString(); return calldataHex; } diff --git a/packages/utils/src/abi_encoder/abstract_data_types/dependent_data_type.ts b/packages/utils/src/abi_encoder/abstract_data_types/dependent_data_type.ts index f4992dd0f..7649b1836 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/dependent_data_type.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/dependent_data_type.ts @@ -28,16 +28,17 @@ export abstract class DependentDataType extends DataType { const dependencyBlock = this._dependency.generateCalldataBlock(value, parentBlock); const name = this.getDataItem().name; const signature = this.getSignature(); - const parentName = parentBlock === undefined ? '' : parentBlock.getName(); + const parentName = parentBlock ? parentBlock.getName() : ''; const block = new DependentCalldataBlock(name, signature, parentName, dependencyBlock, parentBlock); return block; } public generateValue(calldata: RawCalldata, rules: DecodingRules): any { const destinationOffsetBuf = calldata.popWord(); - const currentOffset = calldata.getOffset(); - const destinationOffsetRelative = parseInt(ethUtil.bufferToHex(destinationOffsetBuf), Constants.HEX_BASE); + const destinationOffsetHex = ethUtil.bufferToHex(destinationOffsetBuf); + const destinationOffsetRelative = parseInt(destinationOffsetHex, Constants.HEX_BASE); const destinationOffsetAbsolute = calldata.toAbsoluteOffset(destinationOffsetRelative); + const currentOffset = calldata.getOffset(); calldata.setOffset(destinationOffsetAbsolute); const value = this._dependency.generateValue(calldata, rules); calldata.setOffset(currentOffset); diff --git a/packages/utils/src/abi_encoder/abstract_data_types/interfaces.ts b/packages/utils/src/abi_encoder/abstract_data_types/interfaces.ts index 2ae92659c..9e2a94522 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/interfaces.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/interfaces.ts @@ -14,3 +14,7 @@ export interface DataTypeStaticInterface { encodeValue: (value: any) => Buffer; decodeValue: (rawCalldata: RawCalldata) => any; } + +export interface MemberIndexByName { + [key: string]: number; +} diff --git a/packages/utils/src/abi_encoder/abstract_data_types/member_data_type.ts b/packages/utils/src/abi_encoder/abstract_data_types/member_data_type.ts index 318564e21..dacdbf8af 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/member_data_type.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/member_data_type.ts @@ -9,16 +9,12 @@ import { DecodingRules } from '../utils/rules'; import { DataType } from './data_type'; import { DependentDataType } from './dependent_data_type'; -import { DataTypeFactory } from './interfaces'; - -interface MemberMap { - [key: string]: number; -} +import { DataTypeFactory, MemberIndexByName } from './interfaces'; export abstract class MemberDataType extends DataType { protected readonly _arrayLength: number | undefined; protected readonly _arrayElementType: string | undefined; - private readonly _memberMap: MemberMap; + private readonly _memberIndexByName: MemberIndexByName; private readonly _members: DataType[]; private readonly _isArray: boolean; @@ -30,15 +26,15 @@ export abstract class MemberDataType extends DataType { arrayElementType?: string, ) { super(dataItem, factory); - this._memberMap = {}; + this._memberIndexByName = {}; this._members = []; this._isArray = isArray; this._arrayLength = arrayLength; this._arrayElementType = arrayElementType; if (isArray && arrayLength !== undefined) { - [this._members, this._memberMap] = this._createMembersWithLength(dataItem, arrayLength); + [this._members, this._memberIndexByName] = this._createMembersWithLength(dataItem, arrayLength); } else if (!isArray) { - [this._members, this._memberMap] = this._createMembersWithKeys(dataItem); + [this._members, this._memberIndexByName] = this._createMembersWithKeys(dataItem); } } @@ -65,7 +61,7 @@ export abstract class MemberDataType extends DataType { let value: any[] | object; if (rules.structsAsObjects && !this._isArray) { value = {}; - _.each(this._memberMap, (idx: number, key: string) => { + _.each(this._memberIndexByName, (idx: number, key: string) => { const member = this._members[idx]; const memberValue = member.generateValue(calldata, rules); (value as { [key: string]: any })[key] = memberValue; @@ -149,14 +145,14 @@ export abstract class MemberDataType extends DataType { parentName, ); const memberBlocks: CalldataBlock[] = []; - const childMap = _.cloneDeep(this._memberMap); + const childMap = _.cloneDeep(this._memberIndexByName); _.forOwn(obj, (value: any, key: string) => { if (!(key in childMap)) { throw new Error( `Could not assign tuple to object: unrecognized key '${key}' in object ${this.getDataItem().name}`, ); } - const block = this._members[this._memberMap[key]].generateCalldataBlock(value, methodBlock); + const block = this._members[this._memberIndexByName[key]].generateCalldataBlock(value, methodBlock); memberBlocks.push(block); delete childMap[key]; }); @@ -182,14 +178,14 @@ export abstract class MemberDataType extends DataType { return signature; } - private _createMembersWithKeys(dataItem: DataItem): [DataType[], MemberMap] { + private _createMembersWithKeys(dataItem: DataItem): [DataType[], MemberIndexByName] { // Sanity check if (dataItem.components === undefined) { throw new Error(`Expected components`); } const members: DataType[] = []; - const memberMap: MemberMap = {}; + const memberIndexByName: MemberIndexByName = {}; _.each(dataItem.components, (memberItem: DataItem) => { const childDataItem: DataItem = { type: memberItem.type, @@ -200,16 +196,16 @@ export abstract class MemberDataType extends DataType { childDataItem.components = components; } const child = this.getFactory().create(childDataItem, this); - memberMap[memberItem.name] = members.length; + memberIndexByName[memberItem.name] = members.length; members.push(child); }); - return [members, memberMap]; + return [members, memberIndexByName]; } - private _createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberMap] { + private _createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberIndexByName] { const members: DataType[] = []; - const memberMap: MemberMap = {}; + const memberIndexByName: MemberIndexByName = {}; const range = _.range(length); _.each(range, (idx: number) => { const childDataItem: DataItem = { @@ -221,10 +217,10 @@ export abstract class MemberDataType extends DataType { childDataItem.components = components; } const child = this.getFactory().create(childDataItem, this); - memberMap[idx.toString(Constants.DEC_BASE)] = members.length; + memberIndexByName[idx.toString(Constants.DEC_BASE)] = members.length; members.push(child); }); - return [members, memberMap]; + return [members, memberIndexByName]; } } diff --git a/packages/utils/src/abi_encoder/abstract_data_types/payload_data_type.ts b/packages/utils/src/abi_encoder/abstract_data_types/payload_data_type.ts index 767e64f51..fa420bc74 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/payload_data_type.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/payload_data_type.ts @@ -19,7 +19,7 @@ export abstract class PayloadDataType extends DataType { const encodedValue = this.encodeValue(value); const name = this.getDataItem().name; const signature = this.getSignature(); - const parentName = parentBlock === undefined ? '' : parentBlock.getName(); + const parentName = parentBlock ? parentBlock.getName() : ''; const block = new PayloadCalldataBlock(name, signature, parentName, encodedValue); return block; } |