Skip to content

Commit

Permalink
Slpx supports dynamically adding vTokens on Moonbeam (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwangningbo authored Feb 21, 2024
1 parent a241a2a commit 8d08c3f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pallets/slpx/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use crate::*;
use bifrost_asset_registry::CurrencyIdToLocations;
use bifrost_primitives::{CurrencyId, KSM};
use bifrost_primitives::{CurrencyId, KSM, VKSM};
use frame_benchmarking::v2::*;
use frame_support::{assert_ok, sp_runtime::traits::UniqueSaturatedFrom, BoundedVec};
use frame_system::RawOrigin;
Expand Down
68 changes: 47 additions & 21 deletions pallets/slpx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::types::{
};
use bifrost_asset_registry::AssetMetadata;
use bifrost_primitives::{
currency::{BNC, FIL, VBNC, VDOT, VFIL, VGLMR, VKSM, VMOVR},
currency::{BNC, VFIL},
CurrencyId, CurrencyIdMapping, SlpxOperator, TokenInfo, TryConvertFrom, VtokenMintingInterface,
};
use cumulus_primitives_core::ParaId;
Expand Down Expand Up @@ -52,6 +52,7 @@ use sp_std::{vec, vec::Vec};
use xcm::{latest::prelude::*, v3::MultiLocation};
use zenlink_protocol::AssetBalance;

pub mod migration;
mod types;

pub mod weights;
Expand Down Expand Up @@ -212,6 +213,10 @@ pub mod pallet {
vcurrency_id: CurrencyId,
vtoken_amount: BalanceOf<T>,
},
SetCurrencyToSupportXcmFee {
currency_id: CurrencyId,
is_support: bool,
},
}

#[pallet::error]
Expand Down Expand Up @@ -270,6 +275,11 @@ pub mod pallet {
pub type CurrencyIdList<T: Config> =
StorageValue<_, BoundedVec<CurrencyId, ConstU32<10>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn support_xcm_fee_list)]
pub type SupportXcmFeeList<T: Config> =
StorageValue<_, BoundedVec<CurrencyId, ConstU32<100>>, ValueQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(n: BlockNumberFor<T>, limit: Weight) -> Weight {
Expand Down Expand Up @@ -748,6 +758,32 @@ pub mod pallet {
});
Ok(().into())
}

#[pallet::call_index(10)]
#[pallet::weight(T::DbWeight::get().reads(1) + T::DbWeight::get().writes(1))]
pub fn set_currency_support_xcm_fee(
origin: OriginFor<T>,
currency_id: CurrencyId,
is_support: bool,
) -> DispatchResultWithPostInfo {
// Check the validity of origin
T::ControlOrigin::ensure_origin(origin)?;

let mut currency_list = SupportXcmFeeList::<T>::get();
match is_support {
true => {
ensure!(!currency_list.contains(&currency_id), Error::<T>::ArgumentsError);
currency_list.try_push(currency_id).map_err(|_| Error::<T>::ArgumentsError)?;
},
false => {
ensure!(currency_list.contains(&currency_id), Error::<T>::ArgumentsError);
currency_list.retain(|&x| x != currency_id);
},
};
SupportXcmFeeList::<T>::put(currency_list);
Self::deposit_event(Event::SetCurrencyToSupportXcmFee { currency_id, is_support });
Ok(().into())
}
}
}

Expand Down Expand Up @@ -939,26 +975,16 @@ impl<T: Config> Pallet<T> {
AccountKey20 { network: None, key: receiver.to_fixed_bytes() },
),
};
let fee_amount = Self::transfer_to_fee(SupportChain::Moonbeam)
.unwrap_or_else(|| Self::get_default_fee(BNC));
match currency_id {
VKSM | VMOVR | VBNC | FIL | VFIL | VDOT | VGLMR => {
T::MultiCurrency::transfer(
BNC,
evm_contract_account_id,
&caller,
fee_amount,
)?;
let assets = vec![(currency_id, amount), (BNC, fee_amount)];

T::XcmTransfer::transfer_multicurrencies(
caller, assets, 1, dest, Unlimited,
)?;
},
_ => {
T::XcmTransfer::transfer(caller, currency_id, amount, dest, Unlimited)?;
},
};
if SupportXcmFeeList::<T>::get().contains(&currency_id) {
T::XcmTransfer::transfer(caller, currency_id, amount, dest, Unlimited)?;
} else {
let fee_amount = Self::transfer_to_fee(SupportChain::Moonbeam)
.unwrap_or_else(|| Self::get_default_fee(BNC));
T::MultiCurrency::transfer(BNC, evm_contract_account_id, &caller, fee_amount)?;
let assets = vec![(currency_id, amount), (BNC, fee_amount)];

T::XcmTransfer::transfer_multicurrencies(caller, assets, 1, dest, Unlimited)?;
}
},
};
Ok(())
Expand Down
75 changes: 75 additions & 0 deletions pallets/slpx/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This file is part of Bifrost.

// Copyright (C) Liebi Technologies PTE. LTD.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use bifrost_primitives::currency::{ASTR, BNC, DOT, GLMR, KSM, MANTA, MOVR};
use frame_support::traits::OnRuntimeUpgrade;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;

pub struct BifrostKusamaAddCurrencyToSupportXcmFee<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for BifrostKusamaAddCurrencyToSupportXcmFee<T> {
fn on_runtime_upgrade() -> Weight {
//migrate the value type of SupportXcmFeeList
let currency_list = BoundedVec::try_from(vec![BNC, MOVR, KSM]).unwrap();
SupportXcmFeeList::<T>::put(currency_list);
Weight::from(T::DbWeight::get().reads_writes(1 as u64 + 1, 2 as u64 + 1))
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let currency_count = SupportXcmFeeList::<T>::get().len();
ensure!(currency_count == 0, "SupportXcmFeeList post-migrate storage count not match");

Ok(sp_std::vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_cnt: Vec<u8>) -> Result<(), TryRuntimeError> {
let currency_count_new = SupportXcmFeeList::<T>::get().len();
ensure!(currency_count_new == 3, "Validators post-migrate storage count not match");

Ok(())
}
}

pub struct BifrostPolkadotAddCurrencyToSupportXcmFee<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for BifrostPolkadotAddCurrencyToSupportXcmFee<T> {
fn on_runtime_upgrade() -> Weight {
//migrate the value type of SupportXcmFeeList
let currency_list = BoundedVec::try_from(vec![BNC, GLMR, DOT, ASTR, MANTA]).unwrap();
SupportXcmFeeList::<T>::put(currency_list);
Weight::from(T::DbWeight::get().reads_writes(1 as u64 + 1, 2 as u64 + 1))
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let currency_count = SupportXcmFeeList::<T>::get().len();
ensure!(currency_count == 0, "SupportXcmFeeList post-migrate storage count not match");

Ok(sp_std::vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_cnt: Vec<u8>) -> Result<(), TryRuntimeError> {
let currency_count_new = SupportXcmFeeList::<T>::get().len();
ensure!(currency_count_new == 5, "Validators post-migrate storage count not match");

Ok(())
}
}
14 changes: 14 additions & 0 deletions pallets/slpx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,17 @@ fn test_set_ethereum_call_configration() {
);
})
}

#[test]
fn test_set_currency_to_support_xcm_fee() {
sp_io::TestExternalities::default().execute_with(|| {
assert_ok!(Slpx::set_currency_support_xcm_fee(RuntimeOrigin::root(), BNC, true));
assert_eq!(Slpx::support_xcm_fee_list().to_vec(), vec![BNC]);

assert_ok!(Slpx::set_currency_support_xcm_fee(RuntimeOrigin::root(), KSM, true));
assert_eq!(Slpx::support_xcm_fee_list().to_vec(), vec![BNC, KSM]);

assert_ok!(Slpx::set_currency_support_xcm_fee(RuntimeOrigin::root(), BNC, false));
assert_eq!(Slpx::support_xcm_fee_list().to_vec(), vec![KSM]);
})
}
1 change: 1 addition & 0 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,7 @@ pub mod migrations {
Runtime,
governance::fellowship::FellowshipReferendaInstance,
>,
bifrost_slpx::migration::BifrostKusamaAddCurrencyToSupportXcmFee<Runtime>,
);
}

Expand Down
1 change: 1 addition & 0 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,7 @@ pub mod migrations {
Runtime,
governance::fellowship::FellowshipReferendaInstance,
>,
bifrost_slpx::migration::BifrostPolkadotAddCurrencyToSupportXcmFee<Runtime>,
);
}

Expand Down

0 comments on commit 8d08c3f

Please sign in to comment.