instruction.rs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. //! Instruction types
  2. use crate::{error::TokenError, option::COption};
  3. use solana_sdk::{
  4. instruction::{AccountMeta, Instruction},
  5. program_error::ProgramError,
  6. pubkey::Pubkey,
  7. sysvar,
  8. };
  9. use std::mem::size_of;
  10. /// Minimum number of multisignature signers (min N)
  11. pub const MIN_SIGNERS: usize = 1;
  12. /// Maximum number of multisignature signers (max N)
  13. pub const MAX_SIGNERS: usize = 11;
  14. /// Instructions supported by the token program.
  15. #[repr(C)]
  16. #[derive(Clone, Debug, PartialEq)]
  17. pub enum TokenInstruction {
  18. /// Initializes a new mint and optionally deposits all the newly minted tokens in an account.
  19. ///
  20. /// The `InitializeMint` instruction requires no signers and MUST be included within
  21. /// the same Transaction as the system program's `CreateInstruction` that creates the account
  22. /// being initialized. Otherwise another party can acquire ownership of the uninitialized account.
  23. ///
  24. /// Accounts expected by this instruction:
  25. ///
  26. /// 0. `[writable]` The mint to initialize.
  27. /// 1. `[]` Rent sysvar
  28. ///
  29. InitializeMint {
  30. /// Number of base 10 digits to the right of the decimal place.
  31. decimals: u8,
  32. /// The authority/multisignature to mint tokens.
  33. mint_authority: Pubkey,
  34. /// The freeze authority/multisignature of the mint.
  35. freeze_authority: COption<Pubkey>,
  36. },
  37. /// Initializes a new account to hold tokens. If this account is associated with the native
  38. /// mint then the token balance of the initialized account will be equal to the amount of SOL
  39. /// in the account. If this account is associated with another mint, that mint must be
  40. /// initialized before this command can succeed.
  41. ///
  42. /// The `InitializeAccount` instruction requires no signers and MUST be included within
  43. /// the same Transaction as the system program's `CreateInstruction` that creates the account
  44. /// being initialized. Otherwise another party can acquire ownership of the uninitialized account.
  45. ///
  46. /// Accounts expected by this instruction:
  47. ///
  48. /// 0. `[writable]` The account to initialize.
  49. /// 1. `[]` The mint this account will be associated with.
  50. /// 2. `[]` The new account's owner/multisignature.
  51. /// 3. `[]` Rent sysvar
  52. InitializeAccount,
  53. /// Initializes a multisignature account with N provided signers.
  54. ///
  55. /// Multisignature accounts can used in place of any single owner/delegate accounts in any
  56. /// token instruction that require an owner/delegate to be present. The variant field represents the
  57. /// number of signers (M) required to validate this multisignature account.
  58. ///
  59. /// The `InitializeMultisig` instruction requires no signers and MUST be included within
  60. /// the same Transaction as the system program's `CreateInstruction` that creates the account
  61. /// being initialized. Otherwise another party can acquire ownership of the uninitialized account.
  62. ///
  63. /// Accounts expected by this instruction:
  64. ///
  65. /// 0. `[writable]` The multisignature account to initialize.
  66. /// 1. `[]` Rent sysvar
  67. /// 2. ..2+N. `[]` The signer accounts, must equal to N where 1 <= N <= 11.
  68. InitializeMultisig {
  69. /// The number of signers (M) required to validate this multisignature account.
  70. m: u8,
  71. },
  72. /// Transfers tokens from one account to another either directly or via a delegate. If this
  73. /// account is associated with the native mint then equal amounts of SOL and Tokens will be
  74. /// transferred to the destination account.
  75. ///
  76. /// Accounts expected by this instruction:
  77. ///
  78. /// * Single owner/delegate
  79. /// 0. `[writable]` The source account.
  80. /// 1. `[writable]` The destination account.
  81. /// 2. '[signer]' The source account's owner/delegate.
  82. ///
  83. /// * Multisignature owner/delegate
  84. /// 0. `[writable]` The source account.
  85. /// 1. `[writable]` The destination account.
  86. /// 2. '[]' The source account's multisignature owner/delegate.
  87. /// 3. ..3+M '[signer]' M signer accounts.
  88. Transfer {
  89. /// The amount of tokens to transfer.
  90. amount: u64,
  91. },
  92. /// Approves a delegate. A delegate is given the authority over
  93. /// tokens on behalf of the source account's owner.
  94. ///
  95. /// Accounts expected by this instruction:
  96. ///
  97. /// * Single owner
  98. /// 0. `[writable]` The source account.
  99. /// 1. `[]` The delegate.
  100. /// 2. `[signer]` The source account owner.
  101. ///
  102. /// * Multisignature owner
  103. /// 0. `[writable]` The source account.
  104. /// 1. `[]` The delegate.
  105. /// 2. '[]' The source account's multisignature owner.
  106. /// 3. ..3+M '[signer]' M signer accounts
  107. Approve {
  108. /// The amount of tokens the delegate is approved for.
  109. amount: u64,
  110. },
  111. /// Revokes the delegate's authority.
  112. ///
  113. /// Accounts expected by this instruction:
  114. ///
  115. /// * Single owner
  116. /// 0. `[writable]` The source account.
  117. /// 1. `[signer]` The source account owner.
  118. ///
  119. /// * Multisignature owner
  120. /// 0. `[writable]` The source account.
  121. /// 1. '[]' The source account's multisignature owner.
  122. /// 2. ..2+M '[signer]' M signer accounts
  123. Revoke,
  124. /// Sets a new authority of a mint or account.
  125. ///
  126. /// Accounts expected by this instruction:
  127. ///
  128. /// * Single authority
  129. /// 0. `[writable]` The mint or account to change the authority of.
  130. /// 1. `[signer]` The current authority of the mint or account.
  131. ///
  132. /// * Multisignature authority
  133. /// 0. `[writable]` The mint or account to change the authority of.
  134. /// 1. `[]` The mint's or account's multisignature authority.
  135. /// 2. ..2+M '[signer]' M signer accounts
  136. SetAuthority {
  137. /// The type of authority to update.
  138. authority_type: AuthorityType,
  139. /// The new authority
  140. new_authority: COption<Pubkey>,
  141. },
  142. /// Mints new tokens to an account. The native mint does not support minting.
  143. ///
  144. /// Accounts expected by this instruction:
  145. ///
  146. /// * Single authority
  147. /// 0. `[writable]` The mint.
  148. /// 1. `[writable]` The account to mint tokens to.
  149. /// 2. `[signer]` The mint's minting authority.
  150. ///
  151. /// * Multisignature authority
  152. /// 0. `[writable]` The mint.
  153. /// 1. `[writable]` The account to mint tokens to.
  154. /// 2. `[]` The mint's multisignature mint-tokens authority.
  155. /// 3. ..3+M '[signer]' M signer accounts.
  156. MintTo {
  157. /// The amount of new tokens to mint.
  158. amount: u64,
  159. },
  160. /// Burns tokens by removing them from an account. `Burn` does not support accounts
  161. /// associated with the native mint, use `CloseAccount` instead.
  162. ///
  163. /// Accounts expected by this instruction:
  164. ///
  165. /// * Single owner/delegate
  166. /// 0. `[writable]` The account to burn from.
  167. /// 1. '[writable]' The token mint.
  168. /// 2. `[signer]` The account's owner/delegate.
  169. ///
  170. /// * Multisignature owner/delegate
  171. /// 0. `[writable]` The account to burn from.
  172. /// 1. '[writable]' The token mint.
  173. /// 2. `[]` The account's multisignature owner/delegate.
  174. /// 3. ..3+M '[signer]' M signer accounts.
  175. Burn {
  176. /// The amount of tokens to burn.
  177. amount: u64,
  178. },
  179. /// Close an account by transferring all its SOL to the destination account.
  180. /// Non-native accounts may only be closed if its token amount is zero.
  181. ///
  182. /// Accounts expected by this instruction:
  183. ///
  184. /// * Single owner
  185. /// 0. `[writable]` The account to close.
  186. /// 1. '[writable]' The destination account.
  187. /// 2. `[signer]` The account's owner.
  188. ///
  189. /// * Multisignature owner
  190. /// 0. `[writable]` The account to close.
  191. /// 1. '[writable]' The destination account.
  192. /// 2. `[]` The account's multisignature owner.
  193. /// 3. ..3+M '[signer]' M signer accounts.
  194. CloseAccount,
  195. /// Freeze an Initialized account using the Mint's freeze_authority (if set).
  196. ///
  197. /// Accounts expected by this instruction:
  198. ///
  199. /// * Single owner
  200. /// 0. `[writable]` The account to freeze.
  201. /// 1. '[]' The token mint.
  202. /// 2. `[signer]` The mint freeze authority.
  203. ///
  204. /// * Multisignature owner
  205. /// 0. `[writable]` The account to freeze.
  206. /// 1. '[]' The token mint.
  207. /// 2. `[]` The mint's multisignature freeze authority.
  208. /// 3. ..3+M '[signer]' M signer accounts.
  209. FreezeAccount,
  210. /// Thaw a Frozen account using the Mint's freeze_authority (if set).
  211. ///
  212. /// Accounts expected by this instruction:
  213. ///
  214. /// * Single owner
  215. /// 0. `[writable]` The account to freeze.
  216. /// 1. '[]' The token mint.
  217. /// 2. `[signer]` The mint freeze authority.
  218. ///
  219. /// * Multisignature owner
  220. /// 0. `[writable]` The account to freeze.
  221. /// 1. '[]' The token mint.
  222. /// 2. `[]` The mint's multisignature freeze authority.
  223. /// 3. ..3+M '[signer]' M signer accounts.
  224. ThawAccount,
  225. /// Transfers tokens from one account to another either directly or via a delegate. If this
  226. /// account is associated with the native mint then equal amounts of SOL and Tokens will be
  227. /// transferred to the destination account.
  228. ///
  229. /// This instruction differs from Transfer in that the token mint and decimals value is
  230. /// asserted by the caller. This may be useful when creating transactions offline or within a
  231. /// hardware wallet.
  232. ///
  233. /// Accounts expected by this instruction:
  234. ///
  235. /// * Single owner/delegate
  236. /// 0. `[writable]` The source account.
  237. /// 1. '[]' The token mint.
  238. /// 2. `[writable]` The destination account.
  239. /// 3. '[signer]' The source account's owner/delegate.
  240. ///
  241. /// * Multisignature owner/delegate
  242. /// 0. `[writable]` The source account.
  243. /// 1. '[]' The token mint.
  244. /// 2. `[writable]` The destination account.
  245. /// 3. '[]' The source account's multisignature owner/delegate.
  246. /// 4. ..4+M '[signer]' M signer accounts.
  247. Transfer2 {
  248. /// The amount of tokens to transfer.
  249. amount: u64,
  250. /// Expected number of base 10 digits to the right of the decimal place.
  251. decimals: u8,
  252. },
  253. /// Approves a delegate. A delegate is given the authority over
  254. /// tokens on behalf of the source account's owner.
  255. ///
  256. /// This instruction differs from Approve in that the token mint and decimals value is asserted
  257. /// by the caller. This may be useful when creating transactions offline or within a hardware
  258. /// wallet.
  259. ///
  260. /// Accounts expected by this instruction:
  261. ///
  262. /// * Single owner
  263. /// 0. `[writable]` The source account.
  264. /// 1. '[]' The token mint.
  265. /// 2. `[]` The delegate.
  266. /// 3. `[signer]` The source account owner.
  267. ///
  268. /// * Multisignature owner
  269. /// 0. `[writable]` The source account.
  270. /// 1. '[]' The token mint.
  271. /// 2. `[]` The delegate.
  272. /// 3. '[]' The source account's multisignature owner.
  273. /// 4. ..4+M '[signer]' M signer accounts
  274. Approve2 {
  275. /// The amount of tokens the delegate is approved for.
  276. amount: u64,
  277. /// Expected number of base 10 digits to the right of the decimal place.
  278. decimals: u8,
  279. },
  280. /// Mints new tokens to an account. The native mint does not support minting.
  281. ///
  282. /// This instruction differs from MintTo in that the decimals value is asserted by the
  283. /// caller. This may be useful when creating transactions offline or within a hardware wallet.
  284. ///
  285. /// Accounts expected by this instruction:
  286. ///
  287. /// * Single authority
  288. /// 0. `[writable]` The mint.
  289. /// 1. `[writable]` The account to mint tokens to.
  290. /// 2. `[signer]` The mint's minting authority.
  291. ///
  292. /// * Multisignature authority
  293. /// 0. `[writable]` The mint.
  294. /// 1. `[writable]` The account to mint tokens to.
  295. /// 2. `[]` The mint's multisignature mint-tokens authority.
  296. /// 3. ..3+M '[signer]' M signer accounts.
  297. MintTo2 {
  298. /// The amount of new tokens to mint.
  299. amount: u64,
  300. /// Expected number of base 10 digits to the right of the decimal place.
  301. decimals: u8,
  302. },
  303. /// Burns tokens by removing them from an account. `Burn2` does not support accounts
  304. /// associated with the native mint, use `CloseAccount` instead.
  305. ///
  306. /// This instruction differs from Burn in that the decimals value is asserted by the caller.
  307. /// This may be useful when creating transactions offline or within a hardware wallet.
  308. ///
  309. /// Accounts expected by this instruction:
  310. ///
  311. /// * Single owner/delegate
  312. /// 0. `[writable]` The account to burn from.
  313. /// 1. '[writable]' The token mint.
  314. /// 2. `[signer]` The account's owner/delegate.
  315. ///
  316. /// * Multisignature owner/delegate
  317. /// 0. `[writable]` The account to burn from.
  318. /// 1. '[writable]' The token mint.
  319. /// 2. `[]` The account's multisignature owner/delegate.
  320. /// 3. ..3+M '[signer]' M signer accounts.
  321. Burn2 {
  322. /// The amount of tokens to burn.
  323. amount: u64,
  324. /// Expected number of base 10 digits to the right of the decimal place.
  325. decimals: u8,
  326. },
  327. }
  328. impl TokenInstruction {
  329. /// Unpacks a byte buffer into a [TokenInstruction](enum.TokenInstruction.html).
  330. pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
  331. if input.len() < size_of::<u8>() {
  332. return Err(TokenError::InvalidInstruction.into());
  333. }
  334. Ok(match input[0] {
  335. 0 => {
  336. if input.len() < size_of::<u8>() + size_of::<Pubkey>() + size_of::<bool>() {
  337. return Err(TokenError::InvalidInstruction.into());
  338. }
  339. let mut input_len = 0;
  340. input_len += size_of::<u8>();
  341. let decimals = unsafe { *(&input[input_len] as *const u8) };
  342. input_len += size_of::<u8>();
  343. let mint_authority = unsafe { *(&input[input_len] as *const u8 as *const Pubkey) };
  344. input_len += size_of::<Pubkey>();
  345. let freeze_authority = COption::unpack_or(
  346. input,
  347. &mut input_len,
  348. Into::<ProgramError>::into(TokenError::InvalidInstruction),
  349. )?;
  350. Self::InitializeMint {
  351. mint_authority,
  352. freeze_authority,
  353. decimals,
  354. }
  355. }
  356. 1 => Self::InitializeAccount,
  357. 2 => {
  358. if input.len() < size_of::<u8>() + size_of::<u8>() {
  359. return Err(TokenError::InvalidInstruction.into());
  360. }
  361. #[allow(clippy::cast_ptr_alignment)]
  362. let m = unsafe { *(&input[1] as *const u8) };
  363. Self::InitializeMultisig { m }
  364. }
  365. 3 => {
  366. if input.len() < size_of::<u8>() + size_of::<u64>() {
  367. return Err(TokenError::InvalidInstruction.into());
  368. }
  369. #[allow(clippy::cast_ptr_alignment)]
  370. let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
  371. Self::Transfer { amount }
  372. }
  373. 4 => {
  374. if input.len() < size_of::<u8>() + size_of::<u64>() {
  375. return Err(TokenError::InvalidInstruction.into());
  376. }
  377. #[allow(clippy::cast_ptr_alignment)]
  378. let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
  379. Self::Approve { amount }
  380. }
  381. 5 => Self::Revoke,
  382. 6 => {
  383. if input.len() < size_of::<u8>() + size_of::<u8>() {
  384. return Err(TokenError::InvalidInstruction.into());
  385. }
  386. let mut input_len = 0;
  387. input_len += size_of::<u8>();
  388. let authority_type = AuthorityType::from(input[1])?;
  389. input_len += size_of::<u8>();
  390. let new_authority = COption::unpack_or(
  391. input,
  392. &mut input_len,
  393. Into::<ProgramError>::into(TokenError::InvalidInstruction),
  394. )?;
  395. Self::SetAuthority {
  396. authority_type,
  397. new_authority,
  398. }
  399. }
  400. 7 => {
  401. if input.len() < size_of::<u8>() + size_of::<u64>() {
  402. return Err(TokenError::InvalidInstruction.into());
  403. }
  404. #[allow(clippy::cast_ptr_alignment)]
  405. let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
  406. Self::MintTo { amount }
  407. }
  408. 8 => {
  409. if input.len() < size_of::<u8>() + size_of::<u64>() {
  410. return Err(TokenError::InvalidInstruction.into());
  411. }
  412. #[allow(clippy::cast_ptr_alignment)]
  413. let amount = unsafe { *(&input[size_of::<u8>()] as *const u8 as *const u64) };
  414. Self::Burn { amount }
  415. }
  416. 9 => Self::CloseAccount,
  417. 10 => Self::FreezeAccount,
  418. 11 => Self::ThawAccount,
  419. 12 => {
  420. if input.len() < size_of::<u8>() + size_of::<u64>() + size_of::<u8>() {
  421. return Err(TokenError::InvalidInstruction.into());
  422. }
  423. let mut input_len = 0;
  424. input_len += size_of::<u8>();
  425. #[allow(clippy::cast_ptr_alignment)]
  426. let amount = unsafe { *(&input[input_len] as *const u8 as *const u64) };
  427. input_len += size_of::<u64>();
  428. let decimals = unsafe { *(&input[input_len] as *const u8) };
  429. Self::Transfer2 { amount, decimals }
  430. }
  431. 13 => {
  432. if input.len() < size_of::<u8>() + size_of::<u64>() + size_of::<u8>() {
  433. return Err(TokenError::InvalidInstruction.into());
  434. }
  435. let mut input_len = 0;
  436. input_len += size_of::<u8>();
  437. #[allow(clippy::cast_ptr_alignment)]
  438. let amount = unsafe { *(&input[input_len] as *const u8 as *const u64) };
  439. input_len += size_of::<u64>();
  440. let decimals = unsafe { *(&input[input_len] as *const u8) };
  441. Self::Approve2 { amount, decimals }
  442. }
  443. 14 => {
  444. if input.len() < size_of::<u8>() + size_of::<u64>() + size_of::<u8>() {
  445. return Err(TokenError::InvalidInstruction.into());
  446. }
  447. let mut input_len = 0;
  448. input_len += size_of::<u8>();
  449. #[allow(clippy::cast_ptr_alignment)]
  450. let amount = unsafe { *(&input[input_len] as *const u8 as *const u64) };
  451. input_len += size_of::<u64>();
  452. let decimals = unsafe { *(&input[input_len] as *const u8) };
  453. Self::MintTo2 { amount, decimals }
  454. }
  455. 15 => {
  456. if input.len() < size_of::<u8>() + size_of::<u64>() + size_of::<u8>() {
  457. return Err(TokenError::InvalidInstruction.into());
  458. }
  459. let mut input_len = 0;
  460. input_len += size_of::<u8>();
  461. #[allow(clippy::cast_ptr_alignment)]
  462. let amount = unsafe { *(&input[input_len] as *const u8 as *const u64) };
  463. input_len += size_of::<u64>();
  464. let decimals = unsafe { *(&input[input_len] as *const u8) };
  465. Self::Burn2 { amount, decimals }
  466. }
  467. _ => return Err(TokenError::InvalidInstruction.into()),
  468. })
  469. }
  470. /// Packs a [TokenInstruction](enum.TokenInstruction.html) into a byte buffer.
  471. pub fn pack(&self) -> Result<Vec<u8>, ProgramError> {
  472. let mut output = vec![0u8; size_of::<TokenInstruction>()];
  473. let mut output_len = 0;
  474. match self {
  475. Self::InitializeMint {
  476. mint_authority,
  477. freeze_authority,
  478. decimals,
  479. } => {
  480. output[output_len] = 0;
  481. output_len += size_of::<u8>();
  482. let value = unsafe { &mut *(&mut output[output_len] as *mut u8) };
  483. *value = *decimals;
  484. output_len += size_of::<u8>();
  485. #[allow(clippy::cast_ptr_alignment)]
  486. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut Pubkey) };
  487. *value = *mint_authority;
  488. output_len += size_of::<Pubkey>();
  489. freeze_authority.pack(&mut output, &mut output_len);
  490. }
  491. Self::InitializeAccount => {
  492. output[output_len] = 1;
  493. output_len += size_of::<u8>();
  494. }
  495. Self::InitializeMultisig { m } => {
  496. output[output_len] = 2;
  497. output_len += size_of::<u8>();
  498. #[allow(clippy::cast_ptr_alignment)]
  499. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u8) };
  500. *value = *m;
  501. output_len += size_of::<u8>();
  502. }
  503. Self::Transfer { amount } => {
  504. output[output_len] = 3;
  505. output_len += size_of::<u8>();
  506. #[allow(clippy::cast_ptr_alignment)]
  507. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  508. *value = *amount;
  509. output_len += size_of::<u64>();
  510. }
  511. Self::Approve { amount } => {
  512. output[output_len] = 4;
  513. output_len += size_of::<u8>();
  514. #[allow(clippy::cast_ptr_alignment)]
  515. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  516. *value = *amount;
  517. output_len += size_of::<u64>();
  518. }
  519. Self::Revoke => {
  520. output[output_len] = 5;
  521. output_len += size_of::<u8>();
  522. }
  523. Self::SetAuthority {
  524. authority_type,
  525. new_authority,
  526. } => {
  527. output[output_len] = 6;
  528. output_len += size_of::<u8>();
  529. output[output_len] = authority_type.into();
  530. output_len += size_of::<u8>();
  531. new_authority.pack(&mut output, &mut output_len);
  532. }
  533. Self::MintTo { amount } => {
  534. output[output_len] = 7;
  535. output_len += size_of::<u8>();
  536. #[allow(clippy::cast_ptr_alignment)]
  537. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  538. *value = *amount;
  539. output_len += size_of::<u64>();
  540. }
  541. Self::Burn { amount } => {
  542. output[output_len] = 8;
  543. output_len += size_of::<u8>();
  544. #[allow(clippy::cast_ptr_alignment)]
  545. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  546. *value = *amount;
  547. output_len += size_of::<u64>();
  548. }
  549. Self::CloseAccount => {
  550. output[output_len] = 9;
  551. output_len += size_of::<u8>();
  552. }
  553. Self::FreezeAccount => {
  554. output[output_len] = 10;
  555. output_len += size_of::<u8>();
  556. }
  557. Self::ThawAccount => {
  558. output[output_len] = 11;
  559. output_len += size_of::<u8>();
  560. }
  561. Self::Transfer2 { amount, decimals } => {
  562. output[output_len] = 12;
  563. output_len += size_of::<u8>();
  564. #[allow(clippy::cast_ptr_alignment)]
  565. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  566. *value = *amount;
  567. output_len += size_of::<u64>();
  568. let value = unsafe { &mut *(&mut output[output_len] as *mut u8) };
  569. *value = *decimals;
  570. output_len += size_of::<u8>();
  571. }
  572. Self::Approve2 { amount, decimals } => {
  573. output[output_len] = 13;
  574. output_len += size_of::<u8>();
  575. #[allow(clippy::cast_ptr_alignment)]
  576. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  577. *value = *amount;
  578. output_len += size_of::<u64>();
  579. let value = unsafe { &mut *(&mut output[output_len] as *mut u8) };
  580. *value = *decimals;
  581. output_len += size_of::<u8>();
  582. }
  583. Self::MintTo2 { amount, decimals } => {
  584. output[output_len] = 14;
  585. output_len += size_of::<u8>();
  586. #[allow(clippy::cast_ptr_alignment)]
  587. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  588. *value = *amount;
  589. output_len += size_of::<u64>();
  590. let value = unsafe { &mut *(&mut output[output_len] as *mut u8) };
  591. *value = *decimals;
  592. output_len += size_of::<u8>();
  593. }
  594. Self::Burn2 { amount, decimals } => {
  595. output[output_len] = 15;
  596. output_len += size_of::<u8>();
  597. #[allow(clippy::cast_ptr_alignment)]
  598. let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
  599. *value = *amount;
  600. output_len += size_of::<u64>();
  601. let value = unsafe { &mut *(&mut output[output_len] as *mut u8) };
  602. *value = *decimals;
  603. output_len += size_of::<u8>();
  604. }
  605. }
  606. output.truncate(output_len);
  607. Ok(output)
  608. }
  609. }
  610. /// Specifies the authority type for SetAuthority instructions
  611. #[repr(u8)]
  612. #[derive(Clone, Debug, PartialEq)]
  613. pub enum AuthorityType {
  614. /// Authority to mint new tokens
  615. MintTokens,
  616. /// Authority to freeze any account associated with the Mint
  617. FreezeAccount,
  618. /// Holder of a given token account
  619. AccountHolder,
  620. /// Authority to close a token account
  621. CloseAccount,
  622. }
  623. impl AuthorityType {
  624. fn into(&self) -> u8 {
  625. match self {
  626. AuthorityType::MintTokens => 0,
  627. AuthorityType::FreezeAccount => 1,
  628. AuthorityType::AccountHolder => 2,
  629. AuthorityType::CloseAccount => 3,
  630. }
  631. }
  632. fn from(index: u8) -> Result<Self, ProgramError> {
  633. match index {
  634. 0 => Ok(AuthorityType::MintTokens),
  635. 1 => Ok(AuthorityType::FreezeAccount),
  636. 2 => Ok(AuthorityType::AccountHolder),
  637. 3 => Ok(AuthorityType::CloseAccount),
  638. _ => Err(TokenError::InvalidInstruction.into()),
  639. }
  640. }
  641. }
  642. /// Creates a 'InitializeMint' instruction.
  643. pub fn initialize_mint(
  644. token_program_id: &Pubkey,
  645. mint_pubkey: &Pubkey,
  646. mint_authority_pubkey: &Pubkey,
  647. freeze_authority_pubkey: Option<&Pubkey>,
  648. decimals: u8,
  649. ) -> Result<Instruction, ProgramError> {
  650. let freeze_authority = freeze_authority_pubkey.cloned().into();
  651. let data = TokenInstruction::InitializeMint {
  652. mint_authority: *mint_authority_pubkey,
  653. freeze_authority,
  654. decimals,
  655. }
  656. .pack()?;
  657. let accounts = vec![
  658. AccountMeta::new(*mint_pubkey, false),
  659. AccountMeta::new_readonly(sysvar::rent::id(), false),
  660. ];
  661. Ok(Instruction {
  662. program_id: *token_program_id,
  663. accounts,
  664. data,
  665. })
  666. }
  667. /// Creates a `InitializeAccount` instruction.
  668. pub fn initialize_account(
  669. token_program_id: &Pubkey,
  670. account_pubkey: &Pubkey,
  671. mint_pubkey: &Pubkey,
  672. owner_pubkey: &Pubkey,
  673. ) -> Result<Instruction, ProgramError> {
  674. let data = TokenInstruction::InitializeAccount.pack()?;
  675. let accounts = vec![
  676. AccountMeta::new(*account_pubkey, false),
  677. AccountMeta::new_readonly(*mint_pubkey, false),
  678. AccountMeta::new_readonly(*owner_pubkey, false),
  679. AccountMeta::new_readonly(sysvar::rent::id(), false),
  680. ];
  681. Ok(Instruction {
  682. program_id: *token_program_id,
  683. accounts,
  684. data,
  685. })
  686. }
  687. /// Creates a `InitializeMultisig` instruction.
  688. pub fn initialize_multisig(
  689. token_program_id: &Pubkey,
  690. multisig_pubkey: &Pubkey,
  691. signer_pubkeys: &[&Pubkey],
  692. m: u8,
  693. ) -> Result<Instruction, ProgramError> {
  694. if !is_valid_signer_index(m as usize)
  695. || !is_valid_signer_index(signer_pubkeys.len())
  696. || m as usize > signer_pubkeys.len()
  697. {
  698. return Err(ProgramError::MissingRequiredSignature);
  699. }
  700. let data = TokenInstruction::InitializeMultisig { m }.pack()?;
  701. let mut accounts = Vec::with_capacity(1 + 1 + signer_pubkeys.len());
  702. accounts.push(AccountMeta::new(*multisig_pubkey, false));
  703. accounts.push(AccountMeta::new_readonly(sysvar::rent::id(), false));
  704. for signer_pubkey in signer_pubkeys.iter() {
  705. accounts.push(AccountMeta::new_readonly(**signer_pubkey, false));
  706. }
  707. Ok(Instruction {
  708. program_id: *token_program_id,
  709. accounts,
  710. data,
  711. })
  712. }
  713. /// Creates a `Transfer` instruction.
  714. pub fn transfer(
  715. token_program_id: &Pubkey,
  716. source_pubkey: &Pubkey,
  717. destination_pubkey: &Pubkey,
  718. authority_pubkey: &Pubkey,
  719. signer_pubkeys: &[&Pubkey],
  720. amount: u64,
  721. ) -> Result<Instruction, ProgramError> {
  722. let data = TokenInstruction::Transfer { amount }.pack()?;
  723. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  724. accounts.push(AccountMeta::new(*source_pubkey, false));
  725. accounts.push(AccountMeta::new(*destination_pubkey, false));
  726. accounts.push(AccountMeta::new_readonly(
  727. *authority_pubkey,
  728. signer_pubkeys.is_empty(),
  729. ));
  730. for signer_pubkey in signer_pubkeys.iter() {
  731. accounts.push(AccountMeta::new(**signer_pubkey, true));
  732. }
  733. Ok(Instruction {
  734. program_id: *token_program_id,
  735. accounts,
  736. data,
  737. })
  738. }
  739. /// Creates an `Approve` instruction.
  740. pub fn approve(
  741. token_program_id: &Pubkey,
  742. source_pubkey: &Pubkey,
  743. delegate_pubkey: &Pubkey,
  744. owner_pubkey: &Pubkey,
  745. signer_pubkeys: &[&Pubkey],
  746. amount: u64,
  747. ) -> Result<Instruction, ProgramError> {
  748. let data = TokenInstruction::Approve { amount }.pack()?;
  749. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  750. accounts.push(AccountMeta::new(*source_pubkey, false));
  751. accounts.push(AccountMeta::new_readonly(*delegate_pubkey, false));
  752. accounts.push(AccountMeta::new_readonly(
  753. *owner_pubkey,
  754. signer_pubkeys.is_empty(),
  755. ));
  756. for signer_pubkey in signer_pubkeys.iter() {
  757. accounts.push(AccountMeta::new(**signer_pubkey, true));
  758. }
  759. Ok(Instruction {
  760. program_id: *token_program_id,
  761. accounts,
  762. data,
  763. })
  764. }
  765. /// Creates a `Revoke` instruction.
  766. pub fn revoke(
  767. token_program_id: &Pubkey,
  768. source_pubkey: &Pubkey,
  769. owner_pubkey: &Pubkey,
  770. signer_pubkeys: &[&Pubkey],
  771. ) -> Result<Instruction, ProgramError> {
  772. let data = TokenInstruction::Revoke.pack()?;
  773. let mut accounts = Vec::with_capacity(2 + signer_pubkeys.len());
  774. accounts.push(AccountMeta::new_readonly(*source_pubkey, false));
  775. accounts.push(AccountMeta::new_readonly(
  776. *owner_pubkey,
  777. signer_pubkeys.is_empty(),
  778. ));
  779. for signer_pubkey in signer_pubkeys.iter() {
  780. accounts.push(AccountMeta::new(**signer_pubkey, true));
  781. }
  782. Ok(Instruction {
  783. program_id: *token_program_id,
  784. accounts,
  785. data,
  786. })
  787. }
  788. /// Creates a `SetAuthority` instruction.
  789. pub fn set_authority(
  790. token_program_id: &Pubkey,
  791. owned_pubkey: &Pubkey,
  792. new_authority_pubkey: Option<&Pubkey>,
  793. authority_type: AuthorityType,
  794. owner_pubkey: &Pubkey,
  795. signer_pubkeys: &[&Pubkey],
  796. ) -> Result<Instruction, ProgramError> {
  797. let new_authority = new_authority_pubkey.cloned().into();
  798. let data = TokenInstruction::SetAuthority {
  799. authority_type,
  800. new_authority,
  801. }
  802. .pack()?;
  803. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  804. accounts.push(AccountMeta::new(*owned_pubkey, false));
  805. accounts.push(AccountMeta::new_readonly(
  806. *owner_pubkey,
  807. signer_pubkeys.is_empty(),
  808. ));
  809. for signer_pubkey in signer_pubkeys.iter() {
  810. accounts.push(AccountMeta::new(**signer_pubkey, true));
  811. }
  812. Ok(Instruction {
  813. program_id: *token_program_id,
  814. accounts,
  815. data,
  816. })
  817. }
  818. /// Creates a `MintTo` instruction.
  819. pub fn mint_to(
  820. token_program_id: &Pubkey,
  821. mint_pubkey: &Pubkey,
  822. account_pubkey: &Pubkey,
  823. owner_pubkey: &Pubkey,
  824. signer_pubkeys: &[&Pubkey],
  825. amount: u64,
  826. ) -> Result<Instruction, ProgramError> {
  827. let data = TokenInstruction::MintTo { amount }.pack()?;
  828. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  829. accounts.push(AccountMeta::new(*mint_pubkey, false));
  830. accounts.push(AccountMeta::new(*account_pubkey, false));
  831. accounts.push(AccountMeta::new_readonly(
  832. *owner_pubkey,
  833. signer_pubkeys.is_empty(),
  834. ));
  835. for signer_pubkey in signer_pubkeys.iter() {
  836. accounts.push(AccountMeta::new(**signer_pubkey, true));
  837. }
  838. Ok(Instruction {
  839. program_id: *token_program_id,
  840. accounts,
  841. data,
  842. })
  843. }
  844. /// Creates a `Burn` instruction.
  845. pub fn burn(
  846. token_program_id: &Pubkey,
  847. account_pubkey: &Pubkey,
  848. mint_pubkey: &Pubkey,
  849. authority_pubkey: &Pubkey,
  850. signer_pubkeys: &[&Pubkey],
  851. amount: u64,
  852. ) -> Result<Instruction, ProgramError> {
  853. let data = TokenInstruction::Burn { amount }.pack()?;
  854. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  855. accounts.push(AccountMeta::new(*account_pubkey, false));
  856. accounts.push(AccountMeta::new(*mint_pubkey, false));
  857. accounts.push(AccountMeta::new_readonly(
  858. *authority_pubkey,
  859. signer_pubkeys.is_empty(),
  860. ));
  861. for signer_pubkey in signer_pubkeys.iter() {
  862. accounts.push(AccountMeta::new(**signer_pubkey, true));
  863. }
  864. Ok(Instruction {
  865. program_id: *token_program_id,
  866. accounts,
  867. data,
  868. })
  869. }
  870. /// Creates a `CloseAccount` instruction.
  871. pub fn close_account(
  872. token_program_id: &Pubkey,
  873. account_pubkey: &Pubkey,
  874. destination_pubkey: &Pubkey,
  875. owner_pubkey: &Pubkey,
  876. signer_pubkeys: &[&Pubkey],
  877. ) -> Result<Instruction, ProgramError> {
  878. let data = TokenInstruction::CloseAccount.pack()?;
  879. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  880. accounts.push(AccountMeta::new(*account_pubkey, false));
  881. accounts.push(AccountMeta::new(*destination_pubkey, false));
  882. accounts.push(AccountMeta::new_readonly(
  883. *owner_pubkey,
  884. signer_pubkeys.is_empty(),
  885. ));
  886. for signer_pubkey in signer_pubkeys.iter() {
  887. accounts.push(AccountMeta::new(**signer_pubkey, true));
  888. }
  889. Ok(Instruction {
  890. program_id: *token_program_id,
  891. accounts,
  892. data,
  893. })
  894. }
  895. /// Creates a `FreezeAccount` instruction.
  896. pub fn freeze_account(
  897. token_program_id: &Pubkey,
  898. account_pubkey: &Pubkey,
  899. mint_pubkey: &Pubkey,
  900. owner_pubkey: &Pubkey,
  901. signer_pubkeys: &[&Pubkey],
  902. ) -> Result<Instruction, ProgramError> {
  903. let data = TokenInstruction::FreezeAccount.pack()?;
  904. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  905. accounts.push(AccountMeta::new(*account_pubkey, false));
  906. accounts.push(AccountMeta::new_readonly(*mint_pubkey, false));
  907. accounts.push(AccountMeta::new_readonly(
  908. *owner_pubkey,
  909. signer_pubkeys.is_empty(),
  910. ));
  911. for signer_pubkey in signer_pubkeys.iter() {
  912. accounts.push(AccountMeta::new(**signer_pubkey, true));
  913. }
  914. Ok(Instruction {
  915. program_id: *token_program_id,
  916. accounts,
  917. data,
  918. })
  919. }
  920. /// Creates a `ThawAccount` instruction.
  921. pub fn thaw_account(
  922. token_program_id: &Pubkey,
  923. account_pubkey: &Pubkey,
  924. mint_pubkey: &Pubkey,
  925. owner_pubkey: &Pubkey,
  926. signer_pubkeys: &[&Pubkey],
  927. ) -> Result<Instruction, ProgramError> {
  928. let data = TokenInstruction::ThawAccount.pack()?;
  929. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  930. accounts.push(AccountMeta::new(*account_pubkey, false));
  931. accounts.push(AccountMeta::new_readonly(*mint_pubkey, false));
  932. accounts.push(AccountMeta::new_readonly(
  933. *owner_pubkey,
  934. signer_pubkeys.is_empty(),
  935. ));
  936. for signer_pubkey in signer_pubkeys.iter() {
  937. accounts.push(AccountMeta::new(**signer_pubkey, true));
  938. }
  939. Ok(Instruction {
  940. program_id: *token_program_id,
  941. accounts,
  942. data,
  943. })
  944. }
  945. /// Creates a `Transfer2` instruction.
  946. #[allow(clippy::too_many_arguments)]
  947. pub fn transfer2(
  948. token_program_id: &Pubkey,
  949. source_pubkey: &Pubkey,
  950. mint_pubkey: &Pubkey,
  951. destination_pubkey: &Pubkey,
  952. authority_pubkey: &Pubkey,
  953. signer_pubkeys: &[&Pubkey],
  954. amount: u64,
  955. decimals: u8,
  956. ) -> Result<Instruction, ProgramError> {
  957. let data = TokenInstruction::Transfer2 { amount, decimals }.pack()?;
  958. let mut accounts = Vec::with_capacity(4 + signer_pubkeys.len());
  959. accounts.push(AccountMeta::new(*source_pubkey, false));
  960. accounts.push(AccountMeta::new_readonly(*mint_pubkey, false));
  961. accounts.push(AccountMeta::new(*destination_pubkey, false));
  962. accounts.push(AccountMeta::new_readonly(
  963. *authority_pubkey,
  964. signer_pubkeys.is_empty(),
  965. ));
  966. for signer_pubkey in signer_pubkeys.iter() {
  967. accounts.push(AccountMeta::new(**signer_pubkey, true));
  968. }
  969. Ok(Instruction {
  970. program_id: *token_program_id,
  971. accounts,
  972. data,
  973. })
  974. }
  975. /// Creates an `Approve2` instruction.
  976. #[allow(clippy::too_many_arguments)]
  977. pub fn approve2(
  978. token_program_id: &Pubkey,
  979. source_pubkey: &Pubkey,
  980. mint_pubkey: &Pubkey,
  981. delegate_pubkey: &Pubkey,
  982. owner_pubkey: &Pubkey,
  983. signer_pubkeys: &[&Pubkey],
  984. amount: u64,
  985. decimals: u8,
  986. ) -> Result<Instruction, ProgramError> {
  987. let data = TokenInstruction::Approve2 { amount, decimals }.pack()?;
  988. let mut accounts = Vec::with_capacity(4 + signer_pubkeys.len());
  989. accounts.push(AccountMeta::new(*source_pubkey, false));
  990. accounts.push(AccountMeta::new_readonly(*mint_pubkey, false));
  991. accounts.push(AccountMeta::new_readonly(*delegate_pubkey, false));
  992. accounts.push(AccountMeta::new_readonly(
  993. *owner_pubkey,
  994. signer_pubkeys.is_empty(),
  995. ));
  996. for signer_pubkey in signer_pubkeys.iter() {
  997. accounts.push(AccountMeta::new(**signer_pubkey, true));
  998. }
  999. Ok(Instruction {
  1000. program_id: *token_program_id,
  1001. accounts,
  1002. data,
  1003. })
  1004. }
  1005. /// Creates a `MintTo2` instruction.
  1006. pub fn mint_to2(
  1007. token_program_id: &Pubkey,
  1008. mint_pubkey: &Pubkey,
  1009. account_pubkey: &Pubkey,
  1010. owner_pubkey: &Pubkey,
  1011. signer_pubkeys: &[&Pubkey],
  1012. amount: u64,
  1013. decimals: u8,
  1014. ) -> Result<Instruction, ProgramError> {
  1015. let data = TokenInstruction::MintTo2 { amount, decimals }.pack()?;
  1016. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  1017. accounts.push(AccountMeta::new(*mint_pubkey, false));
  1018. accounts.push(AccountMeta::new(*account_pubkey, false));
  1019. accounts.push(AccountMeta::new_readonly(
  1020. *owner_pubkey,
  1021. signer_pubkeys.is_empty(),
  1022. ));
  1023. for signer_pubkey in signer_pubkeys.iter() {
  1024. accounts.push(AccountMeta::new(**signer_pubkey, true));
  1025. }
  1026. Ok(Instruction {
  1027. program_id: *token_program_id,
  1028. accounts,
  1029. data,
  1030. })
  1031. }
  1032. /// Creates a `Burn2` instruction.
  1033. pub fn burn2(
  1034. token_program_id: &Pubkey,
  1035. account_pubkey: &Pubkey,
  1036. mint_pubkey: &Pubkey,
  1037. authority_pubkey: &Pubkey,
  1038. signer_pubkeys: &[&Pubkey],
  1039. amount: u64,
  1040. decimals: u8,
  1041. ) -> Result<Instruction, ProgramError> {
  1042. let data = TokenInstruction::Burn2 { amount, decimals }.pack()?;
  1043. let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len());
  1044. accounts.push(AccountMeta::new(*account_pubkey, false));
  1045. accounts.push(AccountMeta::new(*mint_pubkey, false));
  1046. accounts.push(AccountMeta::new_readonly(
  1047. *authority_pubkey,
  1048. signer_pubkeys.is_empty(),
  1049. ));
  1050. for signer_pubkey in signer_pubkeys.iter() {
  1051. accounts.push(AccountMeta::new(**signer_pubkey, true));
  1052. }
  1053. Ok(Instruction {
  1054. program_id: *token_program_id,
  1055. accounts,
  1056. data,
  1057. })
  1058. }
  1059. /// Utility function that checks index is between MIN_SIGNERS and MAX_SIGNERS
  1060. pub fn is_valid_signer_index(index: usize) -> bool {
  1061. !(index < MIN_SIGNERS || index > MAX_SIGNERS)
  1062. }
  1063. #[cfg(test)]
  1064. mod test {
  1065. use super::*;
  1066. #[test]
  1067. fn test_instruction_packing() {
  1068. let check = TokenInstruction::InitializeMint {
  1069. decimals: 2,
  1070. mint_authority: Pubkey::new(&[1u8; 32]),
  1071. freeze_authority: COption::None,
  1072. };
  1073. let packed = check.pack().unwrap();
  1074. let mut expect = Vec::from([0u8, 2]);
  1075. expect.extend_from_slice(&[1u8; 32]);
  1076. expect.extend_from_slice(&[0]);
  1077. assert_eq!(packed, expect);
  1078. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1079. assert_eq!(unpacked, check);
  1080. let check = TokenInstruction::InitializeMint {
  1081. decimals: 2,
  1082. mint_authority: Pubkey::new(&[2u8; 32]),
  1083. freeze_authority: COption::Some(Pubkey::new(&[3u8; 32])),
  1084. };
  1085. let packed = check.pack().unwrap();
  1086. let mut expect = vec![0u8, 2];
  1087. expect.extend_from_slice(&[2u8; 32]);
  1088. expect.extend_from_slice(&[1]);
  1089. expect.extend_from_slice(&[3u8; 32]);
  1090. assert_eq!(packed, expect);
  1091. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1092. assert_eq!(unpacked, check);
  1093. let check = TokenInstruction::InitializeAccount;
  1094. let packed = check.pack().unwrap();
  1095. let expect = Vec::from([1u8]);
  1096. assert_eq!(packed, expect);
  1097. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1098. assert_eq!(unpacked, check);
  1099. let check = TokenInstruction::InitializeMultisig { m: 1 };
  1100. let packed = check.pack().unwrap();
  1101. let expect = Vec::from([2u8, 1]);
  1102. assert_eq!(packed, expect);
  1103. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1104. assert_eq!(unpacked, check);
  1105. let check = TokenInstruction::Transfer { amount: 1 };
  1106. let packed = check.pack().unwrap();
  1107. let expect = Vec::from([3u8, 1, 0, 0, 0, 0, 0, 0, 0]);
  1108. assert_eq!(packed, expect);
  1109. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1110. assert_eq!(unpacked, check);
  1111. let check = TokenInstruction::Approve { amount: 1 };
  1112. let packed = check.pack().unwrap();
  1113. let expect = Vec::from([4u8, 1, 0, 0, 0, 0, 0, 0, 0]);
  1114. assert_eq!(packed, expect);
  1115. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1116. assert_eq!(unpacked, check);
  1117. let check = TokenInstruction::Revoke;
  1118. let packed = check.pack().unwrap();
  1119. let expect = Vec::from([5u8]);
  1120. assert_eq!(packed, expect);
  1121. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1122. assert_eq!(unpacked, check);
  1123. let check = TokenInstruction::SetAuthority {
  1124. authority_type: AuthorityType::FreezeAccount,
  1125. new_authority: COption::Some(Pubkey::new(&[4u8; 32])),
  1126. };
  1127. let packed = check.pack().unwrap();
  1128. let mut expect = Vec::from([6u8, 1]);
  1129. expect.extend_from_slice(&[1]);
  1130. expect.extend_from_slice(&[4u8; 32]);
  1131. assert_eq!(packed, expect);
  1132. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1133. assert_eq!(unpacked, check);
  1134. let check = TokenInstruction::MintTo { amount: 1 };
  1135. let packed = check.pack().unwrap();
  1136. let expect = Vec::from([7u8, 1, 0, 0, 0, 0, 0, 0, 0]);
  1137. assert_eq!(packed, expect);
  1138. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1139. assert_eq!(unpacked, check);
  1140. let check = TokenInstruction::Burn { amount: 1 };
  1141. let packed = check.pack().unwrap();
  1142. let expect = Vec::from([8u8, 1, 0, 0, 0, 0, 0, 0, 0]);
  1143. assert_eq!(packed, expect);
  1144. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1145. assert_eq!(unpacked, check);
  1146. let check = TokenInstruction::CloseAccount;
  1147. let packed = check.pack().unwrap();
  1148. let expect = Vec::from([9u8]);
  1149. assert_eq!(packed, expect);
  1150. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1151. assert_eq!(unpacked, check);
  1152. let check = TokenInstruction::FreezeAccount;
  1153. let packed = check.pack().unwrap();
  1154. let expect = Vec::from([10u8]);
  1155. assert_eq!(packed, expect);
  1156. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1157. assert_eq!(unpacked, check);
  1158. let check = TokenInstruction::ThawAccount;
  1159. let packed = check.pack().unwrap();
  1160. let expect = Vec::from([11u8]);
  1161. assert_eq!(packed, expect);
  1162. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1163. assert_eq!(unpacked, check);
  1164. let check = TokenInstruction::Transfer2 {
  1165. amount: 1,
  1166. decimals: 2,
  1167. };
  1168. let packed = check.pack().unwrap();
  1169. let expect = Vec::from([12u8, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
  1170. assert_eq!(packed, expect);
  1171. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1172. assert_eq!(unpacked, check);
  1173. let check = TokenInstruction::Approve2 {
  1174. amount: 1,
  1175. decimals: 2,
  1176. };
  1177. let packed = check.pack().unwrap();
  1178. let expect = Vec::from([13u8, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
  1179. assert_eq!(packed, expect);
  1180. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1181. assert_eq!(unpacked, check);
  1182. let check = TokenInstruction::MintTo2 {
  1183. amount: 1,
  1184. decimals: 2,
  1185. };
  1186. let packed = check.pack().unwrap();
  1187. let expect = Vec::from([14u8, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
  1188. assert_eq!(packed, expect);
  1189. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1190. assert_eq!(unpacked, check);
  1191. let check = TokenInstruction::Burn2 {
  1192. amount: 1,
  1193. decimals: 2,
  1194. };
  1195. let packed = check.pack().unwrap();
  1196. let expect = Vec::from([15u8, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
  1197. assert_eq!(packed, expect);
  1198. let unpacked = TokenInstruction::unpack(&expect).unwrap();
  1199. assert_eq!(unpacked, check);
  1200. }
  1201. }