Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable supplement_fee_reserve #1306

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions pallets/slp/src/agents/astar_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,19 +594,6 @@ impl<T: Config>
Pallet::<T>::inner_charge_hosting_fee(charge_amount, to, vtoken)
}

/// Deposit some amount as fee to nominator accounts.
fn supplement_fee_reserve(
&self,
amount: BalanceOf<T>,
from: &MultiLocation,
to: &MultiLocation,
currency_id: CurrencyId,
) -> Result<(), Error<T>> {
Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;

Ok(())
}

fn check_delegator_ledger_query_response(
&self,
query_id: QueryId,
Expand Down
11 changes: 0 additions & 11 deletions pallets/slp/src/agents/filecoin_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,17 +498,6 @@ impl<T: Config>
Pallet::<T>::inner_charge_hosting_fee(amount, to, currency_id)
}

/// Deposit some amount as fee to nominator accounts.
fn supplement_fee_reserve(
&self,
_amount: BalanceOf<T>,
_from: &MultiLocation,
_to: &MultiLocation,
_currency_id: CurrencyId,
) -> Result<(), Error<T>> {
Err(Error::<T>::Unsupported)
}

fn check_delegator_ledger_query_response(
&self,
_query_id: QueryId,
Expand Down
21 changes: 0 additions & 21 deletions pallets/slp/src/agents/parachain_staking_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,27 +1532,6 @@ impl<T: Config>
Pallet::<T>::inner_charge_hosting_fee(charge_amount, to, vtoken)
}

/// Deposit some amount as fee to nominator accounts.
fn supplement_fee_reserve(
&self,
amount: BalanceOf<T>,
from: &MultiLocation,
to: &MultiLocation,
currency_id: CurrencyId,
) -> Result<(), Error<T>> {
if currency_id == BNC {
ensure!(!amount.is_zero(), Error::<T>::AmountZero);
let from_account_id = Pallet::<T>::multilocation_to_account(from)?;
let to_account_id = Pallet::<T>::multilocation_to_account(to)?;
T::MultiCurrency::transfer(currency_id, &from_account_id, &to_account_id, amount)
.map_err(|_e| Error::<T>::MultiCurrencyError)?;
} else {
Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;
}

Ok(())
}

fn check_delegator_ledger_query_response(
&self,
query_id: QueryId,
Expand Down
13 changes: 0 additions & 13 deletions pallets/slp/src/agents/phala_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,19 +762,6 @@ impl<T: Config>
Pallet::<T>::inner_charge_hosting_fee(charge_amount, to, vtoken)
}

/// Deposit some amount as fee to nominator accounts.
fn supplement_fee_reserve(
&self,
amount: BalanceOf<T>,
from: &MultiLocation,
to: &MultiLocation,
currency_id: CurrencyId,
) -> Result<(), Error<T>> {
Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;

Ok(())
}

fn check_delegator_ledger_query_response(
&self,
query_id: QueryId,
Expand Down
13 changes: 0 additions & 13 deletions pallets/slp/src/agents/polkadot_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,19 +981,6 @@ impl<T: Config>
Pallet::<T>::inner_charge_hosting_fee(charge_amount, to, vtoken)
}

/// Deposit some amount as fee to nominator accounts.
fn supplement_fee_reserve(
&self,
amount: BalanceOf<T>,
from: &MultiLocation,
to: &MultiLocation,
currency_id: CurrencyId,
) -> Result<(), Error<T>> {
Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;

Ok(())
}

fn check_delegator_ledger_query_response(
&self,
query_id: QueryId,
Expand Down
25 changes: 0 additions & 25 deletions pallets/slp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,31 +710,6 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn supplement_fee_reserve() -> Result<(), BenchmarkError> {
let origin = <T as Config>::ControlOrigin::try_successful_origin()
.map_err(|_| BenchmarkError::Weightless)?;
set_mins_and_maxs::<T>(origin.clone());

let (entrance_account, _) = <T as Config>::VtokenMinting::get_entrance_and_exit_accounts();
let entrance_account_32 = Pallet::<T>::account_id_to_account_32(entrance_account).unwrap();
let from = Pallet::<T>::account_32_to_local_location(entrance_account_32).unwrap();

DelegatorsMultilocation2Index::<T>::insert(KSM, DELEGATOR1, 0);

FeeSources::<T>::insert(KSM, (from, BalanceOf::<T>::from(4100000000u32)));

T::XcmWeightAndFeeHandler::set_xcm_dest_weight_and_fee(
KSM,
XcmOperationType::TransferTo,
Some((Weight::from_parts(4000000000, 100000), 0u32.into())),
)?;

#[extrinsic_call]
_(origin as <T as frame_system::Config>::RuntimeOrigin, KSM, Box::new(DELEGATOR1));
Ok(())
}

#[benchmark]
fn charge_host_fee_and_tune_vtoken_exchange_rate() -> Result<(), BenchmarkError> {
let origin = <T as Config>::ControlOrigin::try_successful_origin()
Expand Down
79 changes: 5 additions & 74 deletions pallets/slp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use bifrost_parachain_staking::ParachainStakingInterface;
use bifrost_primitives::{
currency::{BNC, KSM, MANTA, MOVR, PHA},
traits::XcmDestWeightAndFeeHandler,
CurrencyId, CurrencyIdExt, CurrencyIdMapping, DerivativeAccountHandler, DerivativeIndex,
CurrencyId, CurrencyIdMapping, DerivativeAccountHandler, DerivativeIndex,
SlpHostingFeeProvider, SlpOperator, TimeUnit, VtokenMintingOperator, XcmOperationType, ASTR,
DOT, FIL, GLMR,
};
Expand Down Expand Up @@ -1246,80 +1246,11 @@ pub mod pallet {
#[pallet::call_index(19)]
#[pallet::weight(<T as Config>::WeightInfo::supplement_fee_reserve())]
pub fn supplement_fee_reserve(
origin: OriginFor<T>,
currency_id: CurrencyId,
dest: Box<MultiLocation>,
_origin: OriginFor<T>,
_currency_id: CurrencyId,
_dest: Box<MultiLocation>,
) -> DispatchResult {
// Ensure origin
Self::ensure_authorized(origin, currency_id)?;

// Ensure dest is one of delegators accounts, or operators account, or in
// SupplementFeeAccountWhitelist.
let mut valid_account = false;

if DelegatorsMultilocation2Index::<T>::contains_key(currency_id, dest.clone()) {
valid_account = true;
}

if !valid_account {
let dest_account_id = Self::multilocation_to_account(&dest)?;
let operate_account_op = OperateOrigins::<T>::get(currency_id);

if let Some(operate_account) = operate_account_op {
if dest_account_id == operate_account {
valid_account = true;
}
}
}

if !valid_account {
let white_list_op = SupplementFeeAccountWhitelist::<T>::get(currency_id);

if let Some(white_list) = white_list_op {
let multi_hash = T::Hashing::hash(&dest.encode());
white_list
.binary_search_by_key(&multi_hash, |(_multi, hash)| *hash)
.map_err(|_| Error::<T>::DestAccountNotValid)?;

valid_account = true;
}
}

ensure!(valid_account, Error::<T>::DestAccountNotValid);

// Get the fee source account and reserve amount from the FeeSources<T> storage.
let (source_location, reserved_fee) =
FeeSources::<T>::get(currency_id).ok_or(Error::<T>::FeeSourceNotExist)?;

// If currency is BNC, transfer directly.
// Otherwise, call supplement_fee_reserve of StakingFeeManager trait.
if currency_id.is_native() {
let source_account = Self::native_multilocation_to_account(&source_location)?;
let dest_account = Self::native_multilocation_to_account(&dest)?;
T::MultiCurrency::transfer(
currency_id,
&source_account,
&dest_account,
reserved_fee,
)?;
} else {
let staking_agent = Self::get_currency_staking_agent(currency_id)?;
staking_agent.supplement_fee_reserve(
reserved_fee,
&source_location,
&dest,
currency_id,
)?;
}

// Deposit event.
Pallet::<T>::deposit_event(Event::FeeSupplemented {
currency_id,
amount: reserved_fee,
from: source_location,
to: *dest,
});

ensure!(false, Error::<T>::Unsupported);
Ok(())
}

Expand Down
11 changes: 1 addition & 10 deletions pallets/slp/src/mocks/mock_kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate as bifrost_slp;
use crate::{Config, DispatchResult, QueryResponseManager};
use bifrost_asset_registry::AssetIdMaps;
use bifrost_primitives::{
currency::{BNC, KSM, MANTA, VKSM},
currency::{BNC, KSM, MANTA},
Amount, Balance, CurrencyId, DoNothingExecuteXcm, DoNothingRouter, SlpxOperator, TokenSymbol,
XcmDestWeightAndFeeHandler, XcmOperationType,
};
Expand Down Expand Up @@ -660,15 +660,6 @@ impl Default for ExtBuilder {
}

impl ExtBuilder {
pub fn balances(mut self, endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
self.endowed_accounts = endowed_accounts;
self
}

pub fn one_hundred_for_alice(self) -> Self {
self.balances(vec![(ALICE, BNC, 100), (ALICE, KSM, 100), (ALICE, VKSM, 100)])
}

pub fn build(self) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();

Expand Down
45 changes: 2 additions & 43 deletions pallets/slp/src/tests/kusama_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#![cfg(test)]

use crate::{mocks::mock_kusama::*, *};
use bifrost_primitives::currency::{BNC, KSM, VKSM};
use frame_support::{assert_noop, assert_ok, PalletId};
use bifrost_primitives::currency::{KSM, VKSM};
use frame_support::{assert_ok, PalletId};
use orml_traits::MultiCurrency;
use sp_runtime::traits::AccountIdConversion;
use xcm::v3::prelude::*;
Expand Down Expand Up @@ -74,47 +74,6 @@ fn set_fee_source_works() {
});
}

// test native token fee supplement. Non-native will be tested in the integration tests.
#[test]
fn supplement_fee_reserve_works() {
ExtBuilder::default().one_hundred_for_alice().build().execute_with(|| {
// set fee source
let alice_32 = Pallet::<Runtime>::account_id_to_account_32(ALICE).unwrap();
let alice_location = Pallet::<Runtime>::account_32_to_local_location(alice_32).unwrap();
assert_ok!(Slp::set_fee_source(
RuntimeOrigin::signed(ALICE),
BNC,
Some((alice_location, 10))
));

// supplement fee
let bob_32 = Pallet::<Runtime>::account_id_to_account_32(BOB).unwrap();
let bob_location = Pallet::<Runtime>::account_32_to_local_location(bob_32).unwrap();
assert_eq!(Balances::free_balance(&ALICE), 100);
assert_eq!(Balances::free_balance(&BOB), 0);

assert_noop!(
Slp::supplement_fee_reserve(
RuntimeOrigin::signed(ALICE),
BNC,
Box::new(alice_location)
),
Error::<Runtime>::DestAccountNotValid
);

assert_ok!(Slp::set_operate_origin(RuntimeOrigin::signed(ALICE), BNC, Some(BOB)));

assert_ok!(Slp::supplement_fee_reserve(
RuntimeOrigin::signed(ALICE),
BNC,
Box::new(bob_location)
));

assert_eq!(Balances::free_balance(&ALICE), 90);
assert_eq!(Balances::free_balance(&BOB), 10);
});
}

#[test]
fn remove_delegator_works() {
ExtBuilder::default().build().execute_with(|| {
Expand Down
Loading