-
Notifications
You must be signed in to change notification settings - Fork 819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactors referenda pallet to use fungible traits #1785
Open
gpestana
wants to merge
13
commits into
master
Choose a base branch
from
gpestana/referenda-fungibles
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
41da34f
Refactors referenda pallet to use fungible traits
gpestana fe11b96
Adds the OnSlash handler back
gpestana 516631d
Add on slash handler back
gpestana 143c427
tests associated type for OnUnbalanced
gpestana 24840ed
Merge branch 'master' into gpestana/referenda-fungibles
gpestana 6493c28
fix
gpestana bbb1a04
updates treasury config
gpestana 4c2487d
Merge branch 'master' into gpestana/referenda-fungibles
gpestana fabee79
Merge branch 'master' into gpestana/referenda-fungibles
gpestana 2dfd8eb
updates runtimes
gpestana 440b9ff
Finishes FunOnUnbalanced implementation
gpestana 0352c9c
Merge branch 'master' into gpestana/referenda-fungibles
gpestana 14883bf
HoldReason compatible with instantiable pallets
gpestana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,12 +69,13 @@ use frame_support::{ | |
dispatch::DispatchResult, | ||
ensure, | ||
traits::{ | ||
fungible::{hold::Balanced as FnBalanced, MutateHold as FnMutateHold}, | ||
schedule::{ | ||
v3::{Anon as ScheduleAnon, Named as ScheduleNamed}, | ||
DispatchTime, | ||
}, | ||
Currency, LockIdentifier, OnUnbalanced, OriginTrait, PollStatus, Polling, QueryPreimage, | ||
ReservableCurrency, StorePreimage, VoteTally, | ||
tokens::{imbalance::OnUnbalanced, Precision}, | ||
LockIdentifier, OriginTrait, PollStatus, Polling, QueryPreimage, StorePreimage, VoteTally, | ||
}, | ||
BoundedVec, | ||
}; | ||
|
@@ -95,10 +96,10 @@ use self::branch::{BeginDecidingBranch, OneFewerDecidingBranch, ServiceBranch}; | |
pub use self::{ | ||
pallet::*, | ||
types::{ | ||
BalanceOf, BoundedCallOf, CallOf, Curve, DecidingStatus, DecidingStatusOf, Deposit, | ||
InsertSorted, NegativeImbalanceOf, PalletsOriginOf, ReferendumIndex, ReferendumInfo, | ||
ReferendumInfoOf, ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf, | ||
TrackIdOf, TrackInfo, TrackInfoOf, TracksInfo, VotesOf, | ||
BalanceOf, BoundedCallOf, CallOf, CreditOf, Curve, DecidingStatus, DecidingStatusOf, | ||
Deposit, InsertSorted, PalletsOriginOf, ReferendumIndex, ReferendumInfo, ReferendumInfoOf, | ||
ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf, TrackIdOf, TrackInfo, | ||
TrackInfoOf, TracksInfo, VotesOf, | ||
}, | ||
weights::WeightInfo, | ||
}; | ||
|
@@ -139,6 +140,7 @@ const ASSEMBLY_ID: LockIdentifier = *b"assembly"; | |
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
|
||
use super::*; | ||
use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg}; | ||
use frame_system::pallet_prelude::*; | ||
|
@@ -150,6 +152,13 @@ pub mod pallet { | |
#[pallet::storage_version(STORAGE_VERSION)] | ||
pub struct Pallet<T, I = ()>(_); | ||
|
||
/// A reason for this pallet placing a hold on funds. | ||
#[pallet::composite_enum] | ||
pub enum HoldReason<I: 'static = ()> { | ||
/// The funds are held as deposit to start the referendum's decision phase. | ||
Referendum, | ||
} | ||
|
||
#[pallet::config] | ||
pub trait Config<I: 'static = ()>: frame_system::Config + Sized { | ||
// System level stuff. | ||
|
@@ -158,10 +167,13 @@ pub mod pallet { | |
+ From<Call<Self, I>> | ||
+ IsType<<Self as frame_system::Config>::RuntimeCall> | ||
+ From<frame_system::Call<Self>>; | ||
|
||
type RuntimeEvent: From<Event<Self, I>> | ||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
|
||
/// Weight information for extrinsics in this pallet. | ||
type WeightInfo: WeightInfo; | ||
|
||
/// The Scheduler. | ||
type Scheduler: ScheduleAnon< | ||
BlockNumberFor<Self>, | ||
|
@@ -174,23 +186,34 @@ pub mod pallet { | |
PalletsOriginOf<Self>, | ||
Hasher = Self::Hashing, | ||
>; | ||
|
||
/// Currency type for this pallet. | ||
type Currency: ReservableCurrency<Self::AccountId>; | ||
type Currency: FnMutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason> | ||
+ FnBalanced<Self::AccountId>; | ||
|
||
/// The overarching runtime hold reason. | ||
type RuntimeHoldReason: From<HoldReason>; | ||
|
||
// Origins and unbalances. | ||
/// Origin from which proposals may be submitted. | ||
type SubmitOrigin: EnsureOriginWithArg< | ||
Self::RuntimeOrigin, | ||
PalletsOriginOf<Self>, | ||
Success = Self::AccountId, | ||
>; | ||
|
||
/// Origin from which any vote may be cancelled. | ||
type CancelOrigin: EnsureOrigin<Self::RuntimeOrigin>; | ||
|
||
/// Origin from which any vote may be killed. | ||
type KillOrigin: EnsureOrigin<Self::RuntimeOrigin>; | ||
|
||
/// Handler for the unbalanced reduction when slashing a preimage deposit. | ||
type Slash: OnUnbalanced<NegativeImbalanceOf<Self, I>>; | ||
type OnSlash: OnUnbalanced<CreditOf<Self, I>>; | ||
|
||
/// The counting type for votes. Usually just balance. | ||
type Votes: AtLeast32BitUnsigned + Copy + Parameter + Member + MaxEncodedLen; | ||
|
||
/// The tallying type. | ||
type Tally: VoteTally<Self::Votes, TrackIdOf<Self, I>> | ||
+ Clone | ||
|
@@ -536,7 +559,7 @@ pub mod pallet { | |
.take_decision_deposit() | ||
.map_err(|_| Error::<T, I>::Unfinished)? | ||
.ok_or(Error::<T, I>::NoDeposit)?; | ||
Self::refund_deposit(Some(deposit.clone())); | ||
Self::refund_deposit(Some(deposit.clone()))?; | ||
ReferendumInfoFor::<T, I>::insert(index, info); | ||
let e = Event::<T, I>::DecisionDepositRefunded { | ||
index, | ||
|
@@ -674,12 +697,12 @@ pub mod pallet { | |
.take_submission_deposit() | ||
.map_err(|_| Error::<T, I>::BadStatus)? | ||
.ok_or(Error::<T, I>::NoDeposit)?; | ||
Self::refund_deposit(Some(deposit.clone())); | ||
let released = Self::refund_deposit(Some(deposit.clone()))?; | ||
ReferendumInfoFor::<T, I>::insert(index, info); | ||
let e = Event::<T, I>::SubmissionDepositRefunded { | ||
index, | ||
who: deposit.who, | ||
amount: deposit.amount, | ||
amount: released, | ||
}; | ||
Self::deposit_event(e); | ||
Ok(()) | ||
|
@@ -1255,22 +1278,35 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> { | |
who: T::AccountId, | ||
amount: BalanceOf<T, I>, | ||
) -> Result<Deposit<T::AccountId, BalanceOf<T, I>>, DispatchError> { | ||
T::Currency::reserve(&who, amount)?; | ||
T::Currency::hold(&HoldReason::Referendum.into(), &who, amount)?; | ||
Ok(Deposit { who, amount }) | ||
} | ||
|
||
/// Return a deposit, if `Some`. | ||
fn refund_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T, I>>>) { | ||
fn refund_deposit( | ||
deposit: Option<Deposit<T::AccountId, BalanceOf<T, I>>>, | ||
) -> Result<BalanceOf<T, I>, DispatchError> { | ||
if let Some(Deposit { who, amount }) = deposit { | ||
T::Currency::unreserve(&who, amount); | ||
T::Currency::release(&HoldReason::Referendum.into(), &who, amount, Precision::Exact) | ||
} else { | ||
Ok(Zero::zero()) | ||
} | ||
} | ||
|
||
/// Slash a deposit, if `Some`. | ||
fn slash_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T, I>>>) { | ||
if let Some(Deposit { who, amount }) = deposit { | ||
T::Slash::on_unbalanced(T::Currency::slash_reserved(&who, amount).0); | ||
Self::deposit_event(Event::<T, I>::DepositSlashed { who, amount }); | ||
let (imbalance, non_slashed) = <T::Currency as FnBalanced<T::AccountId>>::slash( | ||
&HoldReason::Referendum.into(), | ||
&who, | ||
amount, | ||
); | ||
T::OnSlash::on_unbalanced(imbalance); | ||
|
||
Self::deposit_event(Event::<T, I>::DepositSlashed { | ||
who, | ||
amount: amount.saturating_sub(non_slashed), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its possible to first emit the event and use |
||
}); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.