use solana_program::pubkey::Pubkey;
use std::{error::Error, sync::Mutex};
pub const DEFAULT_FUNDER: Pubkey = Pubkey::new_from_array([0; 32]);
pub static FUNDER: Mutex<Pubkey> = Mutex::new(DEFAULT_FUNDER);
pub fn set_funder(funder: Pubkey) -> Result<(), Box<dyn Error>> {
*FUNDER.try_lock()? = funder;
Ok(())
}
pub const DEFAULT_SLIPPAGE_TOLERANCE_BPS: u16 = 100;
pub static SLIPPAGE_TOLERANCE_BPS: Mutex<u16> = Mutex::new(DEFAULT_SLIPPAGE_TOLERANCE_BPS);
pub fn set_slippage_tolerance_bps(tolerance: u16) -> Result<(), Box<dyn Error>> {
*SLIPPAGE_TOLERANCE_BPS.try_lock()? = tolerance;
Ok(())
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NativeMintWrappingStrategy {
Keypair,
Seed,
Ata,
None,
}
pub const DEFAULT_NATIVE_MINT_WRAPPING_STRATEGY: NativeMintWrappingStrategy = NativeMintWrappingStrategy::Keypair;
pub static NATIVE_MINT_WRAPPING_STRATEGY: Mutex<NativeMintWrappingStrategy> = Mutex::new(DEFAULT_NATIVE_MINT_WRAPPING_STRATEGY);
pub fn set_native_mint_wrapping_strategy(strategy: NativeMintWrappingStrategy) -> Result<(), Box<dyn Error>> {
*NATIVE_MINT_WRAPPING_STRATEGY.try_lock()? = strategy;
Ok(())
}
pub fn reset_configuration() -> Result<(), Box<dyn Error>> {
*FUNDER.try_lock()? = DEFAULT_FUNDER;
*NATIVE_MINT_WRAPPING_STRATEGY.try_lock()? = DEFAULT_NATIVE_MINT_WRAPPING_STRATEGY;
*SLIPPAGE_TOLERANCE_BPS.try_lock()? = DEFAULT_SLIPPAGE_TOLERANCE_BPS;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::str::FromStr;
#[test]
#[serial]
fn test_set_funder() {
let new_funder = Pubkey::from_str("GdDMspJi2oQaKDtABKE24wAQgXhGBoxq8sC21st7GJ3E").unwrap();
set_funder(new_funder).unwrap();
assert_eq!(*FUNDER.lock().unwrap(), new_funder);
reset_configuration().unwrap();
}
#[test]
#[serial]
fn test_set_sol_wrapping_strategy() {
let new_strategy = NativeMintWrappingStrategy::Ata;
set_native_mint_wrapping_strategy(new_strategy).unwrap();
assert_eq!(*NATIVE_MINT_WRAPPING_STRATEGY.lock().unwrap(), new_strategy);
reset_configuration().unwrap();
}
#[test]
#[serial]
fn test_set_slippage_tolerance_bps() {
let new_tolerance = 200;
set_slippage_tolerance_bps(new_tolerance).unwrap();
assert_eq!(*SLIPPAGE_TOLERANCE_BPS.lock().unwrap(), new_tolerance);
reset_configuration().unwrap();
}
#[test]
#[serial]
fn test_reset_configuration() {
reset_configuration().unwrap();
assert_eq!(*FUNDER.lock().unwrap(), Pubkey::default());
assert_eq!(*NATIVE_MINT_WRAPPING_STRATEGY.lock().unwrap(), NativeMintWrappingStrategy::Keypair);
assert_eq!(*SLIPPAGE_TOLERANCE_BPS.lock().unwrap(), 100);
}
}