-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.rs
43 lines (35 loc) · 1.25 KB
/
swap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
system_instruction,
};
use solana_program::program::{invoke};
entrypoint!(process_instruction);
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
let accounts_iter = &mut accounts.iter();
let user = next_account_info(accounts_iter)?;
let sol_source = next_account_info(accounts_iter)?;
let usdc_destination = next_account_info(accounts_iter)?;
let system_program = next_account_info(accounts_iter)?;
if !user.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
let sol_amount = 10_000_000_000; // 10 SOL in lamports
msg!("Swapping {} SOL for USDC", sol_amount);
invoke(
&system_instruction::transfer(user.key, sol_source.key, sol_amount),
&[user.clone(), sol_source.clone(), system_program.clone()],
)?;
// Logic to swap SOL for USDC from a liquidity pool would go here.
// This would require integration with an AMM like Raydium.
msg!("Swap successful, USDC credited to destination");
Ok(())
}