Skip to content

Commit

Permalink
feat: boost fee split
Browse files Browse the repository at this point in the history
  • Loading branch information
msgmaxim committed Dec 12, 2024
1 parent 09292c9 commit e341fe3
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 65 deletions.
6 changes: 5 additions & 1 deletion state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use sp_api::{ApiError, ApiExt, CallApiAt};
use sp_core::U256;
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, UniqueSaturatedInto},
Permill,
Percent, Permill,
};
use sp_state_machine::InspectState;
use state_chain_runtime::{
Expand Down Expand Up @@ -565,6 +565,7 @@ mod boost_pool_rpc {
available_amounts: Vec<AccountAndAmount>,
deposits_pending_finalization: Vec<PendingBoost>,
pending_withdrawals: Vec<PendingWithdrawal>,
network_fee_deduction_percents: Percent,
}

impl BoostPoolDetailsRpc {
Expand Down Expand Up @@ -602,6 +603,7 @@ mod boost_pool_rpc {
pending_deposits,
})
.collect(),
network_fee_deduction_percents: details.network_fee_deduction_percents,
}
}
}
Expand Down Expand Up @@ -2422,6 +2424,7 @@ mod test {
(1, BTreeMap::from([(ID_1.clone(), OwedAmount { total: 1_000, fee: 50 })])),
]),
pending_withdrawals: Default::default(),
network_fee_deduction_percents: Percent::from_percent(40),
}
}

Expand All @@ -2439,6 +2442,7 @@ mod test {
(ID_1.clone(), BTreeSet::from([0])),
(ID_2.clone(), BTreeSet::from([0])),
]),
network_fee_deduction_percents: Percent::from_percent(0),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ expression: val
]
}
],
"pending_withdrawals": []
"pending_withdrawals": [],
"network_fee_deduction_percents": 40
},
{
"fee_tier": 30,
Expand Down Expand Up @@ -72,6 +73,7 @@ expression: val
0
]
}
]
],
"network_fee_deduction_percents": 0
}
]
35 changes: 28 additions & 7 deletions state-chain/pallets/cf-ingress-egress/src/boost_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests;

use frame_support::DefaultNoBound;
use sp_runtime::{
helpers_128bit::multiply_by_rational_with_rounding, Rounding, SaturatedConversion,
helpers_128bit::multiply_by_rational_with_rounding, Percent, Rounding, SaturatedConversion,
};
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};

Expand Down Expand Up @@ -146,6 +146,15 @@ pub struct BoostPool<AccountId, C: Chain> {
pending_withdrawals: BTreeMap<AccountId, BTreeSet<PrewitnessedDepositId>>,
}

#[derive(DefaultNoBound, DebugNoBound, PartialEqNoBound)]
pub struct DepositFinalisationOutcomeForPool<AccountId, C: Chain>
where
AccountId: PartialEq + core::fmt::Debug,
{
pub unlocked_funds: Vec<(AccountId, C::ChainAmount)>,
pub amount_credited: C::ChainAmount,
}

impl<AccountId, C: Chain> BoostPool<AccountId, C>
where
AccountId: PartialEq + Ord + Clone + core::fmt::Debug,
Expand Down Expand Up @@ -225,6 +234,7 @@ where
&mut self,
prewitnessed_deposit_id: PrewitnessedDepositId,
amount_to_boost: C::ChainAmount,
network_fee_deduction: Percent,
) -> Result<(C::ChainAmount, C::ChainAmount), &'static str> {
let amount_to_boost = ScaledAmount::<C>::from_chain_amount(amount_to_boost);
let full_amount_fee = fee_from_boosted_amount(amount_to_boost, self.fee_bps);
Expand All @@ -240,7 +250,12 @@ where
(provided_amount, fee)
};

self.use_funds_for_boosting(prewitnessed_deposit_id, provided_amount, fee_amount)?;
// NOTE: only a portion of the boost fee goes to the boost pool,
// the rest is charged as network fee:
let network_fee = network_fee_deduction * u128::from(fee_amount);
let boost_pool_fee = fee_amount.saturating_sub(ScaledAmount::from(network_fee));

self.use_funds_for_boosting(prewitnessed_deposit_id, provided_amount, boost_pool_fee)?;

Ok((
provided_amount.saturating_add(fee_amount).into_chain_amount(),
Expand All @@ -254,7 +269,7 @@ where
&mut self,
prewitnessed_deposit_id: PrewitnessedDepositId,
required_amount: ScaledAmount<C>,
boost_fee: ScaledAmount<C>,
boost_pool_fee: ScaledAmount<C>,
) -> Result<(), &'static str> {
let current_total_available_amount = self.available_amount;

Expand All @@ -266,7 +281,7 @@ where
let mut total_contributed = ScaledAmount::<C>::default();
let mut to_receive_recorded = ScaledAmount::default();

let amount_to_receive = required_amount.saturating_add(boost_fee);
let amount_to_receive = required_amount.saturating_add(boost_pool_fee);

let mut boosters_to_receive: BTreeMap<_, _> = self
.amounts
Expand Down Expand Up @@ -347,13 +362,14 @@ where
pub(crate) fn process_deposit_as_finalised(
&mut self,
prewitnessed_deposit_id: PrewitnessedDepositId,
) -> Vec<(AccountId, C::ChainAmount)> {
) -> DepositFinalisationOutcomeForPool<AccountId, C> {
let Some(boost_contributions) = self.pending_boosts.remove(&prewitnessed_deposit_id) else {
// The deposit hadn't been boosted
return vec![];
return Default::default();
};

let mut unlocked_funds = vec![];
let mut amount_credited: ScaledAmount<C> = 0.into();

for (booster_id, amount) in boost_contributions {
// Depending on whether the booster is withdrawing, add deposits to
Expand All @@ -371,9 +387,14 @@ where
} else {
self.add_funds_inner(booster_id, amount.total);
}

amount_credited = amount_credited.saturating_add(amount.total);
}

unlocked_funds
DepositFinalisationOutcomeForPool {
unlocked_funds,
amount_credited: amount_credited.into_chain_amount(),
}
}

// Returns the number of boosters affected
Expand Down
Loading

0 comments on commit e341fe3

Please sign in to comment.