governance.rs 2.7 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::addins::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::addins::voter_weight::VoterWeightRecord =
  12. anchor_lang::AnchorDeserialize::deserialize(&mut data)
  13. .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize)?;
  14. if vwr.account_type != spl_governance::addins::voter_weight::VoterWeightAccountType::VoterWeightRecord {
  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::addins::voter_weight::VoterWeightRecord =
  24. anchor_lang::AnchorDeserialize::deserialize(&mut data)
  25. .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize)?;
  26. if vwr.account_type != spl_governance::addins::voter_weight::VoterWeightAccountType::Uninitialized {
  27. return Err(anchor_lang::__private::ErrorCode::AccountDidNotSerialize.into());
  28. }
  29. Ok(VoterWeightRecord(vwr))
  30. }
  31. }
  32. impl anchor_lang::AccountSerialize for VoterWeightRecord {
  33. fn try_serialize<W: std::io::Write>(
  34. &self,
  35. writer: &mut W,
  36. ) -> std::result::Result<(), ProgramError> {
  37. anchor_lang::AnchorSerialize::serialize(&self.0, writer)
  38. .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?;
  39. Ok(())
  40. }
  41. }
  42. impl anchor_lang::Owner for VoterWeightRecord {
  43. fn owner() -> Pubkey {
  44. $id
  45. }
  46. }
  47. impl std::ops::Deref for VoterWeightRecord {
  48. type Target = spl_governance::addins::voter_weight::VoterWeightRecord;
  49. fn deref(&self) -> &Self::Target {
  50. &self.0
  51. }
  52. }
  53. impl std::ops::DerefMut for VoterWeightRecord {
  54. fn deref_mut(&mut self) -> &mut Self::Target {
  55. &mut self.0
  56. }
  57. }
  58. };
  59. }