token.rs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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::solana_program::pubkey::Pubkey;
  7. use anchor_lang::{Accounts, CpiContext};
  8. use std::io::Write;
  9. use std::ops::Deref;
  10. pub use spl_token::ID;
  11. pub fn transfer<'a, 'b, 'c, 'info>(
  12. ctx: CpiContext<'a, 'b, 'c, 'info, Transfer<'info>>,
  13. amount: u64,
  14. ) -> ProgramResult {
  15. let ix = spl_token::instruction::transfer(
  16. &spl_token::ID,
  17. ctx.accounts.from.key,
  18. ctx.accounts.to.key,
  19. ctx.accounts.authority.key,
  20. &[],
  21. amount,
  22. )?;
  23. solana_program::program::invoke_signed(
  24. &ix,
  25. &[
  26. ctx.accounts.from.clone(),
  27. ctx.accounts.to.clone(),
  28. ctx.accounts.authority.clone(),
  29. ctx.program.clone(),
  30. ],
  31. ctx.signer_seeds,
  32. )
  33. }
  34. pub fn mint_to<'a, 'b, 'c, 'info>(
  35. ctx: CpiContext<'a, 'b, 'c, 'info, MintTo<'info>>,
  36. amount: u64,
  37. ) -> ProgramResult {
  38. let ix = spl_token::instruction::mint_to(
  39. &spl_token::ID,
  40. ctx.accounts.mint.key,
  41. ctx.accounts.to.key,
  42. ctx.accounts.authority.key,
  43. &[],
  44. amount,
  45. )?;
  46. solana_program::program::invoke_signed(
  47. &ix,
  48. &[
  49. ctx.accounts.to.clone(),
  50. ctx.accounts.mint.clone(),
  51. ctx.accounts.authority.clone(),
  52. ctx.program.clone(),
  53. ],
  54. ctx.signer_seeds,
  55. )
  56. }
  57. pub fn burn<'a, 'b, 'c, 'info>(
  58. ctx: CpiContext<'a, 'b, 'c, 'info, Burn<'info>>,
  59. amount: u64,
  60. ) -> ProgramResult {
  61. let ix = spl_token::instruction::burn(
  62. &spl_token::ID,
  63. ctx.accounts.to.key,
  64. ctx.accounts.mint.key,
  65. ctx.accounts.authority.key,
  66. &[],
  67. amount,
  68. )?;
  69. solana_program::program::invoke_signed(
  70. &ix,
  71. &[
  72. ctx.accounts.to.clone(),
  73. ctx.accounts.mint.clone(),
  74. ctx.accounts.authority.clone(),
  75. ctx.program.clone(),
  76. ],
  77. ctx.signer_seeds,
  78. )
  79. }
  80. pub fn approve<'a, 'b, 'c, 'info>(
  81. ctx: CpiContext<'a, 'b, 'c, 'info, Approve<'info>>,
  82. amount: u64,
  83. ) -> ProgramResult {
  84. let ix = spl_token::instruction::approve(
  85. &spl_token::ID,
  86. ctx.accounts.to.key,
  87. ctx.accounts.delegate.key,
  88. ctx.accounts.authority.key,
  89. &[],
  90. amount,
  91. )?;
  92. solana_program::program::invoke_signed(
  93. &ix,
  94. &[
  95. ctx.accounts.to.clone(),
  96. ctx.accounts.delegate.clone(),
  97. ctx.accounts.authority.clone(),
  98. ctx.program.clone(),
  99. ],
  100. ctx.signer_seeds,
  101. )
  102. }
  103. pub fn initialize_account<'a, 'b, 'c, 'info>(
  104. ctx: CpiContext<'a, 'b, 'c, 'info, InitializeAccount<'info>>,
  105. ) -> ProgramResult {
  106. let ix = spl_token::instruction::initialize_account(
  107. &spl_token::ID,
  108. ctx.accounts.account.key,
  109. ctx.accounts.mint.key,
  110. ctx.accounts.authority.key,
  111. )?;
  112. solana_program::program::invoke_signed(
  113. &ix,
  114. &[
  115. ctx.accounts.account.clone(),
  116. ctx.accounts.mint.clone(),
  117. ctx.accounts.authority.clone(),
  118. ctx.accounts.rent.clone(),
  119. ctx.program.clone(),
  120. ],
  121. ctx.signer_seeds,
  122. )
  123. }
  124. pub fn close_account<'a, 'b, 'c, 'info>(
  125. ctx: CpiContext<'a, 'b, 'c, 'info, CloseAccount<'info>>,
  126. ) -> ProgramResult {
  127. let ix = spl_token::instruction::close_account(
  128. &spl_token::ID,
  129. ctx.accounts.account.key,
  130. ctx.accounts.destination.key,
  131. ctx.accounts.authority.key,
  132. &[], // TODO: support multisig
  133. )?;
  134. solana_program::program::invoke_signed(
  135. &ix,
  136. &[
  137. ctx.accounts.account.clone(),
  138. ctx.accounts.destination.clone(),
  139. ctx.accounts.authority.clone(),
  140. ],
  141. ctx.signer_seeds,
  142. )
  143. }
  144. pub fn initialize_mint<'a, 'b, 'c, 'info>(
  145. ctx: CpiContext<'a, 'b, 'c, 'info, InitializeMint<'info>>,
  146. decimals: u8,
  147. authority: &Pubkey,
  148. freeze_authority: Option<&Pubkey>,
  149. ) -> ProgramResult {
  150. let ix = spl_token::instruction::initialize_mint(
  151. &spl_token::ID,
  152. ctx.accounts.mint.key,
  153. authority,
  154. freeze_authority,
  155. decimals,
  156. )?;
  157. solana_program::program::invoke_signed(
  158. &ix,
  159. &[
  160. ctx.accounts.mint.clone(),
  161. ctx.accounts.rent.clone(),
  162. ctx.program.clone(),
  163. ],
  164. ctx.signer_seeds,
  165. )
  166. }
  167. pub fn set_authority<'a, 'b, 'c, 'info>(
  168. ctx: CpiContext<'a, 'b, 'c, 'info, SetAuthority<'info>>,
  169. authority_type: spl_token::instruction::AuthorityType,
  170. new_authority: Option<Pubkey>,
  171. ) -> ProgramResult {
  172. let mut spl_new_authority: Option<&Pubkey> = None;
  173. if new_authority.is_some() {
  174. spl_new_authority = new_authority.as_ref()
  175. }
  176. let ix = spl_token::instruction::set_authority(
  177. &spl_token::ID,
  178. ctx.accounts.account_or_mint.key,
  179. spl_new_authority,
  180. authority_type,
  181. ctx.accounts.current_authority.key,
  182. &[], // TODO: Support multisig signers.
  183. )?;
  184. solana_program::program::invoke_signed(
  185. &ix,
  186. &[
  187. ctx.accounts.account_or_mint.clone(),
  188. ctx.accounts.current_authority.clone(),
  189. ctx.program.clone(),
  190. ],
  191. ctx.signer_seeds,
  192. )
  193. }
  194. #[derive(Accounts)]
  195. pub struct Transfer<'info> {
  196. pub from: AccountInfo<'info>,
  197. pub to: AccountInfo<'info>,
  198. pub authority: AccountInfo<'info>,
  199. }
  200. #[derive(Accounts)]
  201. pub struct MintTo<'info> {
  202. pub mint: AccountInfo<'info>,
  203. pub to: AccountInfo<'info>,
  204. pub authority: AccountInfo<'info>,
  205. }
  206. #[derive(Accounts)]
  207. pub struct Burn<'info> {
  208. pub mint: AccountInfo<'info>,
  209. pub to: AccountInfo<'info>,
  210. pub authority: AccountInfo<'info>,
  211. }
  212. #[derive(Accounts)]
  213. pub struct Approve<'info> {
  214. pub to: AccountInfo<'info>,
  215. pub delegate: AccountInfo<'info>,
  216. pub authority: AccountInfo<'info>,
  217. }
  218. #[derive(Accounts)]
  219. pub struct InitializeAccount<'info> {
  220. pub account: AccountInfo<'info>,
  221. pub mint: AccountInfo<'info>,
  222. pub authority: AccountInfo<'info>,
  223. pub rent: AccountInfo<'info>,
  224. }
  225. #[derive(Accounts)]
  226. pub struct CloseAccount<'info> {
  227. pub account: AccountInfo<'info>,
  228. pub destination: AccountInfo<'info>,
  229. pub authority: AccountInfo<'info>,
  230. }
  231. #[derive(Accounts)]
  232. pub struct InitializeMint<'info> {
  233. pub mint: AccountInfo<'info>,
  234. pub rent: AccountInfo<'info>,
  235. }
  236. #[derive(Accounts)]
  237. pub struct SetAuthority<'info> {
  238. pub current_authority: AccountInfo<'info>,
  239. pub account_or_mint: AccountInfo<'info>,
  240. }
  241. #[derive(Clone)]
  242. pub struct TokenAccount(spl_token::state::Account);
  243. impl TokenAccount {
  244. pub const LEN: usize = spl_token::state::Account::LEN;
  245. }
  246. impl anchor_lang::AccountDeserialize for TokenAccount {
  247. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  248. TokenAccount::try_deserialize_unchecked(buf)
  249. }
  250. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  251. spl_token::state::Account::unpack(buf).map(TokenAccount)
  252. }
  253. }
  254. impl anchor_lang::AccountSerialize for TokenAccount {
  255. fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<(), ProgramError> {
  256. // no-op
  257. Ok(())
  258. }
  259. }
  260. impl anchor_lang::Owner for TokenAccount {
  261. fn owner() -> Pubkey {
  262. ID
  263. }
  264. }
  265. impl Deref for TokenAccount {
  266. type Target = spl_token::state::Account;
  267. fn deref(&self) -> &Self::Target {
  268. &self.0
  269. }
  270. }
  271. #[derive(Clone)]
  272. pub struct Mint(spl_token::state::Mint);
  273. impl Mint {
  274. pub const LEN: usize = spl_token::state::Mint::LEN;
  275. }
  276. impl anchor_lang::AccountDeserialize for Mint {
  277. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  278. Mint::try_deserialize_unchecked(buf)
  279. }
  280. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  281. spl_token::state::Mint::unpack(buf).map(Mint)
  282. }
  283. }
  284. impl anchor_lang::AccountSerialize for Mint {
  285. fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<(), ProgramError> {
  286. // no-op
  287. Ok(())
  288. }
  289. }
  290. impl anchor_lang::Owner for Mint {
  291. fn owner() -> Pubkey {
  292. ID
  293. }
  294. }
  295. impl Deref for Mint {
  296. type Target = spl_token::state::Mint;
  297. fn deref(&self) -> &Self::Target {
  298. &self.0
  299. }
  300. }
  301. #[derive(Clone)]
  302. pub struct Token;
  303. impl anchor_lang::AccountDeserialize for Token {
  304. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  305. Token::try_deserialize_unchecked(buf)
  306. }
  307. fn try_deserialize_unchecked(_buf: &mut &[u8]) -> Result<Self, ProgramError> {
  308. Ok(Token)
  309. }
  310. }
  311. impl anchor_lang::Id for Token {
  312. fn id() -> Pubkey {
  313. ID
  314. }
  315. }
  316. // Field parsers to save compute. All account validation is assumed to be done
  317. // outside of these methods.
  318. pub mod accessor {
  319. use super::*;
  320. pub fn amount(account: &AccountInfo) -> Result<u64, ProgramError> {
  321. let bytes = account.try_borrow_data()?;
  322. let mut amount_bytes = [0u8; 8];
  323. amount_bytes.copy_from_slice(&bytes[64..72]);
  324. Ok(u64::from_le_bytes(amount_bytes))
  325. }
  326. pub fn mint(account: &AccountInfo) -> Result<Pubkey, ProgramError> {
  327. let bytes = account.try_borrow_data()?;
  328. let mut mint_bytes = [0u8; 32];
  329. mint_bytes.copy_from_slice(&bytes[..32]);
  330. Ok(Pubkey::new_from_array(mint_bytes))
  331. }
  332. pub fn authority(account: &AccountInfo) -> Result<Pubkey, ProgramError> {
  333. let bytes = account.try_borrow_data()?;
  334. let mut owner_bytes = [0u8; 32];
  335. owner_bytes.copy_from_slice(&bytes[32..64]);
  336. Ok(Pubkey::new_from_array(owner_bytes))
  337. }
  338. }