token.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. use anchor_lang::solana_program;
  2. use anchor_lang::solana_program::account_info::AccountInfo;
  3. use anchor_lang::solana_program::entrypoint::ProgramResult;
  4. use anchor_lang::solana_program::program_error::ProgramError;
  5. use anchor_lang::solana_program::program_pack::Pack;
  6. use anchor_lang::{Accounts, CpiContext};
  7. use std::ops::Deref;
  8. pub use spl_token::ID;
  9. pub fn transfer<'a, 'b, 'c, 'info>(
  10. ctx: CpiContext<'a, 'b, 'c, 'info, Transfer<'info>>,
  11. amount: u64,
  12. ) -> ProgramResult {
  13. let ix = spl_token::instruction::transfer(
  14. &spl_token::ID,
  15. ctx.accounts.from.key,
  16. ctx.accounts.to.key,
  17. ctx.accounts.authority.key,
  18. &[],
  19. amount,
  20. )?;
  21. solana_program::program::invoke_signed(
  22. &ix,
  23. &[
  24. ctx.accounts.from.clone(),
  25. ctx.accounts.to.clone(),
  26. ctx.accounts.authority.clone(),
  27. ctx.program.clone(),
  28. ],
  29. ctx.signer_seeds,
  30. )
  31. }
  32. pub fn mint_to<'a, 'b, 'c, 'info>(
  33. ctx: CpiContext<'a, 'b, 'c, 'info, MintTo<'info>>,
  34. amount: u64,
  35. ) -> ProgramResult {
  36. let ix = spl_token::instruction::mint_to(
  37. &spl_token::ID,
  38. ctx.accounts.mint.key,
  39. ctx.accounts.to.key,
  40. ctx.accounts.authority.key,
  41. &[],
  42. amount,
  43. )?;
  44. solana_program::program::invoke_signed(
  45. &ix,
  46. &[
  47. ctx.accounts.to.clone(),
  48. ctx.accounts.mint.clone(),
  49. ctx.accounts.authority.clone(),
  50. ctx.program.clone(),
  51. ],
  52. ctx.signer_seeds,
  53. )
  54. }
  55. pub fn burn<'a, 'b, 'c, 'info>(
  56. ctx: CpiContext<'a, 'b, 'c, 'info, Burn<'info>>,
  57. amount: u64,
  58. ) -> ProgramResult {
  59. let ix = spl_token::instruction::burn(
  60. &spl_token::ID,
  61. ctx.accounts.to.key,
  62. ctx.accounts.mint.key,
  63. ctx.accounts.authority.key,
  64. &[],
  65. amount,
  66. )?;
  67. solana_program::program::invoke_signed(
  68. &ix,
  69. &[
  70. ctx.accounts.to.clone(),
  71. ctx.accounts.mint.clone(),
  72. ctx.accounts.authority.clone(),
  73. ctx.program.clone(),
  74. ],
  75. ctx.signer_seeds,
  76. )
  77. }
  78. pub fn approve<'a, 'b, 'c, 'info>(
  79. ctx: CpiContext<'a, 'b, 'c, 'info, Approve<'info>>,
  80. amount: u64,
  81. ) -> ProgramResult {
  82. let ix = spl_token::instruction::approve(
  83. &spl_token::ID,
  84. ctx.accounts.to.key,
  85. ctx.accounts.delegate.key,
  86. ctx.accounts.authority.key,
  87. &[],
  88. amount,
  89. )?;
  90. solana_program::program::invoke_signed(
  91. &ix,
  92. &[
  93. ctx.accounts.to.clone(),
  94. ctx.accounts.delegate.clone(),
  95. ctx.accounts.authority.clone(),
  96. ctx.program.clone(),
  97. ],
  98. ctx.signer_seeds,
  99. )
  100. }
  101. #[derive(Accounts)]
  102. pub struct Transfer<'info> {
  103. pub from: AccountInfo<'info>,
  104. pub to: AccountInfo<'info>,
  105. pub authority: AccountInfo<'info>,
  106. }
  107. #[derive(Accounts)]
  108. pub struct MintTo<'info> {
  109. pub mint: AccountInfo<'info>,
  110. pub to: AccountInfo<'info>,
  111. pub authority: AccountInfo<'info>,
  112. }
  113. #[derive(Accounts)]
  114. pub struct Burn<'info> {
  115. pub mint: AccountInfo<'info>,
  116. pub to: AccountInfo<'info>,
  117. pub authority: AccountInfo<'info>,
  118. }
  119. #[derive(Accounts)]
  120. pub struct Approve<'info> {
  121. pub to: AccountInfo<'info>,
  122. pub delegate: AccountInfo<'info>,
  123. pub authority: AccountInfo<'info>,
  124. }
  125. #[derive(Clone)]
  126. pub struct TokenAccount(spl_token::state::Account);
  127. impl anchor_lang::AccountDeserialize for TokenAccount {
  128. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  129. TokenAccount::try_deserialize_unchecked(buf)
  130. }
  131. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  132. spl_token::state::Account::unpack(buf).map(|a| TokenAccount(a))
  133. }
  134. }
  135. impl Deref for TokenAccount {
  136. type Target = spl_token::state::Account;
  137. fn deref(&self) -> &Self::Target {
  138. &self.0
  139. }
  140. }
  141. #[derive(Clone)]
  142. pub struct Mint(spl_token::state::Mint);
  143. impl anchor_lang::AccountDeserialize for Mint {
  144. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  145. Mint::try_deserialize_unchecked(buf)
  146. }
  147. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  148. spl_token::state::Mint::unpack(buf).map(|a| Mint(a))
  149. }
  150. }
  151. impl Deref for Mint {
  152. type Target = spl_token::state::Mint;
  153. fn deref(&self) -> &Self::Target {
  154. &self.0
  155. }
  156. }