2021-10-24 17:42:52 -07:00
|
|
|
/// A macro is exposed so that we can embed the program ID.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! vote_weight_record {
|
|
|
|
($id:expr) => {
|
|
|
|
/// Anchor wrapper for the SPL governance program's VoterWeightRecord type.
|
|
|
|
#[derive(Clone)]
|
2022-02-07 10:26:57 -08:00
|
|
|
pub struct VoterWeightRecord(spl_governance_addin_api::voter_weight::VoterWeightRecord);
|
2021-10-24 17:42:52 -07:00
|
|
|
|
|
|
|
impl anchor_lang::AccountDeserialize for VoterWeightRecord {
|
2022-03-10 12:30:49 -08:00
|
|
|
fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
|
2021-10-24 17:42:52 -07:00
|
|
|
let mut data = buf;
|
2022-02-07 10:26:57 -08:00
|
|
|
let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
|
2021-10-24 17:42:52 -07:00
|
|
|
anchor_lang::AnchorDeserialize::deserialize(&mut data)
|
2022-02-10 12:54:12 -08:00
|
|
|
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;
|
2022-02-07 10:26:57 -08:00
|
|
|
if !solana_program::program_pack::IsInitialized::is_initialized(&vwr) {
|
2022-02-10 12:54:12 -08:00
|
|
|
return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
|
2021-10-24 17:42:52 -07:00
|
|
|
}
|
|
|
|
Ok(VoterWeightRecord(vwr))
|
|
|
|
}
|
|
|
|
|
2022-03-10 12:30:49 -08:00
|
|
|
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
|
2021-10-24 17:42:52 -07:00
|
|
|
let mut data = buf;
|
2022-02-07 10:26:57 -08:00
|
|
|
let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
|
2021-10-24 17:42:52 -07:00
|
|
|
anchor_lang::AnchorDeserialize::deserialize(&mut data)
|
2022-02-10 12:54:12 -08:00
|
|
|
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;
|
2021-10-24 17:42:52 -07:00
|
|
|
Ok(VoterWeightRecord(vwr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl anchor_lang::AccountSerialize for VoterWeightRecord {
|
2022-03-10 12:30:49 -08:00
|
|
|
fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> anchor_lang::Result<()> {
|
2021-10-24 17:42:52 -07:00
|
|
|
anchor_lang::AnchorSerialize::serialize(&self.0, writer)
|
2022-02-10 12:54:12 -08:00
|
|
|
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?;
|
2021-10-24 17:42:52 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl anchor_lang::Owner for VoterWeightRecord {
|
|
|
|
fn owner() -> Pubkey {
|
|
|
|
$id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for VoterWeightRecord {
|
2022-02-07 10:26:57 -08:00
|
|
|
type Target = spl_governance_addin_api::voter_weight::VoterWeightRecord;
|
2021-10-24 17:42:52 -07:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for VoterWeightRecord {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|