-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
types.ts
147 lines (136 loc) · 3.71 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
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-use-before-define */
import type { AbiOutputParameter, AbiParameter } from '../parser/abiParser';
import type { 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 {
if (input.length === 0) {
return '';
}
return `${input
.map(
(input, index) => `${input.name || `arg${index}`}: ${generateInputType(input.type, options)}`
)
.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)).join(', ')}]`;
case 'bool':
return 'boolean';
case 'string':
return 'string';
case 'tuple':
if (svmType.structName && options.useStructs) {
return `${svmType.structName}Input`;
}
return generateTupleType(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)).join(', ')}]`;
case 'bool':
return 'boolean';
case 'string':
return 'string';
case 'tuple':
if (svmType.structName && options.useStructs) {
return svmType.structName;
}
return generateTupleType(svmType, (svmType) =>
generateOutputType(svmType, { ...options, useStructs: true })
);
case 'unknown':
return 'any';
case 'void':
return 'void';
default:
return 'any';
}
}
/**
* Maps Tuple SvmTypes to TS types
* https://docs.ethers.io/ethers.js/html/api-contract.html#types
*/
export function generateTupleType(
tuple: TupleType,
generator: (svmType: SvmType) => string
): string {
return `{${tuple.components
.map((component) => `${component.name}: ${generator(component.type)}`)
.join(', ')}}`;
}