Solana: How can I retrieve Raydium Swap instruction account keys using a mint address in Rust, without making an API call?
I can help you create an application that uses the solana blockchain’s raydium swap feature without first understanding how your own smart contracts are structured.
Here is a simple example of creating a mint in solana using rust:
Mintexample.rs
`rust
use std :: collections :: {hashmap, vecdeque};
Const Mint_name: & str = “Raydiumswap”;
Const mint_address: & str = “1 …”; // replace with your desired mint address
#[Derive (debug)]
Struct Mintinfo {
Pub Name: String,
Pub symbol: String,
}
fn create_mint () -> (Mintinfo, vec
let mut info = mintinfo {
Name: mint_name.to_string (),
symbol: mint_address.to_string (),
};
let mint_info: vec
infest
}
`
Mainexample.rs
`rust
use std :: collections :: {hashmap, vecdeque};
Const main 链 _mint_name: & str = “raydiumswap”;
Const main_chain_mint_address: & str = “1 …”; // replace with your desired mint address
Fn Main () {
let (mint_info, mint_data) = create_mint ();
Println! (“Mint info:”);
Println! (“{:?}”, mint_info);
Println! (“Data Mint:”);
for i in 0..10 {
Print! (“{}: {}”, mint_info.name, mint_info.symbol);
}
}
`
To retrieve
Get_instruction_account_keys.rs
`rust
use solana_program :: {
ACCOUNT_INFO :: ACCOUNTINFO,
EntryPoint :: Programresult,
Program_error :: Printprogramerror,
PUBKEY :: Pubkey,
};
Use std :: collections :: {hashmap, vecdeque};
Const main_chain_mint_address: & pubkey = pubkey :: new (“1 …”); // replace with your desired mint address
fn get_instruction_account_keys (
Account_info: Accountinfo,
key_info_map: & mut hashmap
) -> Programresult {
let mut instruction_keys = vecdeque :: new ();
while let some (info) = account_info.next () {
If info.key == Main_chain_mint_address {
Break;
}
Match key_info_map.get_mut (& info.key) {
Some (key_info) => {
if!
Return Err (
Printprogramerror :: New (“Multiple Instructions with the Same Key Found” .to_string ()),
);
}
instruction_keys.push_back (info.key);
}
None => {}
}
}
Match Key_info_map.Insert (Pubkey :: New (Main_chain_mint_address), Keyinfo {
pub_key: info.key.to_bytes (),
..Default :: default ()
}) {
OK (_) => Println! (“Keys Found”),
ERR (ERR) => Eprintln! (“Failed to Get Instruction Keys: {}”, Err),
}
Instruction_keys
}
Fn Main () -> Programresult {
let mut key_info_map = hashmap :: new ();
for account in solana_program :: test_accounts () {
let info = solana_program :: account_info :: get_account_info (& account.key)?;
if let some (key_info) = key_info_map.get_mut (& info.key) {
Get_instruction_account_keys (info, & mut key_info_map)?;
}
}
Ok (())
}
`
Note that this example assumes you are using Solana’s Rust-SDK version 1.6.0 or later and the Solana-Program
Cate.
Please consult [the official documentation] ( for more information about working with Rust and Solana.