-
Notifications
You must be signed in to change notification settings - Fork 517
/
proving_process.rs
89 lines (78 loc) · 2.56 KB
/
proving_process.rs
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
//! Proving process definitions
use super::*;
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankAccount;
use solana_program::{
borsh::try_from_slice_unchecked,
msg,
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
pubkey::Pubkey,
};
use std::collections::BTreeMap;
/// Proving process
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Default, ShankAccount)]
pub struct ProvingProcess {
/// Account type - ProvingProcess
pub account_type: AccountType,
///
pub wallet_key: Pubkey,
///
pub is_exhausted: bool,
/// Voucher mint
pub voucher_mint: Pubkey,
/// Pack set
pub pack_set: Pubkey,
///
pub cards_redeemed: u32,
/// BTreeMap with cards to redeem and statuses if it's already redeemed
pub cards_to_redeem: BTreeMap<u32, u32>,
}
impl ProvingProcess {
/// Prefix used to generate account
pub const PREFIX: &'static str = "proving";
/// Amount of tokens for prove operation
pub const TOKEN_AMOUNT: u64 = 1;
/// Initialize a ProvingProcess
pub fn init(&mut self, params: InitProvingProcessParams) {
self.account_type = AccountType::ProvingProcess;
self.wallet_key = params.wallet_key;
self.voucher_mint = params.voucher_mint;
self.pack_set = params.pack_set;
self.cards_to_redeem = BTreeMap::new();
}
}
/// Initialize a ProvingProcess params
pub struct InitProvingProcessParams {
/// User wallet key
pub wallet_key: Pubkey,
/// Voucher mint
pub voucher_mint: Pubkey,
/// Pack set
pub pack_set: Pubkey,
}
impl Sealed for ProvingProcess {}
impl Pack for ProvingProcess {
// 1 + 32 + 1 + 32 + 32 + 4 + BTreeMap size for 100 cards(800)
// When calculating size for custom data structures like `BTreeMap` does not
// include structure header size(in that case is always 24-bytes).
// Calculate size for underlying(template) types only(u32 + u32 = 8bytes in this case).
const LEN: usize = 902;
fn pack_into_slice(&self, dst: &mut [u8]) {
let mut slice = dst;
self.serialize(&mut slice).unwrap()
}
fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError> {
try_from_slice_unchecked(src).map_err(|_| {
msg!("Failed to deserialize");
ProgramError::InvalidAccountData
})
}
}
impl IsInitialized for ProvingProcess {
fn is_initialized(&self) -> bool {
self.account_type != AccountType::Uninitialized
&& self.account_type == AccountType::ProvingProcess
}
}