UNPKG

9.02 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.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;
4const _ = require("lodash");
5const signature_parser_1 = require("./utils/signature_parser");
6const address_1 = require("./evm_data_types/address");
7const array_1 = require("./evm_data_types/array");
8const bool_1 = require("./evm_data_types/bool");
9const dynamic_bytes_1 = require("./evm_data_types/dynamic_bytes");
10const int_1 = require("./evm_data_types/int");
11const method_1 = require("./evm_data_types/method");
12const pointer_1 = require("./evm_data_types/pointer");
13const static_bytes_1 = require("./evm_data_types/static_bytes");
14const string_1 = require("./evm_data_types/string");
15const tuple_1 = require("./evm_data_types/tuple");
16const uint_1 = require("./evm_data_types/uint");
17class Address extends address_1.AddressDataType {
18 constructor(dataItem) {
19 super(dataItem, EvmDataTypeFactory.getInstance());
20 }
21}
22exports.Address = Address;
23class Bool extends bool_1.BoolDataType {
24 constructor(dataItem) {
25 super(dataItem, EvmDataTypeFactory.getInstance());
26 }
27}
28exports.Bool = Bool;
29class Int extends int_1.IntDataType {
30 constructor(dataItem) {
31 super(dataItem, EvmDataTypeFactory.getInstance());
32 }
33}
34exports.Int = Int;
35class UInt extends uint_1.UIntDataType {
36 constructor(dataItem) {
37 super(dataItem, EvmDataTypeFactory.getInstance());
38 }
39}
40exports.UInt = UInt;
41class StaticBytes extends static_bytes_1.StaticBytesDataType {
42 constructor(dataItem) {
43 super(dataItem, EvmDataTypeFactory.getInstance());
44 }
45}
46exports.StaticBytes = StaticBytes;
47class DynamicBytes extends dynamic_bytes_1.DynamicBytesDataType {
48 constructor(dataItem) {
49 super(dataItem, EvmDataTypeFactory.getInstance());
50 }
51}
52exports.DynamicBytes = DynamicBytes;
53class String extends string_1.StringDataType {
54 constructor(dataItem) {
55 super(dataItem, EvmDataTypeFactory.getInstance());
56 }
57}
58exports.String = String;
59class Pointer extends pointer_1.PointerDataType {
60 constructor(destDataType, parentDataType) {
61 super(destDataType, parentDataType, EvmDataTypeFactory.getInstance());
62 }
63}
64exports.Pointer = Pointer;
65class Tuple extends tuple_1.TupleDataType {
66 constructor(dataItem) {
67 super(dataItem, EvmDataTypeFactory.getInstance());
68 }
69}
70exports.Tuple = Tuple;
71class Array extends array_1.ArrayDataType {
72 constructor(dataItem) {
73 super(dataItem, EvmDataTypeFactory.getInstance());
74 }
75}
76exports.Array = Array;
77class Method extends method_1.MethodDataType {
78 constructor(abi) {
79 super(abi, EvmDataTypeFactory.getInstance());
80 }
81}
82exports.Method = Method;
83/* tslint:disable no-construct */
84class EvmDataTypeFactory {
85 /* tslint:enable prefer-function-over-method */
86 constructor() { }
87 static getInstance() {
88 if (!EvmDataTypeFactory._instance) {
89 EvmDataTypeFactory._instance = new EvmDataTypeFactory();
90 }
91 return EvmDataTypeFactory._instance;
92 }
93 /* tslint:disable prefer-function-over-method */
94 create(dataItem, parentDataType) {
95 // Create data type
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 // @TODO: DataTypeement Fixed/UFixed types
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}
135exports.EvmDataTypeFactory = EvmDataTypeFactory;
136/**
137 * Convenience function for creating a DataType from different inputs.
138 * @param input A single or set of DataItem or a signature for an EVM data type.
139 * @return DataType corresponding to input.
140 */
141function 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}
168exports.create = create;
169/**
170 * Convenience function to aggregate a single input or a set of inputs into a single DataItem.
171 * An array of data items is grouped into a single tuple.
172 * @param input A single data item; a set of data items; a signature.
173 * @return A single data item corresponding to input.
174 */
175function 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 * Convenience function for creating a Method encoder from different inputs.
192 * @param methodName name of method.
193 * @param input A single data item; a set of data items; a signature; or an array of signatures (optional).
194 * @param output A single data item; a set of data items; a signature; or an array of signatures (optional).
195 * @return Method corresponding to input.
196 */
197function 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 // default fields not used by ABI
206 constant: false,
207 payable: false,
208 stateMutability: 'nonpayable',
209 };
210 const dataType = new Method(methodAbi);
211 return dataType;
212}
213exports.createMethod = createMethod;
214/**
215 * Convenience function that aggregates a single input or a set of inputs into an array of DataItems.
216 * @param input A single data item; a set of data items; a signature; or an array of signatures.
217 * @return Array of data items corresponding to input.
218 */
219function 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/* tslint:enable no-construct */
244//# sourceMappingURL=evm_data_type_factory.js.map
\No newline at end of file