#![cfg_attr(not(feature = "std"), no_std)]
mod benchmarking;
mod tests;
pub mod weights;
extern crate alloc;
use alloc::{boxed::Box, vec};
use frame::{
prelude::*,
traits::{Currency, InstanceFilter, ReservableCurrency},
};
pub use pallet::*;
pub use weights::WeightInfo;
type CallHashOf<T> = <<T as Config>::CallHasher as Hash>::Output;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type BlockNumberFor<T> =
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
Copy,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
MaxEncodedLen,
TypeInfo,
)]
pub struct ProxyDefinition<AccountId, ProxyType, BlockNumber> {
pub delegate: AccountId,
pub proxy_type: ProxyType,
pub delay: BlockNumber,
}
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
Copy,
Eq,
PartialEq,
Debug,
MaxEncodedLen,
TypeInfo,
)]
pub struct Announcement<AccountId, Hash, BlockNumber> {
real: AccountId,
call_hash: Hash,
height: BlockNumber,
}
#[derive(
Encode,
Decode,
Clone,
Copy,
Eq,
PartialEq,
Debug,
MaxEncodedLen,
TypeInfo,
DecodeWithMemTracking,
)]
pub enum DepositKind {
Proxies,
Announcements,
}
#[frame::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type RuntimeCall: Parameter
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ GetDispatchInfo
+ From<frame_system::Call<Self>>
+ IsSubType<Call<Self>>
+ IsType<<Self as frame_system::Config>::RuntimeCall>;
type Currency: ReservableCurrency<Self::AccountId>;
type ProxyType: Parameter
+ Member
+ Ord
+ PartialOrd
+ frame::traits::InstanceFilter<<Self as Config>::RuntimeCall>
+ Default
+ MaxEncodedLen;
#[pallet::constant]
type ProxyDepositBase: Get<BalanceOf<Self>>;
#[pallet::constant]
type ProxyDepositFactor: Get<BalanceOf<Self>>;
#[pallet::constant]
type MaxProxies: Get<u32>;
type WeightInfo: WeightInfo;
#[pallet::constant]
type MaxPending: Get<u32>;
type CallHasher: Hash;
#[pallet::constant]
type AnnouncementDepositBase: Get<BalanceOf<Self>>;
#[pallet::constant]
type AnnouncementDepositFactor: Get<BalanceOf<Self>>;
type BlockNumberProvider: BlockNumberProvider;
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight({
let di = call.get_dispatch_info();
(T::WeightInfo::proxy(T::MaxProxies::get())
// AccountData for inner call origin accountdata.
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
.saturating_add(di.call_weight),
di.class)
})]
pub fn proxy(
origin: OriginFor<T>,
real: AccountIdLookupOf<T>,
force_proxy_type: Option<T::ProxyType>,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let real = T::Lookup::lookup(real)?;
let def = Self::find_proxy(&real, &who, force_proxy_type)?;
ensure!(def.delay.is_zero(), Error::<T>::Unannounced);
Self::do_proxy(def, real, *call);
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::add_proxy(T::MaxProxies::get()))]
pub fn add_proxy(
origin: OriginFor<T>,
delegate: AccountIdLookupOf<T>,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let delegate = T::Lookup::lookup(delegate)?;
Self::add_proxy_delegate(&who, delegate, proxy_type, delay)
}
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::remove_proxy(T::MaxProxies::get()))]
pub fn remove_proxy(
origin: OriginFor<T>,
delegate: AccountIdLookupOf<T>,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let delegate = T::Lookup::lookup(delegate)?;
Self::remove_proxy_delegate(&who, delegate, proxy_type, delay)
}
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::remove_proxies(T::MaxProxies::get()))]
pub fn remove_proxies(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::remove_all_proxy_delegates(&who);
Ok(())
}
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::create_pure(T::MaxProxies::get()))]
pub fn create_pure(
origin: OriginFor<T>,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
index: u16,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let pure = Self::pure_account(&who, &proxy_type, index, None);
ensure!(!Proxies::<T>::contains_key(&pure), Error::<T>::Duplicate);
let proxy_def =
ProxyDefinition { delegate: who.clone(), proxy_type: proxy_type.clone(), delay };
let bounded_proxies: BoundedVec<_, T::MaxProxies> =
vec![proxy_def].try_into().map_err(|_| Error::<T>::TooMany)?;
let deposit = T::ProxyDepositBase::get() + T::ProxyDepositFactor::get();
T::Currency::reserve(&who, deposit)?;
Proxies::<T>::insert(&pure, (bounded_proxies, deposit));
let extrinsic_index = <frame_system::Pallet<T>>::extrinsic_index().unwrap_or_default();
Self::deposit_event(Event::PureCreated {
pure,
who,
proxy_type,
disambiguation_index: index,
at: T::BlockNumberProvider::current_block_number(),
extrinsic_index,
});
Ok(())
}
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::kill_pure(T::MaxProxies::get()))]
pub fn kill_pure(
origin: OriginFor<T>,
spawner: AccountIdLookupOf<T>,
proxy_type: T::ProxyType,
index: u16,
#[pallet::compact] height: BlockNumberFor<T>,
#[pallet::compact] ext_index: u32,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let spawner = T::Lookup::lookup(spawner)?;
let when = (height, ext_index);
let proxy = Self::pure_account(&spawner, &proxy_type, index, Some(when));
ensure!(proxy == who, Error::<T>::NoPermission);
let (_, deposit) = Proxies::<T>::take(&who);
T::Currency::unreserve(&spawner, deposit);
Self::deposit_event(Event::PureKilled {
pure: who,
spawner,
proxy_type,
disambiguation_index: index,
});
Ok(())
}
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::announce(T::MaxPending::get(), T::MaxProxies::get()))]
pub fn announce(
origin: OriginFor<T>,
real: AccountIdLookupOf<T>,
call_hash: CallHashOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let real = T::Lookup::lookup(real)?;
Proxies::<T>::get(&real)
.0
.into_iter()
.find(|x| x.delegate == who)
.ok_or(Error::<T>::NotProxy)?;
let announcement = Announcement {
real: real.clone(),
call_hash,
height: T::BlockNumberProvider::current_block_number(),
};
Announcements::<T>::try_mutate(&who, |(ref mut pending, ref mut deposit)| {
pending.try_push(announcement).map_err(|_| Error::<T>::TooMany)?;
Self::rejig_deposit(
&who,
*deposit,
T::AnnouncementDepositBase::get(),
T::AnnouncementDepositFactor::get(),
pending.len(),
)
.map(|d| {
d.expect("Just pushed; pending.len() > 0; rejig_deposit returns Some; qed")
})
.map(|d| *deposit = d)
})?;
Self::deposit_event(Event::Announced { real, proxy: who, call_hash });
Ok(())
}
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::remove_announcement(
T::MaxPending::get(),
T::MaxProxies::get()
))]
pub fn remove_announcement(
origin: OriginFor<T>,
real: AccountIdLookupOf<T>,
call_hash: CallHashOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let real = T::Lookup::lookup(real)?;
Self::edit_announcements(&who, |ann| ann.real != real || ann.call_hash != call_hash)?;
Ok(())
}
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::reject_announcement(
T::MaxPending::get(),
T::MaxProxies::get()
))]
pub fn reject_announcement(
origin: OriginFor<T>,
delegate: AccountIdLookupOf<T>,
call_hash: CallHashOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let delegate = T::Lookup::lookup(delegate)?;
Self::edit_announcements(&delegate, |ann| {
ann.real != who || ann.call_hash != call_hash
})?;
Ok(())
}
#[pallet::call_index(9)]
#[pallet::weight({
let di = call.get_dispatch_info();
(T::WeightInfo::proxy_announced(T::MaxPending::get(), T::MaxProxies::get())
// AccountData for inner call origin accountdata.
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
.saturating_add(di.call_weight),
di.class)
})]
pub fn proxy_announced(
origin: OriginFor<T>,
delegate: AccountIdLookupOf<T>,
real: AccountIdLookupOf<T>,
force_proxy_type: Option<T::ProxyType>,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResult {
ensure_signed(origin)?;
let delegate = T::Lookup::lookup(delegate)?;
let real = T::Lookup::lookup(real)?;
let def = Self::find_proxy(&real, &delegate, force_proxy_type)?;
let call_hash = T::CallHasher::hash_of(&call);
let now = T::BlockNumberProvider::current_block_number();
Self::edit_announcements(&delegate, |ann| {
ann.real != real ||
ann.call_hash != call_hash ||
now.saturating_sub(ann.height) < def.delay
})
.map_err(|_| Error::<T>::Unannounced)?;
Self::do_proxy(def, real, *call);
Ok(())
}
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::poke_deposit())]
pub fn poke_deposit(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let mut deposit_updated = false;
Proxies::<T>::try_mutate_exists(&who, |maybe_proxies| -> DispatchResult {
let (proxies, old_deposit) = maybe_proxies.take().unwrap_or_default();
let maybe_new_deposit = Self::rejig_deposit(
&who,
old_deposit,
T::ProxyDepositBase::get(),
T::ProxyDepositFactor::get(),
proxies.len(),
)?;
match maybe_new_deposit {
Some(new_deposit) if new_deposit != old_deposit => {
*maybe_proxies = Some((proxies, new_deposit));
deposit_updated = true;
Self::deposit_event(Event::DepositPoked {
who: who.clone(),
kind: DepositKind::Proxies,
old_deposit,
new_deposit,
});
},
Some(_) => {
*maybe_proxies = Some((proxies, old_deposit));
},
None => {
*maybe_proxies = None;
if !old_deposit.is_zero() {
deposit_updated = true;
Self::deposit_event(Event::DepositPoked {
who: who.clone(),
kind: DepositKind::Proxies,
old_deposit,
new_deposit: BalanceOf::<T>::zero(),
});
}
},
}
Ok(())
})?;
Announcements::<T>::try_mutate_exists(&who, |maybe_announcements| -> DispatchResult {
let (announcements, old_deposit) = maybe_announcements.take().unwrap_or_default();
let maybe_new_deposit = Self::rejig_deposit(
&who,
old_deposit,
T::AnnouncementDepositBase::get(),
T::AnnouncementDepositFactor::get(),
announcements.len(),
)?;
match maybe_new_deposit {
Some(new_deposit) if new_deposit != old_deposit => {
*maybe_announcements = Some((announcements, new_deposit));
deposit_updated = true;
Self::deposit_event(Event::DepositPoked {
who: who.clone(),
kind: DepositKind::Announcements,
old_deposit,
new_deposit,
});
},
Some(_) => {
*maybe_announcements = Some((announcements, old_deposit));
},
None => {
*maybe_announcements = None;
if !old_deposit.is_zero() {
deposit_updated = true;
Self::deposit_event(Event::DepositPoked {
who: who.clone(),
kind: DepositKind::Announcements,
old_deposit,
new_deposit: BalanceOf::<T>::zero(),
});
}
},
}
Ok(())
})?;
Ok(if deposit_updated { Pays::No.into() } else { Pays::Yes.into() })
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ProxyExecuted { result: DispatchResult },
PureCreated {
pure: T::AccountId,
who: T::AccountId,
proxy_type: T::ProxyType,
disambiguation_index: u16,
at: BlockNumberFor<T>,
extrinsic_index: u32,
},
PureKilled {
pure: T::AccountId,
spawner: T::AccountId,
proxy_type: T::ProxyType,
disambiguation_index: u16,
},
Announced { real: T::AccountId, proxy: T::AccountId, call_hash: CallHashOf<T> },
ProxyAdded {
delegator: T::AccountId,
delegatee: T::AccountId,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
},
ProxyRemoved {
delegator: T::AccountId,
delegatee: T::AccountId,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
},
DepositPoked {
who: T::AccountId,
kind: DepositKind,
old_deposit: BalanceOf<T>,
new_deposit: BalanceOf<T>,
},
}
#[pallet::error]
pub enum Error<T> {
TooMany,
NotFound,
NotProxy,
Unproxyable,
Duplicate,
NoPermission,
Unannounced,
NoSelfProxy,
}
#[pallet::storage]
pub type Proxies<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
(
BoundedVec<
ProxyDefinition<T::AccountId, T::ProxyType, BlockNumberFor<T>>,
T::MaxProxies,
>,
BalanceOf<T>,
),
ValueQuery,
>;
#[pallet::storage]
pub type Announcements<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
(
BoundedVec<Announcement<T::AccountId, CallHashOf<T>, BlockNumberFor<T>>, T::MaxPending>,
BalanceOf<T>,
),
ValueQuery,
>;
#[pallet::view_functions]
impl<T: Config> Pallet<T> {
pub fn check_permissions(
call: <T as Config>::RuntimeCall,
proxy_type: T::ProxyType,
) -> bool {
proxy_type.filter(&call)
}
pub fn is_superset(to_check: T::ProxyType, against: T::ProxyType) -> bool {
to_check.is_superset(&against)
}
}
}
impl<T: Config> Pallet<T> {
pub fn proxies(
account: T::AccountId,
) -> (
BoundedVec<ProxyDefinition<T::AccountId, T::ProxyType, BlockNumberFor<T>>, T::MaxProxies>,
BalanceOf<T>,
) {
Proxies::<T>::get(account)
}
pub fn announcements(
account: T::AccountId,
) -> (
BoundedVec<Announcement<T::AccountId, CallHashOf<T>, BlockNumberFor<T>>, T::MaxPending>,
BalanceOf<T>,
) {
Announcements::<T>::get(account)
}
pub fn pure_account(
who: &T::AccountId,
proxy_type: &T::ProxyType,
index: u16,
maybe_when: Option<(BlockNumberFor<T>, u32)>,
) -> T::AccountId {
let (height, ext_index) = maybe_when.unwrap_or_else(|| {
(
T::BlockNumberProvider::current_block_number(),
frame_system::Pallet::<T>::extrinsic_index().unwrap_or_default(),
)
});
let entropy = (b"modlpy/proxy____", who, height, ext_index, proxy_type, index)
.using_encoded(blake2_256);
Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref()))
.expect("infinite length input; no invalid inputs for type; qed")
}
pub fn add_proxy_delegate(
delegator: &T::AccountId,
delegatee: T::AccountId,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
) -> DispatchResult {
ensure!(delegator != &delegatee, Error::<T>::NoSelfProxy);
Proxies::<T>::try_mutate(delegator, |(ref mut proxies, ref mut deposit)| {
let proxy_def = ProxyDefinition {
delegate: delegatee.clone(),
proxy_type: proxy_type.clone(),
delay,
};
let i = proxies.binary_search(&proxy_def).err().ok_or(Error::<T>::Duplicate)?;
proxies.try_insert(i, proxy_def).map_err(|_| Error::<T>::TooMany)?;
let new_deposit = Self::deposit(proxies.len() as u32);
if new_deposit > *deposit {
T::Currency::reserve(delegator, new_deposit - *deposit)?;
} else if new_deposit < *deposit {
T::Currency::unreserve(delegator, *deposit - new_deposit);
}
*deposit = new_deposit;
Self::deposit_event(Event::<T>::ProxyAdded {
delegator: delegator.clone(),
delegatee,
proxy_type,
delay,
});
Ok(())
})
}
pub fn remove_proxy_delegate(
delegator: &T::AccountId,
delegatee: T::AccountId,
proxy_type: T::ProxyType,
delay: BlockNumberFor<T>,
) -> DispatchResult {
Proxies::<T>::try_mutate_exists(delegator, |x| {
let (mut proxies, old_deposit) = x.take().ok_or(Error::<T>::NotFound)?;
let proxy_def = ProxyDefinition {
delegate: delegatee.clone(),
proxy_type: proxy_type.clone(),
delay,
};
let i = proxies.binary_search(&proxy_def).ok().ok_or(Error::<T>::NotFound)?;
proxies.remove(i);
let new_deposit = Self::deposit(proxies.len() as u32);
if new_deposit > old_deposit {
T::Currency::reserve(delegator, new_deposit - old_deposit)?;
} else if new_deposit < old_deposit {
T::Currency::unreserve(delegator, old_deposit - new_deposit);
}
if !proxies.is_empty() {
*x = Some((proxies, new_deposit))
}
Self::deposit_event(Event::<T>::ProxyRemoved {
delegator: delegator.clone(),
delegatee,
proxy_type,
delay,
});
Ok(())
})
}
pub fn deposit(num_proxies: u32) -> BalanceOf<T> {
if num_proxies == 0 {
Zero::zero()
} else {
T::ProxyDepositBase::get() + T::ProxyDepositFactor::get() * num_proxies.into()
}
}
fn rejig_deposit(
who: &T::AccountId,
old_deposit: BalanceOf<T>,
base: BalanceOf<T>,
factor: BalanceOf<T>,
len: usize,
) -> Result<Option<BalanceOf<T>>, DispatchError> {
let new_deposit =
if len == 0 { BalanceOf::<T>::zero() } else { base + factor * (len as u32).into() };
if new_deposit > old_deposit {
T::Currency::reserve(who, new_deposit.saturating_sub(old_deposit))?;
} else if new_deposit < old_deposit {
let excess = old_deposit.saturating_sub(new_deposit);
let remaining_unreserved = T::Currency::unreserve(who, excess);
if !remaining_unreserved.is_zero() {
defensive!(
"Failed to unreserve full amount. (Requested, Actual)",
(excess, excess.saturating_sub(remaining_unreserved))
);
}
}
Ok(if len == 0 { None } else { Some(new_deposit) })
}
fn edit_announcements<
F: FnMut(&Announcement<T::AccountId, CallHashOf<T>, BlockNumberFor<T>>) -> bool,
>(
delegate: &T::AccountId,
f: F,
) -> DispatchResult {
Announcements::<T>::try_mutate_exists(delegate, |x| {
let (mut pending, old_deposit) = x.take().ok_or(Error::<T>::NotFound)?;
let orig_pending_len = pending.len();
pending.retain(f);
ensure!(orig_pending_len > pending.len(), Error::<T>::NotFound);
*x = Self::rejig_deposit(
delegate,
old_deposit,
T::AnnouncementDepositBase::get(),
T::AnnouncementDepositFactor::get(),
pending.len(),
)?
.map(|deposit| (pending, deposit));
Ok(())
})
}
pub fn find_proxy(
real: &T::AccountId,
delegate: &T::AccountId,
force_proxy_type: Option<T::ProxyType>,
) -> Result<ProxyDefinition<T::AccountId, T::ProxyType, BlockNumberFor<T>>, DispatchError> {
let f = |x: &ProxyDefinition<T::AccountId, T::ProxyType, BlockNumberFor<T>>| -> bool {
&x.delegate == delegate &&
force_proxy_type.as_ref().map_or(true, |y| &x.proxy_type == y)
};
Ok(Proxies::<T>::get(real).0.into_iter().find(f).ok_or(Error::<T>::NotProxy)?)
}
fn do_proxy(
def: ProxyDefinition<T::AccountId, T::ProxyType, BlockNumberFor<T>>,
real: T::AccountId,
call: <T as Config>::RuntimeCall,
) {
use frame::traits::{InstanceFilter as _, OriginTrait as _};
let mut origin: T::RuntimeOrigin = frame_system::RawOrigin::Signed(real).into();
origin.add_filter(move |c: &<T as frame_system::Config>::RuntimeCall| {
let c = <T as Config>::RuntimeCall::from_ref(c);
match c.is_sub_type() {
Some(Call::add_proxy { ref proxy_type, .. }) |
Some(Call::remove_proxy { ref proxy_type, .. })
if !def.proxy_type.is_superset(proxy_type) =>
{
false
},
Some(Call::remove_proxies { .. }) | Some(Call::kill_pure { .. })
if def.proxy_type != T::ProxyType::default() =>
{
false
},
_ => def.proxy_type.filter(c),
}
});
let e = call.dispatch(origin);
Self::deposit_event(Event::ProxyExecuted { result: e.map(|_| ()).map_err(|e| e.error) });
}
pub fn remove_all_proxy_delegates(delegator: &T::AccountId) {
let (proxies, old_deposit) = Proxies::<T>::take(delegator);
T::Currency::unreserve(delegator, old_deposit);
proxies.into_iter().for_each(|proxy_def| {
Self::deposit_event(Event::<T>::ProxyRemoved {
delegator: delegator.clone(),
delegatee: proxy_def.delegate,
proxy_type: proxy_def.proxy_type,
delay: proxy_def.delay,
});
});
}
}