1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]1819#[macro_use(format)]20extern crate alloc;2122use codec::{Decode, Encode, MaxEncodedLen};23pub use pallet::*;24pub use eth::*;25use scale_info::TypeInfo;26pub mod eth;2728#[frame_support::pallet]29pub mod pallet {30 pub use super::*;31 use evm_coder::execution::Result;32 use frame_support::pallet_prelude::*;33 use sp_core::H160;3435 #[pallet::config]36 pub trait Config:37 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config + pallet_nonfungible::Config38 {39 type ContractAddress: Get<H160>;40 type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;41 }4243 #[pallet::error]44 pub enum Error<T> {45 46 NoPermission,47 }4849 #[pallet::pallet]50 #[pallet::generate_store(pub(super) trait Store)]51 pub struct Pallet<T>(_);5253 #[pallet::storage]54 pub(super) type Owner<T: Config> =55 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;5657 #[pallet::storage]58 #[deprecated]59 pub(super) type SelfSponsoring<T: Config> =60 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;6162 #[pallet::storage]63 pub(super) type SponsoringMode<T: Config> =64 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;6566 #[pallet::storage]67 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<68 Hasher = Twox128,69 Key = H160,70 Value = T::BlockNumber,71 QueryKind = ValueQuery,72 OnEmpty = T::DefaultSponsoringRateLimit,73 >;7475 #[pallet::storage]76 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<77 Hasher1 = Twox128,78 Key1 = H160,79 Hasher2 = Twox128,80 Key2 = H160,81 Value = T::BlockNumber,82 QueryKind = OptionQuery,83 >;8485 #[pallet::storage]86 pub(super) type AllowlistEnabled<T: Config> =87 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;8889 #[pallet::storage]90 pub(super) type Allowlist<T: Config> = StorageDoubleMap<91 Hasher1 = Twox128,92 Key1 = H160,93 Hasher2 = Twox128,94 Key2 = H160,95 Value = bool,96 QueryKind = ValueQuery,97 >;9899 impl<T: Config> Pallet<T> {100 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {101 <SponsoringMode<T>>::get(contract)102 .or_else(|| {103 <SelfSponsoring<T>>::get(contract).then(|| SponsoringModeT::Allowlisted)104 })105 .unwrap_or_default()106 }107 pub fn set_sponsoring_mode(contract: H160, mode: SponsoringModeT) {108 if mode == SponsoringModeT::Disabled {109 <SponsoringMode<T>>::remove(contract);110 } else {111 <SponsoringMode<T>>::insert(contract, mode);112 }113 <SelfSponsoring<T>>::remove(contract)114 }115116 pub fn toggle_sponsoring(contract: H160, enabled: bool) {117 Self::set_sponsoring_mode(118 contract,119 if enabled {120 SponsoringModeT::Allowlisted121 } else {122 SponsoringModeT::Disabled123 },124 )125 }126127 pub fn set_sponsoring_rate_limit(contract: H160, rate_limit: T::BlockNumber) {128 <SponsoringRateLimit<T>>::insert(contract, rate_limit);129 }130131 pub fn allowed(contract: H160, user: H160) -> bool {132 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user133 }134135 pub fn toggle_allowlist(contract: H160, enabled: bool) {136 <AllowlistEnabled<T>>::insert(contract, enabled)137 }138139 pub fn toggle_allowed(contract: H160, user: H160, allowed: bool) {140 <Allowlist<T>>::insert(contract, user, allowed);141 }142143 pub fn ensure_owner(contract: H160, user: H160) -> Result<()> {144 ensure!(<Owner<T>>::get(&contract) == user, "no permission");145 Ok(())146 }147 }148}149150#[derive(Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen)]151pub enum SponsoringModeT {152 Disabled,153 Allowlisted,154 Generous,155}156157impl SponsoringModeT {158 fn from_eth(v: u8) -> Option<Self> {159 Some(match v {160 0 => Self::Disabled,161 1 => Self::Allowlisted,162 2 => Self::Generous,163 _ => return None,164 })165 }166 fn to_eth(self) -> u8 {167 match self {168 SponsoringModeT::Disabled => 0,169 SponsoringModeT::Allowlisted => 1,170 SponsoringModeT::Generous => 2,171 }172 }173}174175impl Default for SponsoringModeT {176 fn default() -> Self {177 Self::Disabled178 }179}