shank-parse 2.0.0

A proc-macro crate that generates Rust client code from Shank/Anchor IDL JSON files for Solana programs.
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
445
446
shank_parse::shank_parse!("../idl/full_shank.json");

#[cfg(test)]
mod tests {
    use super::full_shank::accounts::*;
    use super::full_shank::errors;
    use super::full_shank::instructions::*;
    use super::full_shank::types::*;
    use super::full_shank::ID;
    use shank_parse::__private::Pubkey;

    // ── Program ID ────────────────────────────────────────────────────────────

    #[test]
    fn test_program_id() {
        assert_eq!(ID.to_string(), "FuLLShankNoteBoardProg111111111111111111111");
    }

    // ── types ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_user_status_from_u8() {
        assert_eq!(UserStatus::from_u8(0), Some(UserStatus::Active));
        assert_eq!(UserStatus::from_u8(1), Some(UserStatus::Suspended));
        assert_eq!(UserStatus::from_u8(99), None);
    }

    #[test]
    fn test_user_status_to_u8() {
        assert_eq!(UserStatus::Active.to_u8(), 0);
        assert_eq!(UserStatus::Suspended.to_u8(), 1);
    }

    #[test]
    fn test_post_status_from_u8() {
        assert_eq!(PostStatus::from_u8(0), Some(PostStatus::Published));
        assert_eq!(PostStatus::from_u8(1), Some(PostStatus::Deleted));
        assert_eq!(PostStatus::from_u8(5), None);
    }

    #[test]
    fn test_post_status_to_u8() {
        assert_eq!(PostStatus::Published.to_u8(), 0);
        assert_eq!(PostStatus::Deleted.to_u8(), 1);
    }

    // ── errors ────────────────────────────────────────────────────────────────

    #[test]
    fn test_error_codes() {
        assert_eq!(errors::NOT_ADMIN, 0);
        assert_eq!(errors::BOARD_ALREADY_INITIALIZED, 1);
        assert_eq!(errors::BOARD_NOT_INITIALIZED, 2);
        assert_eq!(errors::PROFILE_ALREADY_EXISTS, 3);
        assert_eq!(errors::PROFILE_NOT_FOUND, 4);
        assert_eq!(errors::NOT_AUTHOR, 5);
        assert_eq!(errors::POST_ALREADY_DELETED, 6);
        assert_eq!(errors::USER_SUSPENDED, 7);
        assert_eq!(errors::NUMERICAL_OVERFLOW, 8);
    }

    // ── accounts ─────────────────────────────────────────────────────────────

    #[test]
    fn test_board_config_from_account_data() {
        let admin = [1u8; 32];
        let post_fee: u64 = 5_000_000;
        let post_count: u64 = 42;
        let is_paused = false;
        let bump: u8 = 254;

        let mut data = Vec::new();
        data.extend_from_slice(&admin);
        data.extend_from_slice(&post_fee.to_le_bytes());
        data.extend_from_slice(&post_count.to_le_bytes());
        data.push(is_paused as u8);
        data.push(bump);

        let config = BoardConfig::from_account_data(&data).unwrap();
        assert_eq!(config.admin, admin);
        assert_eq!(config.post_fee, post_fee);
        assert_eq!(config.post_count, post_count);
        assert_eq!(config.is_paused, is_paused);
        assert_eq!(config.bump, bump);
    }

    #[test]
    fn test_board_config_paused() {
        let mut data = vec![0u8; 32]; // admin
        data.extend_from_slice(&0u64.to_le_bytes()); // post_fee
        data.extend_from_slice(&0u64.to_le_bytes()); // post_count
        data.push(1u8); // is_paused = true
        data.push(1u8); // bump

        let config = BoardConfig::from_account_data(&data).unwrap();
        assert!(config.is_paused);
    }

    #[test]
    fn test_board_config_insufficient_data() {
        let data = vec![0u8; 10];
        assert!(BoardConfig::from_account_data(&data).is_err());
    }

    #[test]
    fn test_user_profile_active() {
        let authority = [2u8; 32];
        let username = [3u8; 32];
        let post_count: u32 = 7;
        let bump: u8 = 200;

        let mut data = Vec::new();
        data.extend_from_slice(&authority);
        data.extend_from_slice(&username);
        data.extend_from_slice(&post_count.to_le_bytes());
        data.push(UserStatus::Active.to_u8());
        data.push(bump);

        let profile = UserProfile::from_account_data(&data).unwrap();
        assert_eq!(profile.authority, authority);
        assert_eq!(profile.username, username);
        assert_eq!(profile.post_count, post_count);
        assert_eq!(profile.status, UserStatus::Active);
        assert_eq!(profile.bump, bump);
    }

    #[test]
    fn test_user_profile_suspended() {
        let mut data = vec![0u8; 32]; // authority
        data.extend_from_slice(&[0u8; 32]); // username
        data.extend_from_slice(&0u32.to_le_bytes()); // post_count
        data.push(UserStatus::Suspended.to_u8());
        data.push(1u8); // bump

        let profile = UserProfile::from_account_data(&data).unwrap();
        assert_eq!(profile.status, UserStatus::Suspended);
    }

    #[test]
    fn test_post_with_reply_to() {
        let author = [4u8; 32];
        let content = [b'X'; 128];
        let reply_to_key = [5u8; 32];
        let post_index: u32 = 99;
        let bump: u8 = 250;

        let mut data = Vec::new();
        data.extend_from_slice(&author);
        data.extend_from_slice(&content);
        data.push(1u8); // reply_to present
        data.extend_from_slice(&reply_to_key);
        data.push(PostStatus::Published.to_u8());
        data.extend_from_slice(&post_index.to_le_bytes());
        data.push(bump);

        let post = Post::from_account_data(&data).unwrap();
        assert_eq!(post.author, author);
        assert_eq!(post.content, content);
        assert_eq!(post.reply_to, Some(reply_to_key));
        assert_eq!(post.status, PostStatus::Published);
        assert_eq!(post.post_index, post_index);
        assert_eq!(post.bump, bump);
    }

    #[test]
    fn test_post_without_reply_to() {
        let mut data = Vec::new();
        data.extend_from_slice(&[1u8; 32]); // author
        data.extend_from_slice(&[0u8; 128]); // content
        data.push(0u8); // reply_to absent
        data.push(PostStatus::Deleted.to_u8());
        data.extend_from_slice(&0u32.to_le_bytes()); // post_index
        data.push(100u8); // bump

        let post = Post::from_account_data(&data).unwrap();
        assert_eq!(post.reply_to, None);
        assert_eq!(post.status, PostStatus::Deleted);
    }

    #[test]
    fn test_post_insufficient_data() {
        assert!(Post::from_account_data(&[0u8; 10]).is_err());
    }

    // ── instructions ─────────────────────────────────────────────────────────

    #[test]
    fn test_init_board_instruction() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = InitBoardAccounts {
            admin: Pubkey::new_from_array([2u8; 32]),
            config: Pubkey::new_from_array([3u8; 32]),
            system_program: Pubkey::new_from_array([4u8; 32]),
        };
        let args = InitBoardArgs { post_fee: 1_000_000 };
        let ix = init_board(&program_id, &accounts, &args).unwrap();

        assert_eq!(ix.program_id, program_id);
        assert_eq!(ix.accounts.len(), 3);
        assert_eq!(ix.data[0], 0); // discriminant
        assert_eq!(
            u64::from_le_bytes(ix.data[1..9].try_into().unwrap()),
            1_000_000
        );
        assert!(!ix.accounts[0].is_writable); // admin: not mut
        assert!(ix.accounts[0].is_signer); // admin: signer
        assert!(ix.accounts[1].is_writable); // config: mut
        assert!(!ix.accounts[1].is_signer); // config: not signer
        assert!(!ix.accounts[2].is_writable); // system_program: not mut
        assert!(!ix.accounts[2].is_signer); // system_program: not signer
    }

    #[test]
    fn test_create_profile_instruction() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = CreateProfileAccounts {
            payer: Pubkey::new_from_array([2u8; 32]),
            profile: Pubkey::new_from_array([3u8; 32]),
            referrer: Pubkey::new_from_array([4u8; 32]),
            system_program: Pubkey::new_from_array([5u8; 32]),
        };
        let username = [b'A'; 32];
        let args = CreateProfileArgs { username };
        let ix = create_profile(&program_id, &accounts, &args).unwrap();

        assert_eq!(ix.data[0], 1); // discriminant
        assert_eq!(&ix.data[1..33], &username);
        assert_eq!(ix.accounts.len(), 4);
        assert!(ix.accounts[0].is_signer); // payer
        assert!(ix.accounts[1].is_writable); // profile
        assert!(!ix.accounts[2].is_signer); // referrer: not signer
        assert!(!ix.accounts[2].is_writable); // referrer: not mut
    }

    #[test]
    fn test_post_message_no_reply_to() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = PostMessageAccounts {
            author: Pubkey::new_from_array([2u8; 32]),
            profile: Pubkey::new_from_array([3u8; 32]),
            post: Pubkey::new_from_array([4u8; 32]),
            system_program: Pubkey::new_from_array([5u8; 32]),
        };
        let args = PostMessageArgs {
            content: [b'M'; 128],
            reply_to: None,
        };
        let ix = post_message(&program_id, &accounts, &args).unwrap();

        assert_eq!(ix.data[0], 2); // discriminant
        assert_eq!(&ix.data[1..129], &[b'M'; 128]); // content
        assert_eq!(ix.data[129], 0); // reply_to absent
        assert_eq!(ix.data.len(), 130);
        assert_eq!(ix.accounts.len(), 4);
        assert!(ix.accounts[0].is_signer); // author
        assert!(ix.accounts[1].is_writable); // profile
        assert!(ix.accounts[2].is_writable); // post
    }

    #[test]
    fn test_post_message_with_reply_to() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = PostMessageAccounts {
            author: Pubkey::new_from_array([2u8; 32]),
            profile: Pubkey::new_from_array([3u8; 32]),
            post: Pubkey::new_from_array([4u8; 32]),
            system_program: Pubkey::new_from_array([5u8; 32]),
        };
        let reply_key = [9u8; 32];
        let args = PostMessageArgs {
            content: [0u8; 128],
            reply_to: Some(reply_key),
        };
        let ix = post_message(&program_id, &accounts, &args).unwrap();

        assert_eq!(ix.data[129], 1); // reply_to present
        assert_eq!(&ix.data[130..162], &reply_key);
        assert_eq!(ix.data.len(), 162);
    }

    #[test]
    fn test_update_username_instruction() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = UpdateUsernameAccounts {
            authority: Pubkey::new_from_array([2u8; 32]),
            profile: Pubkey::new_from_array([3u8; 32]),
        };
        let new_name = [b'Z'; 32];
        let args = UpdateUsernameArgs { username: new_name };
        let ix = update_username(&program_id, &accounts, &args).unwrap();

        assert_eq!(ix.data[0], 3); // discriminant
        assert_eq!(&ix.data[1..33], &new_name);
        assert_eq!(ix.accounts.len(), 2);
        assert!(ix.accounts[0].is_signer); // authority
        assert!(ix.accounts[1].is_writable); // profile
    }

    #[test]
    fn test_delete_post_instruction() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = DeletePostAccounts {
            authority: Pubkey::new_from_array([2u8; 32]),
            post: Pubkey::new_from_array([3u8; 32]),
            refund_to: Pubkey::new_from_array([4u8; 32]),
            config: Pubkey::new_from_array([5u8; 32]),
        };
        let ix = delete_post(&program_id, &accounts).unwrap();

        assert_eq!(ix.data, vec![4u8]); // discriminant only
        assert_eq!(ix.accounts.len(), 4);
        assert!(ix.accounts[0].is_signer); // authority
        assert!(ix.accounts[1].is_writable); // post
        assert!(ix.accounts[2].is_writable); // refund_to
        assert!(!ix.accounts[3].is_writable); // config: not mut
    }

    #[test]
    fn test_suspend_profile_instruction() {
        let program_id = Pubkey::new_from_array([1u8; 32]);
        let accounts = SuspendProfileAccounts {
            admin: Pubkey::new_from_array([2u8; 32]),
            config: Pubkey::new_from_array([3u8; 32]),
            profile: Pubkey::new_from_array([4u8; 32]),
            co_admin: Pubkey::new_from_array([5u8; 32]),
        };
        let ix = suspend_profile(&program_id, &accounts).unwrap();

        assert_eq!(ix.data, vec![5u8]); // discriminant only
        assert_eq!(ix.accounts.len(), 4);
        assert!(ix.accounts[0].is_signer); // admin
        assert!(ix.accounts[1].is_writable); // config
        assert!(ix.accounts[2].is_writable); // profile
        assert!(!ix.accounts[3].is_signer); // co_admin: not signer
    }

    // ── events ───────────────────────────────────────────────────────────────

    // Event payload structs decode via `try_from_slice` from the raw (already
    // base64-decoded) event bytes, WITHOUT a leading discriminant byte.

    #[test]
    fn test_board_initialized_try_from_slice() {
        let admin = [10u8; 32];
        let post_fee: u64 = 500_000;

        let mut payload = Vec::new();
        payload.extend_from_slice(&admin);
        payload.extend_from_slice(&post_fee.to_le_bytes());

        let ev = BoardInitialized::try_from_slice(&payload).unwrap();
        assert_eq!(ev.admin, admin);
        assert_eq!(ev.post_fee, post_fee);
    }

    #[test]
    fn test_message_posted_try_from_slice_no_reply() {
        let author = [15u8; 32];
        let post_index: u32 = 42;

        let mut payload = Vec::new();
        payload.extend_from_slice(&author);
        payload.extend_from_slice(&post_index.to_le_bytes());
        payload.push(0u8); // reply_to absent

        let ev = MessagePosted::try_from_slice(&payload).unwrap();
        assert_eq!(ev.author, author);
        assert_eq!(ev.post_index, post_index);
        assert_eq!(ev.reply_to, None);
    }

    #[test]
    fn test_message_posted_try_from_slice_with_reply() {
        let author = [16u8; 32];
        let post_index: u32 = 7;
        let reply_to = [17u8; 32];

        let mut payload = Vec::new();
        payload.extend_from_slice(&author);
        payload.extend_from_slice(&post_index.to_le_bytes());
        payload.push(1u8); // reply_to present
        payload.extend_from_slice(&reply_to);

        let ev = MessagePosted::try_from_slice(&payload).unwrap();
        assert_eq!(ev.reply_to, Some(reply_to));
    }

    // The event enum's `try_from_slice` reads the leading u8 variant tag
    // (the discriminant) and dispatches to the matching payload struct.

    #[test]
    fn test_board_event_enum_dispatch() {
        // BoardInitialized (tag 0)
        let mut data0 = vec![0u8];
        data0.extend_from_slice(&[1u8; 32]);
        data0.extend_from_slice(&100u64.to_le_bytes());
        match BoardEvent::try_from_slice(&data0).unwrap() {
            BoardEvent::BoardInitialized(e) => assert_eq!(e.post_fee, 100),
            other => panic!("expected BoardInitialized, got {other:?}"),
        }

        // ProfileCreated (tag 1)
        let mut data1 = vec![1u8];
        data1.extend_from_slice(&[2u8; 32]);
        data1.extend_from_slice(&[3u8; 32]);
        match BoardEvent::try_from_slice(&data1).unwrap() {
            BoardEvent::ProfileCreated(e) => assert_eq!(e.authority, [2u8; 32]),
            other => panic!("expected ProfileCreated, got {other:?}"),
        }

        // MessagePosted (tag 3) with reply_to = None
        let mut data3 = vec![3u8];
        data3.extend_from_slice(&[4u8; 32]);
        data3.extend_from_slice(&5u32.to_le_bytes());
        data3.push(0u8);
        match BoardEvent::try_from_slice(&data3).unwrap() {
            BoardEvent::MessagePosted(e) => {
                assert_eq!(e.post_index, 5);
                assert_eq!(e.reply_to, None);
            }
            other => panic!("expected MessagePosted, got {other:?}"),
        }

        // ProfileSuspended (tag 5)
        let mut data5 = vec![5u8];
        data5.extend_from_slice(&[99u8; 32]);
        match BoardEvent::try_from_slice(&data5).unwrap() {
            BoardEvent::ProfileSuspended(e) => assert_eq!(e.profile_authority, [99u8; 32]),
            other => panic!("expected ProfileSuspended, got {other:?}"),
        }
    }

    #[test]
    fn test_board_event_rejects_unknown_tag() {
        // tag 99 is not a declared variant
        let mut data = vec![99u8];
        data.extend_from_slice(&[0u8; 40]);
        assert!(BoardEvent::try_from_slice(&data).is_err());
    }

    #[test]
    fn test_board_event_rejects_truncated_payload() {
        // valid tag 0 but missing the BoardInitialized payload
        assert!(BoardEvent::try_from_slice(&[0u8]).is_err());
    }
}