aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-11-21 05:24:45 +0800
committerGreg Hysen <greg.hysen@gmail.com>2018-11-29 08:38:11 +0800
commitaed8b083b587e7b420ac6129b04004dea95c3f3a (patch)
treef6eacabc4061d6f616b85886a5f02d3149e16537 /packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts
parente6ab6f38bacdec90c960ff1db4781d161b1f4103 (diff)
downloaddexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.tar
dexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.tar.gz
dexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.tar.bz2
dexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.tar.lz
dexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.tar.xz
dexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.tar.zst
dexon-0x-contracts-aed8b083b587e7b420ac6129b04004dea95c3f3a.zip
Split Calldata into multiple files - 1 class per file
Diffstat (limited to 'packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts')
-rw-r--r--packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts b/packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts
new file mode 100644
index 000000000..d6870ec0b
--- /dev/null
+++ b/packages/utils/src/abi_encoder/calldata/dependent_calldata_block.ts
@@ -0,0 +1,59 @@
+import * as ethUtil from 'ethereumjs-util';
+
+import * as Constants from '../constants';
+
+import { CalldataBlock } from './calldata_block';
+
+export class DependentCalldataBlock extends CalldataBlock {
+ public static readonly RAW_DATA_START = new Buffer('<');
+ public static readonly RAW_DATA_END = new Buffer('>');
+ private static readonly _DEPENDENT_PAYLOAD_SIZE_IN_BYTES = 32;
+ private static readonly _EMPTY_HEADER_SIZE = 0;
+ private readonly _parent: CalldataBlock;
+ private readonly _dependency: CalldataBlock;
+ private _aliasFor: CalldataBlock | undefined;
+
+ constructor(name: string, signature: string, parentName: string, dependency: CalldataBlock, parent: CalldataBlock) {
+ const headerSizeInBytes = DependentCalldataBlock._EMPTY_HEADER_SIZE;
+ const bodySizeInBytes = DependentCalldataBlock._DEPENDENT_PAYLOAD_SIZE_IN_BYTES;
+ super(name, signature, parentName, headerSizeInBytes, bodySizeInBytes);
+ this._parent = parent;
+ this._dependency = dependency;
+ this._aliasFor = undefined;
+ }
+
+ public toBuffer(): Buffer {
+ const destinationOffset =
+ this._aliasFor !== undefined ? this._aliasFor.getOffsetInBytes() : this._dependency.getOffsetInBytes();
+ const parentOffset = this._parent.getOffsetInBytes();
+ const parentHeaderSize = this._parent.getHeaderSizeInBytes();
+ const pointer: number = destinationOffset - (parentOffset + parentHeaderSize);
+ const pointerBuf = ethUtil.toBuffer(`0x${pointer.toString(Constants.HEX_BASE)}`);
+ const evmWordWidthInBytes = 32;
+ const pointerBufPadded = ethUtil.setLengthLeft(pointerBuf, evmWordWidthInBytes);
+ return pointerBufPadded;
+ }
+
+ public getDependency(): CalldataBlock {
+ return this._dependency;
+ }
+
+ public setAlias(block: CalldataBlock): void {
+ this._aliasFor = block;
+ this._setName(`${this.getName()} (alias for ${block.getName()})`);
+ }
+
+ public getAlias(): CalldataBlock | undefined {
+ return this._aliasFor;
+ }
+
+ public getRawData(): Buffer {
+ const dependencyRawData = this._dependency.getRawData();
+ const rawDataComponents: Buffer[] = [];
+ rawDataComponents.push(DependentCalldataBlock.RAW_DATA_START);
+ rawDataComponents.push(dependencyRawData);
+ rawDataComponents.push(DependentCalldataBlock.RAW_DATA_END);
+ const rawData = Buffer.concat(rawDataComponents);
+ return rawData;
+ }
+} \ No newline at end of file