pub fn new_instruction_swap(
        &self,
        wallet_address: &Pubkey,
        pool_code: &str,
        from_token: &str,
        to_token: &str,
        ui_amount_in: f64,
        min_ui_amount_out: f64,
    ) -> Result<Instruction, FarmClientError> {
        // get pool to swap in
        let pool = self.find_pools(pool_code, from_token, to_token)?[0];
        let reverse = FarmClient::pool_has_reverse_tokens(&pool.name, from_token)?;

        // get tokens info
        let token_a = self.get_token_by_ref_from_cache(&pool.token_a_ref)?;
        let token_b = self.get_token_by_ref_from_cache(&pool.token_b_ref)?;

        // convert amounts if wrapped tokens are used
        let mut max_amount_in = if reverse {
            self.to_token_amount_option(ui_amount_in, &token_b)?
        } else {
            self.to_token_amount_option(ui_amount_in, &token_a)?
        };
        let mut min_amount_out = if reverse {
            self.to_token_amount_option(min_ui_amount_out, &token_a)?
        } else {
            self.to_token_amount_option(min_ui_amount_out, &token_b)?
        };
        if let PoolRoute::Saber {
            wrapped_token_a_ref,
            wrapped_token_b_ref,
            ..
        } = pool.route
        {
            if let Some(token_ref) = wrapped_token_a_ref {
                let underlying_decimals =
                    token_a.ok_or(ProgramError::UninitializedAccount)?.decimals;
                let wrapped_decimals = self.get_token_by_ref(&token_ref)?.decimals;
                if reverse {
                    min_amount_out = account::to_amount_with_new_decimals(
                        min_amount_out,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                } else {
                    max_amount_in = account::to_amount_with_new_decimals(
                        max_amount_in,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                }
            }
            if let Some(token_ref) = wrapped_token_b_ref {
                let underlying_decimals =
                    token_b.ok_or(ProgramError::UninitializedAccount)?.decimals;
                let wrapped_decimals = self.get_token_by_ref(&token_ref)?.decimals;
                if reverse {
                    max_amount_in = account::to_amount_with_new_decimals(
                        max_amount_in,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                } else {
                    min_amount_out = account::to_amount_with_new_decimals(
                        min_amount_out,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                }
            }
        }

        // fill in accounts and instruction data
        let data = if reverse {
            AmmInstruction::Swap {
                token_a_amount_in: 0,
                token_b_amount_in: max_amount_in,
                min_token_amount_out: min_amount_out,
            }
        } else {
            AmmInstruction::Swap {
                token_a_amount_in: max_amount_in,
                token_b_amount_in: 0,
                min_token_amount_out: min_amount_out,
            }
        }
        .to_vec()?;

        let accounts = match pool.route {
            PoolRoute::Raydium { .. } => {
                self.get_swap_accounts_raydium(wallet_address, &pool.name)?
            }
            PoolRoute::Saber { .. } => self.get_swap_accounts_saber(wallet_address, &pool.name)?,
            PoolRoute::Orca { .. } => self.get_swap_accounts_orca(wallet_address, &pool.name)?,
        };

        Ok(Instruction {
            program_id: pool.router_program_id,
            data,
            accounts,
        })
    }
/// Creates a new Instruction for tokens swap
Creates a new Instruction for tokens swap
Creates a new Instruction for tokens swap
pub fn new_instruction_swap(
        &self,
        wallet_address: &Pubkey,
        pool_code: &str,
        from_token: &str,
        to_token: &str,
        ui_amount_in: f64,
        min_ui_amount_out: f64,
    ) -> Result<Instruction, FarmClientError> {
        // get pool to swap in
        let pool = self.find_pools(pool_code, from_token, to_token)?[0];
        let reverse = FarmClient::pool_has_reverse_tokens(&pool.name, from_token)?;
        // get tokens info
        let token_a = self.get_token_by_ref_from_cache(&pool.token_a_ref)?;
        let token_b = self.get_token_by_ref_from_cache(&pool.token_b_ref)?;
        // convert amounts if wrapped tokens are used
        let mut max_amount_in = if reverse {
            self.to_token_amount_option(ui_amount_in, &token_b)?
        } else {
            self.to_token_amount_option(ui_amount_in, &token_a)?
        };
        let mut min_amount_out = if reverse {
            self.to_token_amount_option(min_ui_amount_out, &token_a)?
        } else {
            self.to_token_amount_option(min_ui_amount_out, &token_b)?
        };
        if let PoolRoute::Saber {
            wrapped_token_a_ref,
            wrapped_token_b_ref,
            ..
        } = pool.route
        {
            if let Some(token_ref) = wrapped_token_a_ref {
                let underlying_decimals =
                    token_a.ok_or(ProgramError::UninitializedAccount)?.decimals;
                let wrapped_decimals = self.get_token_by_ref(&token_ref)?.decimals;
                if reverse {
                    min_amount_out = account::to_amount_with_new_decimals(
                        min_amount_out,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                } else {
                    max_amount_in = account::to_amount_with_new_decimals(
                        max_amount_in,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                }
            }
            if let Some(token_ref) = wrapped_token_b_ref {
                let underlying_decimals =
                    token_b.ok_or(ProgramError::UninitializedAccount)?.decimals;
                let wrapped_decimals = self.get_token_by_ref(&token_ref)?.decimals;
                if reverse {
                    max_amount_in = account::to_amount_with_new_decimals(
                        max_amount_in,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                } else {
                    min_amount_out = account::to_amount_with_new_decimals(
                        min_amount_out,
                        underlying_decimals,
                        wrapped_decimals,
                    )?;
                }
            }
        }
        // fill in accounts and instruction data
        let data = if reverse {
            AmmInstruction::Swap {
                token_a_amount_in: 0,
                token_b_amount_in: max_amount_in,
                min_token_amount_out: min_amount_out,
            }
        } else {
            AmmInstruction::Swap {
                token_a_amount_in: max_amount_in,
                token_b_amount_in: 0,
                min_token_amount_out: min_amount_out,
            }
        }
        .to_vec()?;
        let accounts = match pool.route {
            PoolRoute::Raydium { .. } => {
                self.get_swap_accounts_raydium(wallet_address, &pool.name)?
            }
            PoolRoute::Saber { .. } => self.get_swap_accounts_saber(wallet_address, &pool.name)?,
            PoolRoute::Orca { .. } => self.get_swap_accounts_orca(wallet_address, &pool.name)?,
        };
        Ok(Instruction {
            program_id: pool.router_program_id,
            data,
            accounts,
        })
    }
pub fn new_instruction_swap ( & self , wallet_address : & Pubkey , pool_code : & str , from_token : & str , to_token : & str , ui_amount_in : f64 , min_ui_amount_out : f64 , ) -> Result < Instruction , FarmClientError > { // get pool to swap in let pool = self . find_pools ( pool_code , from_token , to_token ) ? [ 0 ] ; let reverse = FarmClient :: pool_has_reverse_tokens ( & pool . name , from_token ) ? ; // get tokens info let token_a = self . get_token_by_ref_from_cache ( & pool . token_a_ref ) ? ; let token_b = self . get_token_by_ref_from_cache ( & pool . token_b_ref ) ? ; // convert amounts if wrapped tokens are used let mut max_amount_in = if reverse { self . to_token_amount_option ( ui_amount_in , & token_b ) ? } else { self . to_token_amount_option ( ui_amount_in , & token_a ) ? } ; let mut min_amount_out = if reverse { self . to_token_amount_option ( min_ui_amount_out , & token_a ) ? } else { self . to_token_amount_option ( min_ui_amount_out , & token_b ) ? } ; if let PoolRoute :: Saber { wrapped_token_a_ref , wrapped_token_b_ref , .. } = pool . route { if let Some ( token_ref ) = wrapped_token_a_ref { let underlying_decimals = token_a . ok_or ( ProgramError :: UninitializedAccount ) ? . decimals ; let wrapped_decimals = self . get_token_by_ref ( & token_ref ) ? . decimals ; if reverse { min_amount_out = account :: to_amount_with_new_decimals ( min_amount_out , underlying_decimals , wrapped_decimals , ) ? ; } else { max_amount_in = account :: to_amount_with_new_decimals ( max_amount_in , underlying_decimals , wrapped_decimals , ) ? ; } } if let Some ( token_ref ) = wrapped_token_b_ref { let underlying_decimals = token_b . ok_or ( ProgramError :: UninitializedAccount ) ? . decimals ; let wrapped_decimals = self . get_token_by_ref ( & token_ref ) ? . decimals ; if reverse { max_amount_in = account :: to_amount_with_new_decimals ( max_amount_in , underlying_decimals , wrapped_decimals , ) ? ; } else { min_amount_out = account :: to_amount_with_new_decimals ( min_amount_out , underlying_decimals , wrapped_decimals , ) ? ; } } } // fill in accounts and instruction data let data = if reverse { AmmInstruction :: Swap { token_a_amount_in : 0 , token_b_amount_in : max_amount_in , min_token_amount_out : min_amount_out , } } else { AmmInstruction :: Swap { token_a_amount_in : max_amount_in , token_b_amount_in : 0 , min_token_amount_out : min_amount_out , } } . to_vec ( ) ? ; let accounts = match pool . route { PoolRoute :: Raydium { .. } => { self . get_swap_accounts_raydium ( wallet_address , & pool . name ) ? } PoolRoute :: Saber { .. } => self . get_swap_accounts_saber ( wallet_address , & pool . name ) ? , PoolRoute :: Orca { .. } => self . get_swap_accounts_orca ( wallet_address , & pool . name ) ? , } ; Ok ( Instruction { program_id : pool . router_program_id , data , accounts , } ) }
Creates a new Instruction for tokens swap
Creates a new Instruction for tokens swap
&self
wallet_address
&Pubkey
pool_code
&str
from_token
&str
to_token
&str
ui_amount_in
f64
min_ui_amount_out
f64
&self
wallet_address
&Pubkey
pool_code
&str
from_token
&str
to_token
&str
ui_amount_in
f64
min_ui_amount_out
f64