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
use crate::{ parse_account_data::{ParsableAccount, ParseAccountError}, StringAmount, }; use bincode::deserialize; use solana_sdk::clock::{Epoch, UnixTimestamp}; use solana_stake_program::stake_state::{Authorized, Delegation, Lockup, Meta, Stake, StakeState}; pub fn parse_stake(data: &[u8]) -> Result<StakeAccountType, ParseAccountError> { let stake_state: StakeState = deserialize(data) .map_err(|_| ParseAccountError::AccountNotParsable(ParsableAccount::Stake))?; let parsed_account = match stake_state { StakeState::Uninitialized => StakeAccountType::Uninitialized, StakeState::Initialized(meta) => StakeAccountType::Initialized(UiStakeAccount { meta: meta.into(), stake: None, }), StakeState::Stake(meta, stake) => StakeAccountType::Delegated(UiStakeAccount { meta: meta.into(), stake: Some(stake.into()), }), StakeState::RewardsPool => StakeAccountType::RewardsPool, }; Ok(parsed_account) } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase", tag = "type", content = "info")] pub enum StakeAccountType { Uninitialized, Initialized(UiStakeAccount), Delegated(UiStakeAccount), RewardsPool, } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiStakeAccount { pub meta: UiMeta, pub stake: Option<UiStake>, } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiMeta { pub rent_exempt_reserve: StringAmount, pub authorized: UiAuthorized, pub lockup: UiLockup, } impl From<Meta> for UiMeta { fn from(meta: Meta) -> Self { Self { rent_exempt_reserve: meta.rent_exempt_reserve.to_string(), authorized: meta.authorized.into(), lockup: meta.lockup.into(), } } } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiLockup { pub unix_timestamp: UnixTimestamp, pub epoch: Epoch, pub custodian: String, } impl From<Lockup> for UiLockup { fn from(lockup: Lockup) -> Self { Self { unix_timestamp: lockup.unix_timestamp, epoch: lockup.epoch, custodian: lockup.custodian.to_string(), } } } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiAuthorized { pub staker: String, pub withdrawer: String, } impl From<Authorized> for UiAuthorized { fn from(authorized: Authorized) -> Self { Self { staker: authorized.staker.to_string(), withdrawer: authorized.withdrawer.to_string(), } } } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiStake { pub delegation: UiDelegation, pub credits_observed: u64, } impl From<Stake> for UiStake { fn from(stake: Stake) -> Self { Self { delegation: stake.delegation.into(), credits_observed: stake.credits_observed, } } } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct UiDelegation { pub voter: String, pub stake: StringAmount, pub activation_epoch: StringAmount, pub deactivation_epoch: StringAmount, pub warmup_cooldown_rate: f64, } impl From<Delegation> for UiDelegation { fn from(delegation: Delegation) -> Self { Self { voter: delegation.voter_pubkey.to_string(), stake: delegation.stake.to_string(), activation_epoch: delegation.activation_epoch.to_string(), deactivation_epoch: delegation.deactivation_epoch.to_string(), warmup_cooldown_rate: delegation.warmup_cooldown_rate, } } } #[cfg(test)] mod test { use super::*; use bincode::serialize; #[test] fn test_parse_stake() { let stake_state = StakeState::Uninitialized; let stake_data = serialize(&stake_state).unwrap(); assert_eq!( parse_stake(&stake_data).unwrap(), StakeAccountType::Uninitialized ); let pubkey = solana_sdk::pubkey::new_rand(); let custodian = solana_sdk::pubkey::new_rand(); let authorized = Authorized::auto(&pubkey); let lockup = Lockup { unix_timestamp: 0, epoch: 1, custodian, }; let meta = Meta { rent_exempt_reserve: 42, authorized, lockup, }; let stake_state = StakeState::Initialized(meta); let stake_data = serialize(&stake_state).unwrap(); assert_eq!( parse_stake(&stake_data).unwrap(), StakeAccountType::Initialized(UiStakeAccount { meta: UiMeta { rent_exempt_reserve: 42.to_string(), authorized: UiAuthorized { staker: pubkey.to_string(), withdrawer: pubkey.to_string(), }, lockup: UiLockup { unix_timestamp: 0, epoch: 1, custodian: custodian.to_string(), } }, stake: None, }) ); let voter_pubkey = solana_sdk::pubkey::new_rand(); let stake = Stake { delegation: Delegation { voter_pubkey, stake: 20, activation_epoch: 2, deactivation_epoch: std::u64::MAX, warmup_cooldown_rate: 0.25, }, credits_observed: 10, }; let stake_state = StakeState::Stake(meta, stake); let stake_data = serialize(&stake_state).unwrap(); assert_eq!( parse_stake(&stake_data).unwrap(), StakeAccountType::Delegated(UiStakeAccount { meta: UiMeta { rent_exempt_reserve: 42.to_string(), authorized: UiAuthorized { staker: pubkey.to_string(), withdrawer: pubkey.to_string(), }, lockup: UiLockup { unix_timestamp: 0, epoch: 1, custodian: custodian.to_string(), } }, stake: Some(UiStake { delegation: UiDelegation { voter: voter_pubkey.to_string(), stake: 20.to_string(), activation_epoch: 2.to_string(), deactivation_epoch: std::u64::MAX.to_string(), warmup_cooldown_rate: 0.25, }, credits_observed: 10, }) }) ); let stake_state = StakeState::RewardsPool; let stake_data = serialize(&stake_state).unwrap(); assert_eq!( parse_stake(&stake_data).unwrap(), StakeAccountType::RewardsPool ); let bad_data = vec![1, 2, 3, 4]; assert!(parse_stake(&bad_data).is_err()); } }