governance.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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]) -> std::result::Result<Self, ProgramError> {
  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::__private::ErrorCode::AccountDidNotDeserialize)?;
  14. if !solana_program::program_pack::IsInitialized::is_initialized(&vwr) {
  15. return Err(anchor_lang::__private::ErrorCode::AccountDidNotSerialize.into());
  16. }
  17. Ok(VoterWeightRecord(vwr))
  18. }
  19. fn try_deserialize_unchecked(
  20. buf: &mut &[u8],
  21. ) -> std::result::Result<Self, ProgramError> {
  22. let mut data = buf;
  23. let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
  24. anchor_lang::AnchorDeserialize::deserialize(&mut data)
  25. .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize)?;
  26. Ok(VoterWeightRecord(vwr))
  27. }
  28. }
  29. impl anchor_lang::AccountSerialize for VoterWeightRecord {
  30. fn try_serialize<W: std::io::Write>(
  31. &self,
  32. writer: &mut W,
  33. ) -> std::result::Result<(), ProgramError> {
  34. anchor_lang::AnchorSerialize::serialize(&self.0, writer)
  35. .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?;
  36. Ok(())
  37. }
  38. }
  39. impl anchor_lang::Owner for VoterWeightRecord {
  40. fn owner() -> Pubkey {
  41. $id
  42. }
  43. }
  44. impl std::ops::Deref for VoterWeightRecord {
  45. type Target = spl_governance_addin_api::voter_weight::VoterWeightRecord;
  46. fn deref(&self) -> &Self::Target {
  47. &self.0
  48. }
  49. }
  50. impl std::ops::DerefMut for VoterWeightRecord {
  51. fn deref_mut(&mut self) -> &mut Self::Target {
  52. &mut self.0
  53. }
  54. }
  55. };
  56. }