-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
192 lines (162 loc) · 5.65 KB
/
index.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
190
191
192
import * as Yup from 'yup';
const yupAbiInputSchema = Yup.object().shape({
name: Yup.string().strict(true).required(),
type: Yup.string().strict(true).required(),
});
const yupAbiStructMemberSchema = Yup.object().shape({
name: Yup.string().strict(true).required(),
type: Yup.string().strict(true).required(),
});
const yupAbiEnumVariantSchema = Yup.object().shape({
name: Yup.string().strict(true).required(),
type: Yup.string().strict(true).required(),
});
const yupAbiOutputSchema = Yup.object().shape({
type: Yup.string().strict(true).required(),
});
const yupAbiStateMutabilitySchema = Yup.string()
.strict(true)
.required()
.oneOf(['view', 'external']);
const yupAbiEventKindSchema = Yup.string()
.strict(true)
.required()
.oneOf(['struct', 'enum']);
const yupAbiEventFieldSchema = Yup.object().shape({
name: Yup.string().strict(true).required(),
type: Yup.string().strict(true).required(),
kind: Yup.string().strict(true).oneOf(['key', 'data', 'nested', 'flat']),
});
const yupAbiEnumEventSchema = Yup.object().shape({
kind: Yup.string().strict(true).oneOf(['enum']),
variants: yupAbiEventFieldSchema,
});
const yupAbiStructEventSchema = Yup.object().shape({
kind: Yup.string().strict(true).oneOf(['struct']),
members: yupAbiEventFieldSchema,
});
const yupAbiEventSchema = Yup.object().shape({
name: Yup.string().strict(true).required(),
type: Yup.string().strict(true).required().oneOf(['event']),
kind: yupAbiEventKindSchema,
members: Yup.array(yupAbiEventFieldSchema).optional(),
variants: Yup.array(yupAbiEventFieldSchema).optional(),
});
const yupAbiFunctionSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['function', 'constructor']),
name: Yup.string().strict(true).required(),
inputs: Yup.array(yupAbiInputSchema).required(),
outputs: Yup.array(yupAbiOutputSchema).required(),
state_mutability: yupAbiStateMutabilitySchema,
});
const yupAbiConstructorSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['constructor']),
name: Yup.string().strict(true).required().oneOf(['constructor']),
inputs: Yup.array(yupAbiInputSchema).required(),
});
const yupAbiL1HandlerSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['l1_handler']),
name: Yup.string().strict(true).required(),
inputs: Yup.array(yupAbiInputSchema).required(),
outputs: Yup.array(yupAbiOutputSchema).required(),
state_mutability: yupAbiStateMutabilitySchema,
});
const yupAbiStructSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['struct']),
name: Yup.string().strict(true).required(),
members: Yup.array(yupAbiStructMemberSchema).required(),
});
const yupAbiEnumSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['enum']),
name: Yup.string().strict(true).required(),
variants: Yup.array(yupAbiEnumVariantSchema).required(),
});
const yupAbiInterfaceSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['interface']),
name: Yup.string().strict(true).required(),
items: Yup.array(yupAbiFunctionSchema).required(),
});
const yupAbiImplSchema = Yup.object().shape({
type: Yup.string().strict(true).required().oneOf(['impl']),
name: Yup.string().strict(true).required(),
interface_name: Yup.string().strict(true).required(),
});
type ABIFunction = Yup.InferType<typeof yupAbiFunctionSchema>;
type ABIConstructor = Yup.InferType<typeof yupAbiConstructorSchema>;
type ABIL1Handler = Yup.InferType<typeof yupAbiL1HandlerSchema>;
type ABIStruct = Yup.InferType<typeof yupAbiStructSchema>;
type ABIEnum = Yup.InferType<typeof yupAbiEnumSchema>;
type ABIInterface = Yup.InferType<typeof yupAbiInterfaceSchema>;
type ABIImpl = Yup.InferType<typeof yupAbiImplSchema>;
type ABIEvent = Yup.InferType<typeof yupAbiEventSchema>;
type ABIFunctionInputs = Yup.InferType<typeof yupAbiInputSchema>;
const abiSchema = Yup.array().of(
Yup.object().test(
'schema-test',
'Must follow schema of respective type, if type: "function" | "constructor" | "l1_handler" | "struct" | "enum" | "interface" | "impl" | "event"',
(value: any) => {
try {
switch (value.type) {
case 'function':
yupAbiFunctionSchema.validateSync(value);
return true;
case 'constructor':
yupAbiConstructorSchema.validateSync(value);
return true;
case 'l1_handler':
yupAbiL1HandlerSchema.validateSync(value);
return true;
case 'struct':
yupAbiStructSchema.validateSync(value);
return true;
case 'enum':
yupAbiEnumSchema.validateSync(value);
return true;
case 'interface':
yupAbiInterfaceSchema.validateSync(value);
return true;
case 'impl':
yupAbiImplSchema.validateSync(value);
return true;
case 'event':
yupAbiEventSchema.validateSync(value);
return true;
default:
return false;
}
} catch (e: any) {
throw new Yup.ValidationError(e);
}
}
)
);
type ABI = Yup.InferType<typeof abiSchema>;
export {
abiSchema,
yupAbiConstructorSchema,
yupAbiEnumEventSchema,
yupAbiEnumSchema,
yupAbiEnumVariantSchema,
yupAbiStructEventSchema,
yupAbiEventFieldSchema,
yupAbiEventKindSchema,
yupAbiFunctionSchema,
yupAbiEventSchema,
yupAbiImplSchema,
yupAbiInputSchema,
yupAbiStructSchema,
yupAbiInterfaceSchema,
yupAbiL1HandlerSchema,
};
export type {
ABI,
ABIConstructor,
ABIEnum,
ABIEvent,
ABIFunction,
ABIImpl,
ABIInterface,
ABIL1Handler,
ABIStruct,
ABIFunctionInputs,
};