aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_encoder/evm_data_type_factory.ts
blob: 2686491487f7e1df5a6f0755bdec07a37f505da0 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* tslint:disable max-classes-per-file */
import { DataItem, MethodAbi } from 'ethereum-types';
import * as _ from 'lodash';

import { generateDataItemsFromSignature } from './utils/signature_parser';

import { DataType } from './abstract_data_types/data_type';
import { DataTypeFactory } from './abstract_data_types/interfaces';
import { AddressDataType } from './evm_data_types/address';
import { ArrayDataType } from './evm_data_types/array';
import { BoolDataType } from './evm_data_types/bool';
import { DynamicBytesDataType } from './evm_data_types/dynamic_bytes';
import { IntDataType } from './evm_data_types/int';
import { MethodDataType } from './evm_data_types/method';
import { PointerDataType } from './evm_data_types/pointer';
import { StaticBytesDataType } from './evm_data_types/static_bytes';
import { StringDataType } from './evm_data_types/string';
import { TupleDataType } from './evm_data_types/tuple';
import { UIntDataType } from './evm_data_types/uint';

export class Address extends AddressDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Bool extends BoolDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Int extends IntDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class UInt extends UIntDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class StaticBytes extends StaticBytesDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class DynamicBytes extends DynamicBytesDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class String extends StringDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Pointer extends PointerDataType {
    public constructor(destDataType: DataType, parentDataType: DataType) {
        super(destDataType, parentDataType, EvmDataTypeFactory.getInstance());
    }
}

export class Tuple extends TupleDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Array extends ArrayDataType {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Method extends MethodDataType {
    public constructor(abi: MethodAbi) {
        super(abi, EvmDataTypeFactory.getInstance());
    }
}

/* tslint:disable no-construct */
export class EvmDataTypeFactory implements DataTypeFactory {
    private static _instance: DataTypeFactory;

    public static getInstance(): DataTypeFactory {
        if (!EvmDataTypeFactory._instance) {
            EvmDataTypeFactory._instance = new EvmDataTypeFactory();
        }
        return EvmDataTypeFactory._instance;
    }

    /* tslint:disable prefer-function-over-method */
    public create(dataItem: DataItem, parentDataType?: DataType): DataType {
        // Create data type
        let dataType: undefined | DataType;
        if (Array.matchType(dataItem.type)) {
            dataType = new Array(dataItem);
        } else if (Address.matchType(dataItem.type)) {
            dataType = new Address(dataItem);
        } else if (Bool.matchType(dataItem.type)) {
            dataType = new Bool(dataItem);
        } else if (Int.matchType(dataItem.type)) {
            dataType = new Int(dataItem);
        } else if (UInt.matchType(dataItem.type)) {
            dataType = new UInt(dataItem);
        } else if (StaticBytes.matchType(dataItem.type)) {
            dataType = new StaticBytes(dataItem);
        } else if (Tuple.matchType(dataItem.type)) {
            dataType = new Tuple(dataItem);
        } else if (DynamicBytes.matchType(dataItem.type)) {
            dataType = new DynamicBytes(dataItem);
        } else if (String.matchType(dataItem.type)) {
            dataType = new String(dataItem);
        }
        // @TODO: DataTypeement Fixed/UFixed types
        if (_.isUndefined(dataType)) {
            throw new Error(`Unrecognized data type: '${dataItem.type}'`);
        } else if (!_.isUndefined(parentDataType) && !dataType.isStatic()) {
            const pointerToDataType = new Pointer(dataType, parentDataType);
            return pointerToDataType;
        }
        return dataType;
    }
    /* tslint:enable prefer-function-over-method */

    private constructor() {}
}

/**
 * Convenience function for creating a DataType from different inputs.
 * @param input A single or set of DataItem or a DataType signature.
 *              A signature in the form of '<type>' is interpreted as a `DataItem`
 *              For example, 'string' is interpreted as {type: 'string'}
 *              A signature in the form '(<type1>, <type2>, ..., <typen>)' is interpreted as `DataItem[]`
 *              For eaxmple, '(string, uint256)' is interpreted as [{type: 'string'}, {type: 'uint256'}]
 * @return DataType corresponding to input.
 */
export function create(input: DataItem | DataItem[] | string): DataType {
    // Handle different types of input
    const isSignature = typeof input === 'string';
    const isTupleSignature = isSignature && (input as string).startsWith('(');
    const shouldParseAsTuple = isTupleSignature || _.isArray(input);
    // Create input `dataItem`
    let dataItem: DataItem;
    if (shouldParseAsTuple) {
        const dataItems = isSignature ? generateDataItemsFromSignature(input as string) : (input as DataItem[]);
        dataItem = {
            name: '',
            type: 'tuple',
            components: dataItems,
        };
    } else {
        dataItem = isSignature ? generateDataItemsFromSignature(input as string)[0] : (input as DataItem);
    }
    // Create data type
    const dataType = EvmDataTypeFactory.getInstance().create(dataItem);
    return dataType;
}
/* tslint:enable no-construct */