governance.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /// A macro is exposed so that we can embed the program ID.
  2. #[macro_export]
  3. macro_rules! vote_weight_record {
  4. ($id:expr) => {
  5. /// Anchor wrapper for the SPL governance program's VoterWeightRecord type.
  6. #[derive(Clone)]
  7. pub struct VoterWeightRecord(spl_governance_addin_api::voter_weight::VoterWeightRecord);
  8. impl anchor_lang::AccountDeserialize for VoterWeightRecord {
  9. fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
  10. let mut data = buf;
  11. let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
  12. anchor_lang::AnchorDeserialize::deserialize(&mut data)
  13. .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;
  14. if !anchor_lang::solana_program::program_pack::IsInitialized::is_initialized(&vwr) {
  15. return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
  16. }
  17. Ok(VoterWeightRecord(vwr))
  18. }
  19. fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
  20. let mut data = buf;
  21. let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
  22. anchor_lang::AnchorDeserialize::deserialize(&mut data)
  23. .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;
  24. Ok(VoterWeightRecord(vwr))
  25. }
  26. }
  27. impl anchor_lang::AccountSerialize for VoterWeightRecord {
  28. fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> anchor_lang::Result<()> {
  29. anchor_lang::AnchorSerialize::serialize(&self.0, writer)
  30. .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?;
  31. Ok(())
  32. }
  33. }
  34. impl anchor_lang::Owner for VoterWeightRecord {
  35. fn owner() -> Pubkey {
  36. $id
  37. }
  38. }
  39. impl std::ops::Deref for VoterWeightRecord {
  40. type Target = spl_governance_addin_api::voter_weight::VoterWeightRecord;
  41. fn deref(&self) -> &Self::Target {
  42. &self.0
  43. }
  44. }
  45. impl std::ops::DerefMut for VoterWeightRecord {
  46. fn deref_mut(&mut self) -> &mut Self::Target {
  47. &mut self.0
  48. }
  49. }
  50. #[cfg(feature = "idl-build")]
  51. impl anchor_lang::IdlBuild for VoterWeightRecord {}
  52. #[cfg(feature = "idl-build")]
  53. impl anchor_lang::Discriminator for VoterWeightRecord {
  54. const DISCRIMINATOR: &'static [u8] = &[];
  55. }
  56. };
  57. }