This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
forked from keep-starknet-strange/abi-wan-kanabi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkanabi.test-d.ts
277 lines (259 loc) · 11.1 KB
/
kanabi.test-d.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import {
CairoFelt,
CairoInt,
CairoBigInt,
CairoAddress,
CairoFunction,
CairoVoid,
CairoTuple,
FunctionArgs,
FunctionRet,
ExtractAbiFunction,
ExtractAbiFunctionNames,
AbiTypeToPrimitiveType,
AbiParameterToPrimitiveType,
ExtractAbiStruct
} from "./kanabi";
import { test, expectTypeOf, assertType } from "vitest";
import { ABI } from "./test/example";
type TAbi = typeof ABI;
function returnVoid() { }
const voidValue = returnVoid();
const bigIntValue = 105n;
const intValue = 10;
const emptyArray: [] = [];
test("Cairo Types", () => {
assertType<CairoFelt>("core::felt");
assertType<CairoInt>("core::integer::u8");
assertType<CairoInt>("core::integer::u16");
assertType<CairoInt>("core::integer::u32");
assertType<CairoBigInt>("core::integer::u64");
assertType<CairoBigInt>("core::integer::u128");
assertType<CairoBigInt>("core::integer::u256");
assertType<CairoAddress>("core::starknet::ContractAddress");
assertType<CairoFunction>("function");
assertType<CairoVoid>("()");
assertType<CairoTuple>("()");
assertType<CairoTuple>("(1)");
assertType<CairoTuple>("(1, 2n)");
assertType<CairoTuple>("(1, 2n, 'string')");
assertType<CairoTuple>("(1, 2, 3, 4, 5)");
})
test("Cairo Types Errors", () => {
// @ts-expect-error
assertType<CairoFelt>("core::integer:u8");
// @ts-expect-error
assertType<CairoInt>("core::integer::u64");
// @ts-expect-error
assertType<CairoInt>("core::integer::u128");
// @ts-expect-error
assertType<CairoInt>("core::integer::u256");
// @ts-expect-error
assertType<CairoBigInt>("core::integer::u8");
// @ts-expect-error
assertType<CairoBigInt>("core::integer::u16");
// @ts-expect-error
assertType<CairoBigInt>("core::integer::u32");
// @ts-expect-error
assertType<CairoAddress>("core::felt");
// @ts-expect-error
assertType<CairoFunction>("core::integer::u8");
// @ts-expect-error
assertType<CairoVoid>("function");
// @ts-expect-error
assertType<CairoTuple>("(");
})
test("FunctionArgs", () => {
// inputs: felt, felt, u8, u256, ContractAddress
assertType<FunctionArgs<TAbi, 'constructor'>>(
[bigIntValue, bigIntValue, intValue, bigIntValue, bigIntValue]
);
// inputs: []
assertType<FunctionArgs<TAbi, 'get_name'>>(emptyArray);
assertType<FunctionArgs<TAbi, 'get_symbol'>>(emptyArray);
assertType<FunctionArgs<TAbi, 'get_decimals'>>(emptyArray);
assertType<FunctionArgs<TAbi, 'get_total_supply'>>(emptyArray);
// inputs: ContractAddress
assertType<FunctionArgs<TAbi, 'balance_of'>>(bigIntValue);
// inputs: ContractAddress, ContractAddress, u256
assertType<FunctionArgs<TAbi, 'transfer_from'>>([bigIntValue, bigIntValue, bigIntValue]);
// inputs: ContractAddress, u256
assertType<FunctionArgs<TAbi, 'transfer'>>([bigIntValue, bigIntValue]);
assertType<FunctionArgs<TAbi, 'approve'>>([bigIntValue, bigIntValue]);
assertType<FunctionArgs<TAbi, 'increase_allowance'>>([bigIntValue, bigIntValue]);
assertType<FunctionArgs<TAbi, 'decrease_allowance'>>([bigIntValue, bigIntValue]);
// inputs: ContractAddress, ContractAddress
assertType<FunctionArgs<TAbi, 'allowance'>>([bigIntValue, bigIntValue]);
})
test("FunctionArgs Errors", () => {
// inputs: felt, felt, u8, u256, ContractAddress
assertType<FunctionArgs<TAbi, 'constructor'>>(
// @ts-expect-error constructor has 5 arguments
[bigIntValue, bigIntValue, intValue, bigIntValue, bigIntValue, intValue]
);
// inputs: []
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'get_name'>>(intValue);
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'get_symbol'>>(voidValue);
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'get_decimals'>>(intValue);
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'get_total_supply'>>(bigIntValue);
// inputs: ContractAddress
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'balance_of'>>(voidValue);
// inputs: ContractAddress, ContractAddress, u256
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'transfer_from'>>([bigIntValue, intValue, intValue]);
// inputs: ContractAddress, u256
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'transfer'>>([intValue, bigIntValue]);
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'approve'>>([bigIntValue, intValue]);
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'increase_allowance'>>([bigIntValue, voidValue]);
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'decrease_allowance'>>([bigIntValue, emptyArray]);
// inputs: ContractAddress, ContractAddress
// @ts-expect-error
assertType<FunctionArgs<TAbi, 'allowance'>>([intValue, intValue]);
})
test("FunctionRet", () => {
// output: ()
assertType<FunctionRet<TAbi, 'constructor'>>(voidValue);
assertType<FunctionRet<TAbi, 'transfer'>>(voidValue);
assertType<FunctionRet<TAbi, 'approve'>>(voidValue);
assertType<FunctionRet<TAbi, 'increase_allowance'>>(voidValue);
assertType<FunctionRet<TAbi, 'decrease_allowance'>>(voidValue);
assertType<FunctionRet<TAbi, 'transfer_from'>>(voidValue);
// output: felt
assertType<FunctionRet<TAbi, 'get_name'>>(bigIntValue);
assertType<FunctionRet<TAbi, 'get_symbol'>>(bigIntValue);
// output: u8
assertType<FunctionRet<TAbi, 'get_decimals'>>(intValue);
// output: u256
assertType<FunctionRet<TAbi, 'get_total_supply'>>(bigIntValue);
// inputs: u256
assertType<FunctionRet<TAbi, 'balance_of'>>(bigIntValue);
// output: u256
assertType<FunctionRet<TAbi, 'allowance'>>(bigIntValue);
})
test("FunctionRet Errors", () => {
// output: ()
// @ts-expect-error
assertType<FunctionRet<TAbi, 'constructor'>>(intValue);
// @ts-expect-error
assertType<FunctionRet<TAbi, 'transfer'>>(bigIntValue);
// @ts-expect-error
assertType<FunctionRet<TAbi, 'approve'>>(emptyArray);
// @ts-expect-error
assertType<FunctionRet<TAbi, 'increase_allowance'>>(intValue);
// @ts-expect-error
assertType<FunctionRet<TAbi, 'decrease_allowance'>>(emptyArray);
// @ts-expect-error
assertType<FunctionRet<TAbi, 'transfer_from'>>(intValue);
// output: felt
// @ts-expect-error
assertType<FunctionRet<TAbi, 'get_name'>>([intValue, intValue]);
// @ts-expect-error
assertType<FunctionRet<TAbi, 'get_symbol'>>(voidValue);
// output: u8
// @ts-expect-error
assertType<FunctionRet<TAbi, 'get_decimals'>>(emptyArray);
// output: u256
// @ts-expect-error
assertType<FunctionRet<TAbi, 'get_total_supply'>>(intValue);
// inputs: u256
// @ts-expect-error
assertType<FunctionRet<TAbi, 'balance_of'>>([intValue, bigIntValue]);
// output: u256
// @ts-expect-error
assertType<FunctionRet<TAbi, 'allowance'>>([voidValue, intValue]);
})
test("ExtractAbiFunction", () => {
type Expected = {
readonly type: "function";
readonly name: "balance_of";
readonly inputs: readonly [{
readonly name: "account";
readonly ty: "core::starknet::ContractAddress";
}];
readonly output_ty: "core::integer::u256";
readonly state_mutability: "view";
};
expectTypeOf<ExtractAbiFunction<TAbi, "balance_of">>().toEqualTypeOf<Expected>();
})
test("ExtractAbiFunctionName", () => {
type Expected =
| "balance_of"
| "constructor"
| "get_name"
| "get_symbol"
| "get_decimals"
| "get_total_supply"
| "allowance"
| "transfer"
| "transfer_from"
| "approve"
| "increase_allowance"
| "decrease_allowance"
| "test_option"
| "test_array"
| "test_array_of_options"
| "test_array_of_options_of_arrays";
expectTypeOf<ExtractAbiFunctionNames<TAbi>>().toEqualTypeOf<Expected>();
})
test("AbiTypeToPrimitiveType", () => {
assertType<AbiTypeToPrimitiveType<TAbi, CairoFelt>>(bigIntValue);
assertType<AbiTypeToPrimitiveType<TAbi, CairoInt>>(intValue);
assertType<AbiTypeToPrimitiveType<TAbi, CairoInt>>(bigIntValue);
assertType<AbiTypeToPrimitiveType<TAbi, CairoBigInt>>(bigIntValue);
assertType<AbiTypeToPrimitiveType<TAbi, CairoAddress>>(bigIntValue);
assertType<AbiTypeToPrimitiveType<TAbi, CairoFunction>>(intValue);
assertType<AbiTypeToPrimitiveType<TAbi, CairoVoid>>(voidValue);
// Tuple isn't implemented yet, it always returns any
assertType<AbiTypeToPrimitiveType<TAbi, CairoTuple>>(intValue);
})
test("AbiTypeToPrimitiveType Errors", () => {
// @ts-expect-error CairoFelt should be bigint
assertType<AbiTypeToPrimitiveType<TAbi, CairoFelt>>(intValue);
// @ts-expect-error CairoInt should be number or bigint
assertType<AbiTypeToPrimitiveType<TAbi, CairoInt>>(voidValue);
// @ts-expect-error CairoBigInt should be bigint
assertType<AbiTypeToPrimitiveType<TAbi, CairoBigInt>>(intValue);
// @ts-expect-error CairoAddress should be bigint
assertType<AbiTypeToPrimitiveType<TAbi, CairoAddress>>(intValue);
// @ts-expect-error CairoFunction should be int
assertType<AbiTypeToPrimitiveType<TAbi, CairoFunction>>(bigIntValue);
// @ts-expect-error CairoVoid should be void
assertType<AbiTypeToPrimitiveType<TAbi, CairoVoid>>(intValue);
})
test("AbiParameterToPrimitiveType", () => {
// TODO: add tests for struct AbiParameter
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoFelt, name: 'x'}>>(bigIntValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoFelt, name: 'x'}>>(bigIntValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoInt, name: 'x'}>>(intValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoInt, name: 'x'}>>(bigIntValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoBigInt, name: 'x'}>>(bigIntValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoAddress, name: 'x'}>>(bigIntValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoFunction, name: 'x'}>>(intValue);
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoVoid, name: 'x'}>>(voidValue);
// Tuple ins't yet implemented, it always returns any
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoTuple, name: 'x'}>>(intValue);
})
test("AbiParameterToPrimitiveType Errors", () => {
// TODO: add tests for struct AbiParameter
// @ts-expect-error CairoFelt should be bigint
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoFelt, name: 'x'}>>(intValue);
// @ts-expect-error CairoInt should be number or bigint
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoInt, name: 'x'}>>(voidValue);
// @ts-expect-error CairoBigInt should be bigint
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoBigInt, name: 'x'}>>(intValue);
// @ts-expect-error CairoAddress should be bigint
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoAddress, name: 'x'}>>(intValue);
// @ts-expect-error CairoFunction should be int
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoFunction, name: 'x'}>>(bigIntValue);
// @ts-expect-error CairoVoid should be void
assertType<AbiParameterToPrimitiveType<TAbi, {ty: CairoVoid, name: 'x'}>>(intValue);
})