Skip to content

Commit

Permalink
Merge pull request #2931 from subspace/domains_events
Browse files Browse the repository at this point in the history
Domains: add additional staking events when nominated and unlocked
  • Loading branch information
vedhavyas authored Jul 19, 2024
2 parents 047f17a + 71e8c21 commit d749c5f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
38 changes: 19 additions & 19 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,20 @@ mod pallet {
operator_id: OperatorId,
domain_id: DomainId,
},
NominatedStakedUnlocked {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
unlocked_amount: BalanceOf<T>,
},
StorageFeeUnlocked {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
storage_fee: BalanceOf<T>,
},
OperatorNominated {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
amount: BalanceOf<T>,
},
DomainInstantiated {
domain_id: DomainId,
Expand All @@ -928,17 +939,13 @@ mod pallet {
OperatorDeregistered {
operator_id: OperatorId,
},
OperatorUnlocked {
operator_id: OperatorId,
},
WithdrewStake {
NominatorUnlocked {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
},
FundsUnlocked {
WithdrewStake {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
amount: BalanceOf<T>,
},
PreferredOperator {
operator_id: OperatorId,
Expand Down Expand Up @@ -1361,11 +1368,6 @@ mod pallet {
do_nominate_operator::<T>(operator_id, nominator_id.clone(), amount)
.map_err(Error::<T>::from)?;

Self::deposit_event(Event::OperatorNominated {
operator_id,
nominator_id,
});

Ok(())
}

Expand Down Expand Up @@ -1434,13 +1436,8 @@ mod pallet {
#[pallet::weight(T::WeightInfo::unlock_funds())]
pub fn unlock_funds(origin: OriginFor<T>, operator_id: OperatorId) -> DispatchResult {
let nominator_id = ensure_signed(origin)?;
let unlocked_funds = do_unlock_funds::<T>(operator_id, nominator_id.clone())
do_unlock_funds::<T>(operator_id, nominator_id.clone())
.map_err(crate::pallet::Error::<T>::from)?;
Self::deposit_event(Event::FundsUnlocked {
operator_id,
nominator_id,
amount: unlocked_funds,
});
Ok(())
}

Expand All @@ -1451,10 +1448,13 @@ mod pallet {
pub fn unlock_nominator(origin: OriginFor<T>, operator_id: OperatorId) -> DispatchResult {
let nominator = ensure_signed(origin)?;

do_unlock_nominator::<T>(operator_id, nominator)
do_unlock_nominator::<T>(operator_id, nominator.clone())
.map_err(crate::pallet::Error::<T>::from)?;

Self::deposit_event(Event::OperatorUnlocked { operator_id });
Self::deposit_event(Event::NominatorUnlocked {
operator_id,
nominator_id: nominator,
});

Ok(())
}
Expand Down
38 changes: 34 additions & 4 deletions crates/pallet-domains/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ pub(crate) fn do_nominate_operator<T: Config>(
.map_err(Error::BundleStorageFund)?;

hold_deposit::<T>(&nominator_id, operator_id, new_deposit.staking)?;
Pallet::<T>::deposit_event(Event::OperatorNominated {
operator_id,
nominator_id: nominator_id.clone(),
amount: new_deposit.staking,
});

// increment total deposit for operator pool within this epoch
operator.deposits_in_epoch = operator
Expand Down Expand Up @@ -910,7 +915,7 @@ pub(crate) fn do_withdraw_stake<T: Config>(
pub(crate) fn do_unlock_funds<T: Config>(
operator_id: OperatorId,
nominator_id: NominatorId<T>,
) -> Result<BalanceOf<T>, Error> {
) -> Result<(), Error> {
let operator = Operators::<T>::get(operator_id).ok_or(Error::UnknownOperator)?;
ensure!(
*operator.status::<T>(operator_id) == OperatorStatus::Registered,
Expand Down Expand Up @@ -966,6 +971,12 @@ pub(crate) fn do_unlock_funds<T: Config>(
)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::NominatedStakedUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
unlocked_amount: amount_to_unlock,
});

// Release storage fund
let storage_fund_hold_id = T::HoldIdentifier::storage_fund_withdrawal(operator_id);
T::Currency::release(
Expand All @@ -976,6 +987,12 @@ pub(crate) fn do_unlock_funds<T: Config>(
)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::StorageFeeUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
storage_fee: storage_fee_refund,
});

// if there are no withdrawals, then delete the storage as well
if withdrawal.withdrawals.is_empty() && withdrawal.withdrawal_in_shares.is_none() {
*maybe_withdrawal = None;
Expand All @@ -990,7 +1007,7 @@ pub(crate) fn do_unlock_funds<T: Config>(
});
}

Ok(amount_to_unlock)
Ok(())
})
}

Expand Down Expand Up @@ -1101,6 +1118,12 @@ pub(crate) fn do_unlock_nominator<T: Config>(
)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::NominatedStakedUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
unlocked_amount: total_amount_to_unlock,
});

total_stake = total_stake.saturating_sub(nominator_staked_amount);
total_shares = total_shares.saturating_sub(nominator_shares);

Expand All @@ -1120,8 +1143,15 @@ pub(crate) fn do_unlock_nominator<T: Config>(
.map_err(Error::BundleStorageFund)?;

// Release all storage fee that of the nominator.
T::Currency::release_all(&storage_fund_hold_id, &nominator_id, Precision::Exact)
.map_err(|_| Error::RemoveLock)?;
let storage_fee_refund =
T::Currency::release_all(&storage_fund_hold_id, &nominator_id, Precision::Exact)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::StorageFeeUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
storage_fee: storage_fee_refund,
});

// reduce total storage fee deposit with nominator total fee deposit
total_storage_fee_deposit =
Expand Down

0 comments on commit d749c5f

Please sign in to comment.