Skip to content

Instantly share code, notes, and snippets.

@0xdeepmehta
Created October 3, 2023 17:38
Show Gist options
  • Save 0xdeepmehta/2a9b587d076b188dda17adc1fc0a4af5 to your computer and use it in GitHub Desktop.
Save 0xdeepmehta/2a9b587d076b188dda17adc1fc0a4af5 to your computer and use it in GitHub Desktop.
wrap and unwrap sol snippet
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
use spl_token::instruction::close_account;
pub fn make_wrap_sol_ixs(config: &Config) -> Result<()> {
println!("sol transfer :: {}", config.payer.pubkey());
// ata get -> ata create_idempotent -> trasnfer -> sync
let deposit_ata = anchor_spl::associated_token::get_associated_token_address(
&config.payer.pubkey(),
&pubkey!("So11111111111111111111111111111111111111112"),
);
println!("deposit_ata :: {}", deposit_ata.to_string());
println!("spl_token::ID :: {}", spl_token::ID);
let create_ata_ix = create_associated_token_account_idempotent(
&config.payer.pubkey(),
&config.payer.pubkey(),
&pubkey!("So11111111111111111111111111111111111111112"),
&spl_token::ID
);
let transfer_ix = transfer(&config.payer.pubkey(), &deposit_ata, LAMPORTS_PER_SOL/9);
let sync_tx = spl_token::instruction::sync_native(&spl_token::ID, &deposit_ata)?;
let close_ix = close_account(&spl_token::ID, &deposit_ata, &config.payer.pubkey(), &config.payer.pubkey(), &[&config.payer.pubkey()])?;
let tx = Transaction::new_signed_with_payer(
&[create_ata_ix, transfer_ix, sync_tx],
// &[close_ix],
Some(&config.payer.pubkey()),
&[&config.payer],
config.mfi_program.rpc().get_latest_blockhash()?,
);
match process_transaction(&tx, &config.mfi_program.rpc(), false) {
Ok(sig) => println!("Deposit successful: {sig}"),
Err(err) => println!("Error during deposit:\n{err:#?}"),
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment