-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
types.ts
189 lines (175 loc) · 5.1 KB
/
types.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-use-before-define */
import type { AbiOutputParameter, AbiParameter } from '../parser/abiParser';
import type {
EnumType,
StructType,
SvmOutputType,
SvmType,
TupleType,
} from '../parser/parseSvmTypes';
interface GenerateTypeOptions {
returnResultObject?: string;
useStructs?: boolean; // uses struct type for first depth, if false then generates first depth tuple types
}
/**
* Generates the Typescript types for given function
*/
export function generateInputTypes(
input: Array<AbiParameter>,
options: GenerateTypeOptions
): string {
const createInputTypeArgs = (input: AbiParameter, index: number) =>
`${input.name || `arg${index}`}: ${generateInputType(input.type, options)}`;
if (input.length === 0) {
return '[]';
}
return `[${input.map(createInputTypeArgs).join(', ')}]`;
}
/**
* Generates the Typescript types for given function
*/
export function generateOutputTypes(
output: Array<AbiOutputParameter>,
options: GenerateTypeOptions = {}
): string {
if (output.length === 0) {
// If return result object exists them
// return it instead of void
if (options.returnResultObject) {
return options.returnResultObject;
}
return 'void';
}
const outputs = output.map((output) => generateOutputType(output.type, options));
if (outputs.length > 1) {
return `[${outputs.join(',')}]`;
}
return outputs[0];
}
/**
* Maps Input SvmTypes to TS types
* https://docs.ethers.io/ethers.js/html/api-contract.html#types
*/
export function generateInputType(svmType: SvmType, options: GenerateTypeOptions = {}): string {
switch (svmType.type) {
case 'u8':
case 'u16':
case 'u32':
case 'u64':
return 'BigNumberish';
case 'b256':
case 'address':
return 'string';
case 'byte':
return 'BytesLike';
case 'array':
return `[${Array(svmType.size)
.fill(generateInputType(svmType.itemType, { useStructs: true }))
.join(', ')}]`;
case 'bool':
return 'boolean';
case 'string':
return 'string';
case 'tuple':
return generateTupleType(svmType, (svmType) =>
generateInputType(svmType, { ...options, useStructs: true })
);
case 'enum':
if (svmType.structName && options.useStructs) {
return `${svmType.structName}Input`;
}
return generateEnumType(svmType, (svmType) =>
generateInputType(svmType, { ...options, useStructs: true })
);
case 'struct':
if (svmType.structName && options.useStructs) {
return `${svmType.structName}Input`;
}
return generateStructType(svmType, (svmType) =>
generateInputType(svmType, { ...options, useStructs: true })
);
case 'unknown':
return 'any';
default:
return 'any';
}
}
/**
* Maps Output SvmTypes to TS types
* https://docs.ethers.io/ethers.js/html/api-contract.html#types
*/
export function generateOutputType(
svmType: SvmOutputType,
options: GenerateTypeOptions = {}
): string {
if (options.returnResultObject) {
return options.returnResultObject;
}
switch (svmType.type) {
case 'u8':
case 'u16':
return 'number';
case 'u32':
case 'u64':
return 'bigint';
case 'b256':
case 'address':
return 'string';
case 'byte':
return 'BytesLike';
case 'array':
return `[${Array(svmType.size)
.fill(generateOutputType(svmType.itemType, { useStructs: true }))
.join(', ')}]`;
case 'bool':
return 'boolean';
case 'string':
return 'string';
case 'tuple':
return generateTupleType(svmType, (svmType) =>
generateOutputType(svmType, { ...options, useStructs: true })
);
case 'enum':
if (svmType.structName && options.useStructs) {
return `${svmType.structName}Output`;
}
return generateEnumType(svmType, (svmType) =>
generateOutputType(svmType, { ...options, useStructs: true })
);
case 'struct':
if (svmType.structName && options.useStructs) {
return `${svmType.structName}Output`;
}
return generateStructType(svmType, (svmType) =>
generateOutputType(svmType, { ...options, useStructs: true })
);
case 'unknown':
return 'any';
case 'void':
return 'void';
default:
return 'any';
}
}
export function generateTupleType(
tuple: TupleType,
generator: (svmType: SvmType) => string
): string {
return `[${tuple.components.map((component) => generator(component.type)).join(', ')}]`;
}
export function generateStructType(
struct: StructType,
generator: (svmType: SvmType) => string
): string {
return `{${struct.components
.map((component) => `${component.name}: ${generator(component.type)}`)
.join(', ')}}`;
}
export function generateEnumType(_enum: EnumType, generator: (svmType: SvmType) => string): string {
return _enum.structName === 'Option'
? `Option<${generator(_enum.components[0].type)}>`
: `Enum<{${_enum.components
.map((component) => `${component.name}: ${generator(component.type)}`)
.join(', ')}}>`;
}