paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

[feature request] Add voting hooks #14559

Open enthusiastmartin opened 1 year ago

enthusiastmartin commented 1 year ago

Is there an existing issue?

Experiencing problems? Have you tried our Stack Exchange first?

Motivation

We, in HydraDX, would need to execute additional custom logic when referendum vote is added or removed.

Currently, there does not seem to be a way where we can easily add a hook which would execute when a vote is added or removed.

Request

We would like to propose adding hooks into “old” democracy pallet as well as to conviction-voting pallet.

We can easily prepare a PR with such change, but i figured i would open an issue first to discuss if this is something that can be added and/or hear anything against it.

Solution

We would imagine having a trait similar to this one:

pub trait VotingHooks<AccountId, Balance> {
    fn on_vote(who: &AccountId, ref_index: ReferendumIndex, vote: AccountVote<Balance>) -> DispatchResult;
    fn on_remove_vote(who: &AccountId, ref_index: ReferendumIndex) -> DispatchResult;
}

// Pallet's Config

/// Hooks are actions that are executed on certain events - on_vote, on_remove_vote
type VotingHooks: VotingHooks<Self::AccountId, BalanceOf<Self>>;

The hooks would be executed in try_vote and try_remove_vote.

Perhaps , ReferendumIndex would need to be generic as well, to have only one trait used in both democracy pallet and conviction-voting pallet.

Regarding benchmarking and weights - special trait functions can be added to prepare worst case scenario.

Are you willing to help with this request?

Yes!