Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions program-tests/compressed-token-test/tests/transfer2/spl_ctoken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ async fn test_spl_to_ctoken_transfer() {
#[tokio::test]
async fn test_ctoken_to_spl_with_compress_and_close() {
use light_compressed_token_sdk::{
instructions::create_ctoken_to_spl_transfer_and_close_instruction,
token_pool::find_token_pool_pda_with_index,
instructions::CtokenToSplTransferAndClose, token_pool::find_token_pool_pda_with_index,
};

let mut rpc = LightProgramTest::new(ProgramTestConfig::new(true, None))
Expand Down Expand Up @@ -277,22 +276,20 @@ async fn test_ctoken_to_spl_with_compress_and_close() {
// Now transfer back using CompressAndClose instead of regular transfer
println!("Testing reverse transfer with CompressAndClose: ctoken to SPL");

// Get token pool PDA
let (token_pool_pda, token_pool_pda_bump) = find_token_pool_pda_with_index(&mint, 0);

// Create instruction using compress_and_close variant
// Note: Using spl_token::ID because create_mint_helper creates Token (not Token-2022) mints
let transfer_ix = create_ctoken_to_spl_transfer_and_close_instruction(
associated_token_account,
spl_token_account_keypair.pubkey(),
transfer_amount,
recipient.pubkey(),
let transfer_ix = CtokenToSplTransferAndClose {
source_ctoken_account: associated_token_account,
destination_spl_token_account: spl_token_account_keypair.pubkey(),
amount: transfer_amount,
authority: recipient.pubkey(),
mint,
payer.pubkey(),
payer: payer.pubkey(),
token_pool_pda,
token_pool_pda_bump,
anchor_spl::token::ID,
)
spl_token_program: anchor_spl::token::ID,
}
.instruction()
.unwrap();

// Execute transaction
Expand Down
7 changes: 4 additions & 3 deletions sdk-libs/compressed-token-sdk/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ pub use mint_to_compressed::{
create_mint_to_compressed_instruction, get_mint_to_compressed_instruction_account_metas,
DecompressedMintConfig, MintToCompressedInputs, MintToCompressedMetaConfig,
};
pub use transfer_ctoken::{transfer_ctoken, transfer_ctoken_signed};
pub use transfer_ctoken::{TransferCtoken, TransferCtokenAccountInfos};
pub use transfer_interface::{
create_ctoken_to_spl_transfer_and_close_instruction, create_transfer_ctoken_to_spl_instruction,
create_transfer_spl_to_ctoken_instruction, transfer_interface, transfer_interface_signed,
CtokenToSplTransferAndClose, CtokenToSplTransferAndCloseAccountInfos, SplBridgeConfig,
TransferCtokenToSpl, TransferCtokenToSplAccountInfos, TransferInterface, TransferSplToCtoken,
TransferSplToCtokenAccountInfos,
};
pub use update_compressed_mint::{
update_compressed_mint, update_compressed_mint_cpi, UpdateCompressedMintInputs,
Expand Down
108 changes: 54 additions & 54 deletions sdk-libs/compressed-token-sdk/src/instructions/transfer_ctoken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,64 @@ use solana_instruction::{AccountMeta, Instruction};
use solana_program_error::ProgramError;
use solana_pubkey::Pubkey;

/// Create a c-token transfer instruction.
///
/// # Arguments
/// * `source` - Source token account
/// * `destination` - Destination token account
/// * `amount` - Amount to transfer
/// * `authority` - Authority pubkey
///
/// # Returns
/// `Instruction`
fn create_transfer_ctoken_instruction(
source: Pubkey,
destination: Pubkey,
amount: u64,
authority: Pubkey,
) -> Instruction {
Instruction {
program_id: Pubkey::from(C_TOKEN_PROGRAM_ID),
accounts: vec![
AccountMeta::new(source, false),
AccountMeta::new(destination, false),
AccountMeta::new_readonly(authority, true),
],
data: {
// TODO: check why we have 2 discriminators
let mut data = vec![3u8];
data.push(3u8);
data.extend_from_slice(&amount.to_le_bytes());
data
},
}
pub struct TransferCtoken {
pub source: Pubkey,
pub destination: Pubkey,
pub amount: u64,
pub authority: Pubkey,
}

pub struct TransferCtokenAccountInfos<'info> {
pub source: AccountInfo<'info>,
pub destination: AccountInfo<'info>,
pub amount: u64,
pub authority: AccountInfo<'info>,
}

/// Transfer c-tokens
pub fn transfer_ctoken<'info>(
from: &AccountInfo<'info>,
to: &AccountInfo<'info>,
authority: &AccountInfo<'info>,
amount: u64,
) -> Result<(), ProgramError> {
let ix = create_transfer_ctoken_instruction(*from.key, *to.key, amount, *authority.key);
impl<'info> TransferCtokenAccountInfos<'info> {
pub fn instruction(&self) -> Result<Instruction, ProgramError> {
TransferCtoken::from(self).instruction()
}

pub fn invoke(self) -> Result<(), ProgramError> {
let instruction = TransferCtoken::from(&self).instruction()?;
let account_infos = [self.source, self.destination, self.authority];
invoke(&instruction, &account_infos)
}

invoke(&ix, &[from.clone(), to.clone(), authority.clone()])
pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
let instruction = TransferCtoken::from(&self).instruction()?;
let account_infos = [self.source, self.destination, self.authority];
invoke_signed(&instruction, &account_infos, signer_seeds)
}
}

/// Transfer c-tokens CPI
pub fn transfer_ctoken_signed<'info>(
from: &AccountInfo<'info>,
to: &AccountInfo<'info>,
authority: &AccountInfo<'info>,
amount: u64,
signer_seeds: &[&[&[u8]]],
) -> Result<(), ProgramError> {
let ix = create_transfer_ctoken_instruction(*from.key, *to.key, amount, *authority.key);
impl<'info> From<&TransferCtokenAccountInfos<'info>> for TransferCtoken {
fn from(account_infos: &TransferCtokenAccountInfos<'info>) -> Self {
Self {
source: *account_infos.source.key,
destination: *account_infos.destination.key,
amount: account_infos.amount,
authority: *account_infos.authority.key,
}
}
}

invoke_signed(
&ix,
&[from.clone(), to.clone(), authority.clone()],
signer_seeds,
)
impl TransferCtoken {
pub fn instruction(self) -> Result<Instruction, ProgramError> {
Ok(Instruction {
program_id: Pubkey::from(C_TOKEN_PROGRAM_ID),
accounts: vec![
AccountMeta::new(self.source, false),
AccountMeta::new(self.destination, false),
AccountMeta::new_readonly(self.authority, true),
],
data: {
let mut data = vec![3u8];
data.push(3u8);
data.extend_from_slice(&self.amount.to_le_bytes());
data
},
})
}
}
Loading