diff --git a/src/fly/src/app/erc20_bridge/swap_fee.rs b/src/fly/src/app/erc20_bridge/swap_fee.rs index c8f5485..9b39a27 100644 --- a/src/fly/src/app/erc20_bridge/swap_fee.rs +++ b/src/fly/src/app/erc20_bridge/swap_fee.rs @@ -7,6 +7,7 @@ use ic_stable_structures::{DefaultMemoryImpl, StableCell}; use crate::app::memory::{ ERC20_SWAP_FEE_LAST_UPDATE_MEMORY_ID, ERC20_SWAP_FEE_MEMORY_ID, MEMORY_MANAGER, }; +use crate::constants::{ERC20_SWAP_FEE_MULTIPLIER, ONE_WEEK}; use crate::utils::time; thread_local! { @@ -49,6 +50,24 @@ impl SwapFee { Ok(()) } + + /// Returns whether the swap fee should be updated. + pub fn should_update_swap_fee(paid_gas: u64) -> bool { + let current_real_swap_fee = Self::get_real_swap_fee(); + let paid_gas = paid_gas as f64; + + let has_elapsed_one_week = LAST_SWAP_FEE_UPDATE + .with(|lsfu| time() - *lsfu.borrow().get() > ONE_WEEK.as_nanos() as u64); + + (current_real_swap_fee * 1.75 <= paid_gas) + || (current_real_swap_fee * 1.25 <= paid_gas && has_elapsed_one_week) + || (current_real_swap_fee * 0.75 >= paid_gas && has_elapsed_one_week) + } + + /// Get the swap fee without the multiplier. + fn get_real_swap_fee() -> f64 { + SWAP_FEE.with(|sf| *sf.borrow().get()) as f64 / ERC20_SWAP_FEE_MULTIPLIER + } } #[cfg(test)] @@ -71,4 +90,36 @@ mod test { assert_ne!(LAST_SWAP_FEE_UPDATE.with(|lsfu| *lsfu.borrow().get()), 0); } + + #[test] + fn test_should_tell_whether_to_update_swap_fee() { + // init swap fee to 120 (real fee is 100) + SwapFee::set_swap_fee(130).unwrap(); + assert_eq!(SwapFee::get_real_swap_fee(), 100.0); + + // 75% + assert!(SwapFee::should_update_swap_fee(175)); + + // 25 % and one week has elapsed + SwapFee::set_swap_fee(130).unwrap(); + LAST_SWAP_FEE_UPDATE + .with_borrow_mut(|lsfu| lsfu.set(time() - ONE_WEEK.as_nanos() as u64)) + .unwrap(); + assert!(SwapFee::should_update_swap_fee(125)); + + // 25% and one week has not elapsed + SwapFee::set_swap_fee(130).unwrap(); + assert!(!SwapFee::should_update_swap_fee(125)); + + // 0.75 and one week has elapsed + SwapFee::set_swap_fee(130).unwrap(); + LAST_SWAP_FEE_UPDATE + .with_borrow_mut(|lsfu| lsfu.set(time() - ONE_WEEK.as_nanos() as u64)) + .unwrap(); + assert!(SwapFee::should_update_swap_fee(75)); + + // 0.75 and one week has not elapsed + SwapFee::set_swap_fee(130).unwrap(); + assert!(!SwapFee::should_update_swap_fee(75)); + } } diff --git a/src/fly/src/constants.rs b/src/fly/src/constants.rs index ae99725..ded0e60 100644 --- a/src/fly/src/constants.rs +++ b/src/fly/src/constants.rs @@ -18,9 +18,13 @@ pub const INITIAL_RMC: f64 = 0.0000042; /// Minimum reward pub const MIN_REWARD: u64 = ICRC1_FEE * 2; +/// Factor to multiply the swap fee by +pub const ERC20_SWAP_FEE_MULTIPLIER: f64 = 1.3; + +pub const ONE_WEEK: Duration = Duration::from_secs(60 * 60 * 24 * 7); + #[cfg(target_family = "wasm")] -pub const SPEND_ALLOWANCE_EXPIRED_ALLOWANCE_TIMER_INTERVAL: Duration = - Duration::from_secs(60 * 60 * 24 * 7); // 7 days +pub const SPEND_ALLOWANCE_EXPIRED_ALLOWANCE_TIMER_INTERVAL: Duration = ONE_WEEK; #[cfg(target_family = "wasm")] pub const LIQUIDITY_POOL_SWAP_INTERVAL: Duration = Duration::from_secs(60 * 60 * 24); // 1 day