|
| 1 | +#![cfg_attr(not(test), no_std)] |
| 2 | + |
| 3 | +use quasar_lang::{ |
| 4 | + cpi::{CpiCall, InstructionAccount}, |
| 5 | + prelude::*, |
| 6 | +}; |
| 7 | + |
| 8 | +#[cfg(test)] |
| 9 | +mod tests; |
| 10 | + |
| 11 | +declare_id!("22222222222222222222222222222222222222222222"); |
| 12 | + |
| 13 | +/// Correct Token-2022 program ID. |
| 14 | +/// |
| 15 | +/// quasar-spl 0.0.0 ships incorrect bytes for the Token-2022 address |
| 16 | +/// (`TokenzSRvw8aVrEuYKv3gLJaYV39h1EWGpCCGYBJPZQ` instead of the real |
| 17 | +/// `TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`). We define a local |
| 18 | +/// marker with the correct mainnet address until that's fixed upstream. |
| 19 | +pub struct Token2022Program; |
| 20 | + |
| 21 | +impl Id for Token2022Program { |
| 22 | + const ID: Address = Address::new_from_array([ |
| 23 | + 6, 221, 246, 225, 238, 117, 143, 222, 24, 66, 93, 188, 228, 108, 205, 218, |
| 24 | + 182, 26, 252, 77, 131, 185, 13, 39, 254, 189, 249, 40, 216, 161, 139, 252, |
| 25 | + ]); |
| 26 | +} |
| 27 | + |
| 28 | +/// Demonstrates Token-2022 basics: minting tokens and transferring (checked) |
| 29 | +/// via raw CPI to the Token-2022 program. |
| 30 | +#[program] |
| 31 | +mod quasar_token_2022_basics { |
| 32 | + use super::*; |
| 33 | + |
| 34 | + /// Mint tokens to a recipient's token account. |
| 35 | + #[instruction(discriminator = 0)] |
| 36 | + pub fn mint_token(ctx: Ctx<MintToken>, amount: u64) -> Result<(), ProgramError> { |
| 37 | + ctx.accounts.mint_token(amount) |
| 38 | + } |
| 39 | + |
| 40 | + /// Transfer tokens using transfer_checked (required for Token-2022). |
| 41 | + #[instruction(discriminator = 1)] |
| 42 | + pub fn transfer_token(ctx: Ctx<TransferToken>, amount: u64) -> Result<(), ProgramError> { |
| 43 | + ctx.accounts.transfer_token(amount) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Accounts for minting tokens via Token-2022. |
| 48 | +#[derive(Accounts)] |
| 49 | +pub struct MintToken<'info> { |
| 50 | + #[account(mut)] |
| 51 | + pub authority: &'info Signer, |
| 52 | + #[account(mut)] |
| 53 | + pub mint: &'info mut UncheckedAccount, |
| 54 | + #[account(mut)] |
| 55 | + pub receiver: &'info mut UncheckedAccount, |
| 56 | + pub token_program: &'info Program<Token2022Program>, |
| 57 | +} |
| 58 | + |
| 59 | +impl MintToken<'_> { |
| 60 | + #[inline(always)] |
| 61 | + pub fn mint_token(&mut self, amount: u64) -> Result<(), ProgramError> { |
| 62 | + // SPL Token MintTo instruction: opcode 7, amount as u64 LE. |
| 63 | + let data = build_u64_data(7, amount); |
| 64 | + CpiCall::new( |
| 65 | + self.token_program.to_account_view().address(), |
| 66 | + [ |
| 67 | + InstructionAccount::writable(self.mint.to_account_view().address()), |
| 68 | + InstructionAccount::writable(self.receiver.to_account_view().address()), |
| 69 | + InstructionAccount::readonly_signer(self.authority.to_account_view().address()), |
| 70 | + ], |
| 71 | + [ |
| 72 | + self.mint.to_account_view(), |
| 73 | + self.receiver.to_account_view(), |
| 74 | + self.authority.to_account_view(), |
| 75 | + ], |
| 76 | + data, |
| 77 | + ) |
| 78 | + .invoke() |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Accounts for transferring tokens via Token-2022 transfer_checked. |
| 83 | +#[derive(Accounts)] |
| 84 | +pub struct TransferToken<'info> { |
| 85 | + #[account(mut)] |
| 86 | + pub sender: &'info Signer, |
| 87 | + #[account(mut)] |
| 88 | + pub from: &'info mut UncheckedAccount, |
| 89 | + pub mint: &'info UncheckedAccount, |
| 90 | + #[account(mut)] |
| 91 | + pub to: &'info mut UncheckedAccount, |
| 92 | + pub token_program: &'info Program<Token2022Program>, |
| 93 | +} |
| 94 | + |
| 95 | +impl TransferToken<'_> { |
| 96 | + #[inline(always)] |
| 97 | + pub fn transfer_token(&mut self, amount: u64) -> Result<(), ProgramError> { |
| 98 | + // SPL Token TransferChecked instruction: opcode 12, amount as u64 LE, decimals as u8. |
| 99 | + let data = build_transfer_checked_data(amount, 6); |
| 100 | + CpiCall::new( |
| 101 | + self.token_program.to_account_view().address(), |
| 102 | + [ |
| 103 | + InstructionAccount::writable(self.from.to_account_view().address()), |
| 104 | + InstructionAccount::readonly(self.mint.to_account_view().address()), |
| 105 | + InstructionAccount::writable(self.to.to_account_view().address()), |
| 106 | + InstructionAccount::readonly_signer(self.sender.to_account_view().address()), |
| 107 | + ], |
| 108 | + [ |
| 109 | + self.from.to_account_view(), |
| 110 | + self.mint.to_account_view(), |
| 111 | + self.to.to_account_view(), |
| 112 | + self.sender.to_account_view(), |
| 113 | + ], |
| 114 | + data, |
| 115 | + ) |
| 116 | + .invoke() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/// Build a 9-byte instruction data: [opcode, u64 LE amount]. |
| 121 | +#[inline(always)] |
| 122 | +fn build_u64_data(opcode: u8, amount: u64) -> [u8; 9] { |
| 123 | + let mut data = [0u8; 9]; |
| 124 | + data[0] = opcode; |
| 125 | + data[1..9].copy_from_slice(&amount.to_le_bytes()); |
| 126 | + data |
| 127 | +} |
| 128 | + |
| 129 | +/// Build TransferChecked data: [12, u64 LE amount, u8 decimals]. |
| 130 | +#[inline(always)] |
| 131 | +fn build_transfer_checked_data(amount: u64, decimals: u8) -> [u8; 10] { |
| 132 | + let mut data = [0u8; 10]; |
| 133 | + data[0] = 12; |
| 134 | + data[1..9].copy_from_slice(&amount.to_le_bytes()); |
| 135 | + data[9] = decimals; |
| 136 | + data |
| 137 | +} |
0 commit comments