I'm working on creating a Raydium swap instruction for the Solana blockchain using Rust and need to construct it solely with Pubkeys, without making any external API calls. Specifically, I'm looking to build an instruction similar to the "Raydium Liquidity Pool V4: raydium" which can be seen in this transaction example on Solscan.
Below is the code I’ve started with:
async fn make_raydium_swap_instruction( from_mint: Pubkey, to_mint: Pubkey, amount_in: u64, min_amount_out: u64) -> Result<Instruction, Box<dyn std::error::Error>> { let instruction_data_struct = RaydiumSwapInstructionData { opcode: 9, amount_in, min_amount_out, }; let instruction_data = bincode::serialize(&instruction_data_struct)?; // Token program is hardcoded as an example, need similar logic for other keys let token_program_pubkey = pubkey_from_base58(TOKEN_PROGRAM)?; let amm_pubkey = // Logic needed to derive this account let amm_authority_pubkey = pubkey_from_base58(RAYDIUM_AUTHORITY_V4)?; // Additional required accounts also need to be derived}My Goals:
Populate account Pubkeys like amm_pubkey, amm_authority_pubkey, and other required accounts for Raydium swap.Construct the instruction without relying on API calls—ideally, using deterministic Pubkey derivation or similar methods if applicable.
Challenges: I'm unsure how to programmatically determine account Pubkeys like amm_pubkey and others involved in the swap. Are there Rust utilities or specific derivation patterns in Solana that can help fetch these account addresses based on token mint addresses alone? Or are there any recommended approaches to handle this scenario efficiently?
Any guidance on constructing this swap instruction fully in Rust would be greatly appreciated. Thanks in advance!