token_interface.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. use anchor_lang::solana_program::pubkey::Pubkey;
  2. use std::ops::Deref;
  3. static IDS: [Pubkey; 2] = [spl_token::ID, spl_token_2022::ID];
  4. #[derive(Clone, Debug, Default, PartialEq)]
  5. pub struct TokenAccount(spl_token_2022::state::Account);
  6. impl anchor_lang::AccountDeserialize for TokenAccount {
  7. fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
  8. spl_token_2022::extension::StateWithExtensions::<spl_token_2022::state::Account>::unpack(
  9. buf,
  10. )
  11. .map(|t| TokenAccount(t.base))
  12. .map_err(Into::into)
  13. }
  14. }
  15. impl anchor_lang::AccountSerialize for TokenAccount {}
  16. impl anchor_lang::Owners for TokenAccount {
  17. fn owners() -> &'static [Pubkey] {
  18. &IDS
  19. }
  20. }
  21. impl Deref for TokenAccount {
  22. type Target = spl_token_2022::state::Account;
  23. fn deref(&self) -> &Self::Target {
  24. &self.0
  25. }
  26. }
  27. #[derive(Clone, Debug, Default, PartialEq)]
  28. pub struct Mint(spl_token_2022::state::Mint);
  29. impl anchor_lang::AccountDeserialize for Mint {
  30. fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
  31. spl_token_2022::extension::StateWithExtensions::<spl_token_2022::state::Mint>::unpack(buf)
  32. .map(|t| Mint(t.base))
  33. .map_err(Into::into)
  34. }
  35. }
  36. impl anchor_lang::AccountSerialize for Mint {}
  37. impl anchor_lang::Owners for Mint {
  38. fn owners() -> &'static [Pubkey] {
  39. &IDS
  40. }
  41. }
  42. impl Deref for Mint {
  43. type Target = spl_token_2022::state::Mint;
  44. fn deref(&self) -> &Self::Target {
  45. &self.0
  46. }
  47. }
  48. #[derive(Clone)]
  49. pub struct TokenInterface;
  50. impl anchor_lang::Ids for TokenInterface {
  51. fn ids() -> &'static [Pubkey] {
  52. &IDS
  53. }
  54. }
  55. pub use crate::token_2022::*;