Skip to content

Commit

Permalink
feat: swap fee should update
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jan 25, 2024
1 parent 0d74f11 commit 7099222
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/fly/src/app/erc20_bridge/swap_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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)]
Expand All @@ -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));
}
}
8 changes: 6 additions & 2 deletions src/fly/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7099222

Please sign in to comment.