please fix this script write full script use solana_client::rpc_client::RpcClient;
use solana_sdk::{
    commitment_config::CommitmentConfig,
    pubkey::Pubkey,
    signature::{Keypair, Signer},
    transaction::Transaction,
    instruction::{Instruction, AccountMeta},
    message::Message,
};
use spl_associated_token_account::get_associated_token_address;
use std::{fs, str::FromStr, time::Duration};
use serde::Deserialize;
use log::{info, error};

const RAYDIUM_SWAP_V2: &str = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";

#[derive(Deserialize)]
struct Config {
    rpc_url: String,
    keypair_64: String,
    amount_to_trade: f64,
    priority_fee: u64,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    env_logger::init();

    // Load configuration
    let config_content = fs::read_to_string("config.toml")
        .map_err(|e| format!("Failed to read config file: {}", e))?;
    let config: Config = toml::from_str(&config_content)
        .map_err(|e| format!("Failed to parse config: {}", e))?;
    
    // Setup RPC client
    let client = RpcClient::new_with_commitment(&config.rpc_url, CommitmentConfig::confirmed());

    // Load the keypair
    let keypair = load_keypair(&config.keypair_64)?;

    // SOL and USDC mint addresses
    let sol_mint = Pubkey::from_str("So11111111111111111111111111111111111111112")?;
    let usdc_mint = Pubkey::from_str("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")?;

    // Check SOL balance and perform buy
    execute_trade(
        TradeDirection::Buy,
        &client,
        &keypair,
        &sol_mint,
        &usdc_mint,
        config.amount_to_trade,
        config.priority_fee,
    )?;

    // Pause before the next trade
    std::thread::sleep(Duration::from_secs(5));

    // Check USDC balance and perform sell
    execute_trade(
        TradeDirection::Sell,
        &client,
        &keypair,
        &usdc_mint,
        &sol_mint,
        config.amount_to_trade,
        config.priority_fee,
    )?;

    Ok(())
}

fn load_keypair(encoded_keypair: &str) -> Result<Keypair, Box<dyn std::error::Error>> {
    let keypair_bytes: Vec<u8> = encoded_keypair
        .split(',')
        .map(|s| s.trim().parse::<u8>().map_err(|_| "Invalid keypair byte"))
        .collect::<Result<Vec<u8>, _>>()?;

    Keypair::from_bytes(&keypair_bytes).map_err(|_| "Failed to parse keypair".into())
}

enum TradeDirection {
    Buy,
    Sell,
}

fn execute_trade(
    direction: TradeDirection,
    client: &RpcClient,
    keypair: &Keypair,
    token_in_mint: &Pubkey,
    token_out_mint: &Pubkey,
    amount: f64,
    priority_fee: u64,
) -> Result<(), Box<dyn std::error::Error>> {
    let pool_address = Pubkey::from_str("58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2")?;
    let token_amount = (amount * 1e9) as u64; // Example conversion; adapt as needed

    match direction {
        TradeDirection::Buy => {
            info!("Performing buy (SOL to USDC)...");
        },
        TradeDirection::Sell => {
            info!("Performing sell (USDC to SOL)...");
        },
    }

    // Perform the swap operation
    match swap(client, keypair, &pool_address, token_in_mint, token_out_mint, token_amount, priority_fee) {
        Ok(signature) => info!("Transaction signature: {}", signature),
        Err(e) => {
            error!("Transaction failed: {}", e);
            return Err(e);
        }
    }

    Ok(())
}

fn swap(
    client: &RpcClient,
    keypair: &Keypair,
    pool_address: &Pubkey,
    token_in_mint: &Pubkey,
    token_out_mint: &Pubkey,
    amount_in: u64,
    priority_fee: u64,
) -> Result<String, Box<dyn std::error::Error>> {
    let raydium_program_id = Pubkey::from_str(RAYDIUM_SWAP_V2)?;

    // Fetch pool details and other necessary accounts
    let pool_info = client.get_account(pool_address)?;
    let pool_data: Vec<u8> = pool_info.data;

    // Sample assumption for extracting data; adjust according to real data layout
    let amm_authority = Pubkey::new(&pool_data[8..40]);
    let amm_open_orders = Pubkey::new(&pool_data[40..72]);
    let amm_target_orders = Pubkey::new(&pool_data[72..104]);
    let pool_coin_token_account = Pubkey::new(&pool_data[104..136]);
    let pool_pc_token_account = Pubkey::new(&pool_data[136..168]);
    let serum_program_id = Pubkey::new(&pool_data[168..200]);
    let serum_market = Pubkey::new(&pool_data[200..232]);
    let serum_bids = Pubkey::new(&pool_data[232..264]);
    let serum_asks = Pubkey::new(&pool_data[264..296]);
    let serum_event_queue = Pubkey::new(&pool_data[296..328]);
    let serum_coin_vault_account = Pubkey::new(&pool_data[328..360]);
    let serum_pc_vault_account = Pubkey::new(&pool_data[360..392]);
    let serum_vault_signer = Pubkey::new(&pool_data[392..424]);

    // Get associated token accounts
    let token_in_account = get_associated_token_address(&keypair.pubkey(), token_in_mint);
    let token_out_account = get_associated_token_address(&keypair.pubkey(), token_out_mint);

    let instruction_data = vec![0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // Modify based on actual needs

    // Create the Raydium swap instruction
    let swap_instruction = Instruction {
        program_id: raydium_program_id,
        accounts: vec![
            AccountMeta::new(*pool_address, false),
            AccountMeta::new_readonly(amm_authority, false),
            AccountMeta::new_readonly(amm_open_orders, false),
            AccountMeta::new_readonly(amm_target_orders, false),
            AccountMeta::new(pool_coin_token_account, false),
            AccountMeta::new(pool_pc_token_account, false),
            AccountMeta::new_readonly(serum_program_id, false),
            AccountMeta::new(serum_market, false),
            AccountMeta::new(serum_bids, false),
            AccountMeta::new(serum_asks, false),
            AccountMeta::new(serum_event_queue, false),
            AccountMeta::new(serum_coin_vault_account, false),
            AccountMeta::new(serum_pc_vault_account, false),
            AccountMeta::new_readonly(serum_vault_signer, false),
            AccountMeta::new(token_in_account, false),
            AccountMeta::new(token_out_account, false),
            AccountMeta::new(keypair.pubkey(), true),
            AccountMeta::new_readonly(spl_token::id(), false),
        ],
        data: instruction_data,
    };

    let compute_budget_instruction = solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_price(priority_fee);

    let instructions = vec![compute_budget_instruction, swap_instruction];

    for attempt in 1..=3 {
        info!("Attempt {} of 3", attempt);
        
        let recent_blockhash = client.get_latest_blockhash()?;
        
        let message = Message::new(&instructions, Some(&keypair.pubkey()));
        let mut transaction = Transaction::new_unsigned(message);

        transaction.sign(&[keypair], recent_blockhash);

        match client.send_and_confirm_transaction(&transaction) {
            Ok(signature) => return Ok(signature.to_string()),
            Err(e) => {
                if e.to_string().contains("BlockhashNotFound") {
                    error!("Blockhash not found, retrying...");
                    std::thread::sleep(Duration::from_secs(1));
                } else {
                    error!("Transaction error: {}", e);
                    return Err(e.into());
                }
            }
        }
    }

    Err("Failed to send transaction after multiple attempts".into())
}