solify-common 0.1.0

Shared types, errors, and utilities for Solify
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};


#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlData {
    pub name: String,
    pub version: String,
    pub instructions: Vec<IdlInstruction>,
    #[serde(default)]
    pub accounts: Vec<IdlAccount>,
    #[serde(default)]
    pub types: Vec<IdlTypeDef>,
    #[serde(default)]
    pub errors: Vec<IdlError>,
    #[serde(default)]
    pub constants: Vec<IdlConstant>,
    #[serde(default)]
    pub events: Vec<IdlEvent>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlError {
    pub code: u32,
    pub name: String,
    pub msg: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlConstant {
    pub name: String,
    pub constant_type: String,
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlEvent {
    pub name: String,
    #[serde(default)]
    pub discriminator: Vec<u8>,
    pub fields: Vec<IdlField>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlInstruction {
    pub name: String,
    pub accounts: Vec<IdlAccountItem>,
    pub args: Vec<IdlField>,
    pub docs: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlAccountItem {
    pub name: String,
    pub is_mut: bool,
    pub is_signer: bool,
    pub is_optional: bool,
    pub docs: Vec<String>,
    pub pda: Option<IdlPda>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlPda {
    pub seeds: Vec<IdlSeed>,
    #[serde(default)]
    pub program: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlSeed {
    pub kind: String,
    #[serde(default)]
    pub path: String,
    #[serde(default)]
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlAccount {
    pub name: String,
    pub fields: Vec<IdlField>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlField {
    pub name: String,
    pub field_type: String, 
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct IdlTypeDef {
    pub name: String,
    pub kind: String, 
    pub fields: Vec<String>, 
}



#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct ParsedIdl {
    #[serde(default)]
    pub address: String,
    #[serde(default)]
    pub metadata: IdlMetadata,
    pub instructions: Vec<Instruction>,
    #[serde(default)]
    pub accounts: Vec<AccountDef>,
    #[serde(default)]
    pub types: Vec<TypeDef>,
    #[serde(default)]
    pub errors: Vec<ErrorDef>,
    #[serde(default)]
    pub constants: Vec<ConstantDef>,
    #[serde(default)]
    pub events: Vec<EventDef>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct ErrorDef {
    pub code: u32,
    pub name: String,
    #[serde(default)]
    pub msg: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct ConstantDef {
    pub name: String,
    #[serde(rename = "type")]
    pub constant_type: String,
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct EventDef {
    pub name: String,
    #[serde(default)]
    pub discriminator: Vec<u8>,
    #[serde(default)]
    pub fields: Vec<FieldDef>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize, Default)]
pub struct IdlMetadata {
    pub name: String,
    pub version: String,
    #[serde(default)]
    pub spec: String,
    #[serde(default)]
    pub description: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct Instruction {
    pub name: String,
    #[serde(default)]
    pub discriminator: Vec<u8>,
    pub accounts: Vec<AccountInfo>,
    pub args: Vec<ArgumentDef>,
    #[serde(default)]
    pub docs: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct AccountInfo {
    pub name: String,
    #[serde(default)]
    pub writable: bool,
    #[serde(default)]
    pub signer: bool,
    #[serde(default)]
    pub optional: bool,
    #[serde(default)]
    pub address: Option<String>,
    #[serde(default)]
    pub pda: Option<PdaConfig>,
    #[serde(default)]
    pub docs: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct PdaConfig {
    pub seeds: Vec<PdaSeed>,
    #[serde(default)]
    pub program: Option<PdaProgram>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct PdaProgram {
    pub kind: String,
    #[serde(default)]
    pub value: Option<Vec<u8>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct PdaSeed {
    pub kind: String,
    #[serde(default)]
    pub path: String,
    #[serde(default)]
    pub value: Option<Vec<u8>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct ArgumentDef {
    pub name: String,
    
    #[serde(rename = "type")]
    pub arg_type: IdlType,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(untagged)]
pub enum IdlType {
    Simple(String),
    Vec {
        vec: Box<IdlType>
    },
    Option {
        option: Box<IdlType>
    },
    Array {
        array: (Box<IdlType>, usize)
    },
    Defined {
        defined: DefinedType
    },
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(untagged)]
pub enum DefinedType {
    Simple(String),
    Generic {
        name: String,
        #[serde(default)]
        generics: Vec<IdlType>,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct AccountDef {
    pub name: String,
    
    #[serde(default)]
    pub discriminator: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct FieldDef {
    pub name: String,
    
    #[serde(rename = "type")]
    pub field_type: IdlType,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct TypeDef {
    pub name: String,
    
    #[serde(rename = "type")]
    pub type_kind: TypeKind,
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(tag = "kind")]
pub enum TypeKind {
    #[serde(rename = "struct")]
    Struct { 
        fields: Vec<FieldDef> 
    },
    
    #[serde(rename = "enum")]
    Enum { 
        variants: Vec<EnumVariant> 
    },
}

#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct EnumVariant {
    pub name: String,
    
    #[serde(default)]
    pub fields: Option<Vec<FieldDef>>,
}


#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub struct TestMetadata {
    pub instruction_order: Vec<String>,
    pub account_dependencies: Vec<AccountDependency>,
    pub pda_init_sequence: Vec<PdaInit>,
    pub setup_requirements: Vec<SetupRequirement>,
    pub test_cases: Vec<InstructionTestCases>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub struct AccountDependency {
    pub account_name: String,
    pub depends_on: Vec<String>,
    pub is_pda: bool,
    pub is_signer: bool,
    pub is_mut: bool,
    pub must_be_initialized: bool,
    pub initialization_order: u8,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub struct PdaInit {
    pub account_name: String,
    pub seeds: Vec<SeedComponent>,
    pub program_id: String, // Program ID as a string
    pub space: Option<u64>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub struct SeedComponent {
    pub seed_type: SeedType,
    pub value: String,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub enum SeedType {
    Static,
    AccountKey,
    Argument,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub struct SetupRequirement {
    pub requirement_type: SetupType,
    pub description: String,
    pub dependencies: Vec<String>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq)]
pub enum SetupType {
    CreateKeypair,
    FundAccount,
    InitializePda,
    MintTokens,
    CreateAta,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub struct InstructionTestCases {
    pub instruction_name: String,
    pub arguments: Vec<ArgumentInfo>,
    pub positive_cases: Vec<TestCase>,
    pub negative_cases: Vec<TestCase>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub struct ArgumentInfo {
    pub name: String,
    pub arg_type: ArgumentType,
    pub constraints: Vec<ArgumentConstraint>,
    pub is_optional: bool,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub enum ArgumentType {
    U8,
    U16,
    U32,
    U64,
    U128,
    I8,
    I16,
    I32,
    I64,
    I128,
    Bool,
    String { max_length: Option<u32> },
    Pubkey,
    Vec {
        inner_type: Box<ArgumentType>,
        max_length: Option<u32>,
    },
    Option { inner_type: Box<ArgumentType> },
    Struct { name: String },
    Enum {
        name: String,
        variants: Vec<String>,
    },
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub enum ArgumentConstraint {
    Min { value: i64 },
    Max { value: i64 },
    Range { min: i64, max: i64 },
    NonZero,
    MaxLength { value: u32 },
    MinLength { value: u32 },
    Custom { description: String },
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub struct TestCase {
    pub test_type: TestCaseType,
    pub description: String,
    pub argument_values: Vec<TestArgumentValue>,
    pub expected_outcome: ExpectedOutcome,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub enum TestCaseType {
    Positive,
    NegativeBoundary,
    NegativeType,
    NegativeConstraint,
    NegativeNull,
    NegativeOverflow,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
pub struct TestArgumentValue {
    pub argument_name: String,
    pub value_type: TestValueType,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "variant")]
pub enum TestValueType {
    Valid { description: String },
    Invalid { description: String, reason: String },
}

#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "variant")]
pub enum ExpectedOutcome {
    Success { state_changes: Vec<String> },
    Failure {
        error_code: Option<String>,
        error_message: String,
    },
}




// --------------------------------------------------