1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createMethod = exports.create = exports.EvmDataTypeFactory = exports.Method = exports.Array = exports.Tuple = exports.Pointer = exports.String = exports.DynamicBytes = exports.StaticBytes = exports.UInt = exports.Int = exports.Bool = exports.Address = void 0;
|
4 | const _ = require("lodash");
|
5 | const signature_parser_1 = require("./utils/signature_parser");
|
6 | const address_1 = require("./evm_data_types/address");
|
7 | const array_1 = require("./evm_data_types/array");
|
8 | const bool_1 = require("./evm_data_types/bool");
|
9 | const dynamic_bytes_1 = require("./evm_data_types/dynamic_bytes");
|
10 | const int_1 = require("./evm_data_types/int");
|
11 | const method_1 = require("./evm_data_types/method");
|
12 | const pointer_1 = require("./evm_data_types/pointer");
|
13 | const static_bytes_1 = require("./evm_data_types/static_bytes");
|
14 | const string_1 = require("./evm_data_types/string");
|
15 | const tuple_1 = require("./evm_data_types/tuple");
|
16 | const uint_1 = require("./evm_data_types/uint");
|
17 | class Address extends address_1.AddressDataType {
|
18 | constructor(dataItem) {
|
19 | super(dataItem, EvmDataTypeFactory.getInstance());
|
20 | }
|
21 | }
|
22 | exports.Address = Address;
|
23 | class Bool extends bool_1.BoolDataType {
|
24 | constructor(dataItem) {
|
25 | super(dataItem, EvmDataTypeFactory.getInstance());
|
26 | }
|
27 | }
|
28 | exports.Bool = Bool;
|
29 | class Int extends int_1.IntDataType {
|
30 | constructor(dataItem) {
|
31 | super(dataItem, EvmDataTypeFactory.getInstance());
|
32 | }
|
33 | }
|
34 | exports.Int = Int;
|
35 | class UInt extends uint_1.UIntDataType {
|
36 | constructor(dataItem) {
|
37 | super(dataItem, EvmDataTypeFactory.getInstance());
|
38 | }
|
39 | }
|
40 | exports.UInt = UInt;
|
41 | class StaticBytes extends static_bytes_1.StaticBytesDataType {
|
42 | constructor(dataItem) {
|
43 | super(dataItem, EvmDataTypeFactory.getInstance());
|
44 | }
|
45 | }
|
46 | exports.StaticBytes = StaticBytes;
|
47 | class DynamicBytes extends dynamic_bytes_1.DynamicBytesDataType {
|
48 | constructor(dataItem) {
|
49 | super(dataItem, EvmDataTypeFactory.getInstance());
|
50 | }
|
51 | }
|
52 | exports.DynamicBytes = DynamicBytes;
|
53 | class String extends string_1.StringDataType {
|
54 | constructor(dataItem) {
|
55 | super(dataItem, EvmDataTypeFactory.getInstance());
|
56 | }
|
57 | }
|
58 | exports.String = String;
|
59 | class Pointer extends pointer_1.PointerDataType {
|
60 | constructor(destDataType, parentDataType) {
|
61 | super(destDataType, parentDataType, EvmDataTypeFactory.getInstance());
|
62 | }
|
63 | }
|
64 | exports.Pointer = Pointer;
|
65 | class Tuple extends tuple_1.TupleDataType {
|
66 | constructor(dataItem) {
|
67 | super(dataItem, EvmDataTypeFactory.getInstance());
|
68 | }
|
69 | }
|
70 | exports.Tuple = Tuple;
|
71 | class Array extends array_1.ArrayDataType {
|
72 | constructor(dataItem) {
|
73 | super(dataItem, EvmDataTypeFactory.getInstance());
|
74 | }
|
75 | }
|
76 | exports.Array = Array;
|
77 | class Method extends method_1.MethodDataType {
|
78 | constructor(abi) {
|
79 | super(abi, EvmDataTypeFactory.getInstance());
|
80 | }
|
81 | }
|
82 | exports.Method = Method;
|
83 |
|
84 | class EvmDataTypeFactory {
|
85 |
|
86 | constructor() { }
|
87 | static getInstance() {
|
88 | if (!EvmDataTypeFactory._instance) {
|
89 | EvmDataTypeFactory._instance = new EvmDataTypeFactory();
|
90 | }
|
91 | return EvmDataTypeFactory._instance;
|
92 | }
|
93 |
|
94 | create(dataItem, parentDataType) {
|
95 |
|
96 | let dataType;
|
97 | if (Array.matchType(dataItem.type)) {
|
98 | dataType = new Array(dataItem);
|
99 | }
|
100 | else if (Address.matchType(dataItem.type)) {
|
101 | dataType = new Address(dataItem);
|
102 | }
|
103 | else if (Bool.matchType(dataItem.type)) {
|
104 | dataType = new Bool(dataItem);
|
105 | }
|
106 | else if (Int.matchType(dataItem.type)) {
|
107 | dataType = new Int(dataItem);
|
108 | }
|
109 | else if (UInt.matchType(dataItem.type)) {
|
110 | dataType = new UInt(dataItem);
|
111 | }
|
112 | else if (StaticBytes.matchType(dataItem.type)) {
|
113 | dataType = new StaticBytes(dataItem);
|
114 | }
|
115 | else if (Tuple.matchType(dataItem.type)) {
|
116 | dataType = new Tuple(dataItem);
|
117 | }
|
118 | else if (DynamicBytes.matchType(dataItem.type)) {
|
119 | dataType = new DynamicBytes(dataItem);
|
120 | }
|
121 | else if (String.matchType(dataItem.type)) {
|
122 | dataType = new String(dataItem);
|
123 | }
|
124 |
|
125 | if (dataType === undefined) {
|
126 | throw new Error(`Unrecognized data type: '${dataItem.type}'`);
|
127 | }
|
128 | else if (parentDataType !== undefined && !dataType.isStatic()) {
|
129 | const pointerToDataType = new Pointer(dataType, parentDataType);
|
130 | return pointerToDataType;
|
131 | }
|
132 | return dataType;
|
133 | }
|
134 | }
|
135 | exports.EvmDataTypeFactory = EvmDataTypeFactory;
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function create(input, nestedDataItems) {
|
142 | let dataItem = consolidateDataItemsIntoSingle(input);
|
143 | if (nestedDataItems) {
|
144 | const nestedTypes = _.keyBy(nestedDataItems, 'internalType');
|
145 | const replaceTypes = (_dataItem) => {
|
146 | const aliasedType = _dataItem.type;
|
147 | if (Array.matchType(aliasedType)) {
|
148 | const [elementType, arrayLength] = Array.decodeElementTypeAndLengthFromType(aliasedType);
|
149 | if (elementType in nestedTypes) {
|
150 | _dataItem.type = `${nestedTypes[elementType].type}[${arrayLength !== null && arrayLength !== void 0 ? arrayLength : ''}]`;
|
151 | _dataItem.components = nestedTypes[elementType].components;
|
152 | }
|
153 | }
|
154 | else if (aliasedType in nestedTypes) {
|
155 | _dataItem.type = nestedTypes[aliasedType].type;
|
156 | _dataItem.components = nestedTypes[aliasedType].components;
|
157 | }
|
158 | if (_dataItem.components) {
|
159 | _dataItem.components.map(replaceTypes);
|
160 | }
|
161 | };
|
162 | dataItem = _.cloneDeep(dataItem);
|
163 | replaceTypes(dataItem);
|
164 | }
|
165 | const dataType = EvmDataTypeFactory.getInstance().create(dataItem);
|
166 | return dataType;
|
167 | }
|
168 | exports.create = create;
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | function consolidateDataItemsIntoSingle(input) {
|
176 | let dataItem;
|
177 | if (_.isArray(input)) {
|
178 | const dataItems = input;
|
179 | dataItem = {
|
180 | name: '',
|
181 | type: 'tuple',
|
182 | components: dataItems,
|
183 | };
|
184 | }
|
185 | else {
|
186 | dataItem = _.isString(input) ? signature_parser_1.generateDataItemFromSignature(input) : input;
|
187 | }
|
188 | return dataItem;
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | function createMethod(methodName, input, output) {
|
198 | const methodInput = input === undefined ? [] : consolidateDataItemsIntoArray(input);
|
199 | const methodOutput = output === undefined ? [] : consolidateDataItemsIntoArray(output);
|
200 | const methodAbi = {
|
201 | name: methodName,
|
202 | inputs: methodInput,
|
203 | outputs: methodOutput,
|
204 | type: 'function',
|
205 |
|
206 | constant: false,
|
207 | payable: false,
|
208 | stateMutability: 'nonpayable',
|
209 | };
|
210 | const dataType = new Method(methodAbi);
|
211 | return dataType;
|
212 | }
|
213 | exports.createMethod = createMethod;
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function consolidateDataItemsIntoArray(input) {
|
220 | let dataItems;
|
221 | if (_.isArray(input) && _.isEmpty(input)) {
|
222 | dataItems = [];
|
223 | }
|
224 | else if (_.isArray(input) && _.isString(input[0])) {
|
225 | dataItems = [];
|
226 | _.each(input, (signature) => {
|
227 | const dataItem = signature_parser_1.generateDataItemFromSignature(signature);
|
228 | dataItems.push(dataItem);
|
229 | });
|
230 | }
|
231 | else if (_.isArray(input)) {
|
232 | dataItems = input;
|
233 | }
|
234 | else if (typeof input === 'string') {
|
235 | const dataItem = signature_parser_1.generateDataItemFromSignature(input);
|
236 | dataItems = [dataItem];
|
237 | }
|
238 | else {
|
239 | dataItems = [input];
|
240 | }
|
241 | return dataItems;
|
242 | }
|
243 |
|
244 |
|
\ | No newline at end of file |