From 62ca63ef427ca1f1deeefe89e7abe00859f3e1a6 Mon Sep 17 00:00:00 2001 From: NingBo Wang <2536935847@qq.com> Date: Tue, 12 Dec 2023 17:43:30 +0800 Subject: [PATCH] Slp add XCM weight parameters (#1098) * Slp optimises XCM weight parameters * Optimising code * Add construct xcm test --- Cargo.lock | 2 +- integration-tests/bifrost-kusama/src/slp.rs | 54 ++++++++--- pallets/slp/src/agents/astar_agent/agent.rs | 26 ++++- pallets/slp/src/agents/common.rs | 95 ++++++++++++------- .../slp/src/agents/filecoin_agent/agent.rs | 21 +++- .../agents/parachain_staking_agent/agent.rs | 26 ++++- pallets/slp/src/agents/phala_agent/agent.rs | 30 +++++- .../slp/src/agents/polkadot_agent/agent.rs | 32 ++++++- pallets/slp/src/benchmarking.rs | 23 ++++- pallets/slp/src/lib.rs | 61 +++++++++--- pallets/slp/src/tests/filecoin_tests.rs | 47 ++++++--- pallets/slp/src/tests/kusama_tests.rs | 90 ++++++++++++++++++ pallets/slp/src/tests/moonriver_tests.rs | 12 ++- .../slp/src/tests/parachain_staking_tests.rs | 26 ++++- pallets/slp/src/tests/phala_tests.rs | 66 ++++++++++--- pallets/slp/src/traits.rs | 26 ++++- 16 files changed, 527 insertions(+), 110 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b41ee78e7..672d2c8826 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4488,7 +4488,7 @@ dependencies = [ [[package]] name = "fflonk" version = "0.1.0" -source = "git+https://github.com/w3f/fflonk#1beb0585e1c8488956fac7f05da061f9b41e8948" +source = "git+https://github.com/w3f/fflonk#1e854f35e9a65d08b11a86291405cdc95baa0a35" dependencies = [ "ark-ec", "ark-ff", diff --git a/integration-tests/bifrost-kusama/src/slp.rs b/integration-tests/bifrost-kusama/src/slp.rs index 85cdfa1980..a36597c2e0 100644 --- a/integration-tests/bifrost-kusama/src/slp.rs +++ b/integration-tests/bifrost-kusama/src/slp.rs @@ -435,6 +435,7 @@ fn transfer_back() { Box::new(KSM_DELEGATOR_0_MULTILOCATION), Box::new(EXIT_ACCOUNT_MULTILOCATION), 50 * KSM_DECIMALS, + None )); }); @@ -462,7 +463,8 @@ fn bond_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -510,7 +512,8 @@ fn bond_extra_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); BifrostKusama::execute_with(|| { @@ -521,6 +524,7 @@ fn bond_extra_works() { Box::new(KSM_DELEGATOR_0_MULTILOCATION), None, 20 * KSM_DECIMALS, + None )); }); @@ -562,7 +566,8 @@ fn unbond_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -586,6 +591,7 @@ fn unbond_works() { Box::new(KSM_DELEGATOR_0_MULTILOCATION), None, 20 * KSM_DECIMALS, + None )); }); @@ -625,7 +631,8 @@ fn unbond_all_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -642,6 +649,7 @@ fn unbond_all_works() { RuntimeOrigin::signed(AccountId::from(ALICE)), RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), + None )); }); @@ -677,7 +685,8 @@ fn rebond_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -691,7 +700,8 @@ fn rebond_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), None, - 30 * KSM_DECIMALS + 30 * KSM_DECIMALS, + None )); }); @@ -707,6 +717,7 @@ fn rebond_works() { Box::new(KSM_DELEGATOR_0_MULTILOCATION), None, Some(20 * KSM_DECIMALS), + None )); }); @@ -757,7 +768,8 @@ fn delegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -774,6 +786,7 @@ fn delegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION], + None )); }); @@ -806,7 +819,8 @@ fn undelegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -820,6 +834,7 @@ fn undelegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION], + None )); }); @@ -834,6 +849,7 @@ fn undelegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION], + None )); }); @@ -859,7 +875,8 @@ fn redelegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -873,6 +890,7 @@ fn redelegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION], + None )); }); @@ -887,6 +905,7 @@ fn redelegate_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION], + None )); }); @@ -899,7 +918,8 @@ fn redelegate_works() { RuntimeOrigin::signed(AccountId::from(ALICE)), RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), - Some(vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION]) + Some(vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION]), + None )); }); @@ -929,7 +949,8 @@ fn payout_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), Box::new(KUSAMA_ALICE_STASH_MULTILOCATION), - Some(TimeUnit::Era(27)) + Some(TimeUnit::Era(27)), + None )); }); } @@ -944,7 +965,8 @@ fn liquidize_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, - None + None, + None, )); }); @@ -958,6 +980,7 @@ fn liquidize_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION], + None )); }); @@ -971,7 +994,8 @@ fn liquidize_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), None, - 20 * KSM_DECIMALS + 20 * KSM_DECIMALS, + None )); }); @@ -1002,6 +1026,7 @@ fn liquidize_works() { Box::new(KSM_DELEGATOR_0_MULTILOCATION), Some(TimeUnit::SlashingSpan(5)), None, + None, None )); }); @@ -1034,6 +1059,7 @@ fn chill_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), 50 * KSM_DECIMALS, + None, None )); }); @@ -1044,6 +1070,7 @@ fn chill_works() { RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), vec![KUSAMA_ALICE_STASH_MULTILOCATION, KUSAMA_BOB_STASH_MULTILOCATION], + None )); }); @@ -1066,6 +1093,7 @@ fn chill_works() { RuntimeOrigin::signed(AccountId::from(ALICE)), RelayCurrencyId::get(), Box::new(KSM_DELEGATOR_0_MULTILOCATION), + None )); }); diff --git a/pallets/slp/src/agents/astar_agent/agent.rs b/pallets/slp/src/agents/astar_agent/agent.rs index 900899b6b3..43979c6117 100644 --- a/pallets/slp/src/agents/astar_agent/agent.rs +++ b/pallets/slp/src/agents/astar_agent/agent.rs @@ -88,6 +88,7 @@ impl amount: BalanceOf, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let contract_multilocation = validator.ok_or(Error::::ValidatorNotProvided)?; let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; @@ -135,6 +136,7 @@ impl call.encode(), who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -161,6 +163,7 @@ impl _amount: BalanceOf, _validator: &Option, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -172,6 +175,7 @@ impl amount: BalanceOf, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if the unbonding amount exceeds minimum requirement. let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; @@ -205,12 +209,13 @@ impl // Wrap the xcm message as it is sent from a subaccount of the parachain account, and // send it out. - let (query_id, timeout, _fee, xcm_message) = + let (query_id, timeout, _, xcm_message) = Pallet::::construct_xcm_as_subaccount_with_query_id( XcmOperationType::Unbond, call.encode(), who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -235,6 +240,7 @@ impl &self, _who: &MultiLocation, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -246,6 +252,7 @@ impl _amount: Option>, _validator: &Option, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unexpected) } @@ -256,6 +263,7 @@ impl _who: &MultiLocation, _targets: &Vec, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -266,6 +274,7 @@ impl _who: &MultiLocation, _targets: &Vec, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -276,6 +285,7 @@ impl _who: &MultiLocation, _targets: &Option>, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -287,6 +297,7 @@ impl validator: &MultiLocation, _when: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Get the validator account let contract_h160 = Pallet::::multilocation_to_h160_account(&validator)?; @@ -302,6 +313,7 @@ impl call.encode(), who, currency_id, + weight_and_fee, )?; Ok(Zero::zero()) @@ -315,6 +327,7 @@ impl _validator: &Option, currency_id: CurrencyId, _amount: Option>, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is in the delegator set. ensure!( @@ -333,6 +346,7 @@ impl call.encode(), who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -355,7 +369,12 @@ impl /// Chill self. Cancel the identity of delegator in the Relay chain side. /// Unbonding all the active amount should be done before or after chill, /// so that we can collect back all the bonded amount. - fn chill(&self, _who: &MultiLocation, _currency_id: CurrencyId) -> Result> { + fn chill( + &self, + _who: &MultiLocation, + _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, + ) -> Result> { Err(Error::::Unsupported) } @@ -366,6 +385,7 @@ impl to: &MultiLocation, amount: BalanceOf, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result<(), Error> { // Ensure amount is greater than zero. ensure!(!amount.is_zero(), Error::::AmountZero); @@ -420,6 +440,7 @@ impl call.encode(), from, currency_id, + weight_and_fee, )?; Ok(()) @@ -457,6 +478,7 @@ impl _amount: BalanceOf, _currency_id: CurrencyId, _if_from_currency: bool, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } diff --git a/pallets/slp/src/agents/common.rs b/pallets/slp/src/agents/common.rs index 5e62c599bd..7e58039f7e 100644 --- a/pallets/slp/src/agents/common.rs +++ b/pallets/slp/src/agents/common.rs @@ -27,11 +27,10 @@ use crate::{ Junction::{AccountId32, Parachain}, Junctions::X1, Ledger, LedgerUpdateEntry, MinimumsAndMaximums, MultiLocation, Pallet, TimeUnit, Validators, - Vec, Weight, Xcm, XcmOperationType, XcmWeight, Zero, ASTR, BNC, DOT, GLMR, KSM, MANTA, MOVR, - PHA, + Vec, Weight, Xcm, XcmOperationType, Zero, ASTR, BNC, DOT, GLMR, KSM, MANTA, MOVR, PHA, }; use bifrost_primitives::{CurrencyId, VtokenMintingOperator, XcmDestWeightAndFeeHandler}; -use frame_support::{ensure, traits::Len}; +use frame_support::{dispatch::GetDispatchInfo, ensure, traits::Len}; use orml_traits::{MultiCurrency, XcmTransfer}; use polkadot_parachain_primitives::primitives::Sibling; use sp_core::{Get, U256}; @@ -338,11 +337,10 @@ impl Pallet { } pub(crate) fn prepare_send_as_subaccount_call( - operation: XcmOperationType, call: Vec, who: &MultiLocation, currency_id: CurrencyId, - ) -> Result<(Vec, BalanceOf, XcmWeight), Error> { + ) -> Result, Error> { // Get the delegator sub-account index. let sub_account_index = DelegatorsMultilocation2Index::::get(currency_id, who) .ok_or(Error::::DelegatorNotExist)?; @@ -365,11 +363,7 @@ impl Pallet { call_as_subaccount.extend(sub_account_index.encode()); call_as_subaccount.extend(call); - let (weight, fee) = - T::XcmWeightAndFeeHandler::get_operation_weight_and_fee(currency_id, operation) - .ok_or(Error::::WeightAndFeeNotExists)?; - - Ok((call_as_subaccount, fee, weight)) + Ok(call_as_subaccount) } pub(crate) fn construct_xcm_as_subaccount_with_query_id( @@ -377,46 +371,60 @@ impl Pallet { call: Vec, who: &MultiLocation, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result<(QueryId, BlockNumberFor, BalanceOf, Xcm<()>), Error> { // prepare the query_id for reporting back transact status let now = frame_system::Pallet::::block_number(); let timeout = BlockNumberFor::::from(TIMEOUT_BLOCKS).saturating_add(now); - let query_id = Self::get_query_id(currency_id, &operation)?; + let (query_id, notify_call_weight) = + Self::get_query_id_and_notify_call_weight(currency_id, &operation)?; + + let (transact_weight, withdraw_fee) = match weight_and_fee { + Some((weight, fee)) => (weight, fee), + _ => T::XcmWeightAndFeeHandler::get_operation_weight_and_fee(currency_id, operation) + .ok_or(Error::::WeightAndFeeNotExists)?, + }; - let (call_as_subaccount, fee, weight) = - Self::prepare_send_as_subaccount_call(operation, call, who, currency_id)?; + let call_as_subaccount = Self::prepare_send_as_subaccount_call(call, who, currency_id)?; let xcm_message = Self::construct_xcm_message( call_as_subaccount, - fee, - weight, + withdraw_fee, + transact_weight, currency_id, Some(query_id), + Some(notify_call_weight), )?; - Ok((query_id, timeout, fee, xcm_message)) + Ok((query_id, timeout, withdraw_fee, xcm_message)) } - pub(crate) fn get_query_id( + pub(crate) fn get_query_id_and_notify_call_weight( currency_id: CurrencyId, operation: &XcmOperationType, - ) -> Result> { + ) -> Result<(QueryId, Weight), Error> { let now = frame_system::Pallet::::block_number(); let timeout = BlockNumberFor::::from(TIMEOUT_BLOCKS).saturating_add(now); let responder = Self::get_para_multilocation_by_currency_id(currency_id)?; - let callback_option = match (currency_id, operation) { + let (notify_call_weight, callback_option) = match (currency_id, operation) { (DOT, &XcmOperationType::Delegate) | (DOT, &XcmOperationType::Undelegate) | (KSM, &XcmOperationType::Delegate) | - (KSM, &XcmOperationType::Undelegate) => Some(Self::confirm_validators_by_delegator_call()), - _ => Some(Self::confirm_delegator_ledger_call()), + (KSM, &XcmOperationType::Undelegate) => { + let notify_call = Self::confirm_validators_by_delegator_call(); + (notify_call.get_dispatch_info().weight, Some(notify_call)) + }, + _ => { + let notify_call = Self::confirm_delegator_ledger_call(); + (notify_call.get_dispatch_info().weight, Some(notify_call)) + }, }; let query_id = T::SubstrateResponseManager::create_query_record(&responder, callback_option, timeout); - return Ok(query_id); + return Ok((query_id, notify_call_weight)); } pub(crate) fn construct_xcm_and_send_as_subaccount_without_query_id( @@ -424,17 +432,29 @@ impl Pallet { call: Vec, who: &MultiLocation, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result, Error> { - let (call_as_subaccount, fee, weight) = - Self::prepare_send_as_subaccount_call(operation, call, who, currency_id)?; + let (transact_weight, withdraw_fee) = match weight_and_fee { + Some((weight, fee)) => (weight, fee), + _ => T::XcmWeightAndFeeHandler::get_operation_weight_and_fee(currency_id, operation) + .ok_or(Error::::WeightAndFeeNotExists)?, + }; - let xcm_message = - Self::construct_xcm_message(call_as_subaccount, fee, weight, currency_id, None)?; + let call_as_subaccount = Self::prepare_send_as_subaccount_call(call, who, currency_id)?; + + let xcm_message = Self::construct_xcm_message( + call_as_subaccount, + withdraw_fee, + transact_weight, + currency_id, + None, + None, + )?; let dest = Self::get_para_multilocation_by_currency_id(currency_id)?; send_xcm::(dest, xcm_message).map_err(|_e| Error::::XcmFailure)?; - Ok(fee) + Ok(withdraw_fee) } pub(crate) fn get_report_transact_status_instruct( @@ -534,22 +554,29 @@ impl Pallet { pub(crate) fn construct_xcm_message( call: Vec, extra_fee: BalanceOf, - weight: XcmWeight, + transact_weight: Weight, currency_id: CurrencyId, query_id: Option, + notify_call_weight: Option, ) -> Result, Error> { let mut xcm_message = Self::inner_construct_xcm_message(currency_id, extra_fee)?; let transact = Transact { origin_kind: OriginKind::SovereignAccount, - require_weight_at_most: weight, + require_weight_at_most: transact_weight, call: call.into(), }; xcm_message.insert(2, transact); - if let Some(query_id) = query_id { - let report_transact_status_instruct = - Self::get_report_transact_status_instruct(query_id, weight, currency_id); - xcm_message.insert(3, report_transact_status_instruct); - } + match (query_id, notify_call_weight) { + (Some(query_id), Some(notify_call_weight)) => { + let report_transact_status_instruct = Self::get_report_transact_status_instruct( + query_id, + notify_call_weight, + currency_id, + ); + xcm_message.insert(3, report_transact_status_instruct); + }, + _ => {}, + }; Ok(Xcm(xcm_message)) } diff --git a/pallets/slp/src/agents/filecoin_agent/agent.rs b/pallets/slp/src/agents/filecoin_agent/agent.rs index 8d51632b0e..585a4a3246 100644 --- a/pallets/slp/src/agents/filecoin_agent/agent.rs +++ b/pallets/slp/src/agents/filecoin_agent/agent.rs @@ -79,6 +79,7 @@ impl amount: BalanceOf, _validator: &Option, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. ensure!(!DelegatorLedgers::::contains_key(currency_id, who), Error::::AlreadyBonded); @@ -116,6 +117,7 @@ impl amount: BalanceOf, _validator: &Option, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. let ledger = @@ -165,6 +167,7 @@ impl amount: BalanceOf, _validator: &Option, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. let ledger = @@ -211,6 +214,7 @@ impl &self, _who: &MultiLocation, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -222,6 +226,7 @@ impl _amount: Option>, _validator: &Option, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -233,6 +238,7 @@ impl who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { ensure!(targets.len() == 1, Error::::VectorTooLong); let worker = &targets[0]; @@ -273,6 +279,7 @@ impl who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. ensure!( @@ -317,9 +324,10 @@ impl who: &MultiLocation, targets: &Option>, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let targets = targets.as_ref().ok_or(Error::::ValidatorSetNotExist)?; - let query_id = Self::delegate(self, who, targets, currency_id)?; + let query_id = Self::delegate(self, who, targets, currency_id, weight_and_fee)?; Ok(query_id) } @@ -329,6 +337,7 @@ impl _validator: &MultiLocation, _when: &Option, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -340,11 +349,17 @@ impl _validator: &Option, _currency_id: CurrencyId, _amount: Option>, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } - fn chill(&self, _who: &MultiLocation, _currency_id: CurrencyId) -> Result> { + fn chill( + &self, + _who: &MultiLocation, + _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, + ) -> Result> { Err(Error::::Unsupported) } @@ -355,6 +370,7 @@ impl _to: &MultiLocation, _amount: BalanceOf, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result<(), Error> { Err(Error::::Unsupported) } @@ -392,6 +408,7 @@ impl _amount: BalanceOf, _currency_id: CurrencyId, _if_from_currency: bool, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } diff --git a/pallets/slp/src/agents/parachain_staking_agent/agent.rs b/pallets/slp/src/agents/parachain_staking_agent/agent.rs index 91465ccaae..b1cb100bb4 100644 --- a/pallets/slp/src/agents/parachain_staking_agent/agent.rs +++ b/pallets/slp/src/agents/parachain_staking_agent/agent.rs @@ -101,6 +101,7 @@ impl amount: BalanceOf, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // First check if the delegator exists. // If not, check if amount is greater than minimum delegator stake. Afterwards, create the @@ -288,6 +289,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -322,6 +324,7 @@ impl amount: BalanceOf, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if the amount exceeds the minimum requirement. let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; @@ -434,6 +437,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -467,6 +471,7 @@ impl amount: BalanceOf, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if the amount exceeds the minimum requirement. let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; @@ -596,6 +601,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -628,6 +634,7 @@ impl &self, who: &MultiLocation, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { ensure!(currency_id == BNC, Error::::Unsupported); @@ -697,6 +704,7 @@ impl _amount: Option>, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; let collator = (*validator).ok_or(Error::::ValidatorNotProvided)?; @@ -809,6 +817,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -841,6 +850,7 @@ impl _who: &MultiLocation, _targets: &Vec, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -851,6 +861,7 @@ impl who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let validator = targets.first().ok_or(Error::::ValidatorNotProvided)?; @@ -954,6 +965,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -986,6 +998,7 @@ impl who: &MultiLocation, _targets: &Option>, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { ensure!(currency_id == BNC, Error::::Unsupported); @@ -1038,6 +1051,7 @@ impl _validator: &MultiLocation, _when: &Option, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -1050,6 +1064,7 @@ impl validator: &Option, currency_id: CurrencyId, _amount: Option>, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is in the delegator set. let collator = (*validator).ok_or(Error::::ValidatorNotProvided)?; @@ -1283,6 +1298,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -1322,7 +1338,12 @@ impl } /// The same as unbondAll, leaving delegator set. - fn chill(&self, _who: &MultiLocation, _currency_id: CurrencyId) -> Result> { + fn chill( + &self, + _who: &MultiLocation, + _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, + ) -> Result> { Err(Error::::Unsupported) } @@ -1333,6 +1354,7 @@ impl to: &MultiLocation, amount: BalanceOf, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result<(), Error> { // Ensure amount is greater than zero. ensure!(!amount.is_zero(), Error::::AmountZero); @@ -1390,6 +1412,7 @@ impl call, from, currency_id, + weight_and_fee, )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the @@ -1438,6 +1461,7 @@ impl _amount: BalanceOf, _currency_id: CurrencyId, _if_from_currency: bool, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } diff --git a/pallets/slp/src/agents/phala_agent/agent.rs b/pallets/slp/src/agents/phala_agent/agent.rs index 4b059174f5..84cb5119a8 100644 --- a/pallets/slp/src/agents/phala_agent/agent.rs +++ b/pallets/slp/src/agents/phala_agent/agent.rs @@ -88,6 +88,7 @@ impl amount: BalanceOf, share_price: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it has already delegated a validator. let (pool_id, is_vault) = @@ -141,6 +142,7 @@ impl calls, who, currency_id, + weight_and_fee, )?; // Calculate how many shares we can get by the amount at current price @@ -178,8 +180,9 @@ impl amount: BalanceOf, share_price: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { - Self::bond(self, who, amount, share_price, currency_id) + Self::bond(self, who, amount, share_price, currency_id, weight_and_fee) } /// Decrease bonding amount to a delegator. In Phala context, it corresponds to `withdraw` @@ -191,6 +194,7 @@ impl amount: BalanceOf, share_price: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it has already delegated a validator. let (pool_id, active_shares, unlocking_shares, is_vault) = @@ -255,6 +259,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -279,6 +284,7 @@ impl &self, _who: &MultiLocation, _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } @@ -290,11 +296,12 @@ impl amount: Option>, share_price: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let amount = amount.ok_or(Error::::InvalidAmount)?; ensure!(amount > Zero::zero(), Error::::AmountZero); - Self::bond(self, who, amount, share_price, currency_id) + Self::bond(self, who, amount, share_price, currency_id, weight_and_fee) } /// Delegate to some validators. In Phala context, the passed in Multilocation @@ -305,6 +312,7 @@ impl who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is in the delegator set. ensure!( @@ -392,6 +400,7 @@ impl who: &MultiLocation, _targets: &Vec, currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it has already delegated a validator. DelegatorLedgers::::mutate( @@ -437,9 +446,10 @@ impl who: &MultiLocation, targets: &Option>, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let targets = targets.as_ref().ok_or(Error::::ValidatorNotProvided)?; - Self::delegate(self, who, &targets, currency_id) + Self::delegate(self, who, &targets, currency_id, weight_and_fee) } /// Corresponds to the `check_and_maybe_force_withdraw` funtion of PhalaVault pallet. @@ -451,6 +461,7 @@ impl _validator: &MultiLocation, _when: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it has already delegated a validator. let (pool_id, is_vault) = @@ -482,6 +493,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Send out the xcm message. @@ -500,6 +512,7 @@ impl _validator: &Option, currency_id: CurrencyId, amount: Option>, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if amount is provided. This amount will replace the unlocking_shares in ledger. let updated_amount = amount.ok_or(Error::::AmountNotProvided)?; @@ -537,7 +550,12 @@ impl } /// Not supported in Phala. - fn chill(&self, _who: &MultiLocation, _currency_id: CurrencyId) -> Result> { + fn chill( + &self, + _who: &MultiLocation, + _currency_id: CurrencyId, + _weight_and_fee: Option<(Weight, BalanceOf)>, + ) -> Result> { Err(Error::::Unsupported) } @@ -548,6 +566,7 @@ impl to: &MultiLocation, amount: BalanceOf, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result<(), Error> { // Ensure amount is greater than zero. ensure!(!amount.is_zero(), Error::::AmountZero); @@ -574,6 +593,7 @@ impl call.encode(), from, currency_id, + weight_and_fee, )?; Ok(()) @@ -613,6 +633,7 @@ impl amount: BalanceOf, currency_id: CurrencyId, if_from_currency: bool, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if delegator exists. ensure!( @@ -638,6 +659,7 @@ impl call.encode(), who, currency_id, + weight_and_fee, )?; // Send out the xcm message. diff --git a/pallets/slp/src/agents/polkadot_agent/agent.rs b/pallets/slp/src/agents/polkadot_agent/agent.rs index 61feb6455a..98ed5f1a07 100644 --- a/pallets/slp/src/agents/polkadot_agent/agent.rs +++ b/pallets/slp/src/agents/polkadot_agent/agent.rs @@ -87,6 +87,7 @@ impl amount: BalanceOf, _validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. ensure!(!DelegatorLedgers::::contains_key(currency_id, who), Error::::AlreadyBonded); @@ -124,6 +125,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Create a new delegator ledger @@ -162,6 +164,7 @@ impl amount: BalanceOf, _validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. let ledger = @@ -199,6 +202,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -225,6 +229,7 @@ impl amount: BalanceOf, _validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. let ledger = @@ -270,6 +275,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -294,6 +300,7 @@ impl &self, who: &MultiLocation, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Get the active amount of a delegator. let ledger = @@ -317,6 +324,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -346,6 +354,7 @@ impl amount: Option>, _validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let amount = amount.ok_or(Error::::AmountNone)?; // Check if it is bonded already. @@ -386,6 +395,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -411,6 +421,7 @@ impl who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. ensure!( @@ -452,6 +463,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a query record to the ValidatorsByDelegatorXcmUpdateQueue storage. @@ -476,6 +488,7 @@ impl who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is bonded already. ensure!( @@ -524,6 +537,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a query record to the ValidatorsByDelegatorXcmUpdateQueue storage. @@ -548,9 +562,10 @@ impl who: &MultiLocation, targets: &Option>, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { let targets = targets.as_ref().ok_or(Error::::ValidatorSetNotExist)?; - let query_id = Self::delegate(self, who, targets, currency_id)?; + let query_id = Self::delegate(self, who, targets, currency_id, weight_and_fee)?; Ok(query_id) } @@ -561,6 +576,7 @@ impl validator: &MultiLocation, when: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Get the validator account let validator_account = Pallet::::multilocation_to_account(validator)?; @@ -591,6 +607,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Both tokenpool increment and delegator ledger update need to be conducted by backend @@ -607,6 +624,7 @@ impl _validator: &Option, currency_id: CurrencyId, _amount: Option>, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { // Check if it is in the delegator set. ensure!( @@ -638,6 +656,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. @@ -660,7 +679,12 @@ impl /// Chill self. Cancel the identity of delegator in the Relay chain side. /// Unbonding all the active amount should be done before or after chill, /// so that we can collect back all the bonded amount. - fn chill(&self, who: &MultiLocation, currency_id: CurrencyId) -> Result> { + fn chill( + &self, + who: &MultiLocation, + currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, + ) -> Result> { // Check if it is in the delegator set. ensure!( DelegatorsMultilocation2Index::::contains_key(currency_id, who), @@ -682,6 +706,7 @@ impl call, who, currency_id, + weight_and_fee, )?; // Get active amount, if not zero, create an update entry. @@ -718,6 +743,7 @@ impl to: &MultiLocation, amount: BalanceOf, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result<(), Error> { // Ensure amount is greater than zero. ensure!(!amount.is_zero(), Error::::AmountZero); @@ -770,6 +796,7 @@ impl call, from, currency_id, + weight_and_fee, )?; Ok(()) @@ -807,6 +834,7 @@ impl _amount: BalanceOf, _currency_id: CurrencyId, _if_from_currency: bool, + _weight_and_fee: Option<(Weight, BalanceOf)>, ) -> Result> { Err(Error::::Unsupported) } diff --git a/pallets/slp/src/benchmarking.rs b/pallets/slp/src/benchmarking.rs index 1a91ba1182..1a2d0cd9f0 100644 --- a/pallets/slp/src/benchmarking.rs +++ b/pallets/slp/src/benchmarking.rs @@ -59,7 +59,7 @@ pub fn init_bond(origin: ::RuntimeOrigin) ) .unwrap(); - assert_ok!(Pallet::::bond(origin, KSM, Box::new(DELEGATOR1), 10u32.into(), None)); + assert_ok!(Pallet::::bond(origin, KSM, Box::new(DELEGATOR1), 10u32.into(), None, None)); } pub fn init_ongoing_time(origin: ::RuntimeOrigin) { @@ -117,6 +117,7 @@ mod benchmarks { Box::new(DELEGATOR1), 10u32.into(), None, + None, ); Ok(()) @@ -141,6 +142,7 @@ mod benchmarks { Box::new(DELEGATOR1), None, 10u32.into(), + None, ); Ok(()) @@ -166,6 +168,7 @@ mod benchmarks { Box::new(DELEGATOR1), None, 0u32.into(), + None, ); Ok(()) @@ -185,7 +188,7 @@ mod benchmarks { )?; #[extrinsic_call] - _(origin as ::RuntimeOrigin, KSM, Box::new(DELEGATOR1)); + _(origin as ::RuntimeOrigin, KSM, Box::new(DELEGATOR1), None); Ok(()) } @@ -210,6 +213,7 @@ mod benchmarks { Box::new(DELEGATOR1), None, Some(0u32.into()), + None, ); Ok(()) @@ -234,6 +238,7 @@ mod benchmarks { KSM, Box::new(DELEGATOR1), vec![DELEGATOR1], + None, ); Ok(()) @@ -262,6 +267,7 @@ mod benchmarks { KSM, Box::new(DELEGATOR1), vec![DELEGATOR1], + None, ); Ok(()) @@ -286,6 +292,7 @@ mod benchmarks { KSM, Box::new(DELEGATOR1), Some(vec![DELEGATOR1]), + None, ); Ok(()) @@ -310,6 +317,7 @@ mod benchmarks { Box::new(DELEGATOR1), Box::new(DELEGATOR1), Some(TimeUnit::Era(0)), + None, ); Ok(()) @@ -336,6 +344,7 @@ mod benchmarks { Some(TimeUnit::SlashingSpan(0)), None, None, + None, ); Ok(()) @@ -355,7 +364,7 @@ mod benchmarks { )?; #[extrinsic_call] - _(origin as ::RuntimeOrigin, KSM, Box::new(DELEGATOR1)); + _(origin as ::RuntimeOrigin, KSM, Box::new(DELEGATOR1), None); Ok(()) } @@ -384,6 +393,7 @@ mod benchmarks { Box::new(DELEGATOR1), Box::new(to), 10u32.into(), + None, ); Ok(()) @@ -451,6 +461,7 @@ mod benchmarks { Box::new(DELEGATOR1), 10u32.into(), true, + None, ); Ok(()) @@ -855,7 +866,8 @@ mod benchmarks { origin.clone() as ::RuntimeOrigin, KSM, Box::new(DELEGATOR1), - vec![DELEGATOR1] + vec![DELEGATOR1], + None )); ValidatorsByDelegatorXcmUpdateQueue::::insert( 1u64, @@ -893,7 +905,8 @@ mod benchmarks { origin.clone() as ::RuntimeOrigin, KSM, Box::new(DELEGATOR1), - vec![DELEGATOR1] + vec![DELEGATOR1], + None )); ValidatorsByDelegatorXcmUpdateQueue::::insert( 1u64, diff --git a/pallets/slp/src/lib.rs b/pallets/slp/src/lib.rs index 9fc1f20fb0..2763cede2c 100644 --- a/pallets/slp/src/lib.rs +++ b/pallets/slp/src/lib.rs @@ -54,7 +54,7 @@ use sp_std::{boxed::Box, vec, vec::Vec}; pub use weights::WeightInfo; use xcm::{ prelude::*, - v3::{Junction, Junctions, MultiLocation, Weight as XcmWeight, Xcm}, + v3::{Junction, Junctions, MultiLocation, Xcm}, }; mod agents; @@ -93,6 +93,7 @@ pub mod pallet { use super::*; use crate::agents::{AstarAgent, FilecoinAgent, ParachainStakingAgent, PhalaAgent}; use bifrost_primitives::{RedeemType, SlpxOperator}; + use frame_support::dispatch::GetDispatchInfo; use orml_traits::XcmTransfer; use pallet_xcm::ensure_response; use xcm::v3::{MaybeErrorCode, Response}; @@ -103,7 +104,7 @@ pub mod pallet { type RuntimeOrigin: IsType<::RuntimeOrigin> + Into::RuntimeOrigin>>; - type RuntimeCall: Parameter + From>; + type RuntimeCall: Parameter + From> + GetDispatchInfo; /// Currency operations handler type MultiCurrency: MultiCurrency, CurrencyId = CurrencyId>; @@ -713,12 +714,14 @@ pub mod pallet { who: Box, #[pallet::compact] amount: BalanceOf, validator: Option, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.bond(&who, amount, &validator, currency_id)?; + let query_id = + staking_agent.bond(&who, amount, &validator, currency_id, weight_and_fee)?; let query_id_hash = T::Hashing::hash(&query_id.encode()); // Deposit event. @@ -742,12 +745,14 @@ pub mod pallet { who: Box, validator: Option, #[pallet::compact] amount: BalanceOf, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.bond_extra(&who, amount, &validator, currency_id)?; + let query_id = + staking_agent.bond_extra(&who, amount, &validator, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -772,12 +777,14 @@ pub mod pallet { who: Box, validator: Option, #[pallet::compact] amount: BalanceOf, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.unbond(&who, amount, &validator, currency_id)?; + let query_id = + staking_agent.unbond(&who, amount, &validator, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -799,12 +806,13 @@ pub mod pallet { origin: OriginFor, currency_id: CurrencyId, who: Box, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.unbond_all(&who, currency_id)?; + let query_id = staking_agent.unbond_all(&who, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -826,12 +834,14 @@ pub mod pallet { who: Box, validator: Option, amount: Option>, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.rebond(&who, amount, &validator, currency_id)?; + let query_id = + staking_agent.rebond(&who, amount, &validator, currency_id, weight_and_fee)?; let query_id_hash = T::Hashing::hash(&query_id.encode()); // Deposit event. @@ -854,12 +864,13 @@ pub mod pallet { currency_id: CurrencyId, who: Box, targets: Vec, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.delegate(&who, &targets, currency_id)?; + let query_id = staking_agent.delegate(&who, &targets, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -881,12 +892,13 @@ pub mod pallet { currency_id: CurrencyId, who: Box, targets: Vec, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.undelegate(&who, &targets, currency_id)?; + let query_id = staking_agent.undelegate(&who, &targets, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -908,12 +920,13 @@ pub mod pallet { currency_id: CurrencyId, who: Box, targets: Option>, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.redelegate(&who, &targets, currency_id)?; + let query_id = staking_agent.redelegate(&who, &targets, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -936,12 +949,13 @@ pub mod pallet { who: Box, validator: Box, when: Option, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - staking_agent.payout(&who, &validator, &when, currency_id)?; + staking_agent.payout(&who, &validator, &when, currency_id, weight_and_fee)?; // Deposit event. Pallet::::deposit_event(Event::Payout { @@ -962,12 +976,20 @@ pub mod pallet { when: Option, validator: Option, amount: Option>, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.liquidize(&who, &when, &validator, currency_id, amount)?; + let query_id = staking_agent.liquidize( + &who, + &when, + &validator, + currency_id, + amount, + weight_and_fee, + )?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -989,12 +1011,13 @@ pub mod pallet { origin: OriginFor, currency_id: CurrencyId, who: Box, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - let query_id = staking_agent.chill(&who, currency_id)?; + let query_id = staking_agent.chill(&who, currency_id, weight_and_fee)?; let query_id_hash = ::Hashing::hash(&query_id.encode()); // Deposit event. @@ -1015,12 +1038,13 @@ pub mod pallet { from: Box, to: Box, #[pallet::compact] amount: BalanceOf, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - staking_agent.transfer_back(&from, &to, amount, currency_id)?; + staking_agent.transfer_back(&from, &to, amount, currency_id, weight_and_fee)?; // Deposit event. Pallet::::deposit_event(Event::TransferBack { @@ -1071,12 +1095,19 @@ pub mod pallet { who: Box, #[pallet::compact] amount: BalanceOf, if_from_currency: bool, + weight_and_fee: Option<(Weight, BalanceOf)>, ) -> DispatchResult { // Ensure origin Self::ensure_authorized(origin, currency_id)?; let staking_agent = Self::get_currency_staking_agent(currency_id)?; - staking_agent.convert_asset(&who, amount, currency_id, if_from_currency)?; + staking_agent.convert_asset( + &who, + amount, + currency_id, + if_from_currency, + weight_and_fee, + )?; // Deposit event. Pallet::::deposit_event(Event::ConvertAsset { currency_id, who: *who, amount }); diff --git a/pallets/slp/src/tests/filecoin_tests.rs b/pallets/slp/src/tests/filecoin_tests.rs index 934c25f64e..d7acc574b2 100644 --- a/pallets/slp/src/tests/filecoin_tests.rs +++ b/pallets/slp/src/tests/filecoin_tests.rs @@ -60,8 +60,13 @@ fn delegate_setup() { initialize_delegator_setup(); let _ = Slp::add_validator(RuntimeOrigin::signed(ALICE), FIL, Box::new(owner_location)); - let _ = - Slp::delegate(RuntimeOrigin::signed(ALICE), FIL, Box::new(location), vec![owner_location]); + let _ = Slp::delegate( + RuntimeOrigin::signed(ALICE), + FIL, + Box::new(location), + vec![owner_location], + None, + ); } fn bond_setup() { @@ -70,8 +75,14 @@ fn bond_setup() { delegate_setup(); - let _ = - Slp::bond(RuntimeOrigin::signed(ALICE), FIL, Box::new(location), 1_000_000_000_000, None); + let _ = Slp::bond( + RuntimeOrigin::signed(ALICE), + FIL, + Box::new(location), + 1_000_000_000_000, + None, + None, + ); } #[test] @@ -109,6 +120,7 @@ fn bond_should_work() { FIL, Box::new(location), 1_000_000_000_000, + None, None ), Error::::NotExist @@ -117,7 +129,7 @@ fn bond_should_work() { delegate_setup(); assert_noop!( - Slp::bond(RuntimeOrigin::signed(ALICE), FIL, Box::new(location), 1_000, None), + Slp::bond(RuntimeOrigin::signed(ALICE), FIL, Box::new(location), 1_000, None, None), Error::::LowerThanMinimum ); @@ -127,6 +139,7 @@ fn bond_should_work() { FIL, Box::new(location), 300_000_000_000_000, + None, None ), Error::::ExceedActiveMaximum @@ -137,6 +150,7 @@ fn bond_should_work() { FIL, Box::new(location), 1_000_000_000_000, + None, None )); @@ -151,6 +165,7 @@ fn bond_should_work() { FIL, Box::new(location), 1_000_000_000_000, + None, None ), Error::::AlreadyBonded @@ -180,7 +195,8 @@ fn delegate_should_work() { RuntimeOrigin::signed(ALICE), FIL, Box::new(location), - vec![owner_location] + vec![owner_location], + None )); assert_eq!( @@ -203,6 +219,7 @@ fn bond_extra_should_work() { Box::new(location), None, 1_000_000_000_000, + None ), Error::::DelegatorNotBonded ); @@ -215,6 +232,7 @@ fn bond_extra_should_work() { Box::new(location), None, 1_000_000_000_000, + None )); let fil_ledger = FilecoinLedger { account: location, initial_pledge: 2000000000000 }; @@ -237,6 +255,7 @@ fn unbond_should_work() { Box::new(location), None, 500_000_000_000, + None ), Error::::DelegatorNotBonded ); @@ -249,6 +268,7 @@ fn unbond_should_work() { Box::new(location), None, 500_000_000_000, + None )); let fil_ledger = FilecoinLedger { account: location, initial_pledge: 500000000000 }; @@ -275,7 +295,8 @@ fn undelegate_should_work() { RuntimeOrigin::signed(ALICE), FIL, Box::new(location), - vec![owner_location] + vec![owner_location], + None ), Error::::DelegatorNotBonded ); @@ -290,7 +311,8 @@ fn undelegate_should_work() { RuntimeOrigin::signed(ALICE), FIL, Box::new(location), - vec!(owner_location) + vec!(owner_location), + None ), Error::::AmountNotZero ); @@ -305,7 +327,8 @@ fn undelegate_should_work() { RuntimeOrigin::signed(ALICE), FIL, Box::new(location), - vec![other_location] + vec![other_location], + None ), Error::::ValidatorError ); @@ -314,7 +337,8 @@ fn undelegate_should_work() { RuntimeOrigin::signed(ALICE), FIL, Box::new(location), - vec![owner_location] + vec![owner_location], + None )); assert_eq!(ValidatorsByDelegator::::get(FIL, location), None); @@ -459,7 +483,8 @@ fn remove_validator_should_work() { RuntimeOrigin::signed(ALICE), FIL, Box::new(location), - vec![owner_location] + vec![owner_location], + None )); assert_ok!(Slp::remove_validator( diff --git a/pallets/slp/src/tests/kusama_tests.rs b/pallets/slp/src/tests/kusama_tests.rs index 837547b42b..c367ac9708 100644 --- a/pallets/slp/src/tests/kusama_tests.rs +++ b/pallets/slp/src/tests/kusama_tests.rs @@ -545,6 +545,7 @@ fn bond_works() { DOT, Box::new(SUBACCOUNT_0_LOCATION), 1_000_000_000_000, + None, None )); }); @@ -660,3 +661,92 @@ fn register_subaccount_index_0() { Some((20_000_000_000.into(), 10_000_000_000)), )); } + +#[test] +fn test_construct_xcm() { + ExtBuilder::default().build().execute_with(|| { + register_subaccount_index_0(); + + // construct_xcm_as_subaccount_with_query_id + let weight = Weight::from_parts(20000000000, 20000000000); + let (_, _, _, messsage) = + crate::Pallet::::construct_xcm_as_subaccount_with_query_id( + XcmOperationType::Bond, + sp_std::vec![], + &SUBACCOUNT_0_LOCATION, + DOT, + None, + ) + .unwrap(); + + assert_eq!( + messsage.0[1], + BuyExecution { + fees: MultiAsset { + id: Concrete(MultiLocation { parents: 0, interior: Here }), + fun: Fungible(10000000000) + }, + weight_limit: Unlimited + } + ); + assert_eq!( + messsage.0[2], + Transact { + origin_kind: OriginKind::SovereignAccount, + require_weight_at_most: weight, + call: [26, 1, 0, 0].to_vec().into() + } + ); + + let weight = Weight::from_parts(100, 100); + let (_, _, _, messsage) = + crate::Pallet::::construct_xcm_as_subaccount_with_query_id( + XcmOperationType::Bond, + sp_std::vec![], + &SUBACCOUNT_0_LOCATION, + DOT, + Some((weight, 100u32.into())), + ) + .unwrap(); + assert_eq!( + messsage.0[1], + BuyExecution { + fees: MultiAsset { + id: Concrete(MultiLocation { parents: 0, interior: Here }), + fun: Fungible(100) + }, + weight_limit: Unlimited + } + ); + assert_eq!( + messsage.0[2], + Transact { + origin_kind: OriginKind::SovereignAccount, + require_weight_at_most: weight, + call: [26, 1, 0, 0].to_vec().into() + } + ); + + // construct_xcm_and_send_as_subaccount_without_query_id + let fee = crate::Pallet::::construct_xcm_and_send_as_subaccount_without_query_id( + XcmOperationType::Bond, + sp_std::vec![], + &SUBACCOUNT_0_LOCATION, + DOT, + None, + ) + .unwrap(); + + assert_eq!(fee, BalanceOf::::from(10000000000u128)); + + let fee = crate::Pallet::::construct_xcm_and_send_as_subaccount_without_query_id( + XcmOperationType::Bond, + sp_std::vec![], + &SUBACCOUNT_0_LOCATION, + DOT, + Some((Weight::from_parts(100, 100), 100u32.into())), + ) + .unwrap(); + assert_eq!(fee, BalanceOf::::from(100u32)); + }); +} diff --git a/pallets/slp/src/tests/moonriver_tests.rs b/pallets/slp/src/tests/moonriver_tests.rs index c5695ad138..36503b5139 100644 --- a/pallets/slp/src/tests/moonriver_tests.rs +++ b/pallets/slp/src/tests/moonriver_tests.rs @@ -295,7 +295,8 @@ fn moonriver_bond_works() { MOVR, Box::new(subaccount_0_location), 5_000_000_000_000_000_000, - Some(VALIDATOR_0_LOCATION) + Some(VALIDATOR_0_LOCATION), + None ), Error::::XcmFailure ); @@ -349,6 +350,7 @@ fn moonriver_bond_extra_works() { Box::new(subaccount_0_location), Some(VALIDATOR_0_LOCATION), 5_000_000_000_000_000_000, + None ), Error::::XcmFailure ); @@ -402,6 +404,7 @@ fn moonriver_unbond_works() { Box::new(subaccount_0_location), Some(VALIDATOR_0_LOCATION), 2_000_000_000_000_000_000, + None ), Error::::XcmFailure ); @@ -464,6 +467,7 @@ fn moonriver_rebond_works() { MOVR, Box::new(subaccount_0_location), Some(VALIDATOR_0_LOCATION), + None, None ), Error::::XcmFailure @@ -518,6 +522,7 @@ fn moonriver_undelegate_works() { MOVR, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None ), Error::::XcmFailure ); @@ -583,6 +588,7 @@ fn moonriver_liquidize_works() { Box::new(subaccount_0_location), None, Some(VALIDATOR_0_LOCATION), + None, None ), Error::::RequestNotDue @@ -603,6 +609,7 @@ fn moonriver_liquidize_works() { Box::new(subaccount_0_location), None, Some(VALIDATOR_0_LOCATION), + None, None ), Error::::XcmFailure @@ -648,6 +655,7 @@ fn moonriver_liquidize_works() { Box::new(subaccount_0_location), None, Some(VALIDATOR_0_LOCATION), + None, None ), Error::::LeavingNotDue @@ -668,6 +676,7 @@ fn moonriver_liquidize_works() { Box::new(subaccount_0_location), None, Some(VALIDATOR_0_LOCATION), + None, None ), Error::::XcmFailure @@ -1436,6 +1445,7 @@ fn moonriver_transfer_back_works() { Box::new(subaccount_0_location), Box::new(exit_account_location), 5_000_000_000_000_000_000, + None ), Error::::XcmFailure ); diff --git a/pallets/slp/src/tests/parachain_staking_tests.rs b/pallets/slp/src/tests/parachain_staking_tests.rs index f6c91a4e76..2f92c92256 100644 --- a/pallets/slp/src/tests/parachain_staking_tests.rs +++ b/pallets/slp/src/tests/parachain_staking_tests.rs @@ -194,7 +194,8 @@ fn parachain_staking_bond_to_liquidize_works() { BNC, Box::new(subaccount_0_location), 5_000_000_000_000, - Some(validator_0_location) + Some(validator_0_location), + None )); assert_ok!(Slp::bond_extra( RuntimeOrigin::signed(ALICE), @@ -202,6 +203,7 @@ fn parachain_staking_bond_to_liquidize_works() { Box::new(subaccount_0_location), Some(validator_0_location), 5_000_000_000_000, + None )); assert_ok!(Slp::unbond( RuntimeOrigin::signed(ALICE), @@ -209,6 +211,7 @@ fn parachain_staking_bond_to_liquidize_works() { Box::new(subaccount_0_location), Some(validator_0_location), 2_000_000_000_000, + None )); let mut delegation_set: BTreeMap> = BTreeMap::new(); @@ -288,6 +291,7 @@ fn parachain_staking_bond_to_liquidize_works() { Box::new(subaccount_0_location), None, Some(validator_0_location), + None, None )); }); @@ -335,6 +339,7 @@ fn parachain_staking_bond_extra_works() { Box::new(subaccount_0_location), Some(validator_0_location), 5_000_000_000_000, + None ), Error::::Unexpected ); @@ -383,6 +388,7 @@ fn parachain_staking_unbond_works() { Box::new(subaccount_0_location), Some(validator_0_location), 2_000_000_000_000, + None ), Error::::Unexpected ); @@ -425,7 +431,12 @@ fn parachain_staking_unbond_all_works() { DelegatorLedgers::::insert(BNC, subaccount_0_location, ledger); assert_noop!( - Slp::unbond_all(RuntimeOrigin::signed(ALICE), BNC, Box::new(subaccount_0_location),), + Slp::unbond_all( + RuntimeOrigin::signed(ALICE), + BNC, + Box::new(subaccount_0_location), + None + ), Error::::Unexpected ); }); @@ -481,6 +492,7 @@ fn parachain_staking_rebond_works() { BNC, Box::new(subaccount_0_location), Some(validator_0_location), + None, None ), Error::::Unexpected @@ -533,6 +545,7 @@ fn parachain_staking_undelegate_works() { BNC, Box::new(subaccount_0_location), vec![validator_0_location], + None ), Error::::Unexpected ); @@ -588,6 +601,7 @@ fn parachain_staking_redelegate_works() { RuntimeOrigin::signed(ALICE), BNC, Box::new(subaccount_0_location), + None, None ), Error::::Unexpected @@ -648,6 +662,7 @@ fn parachain_staking_liquidize_works() { Box::new(subaccount_0_location), None, Some(validator_0_location), + None, None ), Error::::RequestNotDue @@ -668,6 +683,7 @@ fn parachain_staking_liquidize_works() { Box::new(subaccount_0_location), None, Some(validator_0_location), + None, None ), Error::::Unexpected @@ -697,7 +713,8 @@ fn parachain_staking_liquidize_works() { BNC, Box::new(subaccount_0_location), 5_000_000_000_000, - Some(validator_0_location) + Some(validator_0_location), + None ), Error::::AlreadyBonded ); @@ -723,6 +740,7 @@ fn parachain_staking_liquidize_works() { Box::new(subaccount_0_location), None, Some(validator_0_location), + None, None ), Error::::RequestNotDue @@ -743,6 +761,7 @@ fn parachain_staking_liquidize_works() { Box::new(subaccount_0_location), None, Some(validator_0_location), + None, None ), Error::::Unexpected @@ -783,6 +802,7 @@ fn parachain_staking_transfer_back_works() { Box::new(subaccount_0_location), Box::new(exit_account_location), 5_000_000_000_000, + None )); }); } diff --git a/pallets/slp/src/tests/phala_tests.rs b/pallets/slp/src/tests/phala_tests.rs index f703385d9f..5979ef9a91 100644 --- a/pallets/slp/src/tests/phala_tests.rs +++ b/pallets/slp/src/tests/phala_tests.rs @@ -184,6 +184,7 @@ fn phala_delegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None ), Error::::DelegatorNotExist ); @@ -196,6 +197,7 @@ fn phala_delegate_works() { PHA, Box::new(subaccount_0_location), vec![], + None ), Error::::VectorEmpty ); @@ -206,6 +208,7 @@ fn phala_delegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION_WRONG], + None ), Error::::ValidatorError ); @@ -216,6 +219,7 @@ fn phala_delegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None ), Error::::ValidatorSetNotExist ); @@ -232,6 +236,7 @@ fn phala_delegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None ), Error::::ValidatorNotExist ); @@ -247,6 +252,7 @@ fn phala_delegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None )); let new_ledger = PhalaLedger::> { @@ -393,6 +399,7 @@ fn phala_setup() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None )); } @@ -421,7 +428,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 1_000_000_000_000, - Some(share_price_multilocation) + Some(share_price_multilocation), + None ), Error::::DelegatorNotExist ); @@ -436,7 +444,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 1_000_000_000_000, - Some(share_price_multilocation) + Some(share_price_multilocation), + None ), Error::::DelegatorNotExist ); @@ -452,6 +461,7 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None )); phala_xcm_setup(); @@ -462,7 +472,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 100_000_000_000, - Some(share_price_multilocation) + Some(share_price_multilocation), + None ), Error::::LowerThanMinimum ); @@ -473,7 +484,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 2_000_000_000_000_000_000, - Some(share_price_multilocation) + Some(share_price_multilocation), + None ), Error::::ExceedActiveMaximum ); @@ -485,7 +497,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 1_000_000_000_000, - Some(subaccount_0_location) + Some(subaccount_0_location), + None ), Error::::SharePriceNotValid ); @@ -497,7 +510,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 1_000_000_000_000, - Some(VALIDATOR_0_LOCATION) + Some(VALIDATOR_0_LOCATION), + None ), Error::::DividedByZero ); @@ -508,7 +522,8 @@ fn phala_bond_works() { PHA, Box::new(subaccount_0_location), 1_000_000_000_000, - Some(share_price_multilocation) + Some(share_price_multilocation), + None ), Error::::XcmFailure ); @@ -538,6 +553,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(share_price_multilocation), 1_000_000, + None ), Error::::DelegatorNotExist ); @@ -565,6 +581,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(share_price_multilocation), 1_000_000, + None ), Error::::AlreadyRequested ); @@ -589,6 +606,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(share_price_multilocation), 0, + None ), Error::::AmountZero ); @@ -600,6 +618,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(subaccount_0_location), 1_000_000, + None ), Error::::SharePriceNotValid ); @@ -611,6 +630,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(VALIDATOR_0_LOCATION), 1_000_000, + None ), Error::::DividedByZero ); @@ -622,6 +642,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(share_price_multilocation), 1_000_000, + None ), Error::::LowerThanMinimum ); @@ -633,6 +654,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(share_price_multilocation), 1_000_000_000_000, + None ), Error::::NotEnoughToUnbond ); @@ -657,6 +679,7 @@ fn phala_unbond_works() { Box::new(subaccount_0_location), Some(share_price_multilocation), 1_000_000_000_000, + None ), Error::::XcmFailure ); @@ -691,6 +714,7 @@ fn phala_rebond_works() { PHA, Box::new(subaccount_0_location), Some(share_price_multilocation), + None, None ), Error::::InvalidAmount @@ -702,7 +726,8 @@ fn phala_rebond_works() { PHA, Box::new(subaccount_0_location), Some(share_price_multilocation), - Some(0) + Some(0), + None ), Error::::AmountZero ); @@ -713,7 +738,8 @@ fn phala_rebond_works() { PHA, Box::new(subaccount_0_location), Some(share_price_multilocation), - Some(1_000_000_000_000) + Some(1_000_000_000_000), + None ), Error::::XcmFailure ); @@ -758,6 +784,7 @@ fn phala_undelegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None ), Error::::ValidatorStillInUse ); @@ -781,6 +808,7 @@ fn phala_undelegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None ), Error::::ValidatorStillInUse ); @@ -803,6 +831,7 @@ fn phala_undelegate_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None )); let undelegated_ledger = PhalaLedger::> { @@ -859,6 +888,7 @@ fn phala_redelegate_works() { RuntimeOrigin::signed(ALICE), PHA, Box::new(subaccount_0_location), + None, None ), Error::::ValidatorNotProvided @@ -874,7 +904,8 @@ fn phala_redelegate_works() { RuntimeOrigin::signed(ALICE), PHA, Box::new(subaccount_0_location), - Some(vec![VALIDATOR_1_LOCATION]) + Some(vec![VALIDATOR_1_LOCATION]), + None )); let new_ledger = PhalaLedger::> { @@ -932,7 +963,8 @@ fn phala_liquidize_works() { Box::new(subaccount_0_location), None, None, - Some(2_000_000_000_000) + Some(2_000_000_000_000), + None ), Error::::InvalidAmount ); @@ -943,7 +975,8 @@ fn phala_liquidize_works() { Box::new(subaccount_0_location), None, None, - Some(500_000_000_000) + Some(500_000_000_000), + None )); let compared_ledger_1 = PhalaLedger::> { @@ -966,7 +999,8 @@ fn phala_liquidize_works() { Box::new(subaccount_0_location), None, None, - Some(Zero::zero()) + Some(Zero::zero()), + None )); let compared_ledger_2 = PhalaLedger::> { @@ -1176,6 +1210,7 @@ fn phala_transfer_back_works() { Box::new(subaccount_0_location), Box::new(exit_account_location), Zero::zero(), + None ), Error::::AmountZero ); @@ -1187,6 +1222,7 @@ fn phala_transfer_back_works() { Box::new(subaccount_0_location), Box::new(exit_account_location), 5_000_000_000_000_000_000, + None ), Error::::XcmFailure ); @@ -1496,6 +1532,7 @@ fn add_validator_and_remove_validator_works() { PHA, Box::new(subaccount_0_location), vec![VALIDATOR_0_LOCATION], + None )); assert_ok!(Slp::remove_validator( @@ -1533,7 +1570,8 @@ fn phala_convert_asset_works() { PHA, Box::new(subaccount_0_location), 1_000_000_000_000, - true + true, + None ), Error::::XcmFailure ); diff --git a/pallets/slp/src/traits.rs b/pallets/slp/src/traits.rs index 930a5ef4f1..d83775ba8c 100644 --- a/pallets/slp/src/traits.rs +++ b/pallets/slp/src/traits.rs @@ -20,6 +20,7 @@ use crate::{primitives::QueryId, Box, MultiLocation, TimeUnit}; use bifrost_primitives::CurrencyId; use sp_runtime::DispatchResult; use sp_std::vec::Vec; +use xcm::latest::Weight; /// Abstraction over a staking agent for a certain POS chain. pub trait StakingAgent< @@ -44,6 +45,7 @@ pub trait StakingAgent< amount: Balance, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Bond extra amount to a delegator. @@ -53,6 +55,7 @@ pub trait StakingAgent< amount: Balance, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Decrease the bonding amount of a delegator. @@ -62,10 +65,16 @@ pub trait StakingAgent< amount: Balance, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Unbonding all amount of a delegator. Differentiate from regular unbonding. - fn unbond_all(&self, who: &MultiLocation, currency_id: CurrencyId) -> Result; + fn unbond_all( + &self, + who: &MultiLocation, + currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, + ) -> Result; /// Cancel some unbonding amount. fn rebond( @@ -74,6 +83,7 @@ pub trait StakingAgent< amount: Option, validator: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Delegate to some validators. @@ -82,6 +92,7 @@ pub trait StakingAgent< who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Remove delegation relationship with some validators. @@ -90,6 +101,7 @@ pub trait StakingAgent< who: &MultiLocation, targets: &Vec, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Re-delegate existing delegation to a new validator set. @@ -98,6 +110,7 @@ pub trait StakingAgent< who: &MultiLocation, targets: &Option>, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Initiate payout for a certain delegator. @@ -107,6 +120,7 @@ pub trait StakingAgent< validator: &MultiLocation, when: &Option, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Withdraw the due payout into free balance. @@ -117,10 +131,16 @@ pub trait StakingAgent< validator: &Option, currency_id: CurrencyId, amount: Option, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Cancel the identity of delegator. - fn chill(&self, who: &MultiLocation, currency_id: CurrencyId) -> Result; + fn chill( + &self, + who: &MultiLocation, + currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, + ) -> Result; /// Make token transferred back to Bifrost chain account. fn transfer_back( @@ -129,6 +149,7 @@ pub trait StakingAgent< to: &MultiLocation, amount: Balance, currency_id: CurrencyId, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result<(), Error>; /// Make token from Bifrost chain account to the staking chain account. @@ -147,6 +168,7 @@ pub trait StakingAgent< amount: Balance, currency_id: CurrencyId, if_from_currency: bool, + weight_and_fee: Option<(Weight, Balance)>, ) -> Result; /// Tune the vtoken exchage rate.