-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstake_pool_utils.rs
More file actions
205 lines (175 loc) · 6.5 KB
/
Copy pathstake_pool_utils.rs
File metadata and controls
205 lines (175 loc) · 6.5 KB
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
use std::ops::Deref;
#[cfg(feature = "idl-build")]
use anchor_lang::idl::types::*;
#[cfg(feature = "idl-build")]
use anchor_lang::IdlBuild;
use anchor_lang::{
prelude::{AccountInfo, ProgramError, Pubkey},
Result,
};
use borsh::{BorshDeserialize as Borsh0Deserialize, BorshSerialize as Borsh0Serialize};
use borsh1::{BorshDeserialize, BorshSerialize};
pub struct PreferredValidatorType(spl_stake_pool::instruction::PreferredValidatorType);
impl Borsh0Deserialize for PreferredValidatorType {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
Ok(Self(
spl_stake_pool::instruction::PreferredValidatorType::deserialize_reader(reader)?,
))
}
}
impl Borsh0Serialize for PreferredValidatorType {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
self.0.serialize(writer)
}
}
impl AsRef<spl_stake_pool::instruction::PreferredValidatorType> for PreferredValidatorType {
fn as_ref(&self) -> &spl_stake_pool::instruction::PreferredValidatorType {
&self.0
}
}
impl From<spl_stake_pool::instruction::PreferredValidatorType> for PreferredValidatorType {
fn from(val: spl_stake_pool::instruction::PreferredValidatorType) -> Self {
Self(val)
}
}
#[cfg(feature = "idl-build")]
impl IdlBuild for PreferredValidatorType {
fn get_full_path() -> String {
"PreferredValidatorType".to_string()
}
fn create_type() -> Option<IdlTypeDef> {
Some(IdlTypeDef {
name: "PreferredValidatorType".to_string(),
ty: IdlTypeDefTy::Enum {
variants: vec![
IdlEnumVariant {
name: "Deposit".to_string(),
fields: None,
},
IdlEnumVariant {
name: "Withdraw".to_string(),
fields: None,
},
],
},
docs: Default::default(),
generics: Default::default(),
serialization: Default::default(),
repr: Default::default(),
})
}
fn insert_types(_types: &mut std::collections::BTreeMap<String, IdlTypeDef>) {}
}
// Below are nice to haves for deserializing accounts but not strictly necessary for on-chain logic
// A good amount of this is copied from anchor
#[derive(Clone)]
pub struct StakePool(spl_stake_pool::state::StakePool);
impl AsRef<spl_stake_pool::state::StakePool> for StakePool {
fn as_ref(&self) -> &spl_stake_pool::state::StakePool {
&self.0
}
}
// This is necessary so we can use "anchor_spl::token::Mint::LEN"
// because rust does not resolve "anchor_spl::token::Mint::LEN" to
// "spl_token::state::Mint::LEN" automatically
impl StakePool {
pub const LEN: usize = std::mem::size_of::<spl_stake_pool::state::StakePool>();
}
// You don't have to implement the "try_deserialize" function
// from this trait. It delegates to
// "try_deserialize_unchecked" by default which is what we want here
// because non-anchor accounts don't have a discriminator to check
impl anchor_lang::AccountDeserialize for StakePool {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
Ok(Self(spl_stake_pool::state::StakePool::deserialize(buf)?))
}
}
// AccountSerialize defaults to a no-op which is what we want here
// because it's a foreign program, so our program does not
// have permission to write to the foreign program's accounts anyway
impl anchor_lang::AccountSerialize for StakePool {}
impl anchor_lang::Owner for StakePool {
fn owner() -> Pubkey {
spl_stake_pool::ID
}
}
// Implement the "std::ops::Deref" trait for better user experience
impl Deref for StakePool {
type Target = spl_stake_pool::state::StakePool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub fn deserialize_stake_pool(
account_info: &AccountInfo,
) -> Result<spl_stake_pool::state::StakePool> {
if account_info.owner != &spl_stake_pool::ID {
return Err(ProgramError::InvalidAccountOwner.into());
}
let data = account_info.try_borrow_data()?;
Ok(spl_stake_pool::state::StakePool::deserialize(
&mut data.as_ref(),
)?)
}
#[inline(never)]
pub fn stake_pool_withdraw_bump_seed(account_info: &AccountInfo) -> Result<u8> {
Ok(deserialize_stake_pool(account_info)?.stake_withdraw_bump_seed)
}
#[inline(never)]
pub fn stake_pool_validator_list(account_info: &AccountInfo) -> Result<Pubkey> {
Ok(deserialize_stake_pool(account_info)?.validator_list)
}
#[inline(never)]
pub fn stake_pool_reserve_stake(account_info: &AccountInfo) -> Result<Pubkey> {
Ok(deserialize_stake_pool(account_info)?.reserve_stake)
}
pub fn deserialize_validator_list(
account_info: &AccountInfo,
) -> Result<spl_stake_pool::state::ValidatorList> {
if account_info.owner != &spl_stake_pool::ID {
return Err(ProgramError::InvalidAccountOwner.into());
}
let data = account_info.try_borrow_data()?;
Ok(spl_stake_pool::state::ValidatorList::deserialize(
&mut data.as_ref(),
)?)
}
#[derive(Clone)]
pub struct ValidatorList(spl_stake_pool::state::ValidatorList);
impl AsRef<spl_stake_pool::state::ValidatorList> for ValidatorList {
fn as_ref(&self) -> &spl_stake_pool::state::ValidatorList {
&self.0
}
}
// This is necessary so we can use "anchor_spl::token::Mint::LEN"
// because rust does not resolve "anchor_spl::token::Mint::LEN" to
// "spl_token::state::Mint::LEN" automatically
impl ValidatorList {
pub const LEN: usize = std::mem::size_of::<spl_stake_pool::state::ValidatorList>();
}
// You don't have to implement the "try_deserialize" function
// from this trait. It delegates to
// "try_deserialize_unchecked" by default which is what we want here
// because non-anchor accounts don't have a discriminator to check
impl anchor_lang::AccountDeserialize for ValidatorList {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
Ok(Self(spl_stake_pool::state::ValidatorList::deserialize(
buf,
)?))
}
}
// AccountSerialize defaults to a no-op which is what we want here
// because it's a foreign program, so our program does not
// have permission to write to the foreign program's accounts anyway
impl anchor_lang::AccountSerialize for ValidatorList {}
impl anchor_lang::Owner for ValidatorList {
fn owner() -> Pubkey {
spl_stake_pool::ID
}
}
impl Deref for ValidatorList {
type Target = spl_stake_pool::state::ValidatorList;
fn deref(&self) -> &Self::Target {
&self.0
}
}