aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_encoder/calldata/blocks/set.ts
blob: d1abc4986eb92e1394e4435987f5b821708bf7b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as _ from 'lodash';

import { CalldataBlock } from '../calldata_block';

export class SetCalldataBlock extends CalldataBlock {
    private _header: Buffer | undefined;
    private _members: CalldataBlock[];

    constructor(name: string, signature: string, parentName: string) {
        super(name, signature, parentName, 0, 0);
        this._members = [];
        this._header = undefined;
    }

    public getRawData(): Buffer {
        const rawDataComponents: Buffer[] = [];
        if (!_.isUndefined(this._header)) {
            rawDataComponents.push(this._header);
        }
        _.each(this._members, (member: CalldataBlock) => {
            const memberBuffer = member.getRawData();
            rawDataComponents.push(memberBuffer);
        });
        const rawData = Buffer.concat(rawDataComponents);
        return rawData;
    }

    public setMembers(members: CalldataBlock[]): void {
        this._members = members;
    }

    public setHeader(header: Buffer): void {
        this._setHeaderSize(header.byteLength);
        this._header = header;
    }

    public toBuffer(): Buffer {
        if (!_.isUndefined(this._header)) {
            return this._header;
        }
        return new Buffer('');
    }

    public getMembers(): CalldataBlock[] {
        return this._members;
    }
}