Skip to content

Commit

Permalink
Slp removes refund (#1275)
Browse files Browse the repository at this point in the history
* Slp removes refund

* Fix clippy
  • Loading branch information
hqwangningbo committed Jun 21, 2024
1 parent 66a3ce7 commit b4fdc81
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 555 deletions.
6 changes: 2 additions & 4 deletions integration-tests/src/mock/bifrost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use bifrost_polkadot_runtime::{
ParachainMinFee, SelfRelativeLocation, UniversalLocation,
},
BifrostCurrencyIdConvert, BifrostTreasuryAccount, MaxLengthLimit, MaxRefundPerBlock,
MaxTypeEntryPerBlock, NativeCurrencyId, OnRefund, SelfParaChainId, Slpx,
SubAccountIndexMultiLocationConvertor, VtokenMinting, XcmInterface,
MaxTypeEntryPerBlock, NativeCurrencyId, SelfParaChainId, SubAccountIndexMultiLocationConvertor,
VtokenMinting, XcmInterface,
};
use bifrost_primitives::CurrencyId;
use bifrost_slp::QueryResponseManager;
Expand Down Expand Up @@ -293,13 +293,11 @@ impl bifrost_slp::Config for Runtime {
type ControlOrigin = EnsureRoot<AccountId>;
type WeightInfo = ();
type VtokenMinting = VtokenMinting;
type BifrostSlpx = Slpx;
type AccountConverter = SubAccountIndexMultiLocationConvertor;
type ParachainId = SelfParaChainId;
type SubstrateResponseManager = SubstrateResponseManager;
type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock;
type MaxRefundPerBlock = MaxRefundPerBlock;
type OnRefund = OnRefund;
type ParachainStaking = ();
type XcmTransfer = XTokens;
type MaxLengthLimit = MaxLengthLimit;
Expand Down
2 changes: 0 additions & 2 deletions pallets/buy-back/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,11 @@ impl bifrost_slp::Config for Runtime {
type ControlOrigin = EnsureSignedBy<One, AccountId>;
type WeightInfo = ();
type VtokenMinting = VtokenMinting;
type BifrostSlpx = SlpxInterface;
type AccountConverter = ();
type ParachainId = ParachainId;
type SubstrateResponseManager = SubstrateResponseManager;
type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock;
type MaxRefundPerBlock = MaxRefundPerBlock;
type OnRefund = ();
type ParachainStaking = ();
type XcmTransfer = XTokens;
type MaxLengthLimit = MaxLengthLimit;
Expand Down
2 changes: 0 additions & 2 deletions pallets/fee-share/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,11 @@ impl bifrost_slp::Config for Runtime {
type ControlOrigin = EnsureSignedBy<One, AccountId>;
type WeightInfo = ();
type VtokenMinting = VtokenMinting;
type BifrostSlpx = SlpxInterface;
type AccountConverter = ();
type ParachainId = ParachainId;
type SubstrateResponseManager = SubstrateResponseManager;
type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock;
type MaxRefundPerBlock = MaxRefundPerBlock;
type OnRefund = ();
type ParachainStaking = ();
type XcmTransfer = XTokens;
type MaxLengthLimit = MaxLengthLimit;
Expand Down
12 changes: 4 additions & 8 deletions pallets/slp/src/agents/astar_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<T: Config>
fn transfer_back(
&self,
from: &MultiLocation,
to: &MultiLocation,
_to: &MultiLocation,
amount: BalanceOf<T>,
currency_id: CurrencyId,
weight_and_fee: Option<(Weight, BalanceOf<T>)>,
Expand All @@ -431,22 +431,18 @@ impl<T: Config>
DelegatorsMultilocation2Index::<T>::get(currency_id, from)
.ok_or(Error::<T>::DelegatorNotExist)?;

// Make sure the receiving account is the Exit_account from vtoken-minting module.
let to_account_id = Pallet::<T>::multilocation_to_account(to)?;
let (_, exit_account) = T::VtokenMinting::get_entrance_and_exit_accounts();
ensure!(to_account_id == exit_account, Error::<T>::InvalidAccount);
// Make sure the receiving account is the entrance_account from vtoken-minting module.
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();

// Prepare parameter dest and beneficiary.
let to_32: [u8; 32] = Pallet::<T>::multilocation_to_account_32(to)?;

let dest = Box::new(VersionedLocation::V3(MultiLocation::new(
1,
X1(Parachain(T::ParachainId::get().into())),
)));

let beneficiary = Box::new(VersionedLocation::V3(MultiLocation::from(X1(AccountId32 {
network: None,
id: to_32,
id: entrance_account.encode().try_into().map_err(|_| Error::<T>::FailToConvert)?,
}))));

// Prepare parameter assets.
Expand Down
36 changes: 2 additions & 34 deletions pallets/slp/src/agents/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
ParachainStakingLedgerUpdateEntry, ParachainStakingLedgerUpdateOperation, TIMEOUT_BLOCKS,
},
traits::QueryResponseManager,
vec, AccountIdOf, BalanceOf, BlockNumberFor, BoundedVec, Box, Config, CurrencyDelays,
vec, AccountIdOf, BalanceOf, BlockNumberFor, BoundedVec, Config, CurrencyDelays,
DelegationsOccupied, DelegatorLatestTuneRecord, DelegatorLedgerXcmUpdateQueue,
DelegatorLedgers, DelegatorNextIndex, DelegatorsIndex2Multilocation,
DelegatorsMultilocation2Index, Encode, Event, FeeSources, Ledger, LedgerUpdateEntry,
Expand All @@ -37,10 +37,7 @@ use sp_runtime::{
traits::{AccountIdConversion, CheckedAdd, UniqueSaturatedFrom, UniqueSaturatedInto},
DispatchResult, Saturating,
};
use xcm::{
v3::{prelude::*, MultiLocation},
VersionedLocation,
};
use xcm::v3::{prelude::*, MultiLocation};

// Some common business functions for all agents
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -207,35 +204,6 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn get_transfer_back_dest_and_beneficiary(
from: &MultiLocation,
to: &MultiLocation,
currency_id: CurrencyId,
) -> Result<(Box<VersionedLocation>, Box<VersionedLocation>), Error<T>> {
// Check if from is one of our delegators. If not, return error.
DelegatorsMultilocation2Index::<T>::get(currency_id, from)
.ok_or(Error::<T>::DelegatorNotExist)?;

// Make sure the receiving account is the Exit_account from vtoken-minting module.
let to_account_id = Self::multilocation_to_account(to)?;
let (_, exit_account) = T::VtokenMinting::get_entrance_and_exit_accounts();
ensure!(to_account_id == exit_account, Error::<T>::InvalidAccount);

// Prepare parameter dest and beneficiary.
let to_32: [u8; 32] = Self::multilocation_to_account_32(to)?;

let dest = Box::new(VersionedLocation::V3(Location::from([Parachain(
T::ParachainId::get().into(),
)])));

let beneficiary = Box::new(VersionedLocation::V3(Location::from([AccountId32 {
network: None,
id: to_32,
}])));

Ok((dest, beneficiary))
}

pub(crate) fn tune_vtoken_exchange_rate_without_update_ledger(
who: &MultiLocation,
token_amount: BalanceOf<T>,
Expand Down
26 changes: 14 additions & 12 deletions pallets/slp/src/agents/parachain_staking_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use bifrost_primitives::{
use core::marker::PhantomData;
use frame_support::{ensure, traits::Get};
use orml_traits::MultiCurrency;
use parity_scale_codec::alloc::collections::BTreeMap;
use parity_scale_codec::{alloc::collections::BTreeMap, Encode};
use sp_arithmetic::Percent;
use sp_runtime::{
traits::{CheckedAdd, CheckedSub, Convert, UniqueSaturatedInto, Zero},
Expand All @@ -50,7 +50,7 @@ use sp_std::prelude::*;
use xcm::{
opaque::v3::{
Junction::{AccountId32, Parachain},
MultiLocation, WeightLimit,
MultiLocation,
},
v3::prelude::*,
VersionedLocation,
Expand Down Expand Up @@ -1356,7 +1356,7 @@ impl<T: Config>
fn transfer_back(
&self,
from: &MultiLocation,
to: &MultiLocation,
_to: &MultiLocation,
amount: BalanceOf<T>,
currency_id: CurrencyId,
weight_and_fee: Option<(Weight, BalanceOf<T>)>,
Expand All @@ -1369,23 +1369,25 @@ impl<T: Config>
.ok_or(Error::<T>::DelegatorNotExist)?;

// Make sure the receiving account is the Exit_account from vtoken-minting module.
let to_account_id = Pallet::<T>::multilocation_to_account(&to)?;

let (_, exit_account) = T::VtokenMinting::get_entrance_and_exit_accounts();
ensure!(to_account_id == exit_account, Error::<T>::InvalidAccount);
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();

if currency_id == BNC {
let from_account = Pallet::<T>::multilocation_to_account(from)?;
T::MultiCurrency::transfer(currency_id, &from_account, &to_account_id, amount)
T::MultiCurrency::transfer(currency_id, &from_account, &entrance_account, amount)
.map_err(|_| Error::<T>::Unexpected)?;
} else {
// Prepare parameter dest and beneficiary.
let to_32: [u8; 32] = Pallet::<T>::multilocation_to_account_32(&to)?;
let dest = Box::new(VersionedLocation::V3(MultiLocation {
parents: 1,
interior: X2(
Parachain(T::ParachainId::get().into()),
AccountId32 { network: None, id: to_32 },
AccountId32 {
network: None,
id: entrance_account
.encode()
.try_into()
.map_err(|_| Error::<T>::FailToConvert)?,
},
),
}));

Expand All @@ -1395,15 +1397,15 @@ impl<T: Config>
MoonbeamCurrencyId::SelfReserve,
amount.unique_saturated_into(),
dest,
WeightLimit::Unlimited,
Unlimited,
))
.encode()
.into(),
MANTA => MantaCall::Xtokens(MantaXtokensCall::<T>::Transfer(
MantaCurrencyId::MantaCurrency(1),
amount.unique_saturated_into(),
dest,
WeightLimit::Unlimited,
Unlimited,
))
.encode()
.into(),
Expand Down
23 changes: 19 additions & 4 deletions pallets/slp/src/agents/polkadot_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ use bifrost_primitives::{
use core::marker::PhantomData;
use frame_support::{ensure, traits::Get};
use frame_system::pallet_prelude::BlockNumberFor;
use parity_scale_codec::Encode;
use sp_runtime::{
traits::{CheckedAdd, CheckedSub, Convert, StaticLookup, UniqueSaturatedInto, Zero},
DispatchResult,
};
use sp_std::prelude::*;
use xcm::{opaque::v3::MultiLocation, v3::prelude::*, VersionedAssets};
use xcm::{opaque::v3::MultiLocation, v3::prelude::*, VersionedAssets, VersionedLocation};

/// StakingAgent implementation for Kusama/Polkadot
pub struct PolkadotAgent<T>(PhantomData<T>);
Expand Down Expand Up @@ -788,16 +789,30 @@ impl<T: Config>
fn transfer_back(
&self,
from: &MultiLocation,
to: &MultiLocation,
_to: &MultiLocation,
amount: BalanceOf<T>,
currency_id: CurrencyId,
weight_and_fee: Option<(Weight, BalanceOf<T>)>,
) -> Result<(), Error<T>> {
// Ensure amount is greater than zero.
ensure!(!amount.is_zero(), Error::<T>::AmountZero);

let (dest, beneficiary) =
Pallet::<T>::get_transfer_back_dest_and_beneficiary(from, to, currency_id)?;
// Check if from is one of our delegators. If not, return error.
DelegatorsMultilocation2Index::<T>::get(currency_id, from)
.ok_or(Error::<T>::DelegatorNotExist)?;

// Make sure the receiving account is the Exit_account from vtoken-minting module.
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();

// Prepare parameter dest and beneficiary.
let dest = Box::new(VersionedLocation::V3(Location::from([Parachain(
T::ParachainId::get().into(),
)])));

let beneficiary = Box::new(VersionedLocation::V3(Location::from([AccountId32 {
network: None,
id: entrance_account.encode().try_into().map_err(|_| Error::<T>::FailToConvert)?,
}])));

// Prepare parameter assets.
let asset = MultiAsset {
Expand Down
20 changes: 0 additions & 20 deletions pallets/slp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,26 +710,6 @@ mod benchmarks {
Ok(())
}

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

let (_, exit_account) = <T as Config>::VtokenMinting::get_entrance_and_exit_accounts();
orml_tokens::Pallet::<T>::deposit(
KSM,
&exit_account,
<T as orml_tokens::Config>::Balance::saturated_from(1_000_000_000_000u128),
)
.unwrap();

#[extrinsic_call]
_(origin as <T as frame_system::Config>::RuntimeOrigin, KSM);

Ok(())
}

#[benchmark]
fn supplement_fee_reserve() -> Result<(), BenchmarkError> {
let origin = <T as Config>::ControlOrigin::try_successful_origin()
Expand Down
Loading

0 comments on commit b4fdc81

Please sign in to comment.