-
Notifications
You must be signed in to change notification settings - Fork 35
/
soroban_test_parser.dart
239 lines (227 loc) · 8.01 KB
/
soroban_test_parser.dart
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
import 'package:flutter_test/flutter_test.dart';
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';
void main() {
String contractPath =
"/Users/chris/Soneso/github/stellar_flutter_sdk/test/wasm/soroban_token_contract.wasm";
String _getSpecTypeInfo(XdrSCSpecTypeDef specType) {
switch(specType.discriminant) {
case XdrSCSpecType.SC_SPEC_TYPE_VAL:
return "val";
case XdrSCSpecType.SC_SPEC_TYPE_BOOL:
return "bool";
case XdrSCSpecType.SC_SPEC_TYPE_VOID:
return "void";
case XdrSCSpecType.SC_SPEC_TYPE_ERROR:
return "error";
case XdrSCSpecType.SC_SPEC_TYPE_U32:
return "u32";
case XdrSCSpecType.SC_SPEC_TYPE_I32:
return "i32";
case XdrSCSpecType.SC_SPEC_TYPE_U64:
return "u64";
case XdrSCSpecType.SC_SPEC_TYPE_I64:
return "i64";
case XdrSCSpecType.SC_SPEC_TYPE_TIMEPOINT:
return "timepoint";
case XdrSCSpecType.SC_SPEC_TYPE_DURATION:
return "duration";
case XdrSCSpecType.SC_SPEC_TYPE_U128:
return "u128";
case XdrSCSpecType.SC_SPEC_TYPE_I128:
return "i128";
case XdrSCSpecType.SC_SPEC_TYPE_U256:
return "u256";
case XdrSCSpecType.SC_SPEC_TYPE_I256:
return "i256";
case XdrSCSpecType.SC_SPEC_TYPE_BYTES:
return "bytes";
case XdrSCSpecType.SC_SPEC_TYPE_STRING:
return "string";
case XdrSCSpecType.SC_SPEC_TYPE_SYMBOL:
return "symbol";
case XdrSCSpecType.SC_SPEC_TYPE_ADDRESS:
return "address";
case XdrSCSpecType.SC_SPEC_TYPE_OPTION:
var valueType = _getSpecTypeInfo(specType.option!.valueType);
return "option (value type: $valueType)";
case XdrSCSpecType.SC_SPEC_TYPE_RESULT:
var okType = _getSpecTypeInfo(specType.result!.okType);
var errorType = _getSpecTypeInfo(specType.result!.errorType);
return "result (ok type: $okType , error type: $errorType)";
case XdrSCSpecType.SC_SPEC_TYPE_VEC:
var elementType = _getSpecTypeInfo(specType.vec!.elementType);
return "vec (element type: $elementType)";
case XdrSCSpecType.SC_SPEC_TYPE_MAP:
var keyType = _getSpecTypeInfo(specType.map!.keyType);
var valueType = _getSpecTypeInfo(specType.map!.valueType);
return "map (key type: $keyType , value type: $valueType)";
case XdrSCSpecType.SC_SPEC_TYPE_TUPLE:
var valueTypesStr = "[";
for (var valueType in specType.tuple!.valueTypes) {
valueTypesStr += "${_getSpecTypeInfo(valueType)},";
}
valueTypesStr += "]";
return "tuple (value types: $valueTypesStr)";
case XdrSCSpecType.SC_SPEC_TYPE_BYTES_N:
return "bytesN (n: ${specType.bytesN!.n.uint32})";
case XdrSCSpecType.SC_SPEC_TYPE_UDT:
return "udt (name: ${specType.udt!.name})";
default:
return "unknown";
}
}
_printFunction(XdrSCSpecFunctionV0 function) {
print("Function: ${function.name}");
var index = 0;
for (var input in function.inputs) {
print("input[$index] name: ${input.name}");
print("input[$index] type: ${_getSpecTypeInfo(input.type)}");
if (input.doc.length > 0) {
print("input[$index] doc: ${input.doc}");
}
index ++;
}
index = 0;
for (var output in function.outputs) {
print("output[$index] type: ${_getSpecTypeInfo(output)}");
index ++;
}
if (function.doc.length > 0) {
print("doc : ${function.doc}");
}
}
_printUdtStruct(XdrSCSpecUDTStructV0 udtStruct) {
print("UDT Struct: ${udtStruct.name}");
if (udtStruct.lib.length > 0) {
print("lib : ${udtStruct.lib}");
}
var index = 0;
for (var field in udtStruct.fields) {
print("field[$index] name: ${field.name}");
print("field[$index] type: ${_getSpecTypeInfo(field.type)}");
if (field.doc.length > 0) {
print("field[$index] doc: ${field.doc}");
}
index ++;
}
if (udtStruct.doc.length > 0) {
print("doc : ${udtStruct.doc})");
}
}
_printUdtUnion(XdrSCSpecUDTUnionV0 udtUnion) {
print("UDT Union: ${udtUnion.name}");
if (udtUnion.lib.length > 0) {
print("lib : ${udtUnion.lib}");
}
var index = 0;
for (var uCase in udtUnion.cases) {
switch(uCase.discriminant) {
case XdrSCSpecUDTUnionCaseV0Kind.SC_SPEC_UDT_UNION_CASE_VOID_V0:
print("case[$index] is voidV0");
print("case[$index] name: ${uCase.voidCase!.name}");
if (uCase.voidCase!.doc.length > 0) {
print("case[$index] doc: ${uCase.voidCase!.doc}");
}
break;
case XdrSCSpecUDTUnionCaseV0Kind.SC_SPEC_UDT_UNION_CASE_TUPLE_V0:
print("case[$index] is tupleV0");
print("case[$index] name: ${uCase.tupleCase!.name}");
var valueTypesStr = "[";
for (var valueType in uCase.tupleCase!.type) {
valueTypesStr += "${_getSpecTypeInfo(valueType)},";
}
valueTypesStr += "]";
print("case[$index] types: $valueTypesStr");
if (uCase.tupleCase!.doc.length > 0) {
print("case[$index] doc: ${uCase.tupleCase!.doc}");
}
break;
}
index ++;
}
if (udtUnion.doc.length > 0) {
print("doc : ${udtUnion.doc})");
}
}
_printUdtEnum(XdrSCSpecUDTEnumV0 udtEnum) {
print("UDT Enum : ${udtEnum.name}");
if (udtEnum.lib.length > 0) {
print("lib : ${udtEnum.lib}");
}
var index = 0;
for (var uCase in udtEnum.cases) {
print("case[$index] name: ${uCase.name}");
print("case[$index] value: ${uCase.value}");
if (uCase.doc.length > 0) {
print("case[$index] doc: ${uCase.doc}");
}
index ++;
}
if (udtEnum.doc.length > 0) {
print("doc : ${udtEnum.doc}");
}
}
_printUdtErrorEnum(XdrSCSpecUDTErrorEnumV0 udtErrorEnum) {
print("UDT Error Enum : ${udtErrorEnum.name}");
if (udtErrorEnum.lib.length > 0) {
print("lib : ${udtErrorEnum.lib}");
}
var index = 0;
for (var uCase in udtErrorEnum.cases) {
print("case[$index] name: ${uCase.name}");
print("case[$index] value: ${uCase.value}");
if (uCase.doc.length > 0) {
print("case[$index] doc: ${uCase.doc}");
}
index ++;
}
if (udtErrorEnum.doc.length > 0) {
print("doc : ${udtErrorEnum.doc}");
}
}
test('test token contract parsing', () async {
var byteCode = await Util.readFile(contractPath);
var contractInfo = SorobanContractParser.parseContractByteCode(byteCode);
assert(contractInfo.specEntries.length == 17);
assert(contractInfo.metaEntries.length == 2);
print("--------------------------------");
print("Env Meta:");
print("");
print("Interface version: ${contractInfo.envInterfaceVersion}");
print("--------------------------------");
print("Contract Meta:");
print("");
for (var metaEntry in contractInfo.metaEntries.entries) {
print("${metaEntry.key}: ${metaEntry.value}");
}
print("--------------------------------");
print("Contract Spec:");
print("");
var specEntries = contractInfo.specEntries;
var index = 0;
for (var specEntry in specEntries) {
switch (specEntry.discriminant) {
case XdrSCSpecEntryKind.SC_SPEC_ENTRY_FUNCTION_V0:
_printFunction(specEntry.functionV0!);
break;
case XdrSCSpecEntryKind.SC_SPEC_ENTRY_UDT_STRUCT_V0:
_printUdtStruct(specEntry.udtStructV0!);
break;
case XdrSCSpecEntryKind.SC_SPEC_ENTRY_UDT_UNION_V0:
_printUdtUnion(specEntry.udtUnionV0!);
break;
case XdrSCSpecEntryKind.SC_SPEC_ENTRY_UDT_ENUM_V0:
_printUdtEnum(specEntry.udtEnumV0!);
break;
case XdrSCSpecEntryKind.SC_SPEC_ENTRY_UDT_ERROR_ENUM_V0:
_printUdtErrorEnum(specEntry.udtErrorEnumV0!);
break;
default:
print('specEntry [$index] -> kind(${specEntry.discriminant.value}): unknown');
}
print("");
index++;
}
print("--------------------------------");
});
}