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/abstract_data_types/types/pointer.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/abstract_data_types/types/pointer.ts')
-rw-r--r-- | packages/utils/src/abi_encoder/abstract_data_types/types/pointer.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/abstract_data_types/types/pointer.ts b/packages/utils/src/abi_encoder/abstract_data_types/types/pointer.ts new file mode 100644 index 000000000..0f3c55280 --- /dev/null +++ b/packages/utils/src/abi_encoder/abstract_data_types/types/pointer.ts @@ -0,0 +1,54 @@ +import { DataItem } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { PointerCalldataBlock } from '../../calldata/blocks/pointer'; +import { CalldataBlock } from '../../calldata/calldata_block'; +import { RawCalldata } from '../../calldata/raw_calldata'; +import { constants } from '../../utils/constants'; +import { DecodingRules } from '../../utils/rules'; + +import { DataType } from '../data_type'; +import { DataTypeFactory } from '../interfaces'; + +export abstract class AbstractPointerDataType extends DataType { + protected _destination: DataType; + protected _parent: DataType; + + public constructor(dataItem: DataItem, factory: DataTypeFactory, destination: DataType, parent: DataType) { + super(dataItem, factory); + this._destination = destination; + this._parent = parent; + } + + public generateCalldataBlock(value: any, parentBlock?: CalldataBlock): PointerCalldataBlock { + if (_.isUndefined(parentBlock)) { + throw new Error(`DependentDataType requires a parent block to generate its block`); + } + const destinationBlock = this._destination.generateCalldataBlock(value, parentBlock); + const name = this.getDataItem().name; + const signature = this.getSignature(); + const parentName = parentBlock.getName(); + const block = new PointerCalldataBlock(name, signature, parentName, destinationBlock, parentBlock); + return block; + } + + public generateValue(calldata: RawCalldata, rules: DecodingRules): any { + const destinationOffsetBuf = calldata.popWord(); + 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._destination.generateValue(calldata, rules); + calldata.setOffset(currentOffset); + return value; + } + + // Disable prefer-function-over-method for inherited abstract method. + /* tslint:disable prefer-function-over-method */ + public isStatic(): boolean { + return true; + } + /* tslint:enable prefer-function-over-method */ +} |