token.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use anchor_lang::{Accounts, CpiContext};
  2. use solana_program::account_info::AccountInfo;
  3. use solana_program::entrypoint::ProgramResult;
  4. pub fn transfer<'a, 'b, 'c, 'info>(
  5. ctx: CpiContext<'a, 'b, 'c, 'info, Transfer<'info>>,
  6. amount: u64,
  7. ) -> ProgramResult {
  8. let ix = spl_token::instruction::transfer(
  9. &spl_token::ID,
  10. ctx.accounts.from.key,
  11. ctx.accounts.to.key,
  12. ctx.accounts.authority.key,
  13. &[],
  14. amount,
  15. )?;
  16. solana_program::program::invoke_signed(
  17. &ix,
  18. &[
  19. ctx.accounts.from.clone(),
  20. ctx.accounts.to.clone(),
  21. ctx.accounts.authority.clone(),
  22. ctx.program.clone(),
  23. ],
  24. ctx.signer_seeds,
  25. )
  26. }
  27. pub fn mint_to<'a, 'b, 'c, 'info>(
  28. ctx: CpiContext<'a, 'b, 'c, 'info, MintTo<'info>>,
  29. amount: u64,
  30. ) -> ProgramResult {
  31. let ix = spl_token::instruction::mint_to(
  32. &spl_token::ID,
  33. ctx.accounts.mint.key,
  34. ctx.accounts.to.key,
  35. ctx.accounts.authority.key,
  36. &[],
  37. amount,
  38. )?;
  39. solana_program::program::invoke_signed(
  40. &ix,
  41. &[
  42. ctx.accounts.mint.clone(),
  43. ctx.accounts.to.clone(),
  44. ctx.accounts.authority.clone(),
  45. ctx.program.clone(),
  46. ],
  47. ctx.signer_seeds,
  48. )
  49. }
  50. pub fn burn<'a, 'b, 'c, 'info>(
  51. ctx: CpiContext<'a, 'b, 'c, 'info, Burn<'info>>,
  52. amount: u64,
  53. ) -> ProgramResult {
  54. let ix = spl_token::instruction::burn(
  55. &spl_token::ID,
  56. ctx.accounts.to.key,
  57. ctx.accounts.mint.key,
  58. ctx.accounts.authority.key,
  59. &[],
  60. amount,
  61. )?;
  62. solana_program::program::invoke_signed(
  63. &ix,
  64. &[
  65. ctx.accounts.to.clone(),
  66. ctx.accounts.mint.clone(),
  67. ctx.accounts.authority.clone(),
  68. ctx.program.clone(),
  69. ],
  70. ctx.signer_seeds,
  71. )
  72. }
  73. #[derive(Accounts)]
  74. pub struct Transfer<'info> {
  75. pub from: AccountInfo<'info>,
  76. pub to: AccountInfo<'info>,
  77. pub authority: AccountInfo<'info>,
  78. }
  79. #[derive(Accounts)]
  80. pub struct MintTo<'info> {
  81. pub mint: AccountInfo<'info>,
  82. pub to: AccountInfo<'info>,
  83. pub authority: AccountInfo<'info>,
  84. }
  85. #[derive(Accounts)]
  86. pub struct Burn<'info> {
  87. pub mint: AccountInfo<'info>,
  88. pub to: AccountInfo<'info>,
  89. pub authority: AccountInfo<'info>,
  90. }