Skip to content

Commit

Permalink
account for fee taken by staking
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin committed Jan 16, 2025
1 parent 154259d commit a6dff77
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
22 changes: 22 additions & 0 deletions math/src/omnipool/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ impl<
pub fn total_delta_hub_reserve(&self) -> BalanceUpdate<Balance> {
self.delta_hub_reserve.saturating_merge(self.extra_hub_reserve_amount)
}

pub fn adjust_extra_hub_amount(self, taken_fee: Balance) -> Self {
self
}
}

/// Information about trade fee amounts
Expand All @@ -214,6 +218,15 @@ where
pub fee: TradeFee<Balance>,
}

impl<Balance> TradeStateChange<Balance>
where
Balance: Into<<FixedU128 as FixedPointNumber>::Inner> + CheckedAdd + CheckedSub + Default,
{
pub fn account_for_fee_taken(self, taken_fee: Balance) -> Self {
self
}
}

/// Delta changes after a trade with hub asset is executed.
#[derive(Default, Debug)]
pub struct HubTradeStateChange<Balance>
Expand All @@ -224,6 +237,15 @@ where
pub fee: TradeFee<Balance>,
}

impl<Balance> HubTradeStateChange<Balance>
where
Balance: Into<<FixedU128 as FixedPointNumber>::Inner> + CheckedAdd + CheckedSub + Default,
{
pub fn account_for_fee_taken(self, taken_fee: Balance) -> Self {
self
}
}

/// Delta changes after add or remove liquidity.
#[derive(Default)]
pub struct LiquidityStateChange<Balance>
Expand Down
1 change: 1 addition & 0 deletions pallets/omnipool/proptest-regressions/tests/invariants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ cc 29ab5ba11f0a5980840c43af311126f1c882ae1530118524d020e56abce54288 # shrinks to
cc 2d563c7c39d1a02219a9603504523f7cd78254dea18e0d5a2476d03720ea60c2 # shrinks to amount = 350491443827351, stable_price = FixedU128(0.393894479365242048), stable_reserve = 1489602913607245781, native_reserve = 3282830426162518235, token_1 = PoolToken { asset_id: 100, amount: 101809595835529905, price: FixedU128(1.396261122276604928) }, token_2 = PoolToken { asset_id: 200, amount: 8186533104258109521, price: FixedU128(0.604390668931716608) }, token_3 = PoolToken { asset_id: 300, amount: 8778508685399585605, price: FixedU128(0.891614701325323008) }, token_4 = PoolToken { asset_id: 400, amount: 6954874153624684200, price: FixedU128(0.808714167682048768) }, asset_fee = 0.026%, protocol_fee = 0.009%
cc 2146b455576c54416c3bff7bcadad08e8a3209886a769221b3af5b67cd4d0b8e # shrinks to amount = 1813296381012320, stable_price = FixedU128(1.657942458730811136), stable_reserve = 8671637522539599823, native_reserve = 685597774452072696, token_1 = PoolToken { asset_id: 100, amount: 7326022736421641533, price: FixedU128(0.272494203494145440) }, token_2 = PoolToken { asset_id: 200, amount: 7956036036644413967, price: FixedU128(0.805339542869442816) }, token_3 = PoolToken { asset_id: 300, amount: 3378854910706478466, price: FixedU128(1.085459180271245056) }, token_4 = PoolToken { asset_id: 400, amount: 7022260429561079464, price: FixedU128(0.489401494754793024) }, asset_fee = 2.2%, protocol_fee = 1.2%
cc 8c86bd86f8951fcd5c62113f11224eee27db41398b6ebcc919294249f74d1ff1 # shrinks to amount = 3637696600285752, stable_price = FixedU128(0.100000000000000000), stable_reserve = 100000000000000000, native_reserve = 100000000000000000, token_1 = PoolToken { asset_id: 100, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_2 = PoolToken { asset_id: 200, amount: 7749862111297083768, price: FixedU128(1.394754937095258624) }, token_3 = PoolToken { asset_id: 300, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_4 = PoolToken { asset_id: 400, amount: 100000001362720259, price: FixedU128(0.446104418147860544) }, asset_fee = 1.8%, protocol_fee = 2.7%
cc e17488db555c9b9c26cc006e0fc9053645992bb872266df3e8e0193d99168f59 # shrinks to amount = 3557444111425263, stable_price = FixedU128(0.100000000000000000), stable_reserve = 100000000000000000, native_reserve = 100000000000000000, token_1 = PoolToken { asset_id: 100, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_2 = PoolToken { asset_id: 200, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_3 = PoolToken { asset_id: 300, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_4 = PoolToken { asset_id: 400, amount: 100000000018989240, price: FixedU128(1.395851538691729408) }, asset_fee = 0.08%, protocol_fee = 2.7%
34 changes: 18 additions & 16 deletions pallets/omnipool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,12 +944,10 @@ pub mod pallet {
*state_changes.asset_out.delta_reserve > Balance::zero(),
Error::<T>::ZeroAmountOut
);

ensure!(
*state_changes.asset_out.delta_reserve >= min_buy_amount,
Error::<T>::BuyLimitNotReached
);

ensure!(
*state_changes.asset_out.delta_reserve
<= asset_out_state
Expand All @@ -959,6 +957,10 @@ pub mod pallet {
Error::<T>::MaxOutRatioExceeded
);

let (taken_fee, trade_fees) = Self::process_trade_fee(&who, asset_out, state_changes.fee.asset_fee)?;
// We need to adjust the state changes to account for the fee taken
let state_changes = state_changes.account_for_fee_taken(taken_fee);

let new_asset_in_state = asset_in_state
.delta_update(&state_changes.asset_in)
.ok_or(ArithmeticError::Overflow)?;
Expand Down Expand Up @@ -1035,9 +1037,6 @@ pub mod pallet {

Self::process_extra_protocol_fee_amount(state_changes.extra_protocol_fee_amount)?;

let trade_fees = Self::process_trade_fee(&who, asset_out, state_changes.fee.asset_fee)?;
//TODO: we need to burn additional lrna amt from the asset in based on how much asset fee was taken

Self::deposit_event(Event::SellExecuted {
who: who.clone(),
asset_in,
Expand Down Expand Up @@ -1193,6 +1192,10 @@ pub mod pallet {
Error::<T>::MaxInRatioExceeded
);

let (taken_fee, trade_fees) = Self::process_trade_fee(&who, asset_out, state_changes.fee.asset_fee)?;
// We need to adjust the state changes to account for the fee taken
let state_changes = state_changes.account_for_fee_taken(taken_fee);

let new_asset_in_state = asset_in_state
.delta_update(&state_changes.asset_in)
.ok_or(ArithmeticError::Overflow)?;
Expand Down Expand Up @@ -1269,9 +1272,6 @@ pub mod pallet {

Self::process_extra_protocol_fee_amount(state_changes.extra_protocol_fee_amount)?;

let trade_fees = Self::process_trade_fee(&who, asset_out, state_changes.fee.asset_fee)?;
//TODO: we need to burn additional lrna amt from the asset in based on how much asset fee was taken

Self::deposit_event(Event::BuyExecuted {
who: who.clone(),
asset_in,
Expand Down Expand Up @@ -1721,6 +1721,10 @@ impl<T: Config> Pallet<T> {
Error::<T>::MaxOutRatioExceeded
);

let (taken_fee, trade_fees) = Self::process_trade_fee(who, asset_out, state_changes.fee.asset_fee)?;
// We need to adjust the state changes to account for the fee taken
let state_changes = state_changes.account_for_fee_taken(taken_fee);

let new_asset_out_state = asset_state
.delta_update(&state_changes.asset)
.ok_or(ArithmeticError::Overflow)?;
Expand Down Expand Up @@ -1752,9 +1756,6 @@ impl<T: Config> Pallet<T> {

Self::set_asset_state(asset_out, new_asset_out_state);

let trade_fees = Self::process_trade_fee(who, asset_out, state_changes.fee.asset_fee)?;
//TODO: we need to burn additional lrna amt from the asset in based on how much asset fee was taken

Self::deposit_event(Event::SellExecuted {
who: who.clone(),
asset_in: T::HubAssetId::get(),
Expand Down Expand Up @@ -1836,6 +1837,10 @@ impl<T: Config> Pallet<T> {
Error::<T>::MaxInRatioExceeded
);

let (taken_fee, trade_fees) = Self::process_trade_fee(who, asset_out, state_changes.fee.asset_fee)?;
// We need to adjust the state changes to account for the fee taken
let state_changes = state_changes.account_for_fee_taken(taken_fee);

let new_asset_out_state = asset_state
.delta_update(&state_changes.asset)
.ok_or(ArithmeticError::Overflow)?;
Expand Down Expand Up @@ -1866,9 +1871,6 @@ impl<T: Config> Pallet<T> {

Self::set_asset_state(asset_out, new_asset_out_state);

let trade_fees = Self::process_trade_fee(who, asset_out, state_changes.fee.asset_fee)?;
//TODO: we need to burn additional lrna amt from the asset in based on how much asset fee was taken

// TODO: Deprecated, remove when ready
Self::deposit_event(Event::BuyExecuted {
who: who.clone(),
Expand Down Expand Up @@ -1995,7 +1997,7 @@ impl<T: Config> Pallet<T> {
trader: &T::AccountId,
asset: T::AssetId,
amount: Balance,
) -> Result<Vec<Fee<T::AccountId>>, DispatchError> {
) -> Result<(Balance, Vec<Fee<T::AccountId>>), DispatchError> {
let account = Self::protocol_account();
let original_asset_reserve = T::Currency::free_balance(asset, &account);

Expand Down Expand Up @@ -2029,7 +2031,7 @@ impl<T: Config> Pallet<T> {

all_trade_fees.extend(taken_fee_entries);

Ok(all_trade_fees)
Ok((taken_fee_total, all_trade_fees))
}

pub fn process_hub_amount(amount: Balance, dest: &T::AccountId) -> DispatchResult {
Expand Down

0 comments on commit a6dff77

Please sign in to comment.