solana_exec/utilities/
wrap_sol.rs

1use solana_central::CentralContext;
2use solana_central::constants::{SOLANA_PROGRAMS, TOKENS};
3use solana_compute_budget_interface::ComputeBudgetInstruction;
4use solana_sdk::instruction::Instruction;
5use solana_sdk::signature::{Keypair, Signer};
6use solana_system_interface::instruction::transfer;
7use spl_associated_token_account::get_associated_token_address;
8use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
9use spl_token::instruction::sync_native;
10
11/// Wrap SOL into wrapped SOL (WSOL) token account. Creates a wrapped SOL token account and transfers the specified amount of SOL into it. Sends and confirms the transaction via JSON RPC.
12pub fn wrap_sol(keypair: &Keypair, lamports: u64, context: &CentralContext) {
13  let ata = get_associated_token_address(&keypair.pubkey(), &TOKENS.wsol);
14
15  let ixs: Vec<Instruction> = vec![
16    ComputeBudgetInstruction::set_compute_unit_limit(200000),
17    ComputeBudgetInstruction::set_compute_unit_price(400000),
18    create_associated_token_account_idempotent(
19      &keypair.pubkey(),
20      &keypair.pubkey(),
21      &TOKENS.wsol,
22      &SOLANA_PROGRAMS.token_program,
23    ),
24    transfer(&keypair.pubkey(), &ata, lamports),
25    sync_native(&SOLANA_PROGRAMS.token_program, &ata).unwrap(),
26  ];
27
28  let blockhash = context.json_rpc_client.get_latest_blockhash().unwrap();
29  let message = solana_sdk::message::Message::new(&ixs, Some(&keypair.pubkey()));
30  let transaction = solana_sdk::transaction::Transaction::new(&[&keypair], message, blockhash);
31
32  let signature = context
33    .json_rpc_client
34    .send_and_confirm_transaction(&transaction)
35    .unwrap();
36  println!("wrap_sol: transaction sent with signature: {}", signature);
37}