difftreelog
feat default sponsoring rate limit
in: master
2 files changed
pallets/contract-helpers/src/lib.rsdiffbeforeafterboth--- a/pallets/contract-helpers/src/lib.rs
+++ b/pallets/contract-helpers/src/lib.rs
@@ -22,7 +22,9 @@
}
#[pallet::config]
- pub trait Config: frame_system::Config + pallet_contracts::Config {}
+ pub trait Config: frame_system::Config + pallet_contracts::Config {
+ type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;
+ }
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
@@ -60,6 +62,7 @@
Key = T::AccountId,
Value = T::BlockNumber,
QueryKind = ValueQuery,
+ OnEmpty = T::DefaultSponsoringRateLimit,
>;
#[pallet::storage]
@@ -69,7 +72,7 @@
Hasher2 = Twox128,
Key2 = T::AccountId,
Value = T::BlockNumber,
- QueryKind = ValueQuery,
+ QueryKind = OptionQuery,
>;
#[pallet::call]
pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23pub use pallet::*;4pub use eth::*;5pub mod eth;67#[frame_support::pallet]8pub mod pallet {9 use evm_coder::execution::Result;10 use frame_support::pallet_prelude::*;11 use sp_core::H160;1213 #[pallet::config]14 pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {15 type ContractAddress: Get<H160>;16 }1718 #[pallet::error]19 pub enum Error<T> {20 /// This method is only executable by owner21 NoPermission,22 }2324 #[pallet::pallet]25 #[pallet::generate_store(pub(super) trait Store)]26 pub struct Pallet<T>(_);2728 #[pallet::storage]29 pub(super) type Owner<T: Config> =30 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;3132 #[pallet::storage]33 pub(super) type SelfSponsoring<T: Config> =34 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;3536 #[pallet::storage]37 pub(super) type SponsoringRateLimit<T: Config> =38 StorageMap<Hasher = Twox128, Key = H160, Value = T::BlockNumber, QueryKind = ValueQuery>;3940 #[pallet::storage]41 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<42 Hasher1 = Twox128,43 Key1 = H160,44 Hasher2 = Twox128,45 Key2 = H160,46 Value = T::BlockNumber,47 QueryKind = OptionQuery,48 >;4950 #[pallet::storage]51 pub(super) type AllowlistEnabled<T: Config> =52 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;5354 #[pallet::storage]55 pub(super) type Allowlist<T: Config> = StorageDoubleMap<56 Hasher1 = Twox128,57 Key1 = H160,58 Hasher2 = Twox128,59 Key2 = H160,60 Value = bool,61 QueryKind = ValueQuery,62 >;6364 impl<T: Config> Pallet<T> {65 pub fn toggle_sponsoring(contract: H160, enabled: bool) {66 <SelfSponsoring<T>>::insert(contract, enabled);67 }6869 pub fn set_sponsoring_rate_limit(contract: H160, rate_limit: T::BlockNumber) {70 <SponsoringRateLimit<T>>::insert(contract, rate_limit);71 }7273 pub fn allowed(contract: H160, user: H160) -> bool {74 if !<AllowlistEnabled<T>>::get(contract) {75 return true;76 }77 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user78 }7980 pub fn toggle_allowlist(contract: H160, enabled: bool) {81 <AllowlistEnabled<T>>::insert(contract, enabled)82 }8384 pub fn toggle_allowed(contract: H160, user: H160, allowed: bool) {85 <Allowlist<T>>::insert(contract, user, allowed);86 }8788 pub fn ensure_owner(contract: H160, user: H160) -> Result<()> {89 ensure!(<Owner<T>>::get(&contract) == user, "no permission");90 Ok(())91 }92 }93}1#![cfg_attr(not(feature = "std"), no_std)]23pub use pallet::*;4pub use eth::*;5pub mod eth;67#[frame_support::pallet]8pub mod pallet {9 use evm_coder::execution::Result;10 use frame_support::pallet_prelude::*;11 use sp_core::H160;1213 #[pallet::config]14 pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {15 type ContractAddress: Get<H160>;16 type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;17 }1819 #[pallet::error]20 pub enum Error<T> {21 /// This method is only executable by owner22 NoPermission,23 }2425 #[pallet::pallet]26 #[pallet::generate_store(pub(super) trait Store)]27 pub struct Pallet<T>(_);2829 #[pallet::storage]30 pub(super) type Owner<T: Config> =31 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;3233 #[pallet::storage]34 pub(super) type SelfSponsoring<T: Config> =35 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;3637 #[pallet::storage]38 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<39 Hasher = Twox128,40 Key = H160,41 Value = T::BlockNumber,42 QueryKind = ValueQuery,43 OnEmpty = T::DefaultSponsoringRateLimit,44 >;4546 #[pallet::storage]47 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<48 Hasher1 = Twox128,49 Key1 = H160,50 Hasher2 = Twox128,51 Key2 = H160,52 Value = T::BlockNumber,53 QueryKind = OptionQuery,54 >;5556 #[pallet::storage]57 pub(super) type AllowlistEnabled<T: Config> =58 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;5960 #[pallet::storage]61 pub(super) type Allowlist<T: Config> = StorageDoubleMap<62 Hasher1 = Twox128,63 Key1 = H160,64 Hasher2 = Twox128,65 Key2 = H160,66 Value = bool,67 QueryKind = ValueQuery,68 >;6970 impl<T: Config> Pallet<T> {71 pub fn toggle_sponsoring(contract: H160, enabled: bool) {72 <SelfSponsoring<T>>::insert(contract, enabled);73 }7475 pub fn set_sponsoring_rate_limit(contract: H160, rate_limit: T::BlockNumber) {76 <SponsoringRateLimit<T>>::insert(contract, rate_limit);77 }7879 pub fn allowed(contract: H160, user: H160) -> bool {80 if !<AllowlistEnabled<T>>::get(contract) {81 return true;82 }83 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user84 }8586 pub fn toggle_allowlist(contract: H160, enabled: bool) {87 <AllowlistEnabled<T>>::insert(contract, enabled)88 }8990 pub fn toggle_allowed(contract: H160, user: H160, allowed: bool) {91 <Allowlist<T>>::insert(contract, user, allowed);92 }9394 pub fn ensure_owner(contract: H160, user: H160) -> Result<()> {95 ensure!(<Owner<T>>::get(&contract) == user, "no permission");96 Ok(())97 }98 }99}