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
//! This account contains the current cluster rewards point values
//!
use crate::account::Account;
use crate::sysvar;
use bincode::serialized_size;

///  account pubkey
const ID: [u8; 32] = [
    6, 167, 213, 23, 25, 44, 97, 55, 206, 224, 146, 217, 182, 146, 62, 225, 204, 214, 25, 3, 250,
    130, 184, 161, 97, 145, 87, 141, 128, 0, 0, 0,
];

crate::solana_name_id!(ID, "SysvarRewards111111111111111111111111111111");

#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Rewards {
    pub validator_point_value: f64,
    pub storage_point_value: f64,
}

impl Rewards {
    pub fn from(account: &Account) -> Option<Self> {
        account.deserialize_data().ok()
    }
    pub fn to(&self, account: &mut Account) -> Option<()> {
        account.serialize_data(self).ok()
    }
    pub fn size_of() -> usize {
        serialized_size(&Self::default()).unwrap() as usize
    }
}

pub fn create_account(
    lamports: u64,
    validator_point_value: f64,
    storage_point_value: f64,
) -> Account {
    Account::new_data(
        lamports,
        &Rewards {
            validator_point_value,
            storage_point_value,
        },
        &sysvar::id(),
    )
    .unwrap()
}

use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Rewards, InstructionError> {
    if !check_id(account.unsigned_key()) {
        dbg!(account.unsigned_key());
        return Err(InstructionError::InvalidArgument);
    }
    Rewards::from(account.account).ok_or(InstructionError::InvalidAccountData)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_account() {
        let account = create_account(1, 0.0, 0.0);
        let rewards = Rewards::from(&account).unwrap();
        assert_eq!(rewards, Rewards::default());
    }
}