squads-program 2.0.1

Squads is an on-chain program that allows team to manage digital assets together, create proposals, and more.
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
use arrayref::{array_mut_ref, array_ref, array_refs, mut_array_refs};
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
    program_error::ProgramError,
    program_pack::{IsInitialized, Pack, Sealed},
    pubkey::Pubkey,
};

use num_derive::FromPrimitive;
use std::convert::TryInto;

use crate::UnixTimestamp;

const PUBLIC_KEY_BYTES: usize = 32;
const TIMESTAMP_BYTES: usize = 8;

// proposal bytes
const PROPOSAL_SETTING_BYTES: usize = 1;
const PROPOSAL_EXECUTION_AMOUNT_BYTES: usize = 8;
const PROPOSAL_EXECUTION_SOURCE_BYTES: usize = PUBLIC_KEY_BYTES;
const PROPOSAL_EXECUTION_DESTINATION_BYTES: usize = PUBLIC_KEY_BYTES;
const PROPOSAL_TITLE_BYTES: usize = 36;
const PROPOSAL_DESCRIPTION_BYTES: usize = 496;
const PROPOSAL_LINK_BYTES: usize = 48;
const PROPOSAL_VOTE_OPTIONS_NUM: usize = 5;
const PROPOSAL_HAS_VOTED_NUM_BYTES: usize = 1;
const PROPOSAL_HAS_VOTED_BYTES: usize = (150 * PUBLIC_KEY_BYTES) + 4;
const PROPOSAL_OPTIONS_BYTES: usize = PROPOSAL_VOTE_OPTIONS_NUM * 8;
const PROPOSAL_OPTIONS_LABELS_BYTES: usize = PROPOSAL_VOTE_OPTIONS_NUM * 44;
const PROPOSAL_RESERVED_BYTES: usize = 8 * 16;
const SUPPLY_AT_EXECUTE_BYTES: usize = 8;
const MEMBERS_AT_EXECUTE_BYTES: usize = 1;
const THRESHOLD_AT_EXECUTE_BYTES: usize = 1;
const PROPOSAL_INDEX_BYTES: usize = 4;

#[derive(FromPrimitive)]
pub enum ProposalType {
    Text = 0,
    Support = 1,
    Quorum = 2,
    WithdrawSol = 3,
    WithdrawSpl = 4,
    AddMember = 5,
    RemoveMember = 6,
    MintMemberToken = 7,
    Swap = 8,
}

// PROPOSAL STRUCT
const PROPOSAL_TOTAL_BYTES: usize = PROPOSAL_SETTING_BYTES +                // is_initialized 1
    PROPOSAL_SETTING_BYTES +                // proposal_type 1
    PROPOSAL_EXECUTION_AMOUNT_BYTES +       // execution_amount 8
    PROPOSAL_EXECUTION_AMOUNT_BYTES +       // execution_amount_out 8
    PROPOSAL_EXECUTION_SOURCE_BYTES +       // execution_source 32
    PROPOSAL_EXECUTION_DESTINATION_BYTES +  // execution_source 32 
    PUBLIC_KEY_BYTES +                      // creator 32
    PUBLIC_KEY_BYTES +                      // squad_address 32
    PROPOSAL_TITLE_BYTES +                  // title of the proposal 36
    PROPOSAL_DESCRIPTION_BYTES +            // description of the proposal 496
    PROPOSAL_LINK_BYTES +                   // link bytes 48
    PROPOSAL_SETTING_BYTES +                // vote options num 1
    PROPOSAL_HAS_VOTED_NUM_BYTES +
    PROPOSAL_HAS_VOTED_BYTES +              // 100 * 32 + 4
    PROPOSAL_OPTIONS_BYTES +                // 5 * 4
    PROPOSAL_OPTIONS_LABELS_BYTES +         // 5 * 44 = 220
    TIMESTAMP_BYTES +                       // start date 8
    TIMESTAMP_BYTES +                       // proposal 8
    TIMESTAMP_BYTES +                       // created_on bytes 8
    SUPPLY_AT_EXECUTE_BYTES +               // supply_at_execute 8
    MEMBERS_AT_EXECUTE_BYTES +              // members_at_execute 1
    THRESHOLD_AT_EXECUTE_BYTES +              // members_at_execute 1
    PROPOSAL_SETTING_BYTES +                // executed 1
    PROPOSAL_SETTING_BYTES +                // execute_ready 1
    TIMESTAMP_BYTES +                       // execution_date bytes 8
    PROPOSAL_SETTING_BYTES +                // instruction_index 1
    PROPOSAL_SETTING_BYTES +                // multiple_choice 1
    PUBLIC_KEY_BYTES +                      // executed_by 32
    PROPOSAL_INDEX_BYTES +                  // the proposal index
    PROPOSAL_RESERVED_BYTES; // reserved for updates

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
pub struct Proposal {
    pub is_initialized: bool,
    // 0 - text proposal
    // 1 - change support
    // 2 - change quorum
    // 3 - emergency quorum
    // 4 - withdraw SOL
    // 5 - withdraw token
    // 6 - add member
    // 7 - remove member
    // 8 - mint more tokens to a member
    pub proposal_type: u8,
    pub execution_amount: u64,
    pub execution_amount_out: u64,
    pub execution_source: Pubkey,
    pub execution_destination: Pubkey,
    pub creator: Pubkey,
    pub squad_address: Pubkey,
    pub title: String,
    pub description: String,
    pub link: String,
    // number of vote options
    pub votes_num: u8,
    pub has_voted_num: u8,
    pub has_voted: Vec<Pubkey>,
    pub votes: Vec<u64>,
    // labels of the vote options
    pub votes_labels: Vec<String>,
    pub start_timestamp: UnixTimestamp,
    pub close_timestamp: UnixTimestamp,
    pub created_timestamp: UnixTimestamp,
    pub supply_at_execute: u64,
    pub members_at_execute: u8,
    pub threshold_at_execute: u8,
    pub executed: bool,
    pub execute_ready: bool,
    pub execution_date: UnixTimestamp,

    pub instruction_index: u8,
    pub multiple_choice: bool,

    pub executed_by: Pubkey,
    pub proposal_index: u32,
    // reserved for future updates
    pub reserved: [u64; 16],
}

impl Sealed for Proposal {}

impl IsInitialized for Proposal {
    fn is_initialized(&self) -> bool {
        self.is_initialized
    }
}

impl Proposal {
    pub fn save_text(
        &mut self,
        proposal_type: u8,
        title: String,
        description: String,
        link: String,
        initializer: &Pubkey,
        votes_num: u8,
        squad_account_key: &Pubkey,
        vote_labels: Vec<String>,
        start_timestamp: i64,
        close_timestamp: i64,
        created_timestamp: i64,
        proposal_index: u32,
    ) {
        self.is_initialized = true;
        self.proposal_type = proposal_type;
        self.title = title;
        self.description = description;
        self.link = link;
        self.creator = *initializer;
        self.votes_num = votes_num;
        self.squad_address = *squad_account_key;
        self.votes_labels = vote_labels;
        self.start_timestamp = start_timestamp;
        self.close_timestamp = close_timestamp;
        self.created_timestamp = created_timestamp;
        self.executed = false;
        self.execution_amount = 0;
        self.execute_ready = false;
        self.execution_date = 0 as i64;
        self.proposal_index = proposal_index;
    }

    pub fn save_core(
        &mut self,
        proposal_type: u8,
        title: String,
        description: String,
        link: String,
        initializer: &Pubkey,
        votes_num: u8,
        squad_account_key: &Pubkey,
        vote_labels: Vec<String>,
        start_timestamp: i64,
        close_timestamp: i64,
        created_timestamp: i64,
        amount: u64,
        proposal_index: u32,
    ) {
        self.is_initialized = true;
        self.proposal_type = proposal_type;
        self.title = title;
        self.description = description;
        self.link = link;
        self.creator = *initializer;
        self.votes_num = votes_num;
        self.squad_address = *squad_account_key;
        self.votes_labels = vote_labels;
        self.start_timestamp = start_timestamp;
        self.close_timestamp = close_timestamp;
        self.execution_amount = amount;
        self.created_timestamp = created_timestamp;
        self.executed = false;
        self.execute_ready = false;
        self.execution_date = 0 as i64;
        self.proposal_index = proposal_index;
    }

    pub fn save_withdraw(
        &mut self,
        proposal_type: u8,
        title: String,
        description: String,
        link: String,
        source: &Pubkey,
        destination: &Pubkey,
        initializer: &Pubkey,
        votes_num: u8,
        squad_account_key: &Pubkey,
        vote_labels: Vec<String>,
        start_timestamp: i64,
        close_timestamp: i64,
        created_timestamp: i64,
        amount: u64,
        proposal_index: u32,
    ) {
        self.is_initialized = true;
        self.proposal_type = proposal_type;
        self.title = title;
        self.description = description;
        self.link = link;
        self.execution_source = *source;
        self.execution_destination = *destination;
        self.creator = *initializer;
        self.votes_num = votes_num;
        self.squad_address = *squad_account_key;
        self.votes_labels = vote_labels;
        self.start_timestamp = start_timestamp;
        self.close_timestamp = close_timestamp;
        self.execution_amount = amount;
        self.created_timestamp = created_timestamp;
        self.executed = false;
        self.execute_ready = false;
        self.execution_date = 0 as i64;
        self.proposal_index = proposal_index;
    }

    pub fn save_member(
        &mut self,
        proposal_type: u8,
        title: String,
        description: String,
        link: String,
        member: &Pubkey,
        initializer: &Pubkey,
        votes_num: u8,
        squad_account: &Pubkey,
        vote_labels: Vec<String>,
        start_timestamp: i64,
        close_timestamp: i64,
        created_timestamp: i64,
        amount: u64,
        proposal_index: u32,
    ) {
        self.is_initialized = true;
        self.proposal_type = proposal_type;
        self.title = title;
        self.description = description;
        self.link = link;
        self.execution_source = *squad_account;
        self.execution_destination = *member;
        self.creator = *initializer;
        self.votes_num = votes_num;
        self.squad_address = *squad_account;
        self.votes_labels = vote_labels;
        self.start_timestamp = start_timestamp;
        self.close_timestamp = close_timestamp;
        self.created_timestamp = created_timestamp;
        self.executed = false;
        self.execution_amount = amount;
        self.execute_ready = false;
        self.execution_date = 0 as i64;
        self.proposal_index = proposal_index;
    }

    pub fn save_swap(
        &mut self,
        proposal_type: u8,
        title: String,
        description: String,
        link: String,
        source: &Pubkey,
        destination: &Pubkey,
        initializer: &Pubkey,
        votes_num: u8,
        squad_account: &Pubkey,
        vote_labels: Vec<String>,
        start_timestamp: i64,
        close_timestamp: i64,
        created_timestamp: i64,
        amount: u64,
        minimum_out: u64,
        proposal_index: u32,
    ) {
        self.is_initialized = true;
        self.proposal_type = proposal_type;
        self.title = title;
        self.description = description;
        self.link = link;
        self.execution_source = *source;
        self.execution_destination = *destination;
        self.creator = *initializer;
        self.votes_num = votes_num;
        self.squad_address = *squad_account;
        self.votes_labels = vote_labels;
        self.start_timestamp = start_timestamp;
        self.close_timestamp = close_timestamp;
        self.execution_amount = amount;
        self.execution_amount_out = minimum_out;
        self.created_timestamp = created_timestamp;
        self.executed = false;
        self.execute_ready = false;
        self.execution_date = 0 as i64;
        self.proposal_index = proposal_index;
    }
}

impl Pack for Proposal {
    const LEN: usize = PROPOSAL_TOTAL_BYTES;

    fn pack_into_slice(&self, dst: &mut [u8]) {
        let dst = array_mut_ref![dst, 0, PROPOSAL_TOTAL_BYTES];

        let (
            is_initialized_dst,
            proposal_type_dst,
            execution_amount_dst,
            execution_amount_out_dst,
            execution_source_dst,
            execution_destination_dst,
            creator_dst,
            squad_address_dst,
            title_dst,
            description_dst,
            link_dst,
            // will be fixed to 5 options, max
            votes_num_dst,
            has_voted_num_dst,
            has_voted_dst,
            votes_dst,
            votes_labels_dst,
            // timestamp bytes
            start_timestamp_dst,
            close_timestamp_dst,
            created_timestamp_dst,
            supply_at_execute_dst,
            members_at_execute_dst,
            threshold_at_execute_dst,
            executed_dst,
            execute_ready_dst,
            execution_date_dst,
            instruction_index_dst,
            multiple_choice_dst,
            executed_by_dst,
            proposal_index_dst,
            _reserved,
        ) = mut_array_refs![
            dst,
            PROPOSAL_SETTING_BYTES,               // is_initialized 1
            PROPOSAL_SETTING_BYTES,               // type 1
            PROPOSAL_EXECUTION_AMOUNT_BYTES,      // execution amount 8
            PROPOSAL_EXECUTION_AMOUNT_BYTES,      // execution amount out 8
            PROPOSAL_EXECUTION_SOURCE_BYTES,      // execution source 32
            PROPOSAL_EXECUTION_DESTINATION_BYTES, // execution destination 32
            PUBLIC_KEY_BYTES,                     // creator 32
            PUBLIC_KEY_BYTES,                     // squad address 32
            PROPOSAL_TITLE_BYTES,                 // title of the proposal 36
            PROPOSAL_DESCRIPTION_BYTES,           // description of the proposal 496
            PROPOSAL_LINK_BYTES,                  // link bytes 48
            PROPOSAL_SETTING_BYTES,               // vote options num 1
            PROPOSAL_HAS_VOTED_NUM_BYTES,         // 1
            PROPOSAL_HAS_VOTED_BYTES,             // 100 * 32
            PROPOSAL_OPTIONS_BYTES,               // 5 * 4
            PROPOSAL_OPTIONS_LABELS_BYTES,        // 5 * 44 = 220
            TIMESTAMP_BYTES,                      // start date 8
            TIMESTAMP_BYTES,                      // proposal 8
            TIMESTAMP_BYTES,                      // created_on bytes 8
            SUPPLY_AT_EXECUTE_BYTES,              // supply_at_execute 8
            MEMBERS_AT_EXECUTE_BYTES,             // members_at_execute 1
            THRESHOLD_AT_EXECUTE_BYTES,           // threshold_at_execute 1
            PROPOSAL_SETTING_BYTES,               // executed 1
            PROPOSAL_SETTING_BYTES,               // execute_ready 1
            TIMESTAMP_BYTES,                      // execution_date bytes 8
            PROPOSAL_SETTING_BYTES,               // instruction_index 1
            PROPOSAL_SETTING_BYTES,               // multiple_choice 1
            PUBLIC_KEY_BYTES,                     // executed_by 32
            PROPOSAL_INDEX_BYTES,                 // proposal index
            PROPOSAL_RESERVED_BYTES
        ];

        let Proposal {
            is_initialized,
            proposal_type,
            execution_amount,
            execution_amount_out,
            execution_source,
            execution_destination,
            creator,
            squad_address,
            title,
            description,
            link,
            // will be fixed to 5 options, max
            votes_num,
            has_voted_num,
            has_voted,
            votes,
            votes_labels,
            start_timestamp,
            close_timestamp,
            created_timestamp,
            supply_at_execute,
            members_at_execute,
            threshold_at_execute,
            executed,
            execute_ready,
            execution_date,
            instruction_index,
            multiple_choice,
            executed_by,
            proposal_index,
            reserved: _,
        } = self;

        is_initialized_dst[0] = *is_initialized as u8;
        *proposal_type_dst = proposal_type.to_le_bytes();
        *execution_amount_dst = execution_amount.to_le_bytes();
        *execution_amount_out_dst = execution_amount_out.to_le_bytes();
        execution_source_dst.copy_from_slice(execution_source.as_ref());
        execution_destination_dst.copy_from_slice(execution_destination.as_ref());
        creator_dst.copy_from_slice(creator.as_ref());
        squad_address_dst.copy_from_slice(squad_address.as_ref());
        // pack the description

        let title_ser = title.as_bytes();
        title_dst[..title_ser.len()].copy_from_slice(title_ser);

        let description_ser = description.as_bytes();
        description_dst[..description_ser.len()].copy_from_slice(description_ser);

        let link_ser = link.as_bytes();
        link_dst[..link_ser.len()].copy_from_slice(link_ser);

        *votes_num_dst = votes_num.to_le_bytes();

        let votes_len = votes.len();
        let mut votes_check = votes.clone();
        for _i in 0..PROPOSAL_VOTE_OPTIONS_NUM - votes_len {
            votes_check.push(0);
        }

        let vote_byte_collect: Vec<Vec<u8>> = votes_check
            .iter()
            .map(|v| v.to_le_bytes().to_vec())
            .collect();

        let votes_ser: Vec<u8> = vote_byte_collect.into_iter().flatten().collect();

        *has_voted_num_dst = has_voted_num.to_le_bytes();

        let has_voted_ser_num = has_voted.len();
        let has_voted_ser: Vec<u8> = has_voted.try_to_vec().unwrap();
        has_voted_dst[..has_voted_ser_num * 32 as usize + 4 as usize]
            .copy_from_slice(&has_voted_ser);

        votes_dst[..].copy_from_slice(&votes_ser.as_slice());
        *created_timestamp_dst = created_timestamp.to_le_bytes();
        *start_timestamp_dst = start_timestamp.to_le_bytes();
        *close_timestamp_dst = close_timestamp.to_le_bytes();

        *supply_at_execute_dst = supply_at_execute.to_le_bytes();
        *members_at_execute_dst = members_at_execute.to_le_bytes();
        *threshold_at_execute_dst = threshold_at_execute.to_le_bytes();

        executed_dst[0] = *executed as u8;
        execute_ready_dst[0] = *execute_ready as u8;
        *execution_date_dst = execution_date.to_le_bytes();

        *instruction_index_dst = instruction_index.to_le_bytes();
        multiple_choice_dst[0] = *multiple_choice as u8;

        executed_by_dst.copy_from_slice(executed_by.as_ref());

        let votes_labels_len = votes_labels.len();
        let mut votes_labels_check = votes_labels.clone();

        // make sure that we fill up all labels
        for _s in 0..PROPOSAL_VOTE_OPTIONS_NUM - votes_labels_len {
            votes_labels_check.push(String::from_utf8(vec![0; 44]).unwrap());
        }

        // make sure all labels are 44 len
        let votes_labels_ser: Vec<u8> = votes_labels_check
            .iter()
            .map(|l| {
                let mut s_check = l.clone();
                for _c in 0..(44 - s_check.len()) {
                    s_check.push('\u{0}');
                }
                s_check.as_bytes().to_vec()
            })
            .flatten()
            .collect();

        votes_labels_dst[..].copy_from_slice(votes_labels_ser.as_slice());

        *proposal_index_dst = proposal_index.to_le_bytes();
    }

    fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError> {
        let src = array_ref![src, 0, PROPOSAL_TOTAL_BYTES];
        let (
            is_initialized,
            proposal_type,
            execution_amount,
            execution_amount_out,
            execution_source,
            execution_destination,
            creator,
            squad_address,
            title_src,
            description_src,
            link_src,
            // will be fixed to 16 options, max
            votes_num,
            has_voted_num,
            has_voted_src,
            votes,
            votes_labels_src,
            start_timestamp,
            close_timestamp,
            created_timestamp,
            supply_at_execute,
            members_at_execute,
            threshold_at_execute,
            executed,
            execute_ready,
            execution_date,
            instruction_index,
            multiple_choice,
            executed_by,
            proposal_index,
            _reserved,
        ) = array_refs![
            src,
            PROPOSAL_SETTING_BYTES,          // is_initialized
            PROPOSAL_SETTING_BYTES,          // proposal_type
            PROPOSAL_EXECUTION_AMOUNT_BYTES, // execution amount
            PROPOSAL_EXECUTION_AMOUNT_BYTES, // execution amount out
            PROPOSAL_EXECUTION_SOURCE_BYTES, // execution source
            PROPOSAL_EXECUTION_DESTINATION_BYTES,
            PUBLIC_KEY_BYTES,             // proposal creator
            PUBLIC_KEY_BYTES,             // squad_account
            PROPOSAL_TITLE_BYTES,         // title
            PROPOSAL_DESCRIPTION_BYTES,   // description
            PROPOSAL_LINK_BYTES,          // link
            PROPOSAL_SETTING_BYTES,       // numer of vote options
            PROPOSAL_HAS_VOTED_NUM_BYTES, // bytes for has voted num
            PROPOSAL_HAS_VOTED_BYTES,     // bytes for Vec Pubkey
            PROPOSAL_OPTIONS_BYTES,       // bytes for BTreeMap buckets
            PROPOSAL_OPTIONS_LABELS_BYTES,
            TIMESTAMP_BYTES,            // start proposal
            TIMESTAMP_BYTES,            // close proposal
            TIMESTAMP_BYTES,            // created on
            SUPPLY_AT_EXECUTE_BYTES,    // supply_at_execute 8
            MEMBERS_AT_EXECUTE_BYTES,   // members_at_execute 1
            THRESHOLD_AT_EXECUTE_BYTES, // threshold_at_execute 1
            PROPOSAL_SETTING_BYTES,     // executed 1
            PROPOSAL_SETTING_BYTES,     // execute_ready 1
            TIMESTAMP_BYTES,            // execution_date
            PROPOSAL_SETTING_BYTES,     // instruction_index 1
            PROPOSAL_SETTING_BYTES,     // multiple_choice 1
            PUBLIC_KEY_BYTES,           // executed_by 32
            PROPOSAL_INDEX_BYTES,       // proposal index
            PROPOSAL_RESERVED_BYTES
        ];

        let is_initialized = match is_initialized {
            [0] => false,
            [1] => true,
            _ => return Err(ProgramError::InvalidAccountData),
        };

        let executed = match executed {
            [0] => false,
            [1] => true,
            _ => return Err(ProgramError::InvalidAccountData),
        };

        let execute_ready = match execute_ready {
            [0] => false,
            [1] => true,
            _ => return Err(ProgramError::InvalidAccountData),
        };

        let multiple_choice = match multiple_choice {
            [0] => false,
            [1] => true,
            _ => return Err(ProgramError::InvalidAccountData),
        };

        let title_deser = String::from_utf8(title_src.to_vec()).unwrap();
        let description_deser = String::from_utf8(description_src.to_vec()).unwrap();
        let link_deser = String::from_utf8(link_src.to_vec()).unwrap();
        let votes_num_deser = votes_num[0];

        let votes_iter = votes.chunks(8);
        let votes = votes_iter
            .map(|slice| u64::from_le_bytes(slice.try_into().unwrap()))
            .collect();

        let mut has_voted_deser = Vec::<Pubkey>::new();
        let has_voted_num = u8::from_le_bytes(*has_voted_num);
        if has_voted_num > 0 {
            has_voted_deser = Vec::<Pubkey>::try_from_slice(
                &has_voted_src[0..32 * has_voted_num as usize + 4 as usize],
            )
            .unwrap();
        }

        let vote_options_deser: Vec<String> = votes_labels_src
            .chunks_exact(44)
            .map(|oc| String::from_utf8(oc.to_vec()).unwrap())
            .collect();

        Ok(Proposal {
            // low level settings
            is_initialized,
            // squad settings
            proposal_type: u8::from_le_bytes(*proposal_type),
            execution_amount: u64::from_le_bytes(*execution_amount),
            execution_amount_out: u64::from_le_bytes(*execution_amount_out),
            execution_source: Pubkey::new(execution_source),
            execution_destination: Pubkey::new(execution_destination),
            creator: Pubkey::new(creator),
            squad_address: Pubkey::new(squad_address),
            title: title_deser,
            description: description_deser,
            link: link_deser,
            votes_num: votes_num_deser,
            has_voted_num,
            has_voted: has_voted_deser,
            votes,
            votes_labels: vote_options_deser,
            // proposal nonce
            start_timestamp: i64::from_le_bytes(*start_timestamp),
            close_timestamp: i64::from_le_bytes(*close_timestamp),
            //creation time
            created_timestamp: i64::from_le_bytes(*created_timestamp),
            // execution params
            supply_at_execute: u64::from_le_bytes(*supply_at_execute),
            members_at_execute: u8::from_le_bytes(*members_at_execute),
            threshold_at_execute: u8::from_le_bytes(*threshold_at_execute),
            executed,
            execute_ready,
            execution_date: i64::from_le_bytes(*execution_date),
            instruction_index: u8::from_le_bytes(*instruction_index),
            multiple_choice,
            executed_by: Pubkey::new(executed_by),
            proposal_index: u32::from_le_bytes(*proposal_index),
            reserved: [0; 16],
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono;
    use solana_program::pubkey::Pubkey;

    #[test]
    fn proposal_struct_size() {
        let mut test_dst: [u8; PROPOSAL_TOTAL_BYTES] = [0; PROPOSAL_TOTAL_BYTES];
        let description_vec = vec![0; 496];
        let description = String::from_utf8(description_vec).unwrap();

        let link_vec = vec![0; 48];
        let link = String::from_utf8(link_vec).unwrap();

        let votes = vec![0, 3, 2000, 7, 0];

        let has_voted = vec![
            Pubkey::new_unique(),
            Pubkey::new_unique(),
            Pubkey::new_unique(),
        ];
        let has_voted_num = has_voted.len() as u8;

        let votes_labels_vec = vec![0; 5];
        let votes_labels = votes_labels_vec
            .iter()
            .map(|v| String::from("asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadfa"))
            .collect();

        let test_proposal = Proposal {
            is_initialized: true,
            proposal_type: 0,
            execution_amount: 100,
            execution_amount_out: 100,
            execution_source: Pubkey::new_unique(),
            execution_destination: Pubkey::new_unique(),
            creator: Pubkey::new_unique(),
            squad_address: Pubkey::new_unique(),
            title: String::from("This is a test adfasdfasdfasdfasdfff"),
            description,
            link, // 160 - fixed bytes
            // will be fixed to 16 options, max
            votes_num: 5,
            has_voted_num,
            has_voted,
            votes,
            // will be fixed to 16 items to match above
            votes_labels,
            start_timestamp: chrono::offset::Utc::now().timestamp(),
            close_timestamp: chrono::offset::Utc::now().timestamp(),
            created_timestamp: chrono::offset::Utc::now().timestamp(),
            supply_at_execute: 0,
            members_at_execute: 0,
            threshold_at_execute: 0,
            executed: false,
            execute_ready: false,
            execution_date: chrono::offset::Utc::now().timestamp(),
            instruction_index: 0,
            multiple_choice: false,
            executed_by: Pubkey::new_unique(),
            proposal_index: 0,
            reserved: [0; 16],
        };

        Proposal::pack(test_proposal, &mut test_dst);

        let mut test_proposal_deser = Proposal::unpack_unchecked(&test_dst).unwrap();
        println!("proposal unpack: {:?}", test_proposal_deser);

        test_proposal_deser.has_voted.push(Pubkey::new_unique());
        test_proposal_deser.has_voted_num = test_proposal_deser.has_voted.len() as u8;
        Proposal::pack(test_proposal_deser, &mut test_dst);

        let test_proposal_deser = Proposal::unpack_unchecked(&test_dst).unwrap();
        println!("proposal unpack: {:?}", test_proposal_deser);

        println!("proposal packed len: {:?}", Proposal::get_packed_len());
        println!("total proposal size: {:?}", PROPOSAL_TOTAL_BYTES);
    }
}