Skip to content

Commit

Permalink
Fix slp (#1016)
Browse files Browse the repository at this point in the history
* Add v0.9.80_sudo

* Fix slp
  • Loading branch information
hqwangningbo committed Aug 17, 2023
1 parent 4b14ff7 commit fcdd1b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 354 deletions.
154 changes: 20 additions & 134 deletions pallets/slp/src/agents/moonbeam_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use node_primitives::{
};
use orml_traits::MultiCurrency;
use polkadot_parachain::primitives::Sibling;
use sp_arithmetic::Percent;
use sp_runtime::{
traits::{
AccountIdConversion, CheckedAdd, CheckedSub, Convert, Saturating, UniqueSaturatedInto, Zero,
Expand Down Expand Up @@ -204,9 +205,11 @@ impl<T: Config>
let delegation_count: u32 = mins_maxs.validators_back_maximum;

// Construct xcm message.
let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::Delegate(
let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::DelegateWithAutoCompound(
validator_account_id_20,
amount,
Percent::from_percent(100),
candidate_delegation_count,
candidate_delegation_count,
delegation_count,
));
Expand Down Expand Up @@ -413,52 +416,10 @@ impl<T: Config>
/// function.
fn unbond_all(
&self,
who: &MultiLocation,
currency_id: CurrencyId,
_who: &MultiLocation,
_currency_id: CurrencyId,
) -> Result<QueryId, Error<T>> {
// check if the delegator exists.
let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);

if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
// check if the delegator is in the state of leaving.
ensure!(ledger.status == OneToManyDelegatorStatus::Active, Error::<T>::AlreadyLeaving);
} else {
Err(Error::<T>::DelegatorNotExist)?;
}

// Construct xcm message.
let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ScheduleLeaveDelegators);

// 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) =
Self::construct_xcm_as_subaccount_with_query_id(
XcmOperation::Chill,
call,
who,
currency_id,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Self::burn_fee_from_source_account(fee, currency_id)?;

// Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue<T>.
Self::insert_delegator_ledger_update_entry(
who,
None,
MoonbeamLedgerUpdateOperation::LeaveDelegator,
Zero::zero(),
query_id,
timeout,
currency_id,
)?;

// Send out the xcm message.
let dest = Self::get_moonbeam_para_multilocation(currency_id)?;
send_xcm::<T::XcmRouter>(dest, xcm_message).map_err(|_e| Error::<T>::XcmFailure)?;

Ok(query_id)
Err(Error::<T>::Unsupported)
}

/// Cancel pending request
Expand Down Expand Up @@ -553,7 +514,6 @@ impl<T: Config>
targets: &Vec<MultiLocation>,
currency_id: CurrencyId,
) -> Result<QueryId, Error<T>> {
let mins_maxs = MinimumsAndMaximums::<T>::get(currency_id).ok_or(Error::<T>::NotExist)?;
let validator = targets.first().ok_or(Error::<T>::ValidatorNotProvided)?;

// First, check if the delegator exists.
Expand All @@ -566,18 +526,6 @@ impl<T: Config>
// Second, check the validators one by one to see if all exist.
ensure!(ledger.delegations.contains_key(validator), Error::<T>::ValidatorNotBonded);
ensure!(!ledger.request_briefs.contains_key(validator), Error::<T>::AlreadyRequested);
let unbond_amount = ledger.delegations.get(&validator).ok_or(Error::<T>::OverFlow)?;

// Check after undelegating all these validators, if the delegator still meets the
// requirement.
let active =
ledger.total.checked_sub(&ledger.less_total).ok_or(Error::<T>::UnderFlow)?;
let unbond_after_amount =
active.checked_sub(&unbond_amount).ok_or(Error::<T>::UnderFlow)?;
ensure!(
unbond_after_amount >= mins_maxs.delegator_bonded_minimum,
Error::<T>::LowerThanMinimum
);
} else {
Err(Error::<T>::DelegatorNotExist)?;
}
Expand Down Expand Up @@ -624,55 +572,11 @@ impl<T: Config>
/// Cancel leave delegator set.
fn redelegate(
&self,
who: &MultiLocation,
_who: &MultiLocation,
_targets: &Option<Vec<MultiLocation>>,
currency_id: CurrencyId,
_currency_id: CurrencyId,
) -> Result<QueryId, Error<T>> {
// first check if the delegator exists.
let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
if let Some(Ledger::Moonbeam(ledger)) = ledger_option {
// check if the delegator is in the state of leaving.
match ledger.status {
OneToManyDelegatorStatus::Leaving(_) => Ok(()),
_ => Err(Error::<T>::DelegatorNotLeaving),
}?;
} else {
Err(Error::<T>::DelegatorNotExist)?;
}
// do the cancellation.
// Construct xcm message.
let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::CancelLeaveDelegators);

// 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) =
Self::construct_xcm_as_subaccount_with_query_id(
XcmOperation::CancelLeave,
call,
who,
currency_id,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Self::burn_fee_from_source_account(fee, currency_id)?;

// Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue<T>.
Self::insert_delegator_ledger_update_entry(
who,
None,
MoonbeamLedgerUpdateOperation::CancelLeave,
Zero::zero(),
query_id,
timeout,
currency_id,
)?;

// Send out the xcm message.
let dest = Self::get_moonbeam_para_multilocation(currency_id)?;
send_xcm::<T::XcmRouter>(dest, xcm_message).map_err(|_e| Error::<T>::XcmFailure)?;

Ok(query_id)
Err(Error::<T>::Unsupported)
}

/// Initiate payout for a certain delegator.
Expand Down Expand Up @@ -700,7 +604,6 @@ impl<T: Config>
let mut leaving = false;
let now = T::VtokenMinting::get_ongoing_time_unit(currency_id)
.ok_or(Error::<T>::TimeUnitNotExist)?;
let mins_maxs = MinimumsAndMaximums::<T>::get(currency_id).ok_or(Error::<T>::NotExist)?;

let ledger_option = DelegatorLedgers::<T>::get(currency_id, who);
let mut due_amount = Zero::zero();
Expand All @@ -725,33 +628,19 @@ impl<T: Config>

// Construct xcm message.
let delegator_h160_account = Pallet::<T>::multilocation_to_h160_account(who)?;
let call;
let (query_id, timeout, fee, xcm_message) = if leaving {
call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ExecuteLeaveDelegators(
delegator_h160_account,
mins_maxs.validators_back_maximum,
));

Self::construct_xcm_as_subaccount_with_query_id(
XcmOperation::ExecuteLeave,
call.clone(),
who,
currency_id,
)
} else {
let validator_h160_account = Pallet::<T>::multilocation_to_h160_account(&collator)?;
call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ExecuteDelegationRequest(
delegator_h160_account,
validator_h160_account,
));
let validator_h160_account = Pallet::<T>::multilocation_to_h160_account(&collator)?;
let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ExecuteDelegationRequest(
delegator_h160_account,
validator_h160_account,
));

let (query_id, timeout, fee, xcm_message) =
Self::construct_xcm_as_subaccount_with_query_id(
XcmOperation::Liquidize,
call.clone(),
who,
currency_id,
)
}?;
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Expand Down Expand Up @@ -788,8 +677,8 @@ impl<T: Config>
}

/// The same as unbondAll, leaving delegator set.
fn chill(&self, who: &MultiLocation, currency_id: CurrencyId) -> Result<QueryId, Error<T>> {
Self::unbond_all(&self, who, currency_id)
fn chill(&self, _who: &MultiLocation, _currency_id: CurrencyId) -> Result<QueryId, Error<T>> {
Err(Error::<T>::Unsupported)
}

/// Make token transferred back to Bifrost chain account.
Expand Down Expand Up @@ -1071,11 +960,8 @@ impl<T: Config> MoonbeamAgent<T> {
XcmOperation::Bond |
XcmOperation::BondExtra |
XcmOperation::Unbond |
XcmOperation::Chill |
XcmOperation::Rebond |
XcmOperation::Undelegate |
XcmOperation::CancelLeave |
XcmOperation::ExecuteLeave |
XcmOperation::Liquidize => T::SubstrateResponseManager::create_query_record(
&responder,
Some(Pallet::<T>::confirm_delegator_ledger_call()),
Expand Down Expand Up @@ -1674,7 +1560,7 @@ impl<T: Config> MoonbeamAgent<T> {

fn get_report_transact_status_instruct(query_id: QueryId, max_weight: Weight) -> Instruction {
ReportTransactStatus(QueryResponseInfo {
destination: MultiLocation::from(X1(Parachain(u32::from(T::ParachainId::get())))),
destination: MultiLocation::new(1, X1(Parachain(u32::from(T::ParachainId::get())))),
query_id,
max_weight,
})
Expand Down
8 changes: 6 additions & 2 deletions pallets/slp/src/agents/moonbeam_agent/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{BalanceOf, Config};
use codec::{Decode, Encode};
use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
use sp_arithmetic::Percent;
use sp_core::H160;
use sp_runtime::traits::{IdentityLookup, StaticLookup};
use sp_std::{boxed::Box, vec::Vec};
Expand Down Expand Up @@ -56,12 +57,15 @@ pub enum MoonbeamUtilityCall<MoonbeamCall> {

#[derive(Encode, Decode, RuntimeDebug, Clone)]
pub enum MoonbeamParachainStakingCall<T: Config> {
#[codec(index = 17)]
Delegate(H160, BalanceOf<T>, u32, u32),
#[codec(index = 18)]
DelegateWithAutoCompound(H160, BalanceOf<T>, Percent, u32, u32, u32),
// schedule_revoke_delegation
#[codec(index = 19)]
ScheduleLeaveDelegators,
// execute_delegation_request
#[codec(index = 20)]
ExecuteLeaveDelegators(H160, u32),
// cancel_delegation_request
#[codec(index = 21)]
CancelLeaveDelegators,
#[codec(index = 22)]
Expand Down
2 changes: 1 addition & 1 deletion pallets/slp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ pub mod pallet {
if let Response::DispatchResult(MaybeErrorCode::Success) = response {
Self::get_ledger_update_agent_then_process(query_id, true)?;
} else {
Self::do_fail_validators_by_delegator_query_response(query_id)?;
Self::do_fail_delegator_ledger_query_response(query_id)?;
}
Ok(())
}
Expand Down
Loading

0 comments on commit fcdd1b4

Please sign in to comment.