Skip to content

Conversation

moliholy
Copy link

@moliholy moliholy commented May 1, 2025

Overview

This pull request introduces two major features to the pallet-collator-staking module:

  • Locking Mechanism: Implements the ability to lock staked funds, preventing premature withdrawals and enhancing protocol security.
  • Autocompounding: Enables automatic reinvestment of staking rewards, allowing users to maximize their returns without manual intervention.

Features

1. Locking

  • Adds support for locking user stakes for a configurable duration.
  • Ensures that locked funds cannot be withdrawn or transferred until the lock period expires.
  • Includes logic for administrators/collators to set or update lock parameters.

2. Autocompounding

  • Rewards earned from staking are automatically restaked, provided a minimum stake threshold is met.
  • Users can opt-in or opt-out of autocompounding via extrinsics.
  • Includes state and event tracking for autocompound actions.

Implementation Notes

  • New storage items and events have been introduced to track locking status and autocompound preferences.
  • Extrinsics updated/added:
    • lock_stake
    • unlock_stake
    • enable_autocompound
    • disable_autocompound
  • Logic added to staking reward distribution to support autocompounding.

Migration

  • Migration code is included to safely update existing on-chain storage to the new format (if required).

@moliholy moliholy requested a review from metricaez May 1, 2025 15:35
@moliholy moliholy self-assigned this May 1, 2025
@moliholy moliholy changed the base branch from main to feat/optimized-rewrads May 1, 2025 15:42
Balance: Saturating + Copy,
{
/// Returns the total amount of locked balance that is not part of staking.
pub fn total(&self) -> Balance {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name is misleading as it is not the total locked balance as you perfectly explain on the comment.

Copy link
Author

@moliholy moliholy May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how'd you name it then? 🤔

I mean, I don't think a very verbose name is the solution. That's why I added the comment for.

pub type NextSystemOperation<T: Config> = StorageValue<_, OperationFor<T>, ValueQuery>;

#[pallet::storage]
pub type LockedBalances<T: Config> =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also a comment here could help to understand that this is the non staked locked balance

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 617cb00.

let bond =
T::Currency::balance_frozen(&FreezeReason::CandidacyBond.into(), &candidate);
let _ = T::Currency::thaw(&FreezeReason::CandidacyBond.into(), &candidate);
let _ = T::Currency::thaw(&FreezeReason::Releasing.into(), &candidate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please elaborate on why it is also needed also on the candidacy_bond migration when it is addressed on the release_queue migration

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because releasing the candidacy bond does not create an element in the release queue that gets inspected. That was actually a bug detected in the previous security audit which forced us to have a separate mechanism for handling the release of the candidacy bond, yet that amount gets locked under the Releasing reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants