Skip to content

Commit 8357581

Browse files
authored
Apply some new pallet macro conventions (#362)
* Tidy up oracle module. * Tokens and currencies module. * Apply convention to modules.
1 parent d64c99e commit 8357581

File tree

25 files changed

+1905
-1958
lines changed

25 files changed

+1905
-1958
lines changed

auction/src/lib.rs

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
#![allow(clippy::string_lit_as_bytes)]
1414
#![allow(clippy::unused_unit)]
1515

16+
use frame_support::pallet_prelude::*;
17+
use frame_system::{ensure_signed, pallet_prelude::*};
18+
use orml_traits::{Auction, AuctionHandler, AuctionInfo, Change};
19+
use sp_runtime::{
20+
traits::{AtLeast32BitUnsigned, Bounded, MaybeSerializeDeserialize, Member, One, Zero},
21+
DispatchError, DispatchResult,
22+
};
23+
1624
mod default_weight;
1725
mod mock;
1826
mod tests;
@@ -21,13 +29,7 @@ pub use module::*;
2129

2230
#[frame_support::pallet]
2331
pub mod module {
24-
use frame_support::pallet_prelude::*;
25-
use frame_system::{ensure_signed, pallet_prelude::*};
26-
use orml_traits::{Auction, AuctionHandler, AuctionInfo, Change};
27-
use sp_runtime::{
28-
traits::{AtLeast32BitUnsigned, Bounded, MaybeSerializeDeserialize, Member, One, Zero},
29-
DispatchError, DispatchResult,
30-
};
32+
use super::*;
3133

3234
pub trait WeightInfo {
3335
fn bid_collateral_auction() -> Weight;
@@ -77,20 +79,20 @@ pub mod module {
7779
Bid(T::AuctionId, T::AccountId, T::Balance),
7880
}
7981

82+
/// Stores on-going and future auctions. Closed auction are removed.
8083
#[pallet::storage]
8184
#[pallet::getter(fn auctions)]
82-
/// Stores on-going and future auctions. Closed auction are removed.
8385
pub type Auctions<T: Config> =
8486
StorageMap<_, Twox64Concat, T::AuctionId, AuctionInfo<T::AccountId, T::Balance, T::BlockNumber>, OptionQuery>;
8587

88+
/// Track the next auction ID.
8689
#[pallet::storage]
8790
#[pallet::getter(fn auctions_index)]
88-
/// Track the next auction ID.
8991
pub type AuctionsIndex<T: Config> = StorageValue<_, T::AuctionId, ValueQuery>;
9092

93+
/// Index auctions by end time.
9194
#[pallet::storage]
9295
#[pallet::getter(fn auction_end_time)]
93-
/// Index auctions by end time.
9496
pub type AuctionEndTime<T: Config> =
9597
StorageDoubleMap<_, Twox64Concat, T::BlockNumber, Blake2_128Concat, T::AuctionId, (), OptionQuery>;
9698

@@ -163,55 +165,55 @@ pub mod module {
163165
Ok(().into())
164166
}
165167
}
168+
}
166169

167-
impl<T: Config> Auction<T::AccountId, T::BlockNumber> for Pallet<T> {
168-
type AuctionId = T::AuctionId;
169-
type Balance = T::Balance;
170+
impl<T: Config> Auction<T::AccountId, T::BlockNumber> for Pallet<T> {
171+
type AuctionId = T::AuctionId;
172+
type Balance = T::Balance;
170173

171-
fn auction_info(id: Self::AuctionId) -> Option<AuctionInfo<T::AccountId, Self::Balance, T::BlockNumber>> {
172-
Self::auctions(id)
173-
}
174+
fn auction_info(id: Self::AuctionId) -> Option<AuctionInfo<T::AccountId, Self::Balance, T::BlockNumber>> {
175+
Self::auctions(id)
176+
}
174177

175-
fn update_auction(
176-
id: Self::AuctionId,
177-
info: AuctionInfo<T::AccountId, Self::Balance, T::BlockNumber>,
178-
) -> DispatchResult {
179-
let auction = Auctions::<T>::get(id).ok_or(Error::<T>::AuctionNotExist)?;
180-
if let Some(old_end) = auction.end {
181-
AuctionEndTime::<T>::remove(&old_end, id);
182-
}
183-
if let Some(new_end) = info.end {
184-
AuctionEndTime::<T>::insert(&new_end, id, ());
185-
}
186-
Auctions::<T>::insert(id, info);
187-
Ok(())
178+
fn update_auction(
179+
id: Self::AuctionId,
180+
info: AuctionInfo<T::AccountId, Self::Balance, T::BlockNumber>,
181+
) -> DispatchResult {
182+
let auction = Auctions::<T>::get(id).ok_or(Error::<T>::AuctionNotExist)?;
183+
if let Some(old_end) = auction.end {
184+
AuctionEndTime::<T>::remove(&old_end, id);
188185
}
186+
if let Some(new_end) = info.end {
187+
AuctionEndTime::<T>::insert(&new_end, id, ());
188+
}
189+
Auctions::<T>::insert(id, info);
190+
Ok(())
191+
}
189192

190-
fn new_auction(
191-
start: T::BlockNumber,
192-
end: Option<T::BlockNumber>,
193-
) -> sp_std::result::Result<Self::AuctionId, DispatchError> {
194-
let auction = AuctionInfo { bid: None, start, end };
195-
let auction_id =
196-
<AuctionsIndex<T>>::try_mutate(|n| -> sp_std::result::Result<Self::AuctionId, DispatchError> {
197-
let id = *n;
198-
ensure!(id != Self::AuctionId::max_value(), Error::<T>::NoAvailableAuctionId);
199-
*n += One::one();
200-
Ok(id)
201-
})?;
202-
Auctions::<T>::insert(auction_id, auction);
203-
if let Some(end_block) = end {
204-
AuctionEndTime::<T>::insert(&end_block, auction_id, ());
205-
}
206-
207-
Ok(auction_id)
193+
fn new_auction(
194+
start: T::BlockNumber,
195+
end: Option<T::BlockNumber>,
196+
) -> sp_std::result::Result<Self::AuctionId, DispatchError> {
197+
let auction = AuctionInfo { bid: None, start, end };
198+
let auction_id =
199+
<AuctionsIndex<T>>::try_mutate(|n| -> sp_std::result::Result<Self::AuctionId, DispatchError> {
200+
let id = *n;
201+
ensure!(id != Self::AuctionId::max_value(), Error::<T>::NoAvailableAuctionId);
202+
*n += One::one();
203+
Ok(id)
204+
})?;
205+
Auctions::<T>::insert(auction_id, auction);
206+
if let Some(end_block) = end {
207+
AuctionEndTime::<T>::insert(&end_block, auction_id, ());
208208
}
209209

210-
fn remove_auction(id: Self::AuctionId) {
211-
if let Some(auction) = Auctions::<T>::take(&id) {
212-
if let Some(end_block) = auction.end {
213-
AuctionEndTime::<T>::remove(end_block, id);
214-
}
210+
Ok(auction_id)
211+
}
212+
213+
fn remove_auction(id: Self::AuctionId) {
214+
if let Some(auction) = Auctions::<T>::take(&id) {
215+
if let Some(end_block) = auction.end {
216+
AuctionEndTime::<T>::remove(end_block, id);
215217
}
216218
}
217219
}

auction/src/mock.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use super::*;
66
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
77
use orml_traits::OnNewBidResult;
8-
use orml_traits::{AuctionHandler, Change};
98
use sp_core::H256;
109
use sp_runtime::{testing::Header, traits::IdentityLookup};
1110

auction/src/tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
#![cfg(test)]
44

55
use super::*;
6-
use frame_support::{assert_noop, assert_ok, traits::OnFinalize};
6+
use frame_support::{assert_noop, assert_ok};
77
use mock::*;
8-
use orml_traits::{Auction, AuctionInfo};
98

109
#[test]
1110
fn new_auction_should_work() {

authority/src/lib.rs

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,106 @@
1919
#![allow(clippy::borrowed_box)]
2020
#![allow(clippy::unused_unit)]
2121

22+
use frame_support::{
23+
dispatch::PostDispatchInfo,
24+
pallet_prelude::*,
25+
traits::{
26+
schedule::{DispatchTime, Named as ScheduleNamed, Priority},
27+
EnsureOrigin, Get, IsType, OriginTrait,
28+
},
29+
weights::GetDispatchInfo,
30+
};
31+
use frame_system::pallet_prelude::*;
32+
use sp_runtime::{
33+
traits::{CheckedSub, Dispatchable, Saturating},
34+
DispatchError, DispatchResult, RuntimeDebug,
35+
};
36+
use sp_std::prelude::*;
37+
2238
mod default_weight;
2339
mod mock;
2440
mod tests;
2541

42+
/// A delayed origin. Can only be dispatched via `dispatch_as` with a delay.
43+
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode)]
44+
pub struct DelayedOrigin<BlockNumber, PalletsOrigin> {
45+
/// Number of blocks that this call have been delayed.
46+
pub delay: BlockNumber,
47+
/// The initial origin.
48+
pub origin: Box<PalletsOrigin>,
49+
}
50+
51+
/// Ensure the origin have a minimum amount of delay.
52+
pub struct EnsureDelayed<Delay, Inner, BlockNumber, PalletsOrigin>(
53+
sp_std::marker::PhantomData<(Delay, Inner, BlockNumber, PalletsOrigin)>,
54+
);
55+
impl<
56+
PalletsOrigin: Into<O>,
57+
O: Into<Result<DelayedOrigin<BlockNumber, PalletsOrigin>, O>> + From<DelayedOrigin<BlockNumber, PalletsOrigin>>,
58+
Delay: Get<BlockNumber>,
59+
Inner: EnsureOrigin<O>,
60+
BlockNumber: PartialOrd,
61+
> EnsureOrigin<O> for EnsureDelayed<Delay, Inner, BlockNumber, PalletsOrigin>
62+
{
63+
type Success = Inner::Success;
64+
65+
fn try_origin(o: O) -> Result<Self::Success, O> {
66+
o.into().and_then(|delayed_origin| {
67+
if delayed_origin.delay >= Delay::get() {
68+
let pallets_origin = *delayed_origin.origin;
69+
Inner::try_origin(pallets_origin.into())
70+
} else {
71+
Err(delayed_origin.into())
72+
}
73+
})
74+
}
75+
76+
#[cfg(feature = "runtime-benchmarks")]
77+
fn successful_origin() -> O {
78+
unimplemented!()
79+
}
80+
}
81+
82+
/// Config for orml-authority
83+
pub trait AuthorityConfig<Origin, PalletsOrigin, BlockNumber> {
84+
/// Check if the `origin` is allowed to schedule a dispatchable call
85+
/// with a given `priority`.
86+
fn check_schedule_dispatch(origin: Origin, priority: Priority) -> DispatchResult;
87+
/// Check if the `origin` is allow to fast track a scheduled task that
88+
/// initially created by `initial_origin`. `new_delay` is number of
89+
/// blocks this dispatchable will be dispatched from now after fast
90+
/// track.
91+
fn check_fast_track_schedule(
92+
origin: Origin,
93+
initial_origin: &PalletsOrigin,
94+
new_delay: BlockNumber,
95+
) -> DispatchResult;
96+
/// Check if the `origin` is allow to delay a scheduled task that
97+
/// initially created by `inital_origin`.
98+
fn check_delay_schedule(origin: Origin, initial_origin: &PalletsOrigin) -> DispatchResult;
99+
/// Check if the `origin` is allow to cancel a scheduled task that
100+
/// initially created by `inital_origin`.
101+
fn check_cancel_schedule(origin: Origin, initial_origin: &PalletsOrigin) -> DispatchResult;
102+
}
103+
104+
/// Represent an origin that can be dispatched by other origins with
105+
/// permission check.
106+
pub trait AsOriginId<Origin, PalletsOrigin> {
107+
/// Convert into `PalletsOrigin`
108+
fn into_origin(self) -> PalletsOrigin;
109+
/// Check if the `origin` is allow to dispatch call on behalf of this
110+
/// origin.
111+
fn check_dispatch_from(&self, origin: Origin) -> DispatchResult;
112+
}
113+
114+
/// The schedule task index type.
115+
pub type ScheduleTaskIndex = u32;
116+
26117
pub use module::*;
27118

28119
#[frame_support::pallet]
29120
pub mod module {
30-
use frame_support::{
31-
dispatch::PostDispatchInfo,
32-
pallet_prelude::*,
33-
traits::{
34-
schedule::{DispatchTime, Named as ScheduleNamed, Priority},
35-
EnsureOrigin, Get, IsType, OriginTrait,
36-
},
37-
weights::GetDispatchInfo,
38-
};
39-
use frame_system::pallet_prelude::*;
40-
use sp_runtime::{
41-
traits::{CheckedSub, Dispatchable, Saturating},
42-
DispatchError, DispatchResult, RuntimeDebug,
43-
};
44-
use sp_std::prelude::*;
121+
use super::*;
45122

46123
pub trait WeightInfo {
47124
fn dispatch_as() -> Weight;
@@ -52,86 +129,9 @@ pub mod module {
52129
fn cancel_scheduled_dispatch() -> Weight;
53130
}
54131

55-
/// A delayed origin. Can only be dispatched via `dispatch_as` with a delay.
56-
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode)]
57-
pub struct DelayedOrigin<BlockNumber, PalletsOrigin> {
58-
/// Number of blocks that this call have been delayed.
59-
pub delay: BlockNumber,
60-
/// The initial origin.
61-
pub origin: Box<PalletsOrigin>,
62-
}
63-
64-
/// Ensure the origin have a minimum amount of delay.
65-
pub struct EnsureDelayed<Delay, Inner, BlockNumber, PalletsOrigin>(
66-
sp_std::marker::PhantomData<(Delay, Inner, BlockNumber, PalletsOrigin)>,
67-
);
68-
impl<
69-
PalletsOrigin: Into<O>,
70-
O: Into<Result<DelayedOrigin<BlockNumber, PalletsOrigin>, O>>
71-
+ From<DelayedOrigin<BlockNumber, PalletsOrigin>>,
72-
Delay: Get<BlockNumber>,
73-
Inner: EnsureOrigin<O>,
74-
BlockNumber: PartialOrd,
75-
> EnsureOrigin<O> for EnsureDelayed<Delay, Inner, BlockNumber, PalletsOrigin>
76-
{
77-
type Success = Inner::Success;
78-
79-
fn try_origin(o: O) -> Result<Self::Success, O> {
80-
o.into().and_then(|delayed_origin| {
81-
if delayed_origin.delay >= Delay::get() {
82-
let pallets_origin = *delayed_origin.origin;
83-
Inner::try_origin(pallets_origin.into())
84-
} else {
85-
Err(delayed_origin.into())
86-
}
87-
})
88-
}
89-
90-
#[cfg(feature = "runtime-benchmarks")]
91-
fn successful_origin() -> O {
92-
unimplemented!()
93-
}
94-
}
95-
96132
/// Origin for the authority module.
97133
pub type Origin<T> = DelayedOrigin<<T as frame_system::Config>::BlockNumber, <T as Config>::PalletsOrigin>;
98-
99-
/// Config for orml-authority
100-
pub trait AuthorityConfig<Origin, PalletsOrigin, BlockNumber> {
101-
/// Check if the `origin` is allowed to schedule a dispatchable call
102-
/// with a given `priority`.
103-
fn check_schedule_dispatch(origin: Origin, priority: Priority) -> DispatchResult;
104-
/// Check if the `origin` is allow to fast track a scheduled task that
105-
/// initially created by `initial_origin`. `new_delay` is number of
106-
/// blocks this dispatchable will be dispatched from now after fast
107-
/// track.
108-
fn check_fast_track_schedule(
109-
origin: Origin,
110-
initial_origin: &PalletsOrigin,
111-
new_delay: BlockNumber,
112-
) -> DispatchResult;
113-
/// Check if the `origin` is allow to delay a scheduled task that
114-
/// initially created by `inital_origin`.
115-
fn check_delay_schedule(origin: Origin, initial_origin: &PalletsOrigin) -> DispatchResult;
116-
/// Check if the `origin` is allow to cancel a scheduled task that
117-
/// initially created by `inital_origin`.
118-
fn check_cancel_schedule(origin: Origin, initial_origin: &PalletsOrigin) -> DispatchResult;
119-
}
120-
121-
/// Represent an origin that can be dispatched by other origins with
122-
/// permission check.
123-
pub trait AsOriginId<Origin, PalletsOrigin> {
124-
/// Convert into `PalletsOrigin`
125-
fn into_origin(self) -> PalletsOrigin;
126-
/// Check if the `origin` is allow to dispatch call on behalf of this
127-
/// origin.
128-
fn check_dispatch_from(&self, origin: Origin) -> DispatchResult;
129-
}
130-
131-
type CallOf<T> = <T as Config>::Call;
132-
133-
/// The schedule task index type.
134-
pub type ScheduleTaskIndex = u32;
134+
pub(crate) type CallOf<T> = <T as Config>::Call;
135135

136136
#[pallet::config]
137137
pub trait Config: frame_system::Config {

authority/src/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use super::*;
66
use codec::{Decode, Encode};
77
use frame_support::{
88
parameter_types,
9-
traits::{schedule::Priority, OnFinalize, OnInitialize, OriginTrait},
9+
traits::{OnFinalize, OnInitialize},
1010
weights::Weight,
1111
};
1212
use frame_system::{ensure_root, ensure_signed, EnsureRoot};
1313
use sp_core::H256;
1414
use sp_runtime::{
1515
testing::Header,
1616
traits::{BadOrigin, IdentityLookup},
17-
DispatchResult, Perbill,
17+
Perbill,
1818
};
1919

2020
pub use crate as authority;

0 commit comments

Comments
 (0)