Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make public is_passing and ReferendumStatus #12667

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,48 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}

/// Returns the referendum's current approval/support and TrackId.
/// Referendum must be ongoing.
pub fn current_referendum_threshold(
ref_index: ReferendumIndex,
) -> Result<(Perbill, Perbill, Option<T::BlockNumber>, TrackIdOf<T, I>), DispatchError> {
let info = ReferendumInfoFor::<T, I>::get(ref_index).ok_or(Error::<T, I>::BadReferendum)?;
match info {
ReferendumInfo::Ongoing(status) => {
let maybe_since = if let Some(d) = status.deciding { Some(d.since) } else { None };
let (current_approval, current_support) =
(status.tally.approval(status.track), status.tally.support(status.track));
Ok((current_approval, current_support, maybe_since, status.track))
},
_ => Err(Error::<T, I>::NotOngoing.into()),
}
}

/// Returns whether the referendum is passing.
/// Referendum must be ongoing and its track must exist.
pub fn is_referendum_passing(ref_index: ReferendumIndex) -> Result<bool, DispatchError> {
let info = ReferendumInfoFor::<T, I>::get(ref_index).ok_or(Error::<T, I>::BadReferendum)?;
match info {
ReferendumInfo::Ongoing(status) => {
let track = Self::track(status.track).ok_or(Error::<T, I>::NoTrack)?;
let elapsed = if let Some(deciding) = status.deciding {
frame_system::Pallet::<T>::block_number().saturating_sub(deciding.since)
} else {
Zero::zero()
};
Ok(Self::is_passing(
&status.tally,
elapsed,
track.decision_period,
&track.min_support,
&track.min_approval,
status.track,
))
},
_ => Err(Error::<T, I>::NotOngoing.into()),
}
}

// Enqueue a proposal from a referendum which has presumably passed.
fn schedule_enactment(
index: ReferendumIndex,
Expand Down
27 changes: 27 additions & 0 deletions frame/referenda/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ pub struct ReferendumStatus<
pub(crate) alarm: Option<(Moment, ScheduleAddress)>,
}

impl<
TrackId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
RuntimeOrigin: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
Moment: Parameter + Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone + EncodeLike,
Call: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
Balance: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
Tally: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
AccountId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
bkchr marked this conversation as resolved.
Show resolved Hide resolved
>
ReferendumStatus<TrackId, RuntimeOrigin, Moment, Call, Balance, Tally, AccountId, ScheduleAddress>
{
/// Return whether referendum status is deciding.
/// confirming => deciding so deciding does not preclude confirming
pub fn is_deciding(&self) -> bool {
self.deciding.is_some()
}
/// Return whether referendum status is confirming.
pub fn is_confirming(&self) -> bool {
if let Some(deciding_status) = &self.deciding {
deciding_status.confirming.is_some()
} else {
false
}
}
}

/// Info regarding a referendum, present or past.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum ReferendumInfo<
Expand Down