difftreelog
chore store limits in BTreeMap
in: master
3 files changed
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -26,7 +26,7 @@
};
use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};
use pallet_evm_transaction_payment::CallContext;
-use sp_core::H160;
+use sp_core::{H160, U256};
use up_data_structs::SponsorshipState;
use crate::{
AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringFeeLimit,
@@ -258,12 +258,13 @@
fee_limit: uint256,
) -> Result<void> {
<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
- <Pallet<T>>::set_sponsoring_fee_limit(contract_address, fee_limit.into());
+ <Pallet<T>>::set_sponsoring_fee_limit(contract_address, fee_limit.into())
+ .map_err(dispatch_to_evm::<T>)?;
Ok(())
}
fn get_sponsoring_fee_limit(&self, contract_address: address) -> Result<uint256> {
- Ok(<SponsoringFeeLimit<T>>::get(contract_address, 0xffffffff))
+ Ok(get_sponsoring_fee_limit::<T>(contract_address))
}
/// Is specified user present in contract allow list
@@ -413,7 +414,7 @@
}
}
- let sponsored_fee_limit = <SponsoringFeeLimit<T>>::get(contract_address, 0xffffffff);
+ let sponsored_fee_limit = get_sponsoring_fee_limit::<T>(contract_address);
if call_context.max_fee > sponsored_fee_limit {
return None;
@@ -425,5 +426,12 @@
}
}
+fn get_sponsoring_fee_limit<T: Config>(contract_address: address) -> uint256 {
+ <SponsoringFeeLimit<T>>::get(contract_address)
+ .get(&0xffffffff)
+ .cloned()
+ .unwrap_or(U256::MAX)
+}
+
generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);
generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);
pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![doc = include_str!("../README.md")]18#![cfg_attr(not(feature = "std"), no_std)]19#![warn(missing_docs)]2021use codec::{Decode, Encode, MaxEncodedLen};22pub use pallet::*;23pub use eth::*;24use scale_info::TypeInfo;25pub mod eth;2627#[frame_support::pallet]28pub mod pallet {29 pub use super::*;30 use crate::eth::ContractHelpersEvents;31 use frame_support::pallet_prelude::*;32 use pallet_evm_coder_substrate::DispatchResult;33 use sp_core::{H160, U256};34 use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};35 use up_data_structs::SponsorshipState;36 use evm_coder::ToLog;3738 #[pallet::config]39 pub trait Config:40 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config41 {42 /// Overarching event type.43 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;4445 /// Address, under which magic contract will be available46 type ContractAddress: Get<H160>;4748 /// In case of enabled sponsoring, but no sponsoring rate limit set,49 /// this value will be used implicitly50 type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;51 /// In case of enabled sponsoring, but no sponsoring fee limit set,52 /// this value will be used implicitly53 type DefaultSponsoringFeeLimit: Get<U256>;54 }5556 #[pallet::error]57 pub enum Error<T> {58 /// This method is only executable by contract owner59 NoPermission,6061 /// No pending sponsor for contract.62 NoPendingSponsor,63 }6465 #[pallet::pallet]66 #[pallet::generate_store(pub(super) trait Store)]67 pub struct Pallet<T>(_);6869 /// Store owner for contract.70 ///71 /// * **Key** - contract address.72 /// * **Value** - owner for contract.73 #[pallet::storage]74 pub(super) type Owner<T: Config> =75 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;7677 #[pallet::storage]78 #[deprecated]79 pub(super) type SelfSponsoring<T: Config> =80 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;8182 /// Store for contract sponsorship state.83 ///84 /// * **Key** - contract address.85 /// * **Value** - sponsorship state.86 #[pallet::storage]87 pub(super) type Sponsoring<T: Config> = StorageMap<88 Hasher = Twox64Concat,89 Key = H160,90 Value = SponsorshipState<T::CrossAccountId>,91 QueryKind = ValueQuery,92 >;9394 /// Store for sponsoring mode.95 ///96 /// ### Usage97 /// Prefer to delete collection from storage if mode chaged to [`Disabled`](SponsoringModeT::Disabled).98 ///99 /// * **Key** - contract address.100 /// * **Value** - [`sponsoring mode`](SponsoringModeT).101 #[pallet::storage]102 pub(super) type SponsoringMode<T: Config> =103 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;104105 /// Storage for sponsoring rate limit in blocks.106 ///107 /// * **Key** - contract address.108 /// * **Value** - amount of sponsored blocks.109 #[pallet::storage]110 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<111 Hasher = Twox128,112 Key = H160,113 Value = T::BlockNumber,114 QueryKind = ValueQuery,115 OnEmpty = T::DefaultSponsoringRateLimit,116 >;117118 /// Storage for last sponsored block.119 ///120 /// * **Key1** - contract address.121 /// * **Key2** - sponsored user address.122 /// * **Value** - last sponsored block number.123 #[pallet::storage]124 pub(super) type SponsoringFeeLimit<T: Config> = StorageDoubleMap<125 Hasher1 = Twox128,126 Key1 = H160,127 Hasher2 = Blake2_128Concat,128 Key2 = u32,129 Value = U256,130 QueryKind = ValueQuery,131 OnEmpty = T::DefaultSponsoringFeeLimit,132 >;133134 #[pallet::storage]135 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<136 Hasher1 = Twox128,137 Key1 = H160,138 Hasher2 = Twox128,139 Key2 = H160,140 Value = T::BlockNumber,141 QueryKind = OptionQuery,142 >;143144 /// Storege for contracts with [`Allowlisted`](SponsoringModeT::Allowlisted) sponsoring mode.145 ///146 /// ### Usage147 /// Prefer to delete collection from storage if mode chaged to non `Allowlisted`, than set **Value** to **false**.148 ///149 /// * **Key** - contract address.150 /// * **Value** - is contract in [`Allowlisted`](SponsoringModeT::Allowlisted) mode.151 #[pallet::storage]152 pub(super) type AllowlistEnabled<T: Config> =153 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;154155 /// Storage for users that allowed for sponsorship.156 ///157 /// ### Usage158 /// Prefer to delete record from storage if user no more allowed for sponsorship.159 ///160 /// * **Key1** - contract address.161 /// * **Key2** - user that allowed for sponsorship.162 /// * **Value** - allowance for sponsorship.163 #[pallet::storage]164 pub(super) type Allowlist<T: Config> = StorageDoubleMap<165 Hasher1 = Twox128,166 Key1 = H160,167 Hasher2 = Twox128,168 Key2 = H160,169 Value = bool,170 QueryKind = ValueQuery,171 >;172173 #[pallet::event]174 #[pallet::generate_deposit(pub fn deposit_event)]175 pub enum Event<T: Config> {176 /// Contract sponsor was set.177 ContractSponsorSet(178 /// Contract address of the affected collection.179 H160,180 /// New sponsor address.181 T::AccountId,182 ),183184 /// New sponsor was confirm.185 ContractSponsorshipConfirmed(186 /// Contract address of the affected collection.187 H160,188 /// New sponsor address.189 T::AccountId,190 ),191192 /// Collection sponsor was removed.193 ContractSponsorRemoved(194 /// Contract address of the affected collection.195 H160,196 ),197 }198199 impl<T: Config> Pallet<T> {200 /// Get contract owner.201 pub fn contract_owner(contract: H160) -> H160 {202 <Owner<T>>::get(contract)203 }204205 /// Set `sponsor` for `contract`.206 ///207 /// `sender` must be owner of contract.208 pub fn set_sponsor(209 sender: &T::CrossAccountId,210 contract: H160,211 sponsor: &T::CrossAccountId,212 ) -> DispatchResult {213 Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;214 Sponsoring::<T>::insert(215 contract,216 SponsorshipState::<T::CrossAccountId>::Unconfirmed(sponsor.clone()),217 );218219 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorSet(220 contract,221 sponsor.as_sub().clone(),222 ));223 <PalletEvm<T>>::deposit_log(224 ContractHelpersEvents::ContractSponsorSet {225 contract_address: contract,226 sponsor: *sponsor.as_eth(),227 }228 .to_log(contract),229 );230 Ok(())231 }232233 /// TO-DO234 ///235 ///236 pub fn force_set_sponsor(237 contract_address: H160,238 sponsor: &T::CrossAccountId,239 ) -> DispatchResult {240 Sponsoring::<T>::insert(241 contract_address,242 SponsorshipState::<T::CrossAccountId>::Confirmed(sponsor.clone()),243 );244245 let eth_sponsor = *sponsor.as_eth();246 let sub_sponsor = sponsor.as_sub().clone();247248 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorSet(249 contract_address,250 sub_sponsor.clone(),251 ));252 <PalletEvm<T>>::deposit_log(253 ContractHelpersEvents::ContractSponsorSet {254 contract_address,255 sponsor: eth_sponsor,256 }257 .to_log(contract_address),258 );259260 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorshipConfirmed(261 contract_address,262 sub_sponsor,263 ));264 <PalletEvm<T>>::deposit_log(265 ContractHelpersEvents::ContractSponsorshipConfirmed {266 contract_address,267 sponsor: eth_sponsor,268 }269 .to_log(contract_address),270 );271272 Ok(())273 }274275 /// Remove sponsor for `contract`.276 ///277 /// `sender` must be owner of contract.278 pub fn remove_sponsor(279 sender: &T::CrossAccountId,280 contract_address: H160,281 ) -> DispatchResult {282 Self::ensure_owner(contract_address, *sender.as_eth())?;283 Self::force_remove_sponsor(contract_address)284 }285286 /// TO-DO287 ///288 ///289 pub fn force_remove_sponsor(contract_address: H160) -> DispatchResult {290 Sponsoring::<T>::remove(contract_address);291292 Self::deposit_event(Event::<T>::ContractSponsorRemoved(contract_address));293 <PalletEvm<T>>::deposit_log(294 ContractHelpersEvents::ContractSponsorRemoved { contract_address }295 .to_log(contract_address),296 );297298 Ok(())299 }300301 /// Confirm sponsorship.302 ///303 /// `sender` must be same that set via [`set_sponsor`].304 pub fn confirm_sponsorship(305 sender: &T::CrossAccountId,306 contract_address: H160,307 ) -> DispatchResult {308 match Sponsoring::<T>::get(contract_address) {309 SponsorshipState::Unconfirmed(sponsor) => {310 ensure!(sponsor == *sender, Error::<T>::NoPermission);311 let eth_sponsor = *sponsor.as_eth();312 let sub_sponsor = sponsor.as_sub().clone();313 Sponsoring::<T>::insert(314 contract_address,315 SponsorshipState::<T::CrossAccountId>::Confirmed(sponsor),316 );317318 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorshipConfirmed(319 contract_address,320 sub_sponsor,321 ));322 <PalletEvm<T>>::deposit_log(323 ContractHelpersEvents::ContractSponsorshipConfirmed {324 contract_address,325 sponsor: eth_sponsor,326 }327 .to_log(contract_address),328 );329330 Ok(())331 }332 SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => {333 Err(Error::<T>::NoPendingSponsor.into())334 }335 }336 }337338 /// Get sponsor.339 pub fn get_sponsor(contract: H160) -> Option<T::CrossAccountId> {340 match Sponsoring::<T>::get(contract) {341 SponsorshipState::Disabled | SponsorshipState::Unconfirmed(_) => None,342 SponsorshipState::Confirmed(sponsor) => Some(sponsor),343 }344 }345346 /// Get current sponsoring mode, performing lazy migration from legacy storage347 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {348 <SponsoringMode<T>>::get(contract)349 .or_else(|| {350 <SelfSponsoring<T>>::get(contract).then(|| SponsoringModeT::Allowlisted)351 })352 .unwrap_or_default()353 }354355 /// Reconfigure contract sponsoring mode356 pub fn set_sponsoring_mode(contract: H160, mode: SponsoringModeT) {357 if mode == SponsoringModeT::Disabled {358 <SponsoringMode<T>>::remove(contract);359 } else {360 <SponsoringMode<T>>::insert(contract, mode);361 }362 <SelfSponsoring<T>>::remove(contract)363 }364365 /// Set duration between two sponsored contract calls366 pub fn set_sponsoring_rate_limit(contract: H160, rate_limit: T::BlockNumber) {367 <SponsoringRateLimit<T>>::insert(contract, rate_limit);368 }369370 /// Set maximum for gas limit of transaction371 pub fn set_sponsoring_fee_limit(contract: H160, fee_limit: U256) {372 <SponsoringFeeLimit<T>>::insert(contract, 0xffffffff, fee_limit);373 }374375 /// Is user added to allowlist, or he is owner of specified contract376 pub fn allowed(contract: H160, user: H160) -> bool {377 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user378 }379380 /// Toggle contract allowlist access381 pub fn toggle_allowlist(contract: H160, enabled: bool) {382 <AllowlistEnabled<T>>::insert(contract, enabled)383 }384385 /// Toggle user presence in contract's allowlist386 pub fn toggle_allowed(contract: H160, user: H160, allowed: bool) {387 <Allowlist<T>>::insert(contract, user, allowed);388 }389390 /// Throw error if user is not allowed to reconfigure target contract391 pub fn ensure_owner(contract: H160, user: H160) -> DispatchResult {392 ensure!(<Owner<T>>::get(&contract) == user, Error::<T>::NoPermission);393 Ok(())394 }395 }396}397398/// Available contract sponsoring modes399#[derive(Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen, Default)]400pub enum SponsoringModeT {401 /// Sponsoring is disabled402 #[default]403 Disabled,404 /// Only users from allowlist will be sponsored405 Allowlisted,406 /// All users will be sponsored407 Generous,408}409410impl SponsoringModeT {411 fn from_eth(v: u8) -> Option<Self> {412 Some(match v {413 0 => Self::Disabled,414 1 => Self::Allowlisted,415 2 => Self::Generous,416 _ => return None,417 })418 }419 fn to_eth(self) -> u8 {420 match self {421 SponsoringModeT::Disabled => 0,422 SponsoringModeT::Allowlisted => 1,423 SponsoringModeT::Generous => 2,424 }425 }426}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![doc = include_str!("../README.md")]18#![cfg_attr(not(feature = "std"), no_std)]19#![warn(missing_docs)]2021use codec::{Decode, Encode, MaxEncodedLen};22pub use pallet::*;23pub use eth::*;24use scale_info::TypeInfo;25use frame_support::storage::bounded_btree_map::BoundedBTreeMap;26pub mod eth;2728/// Maximum number of methods per contract that could have fee limit29pub const MAX_FEE_LIMITED_METHODS: u32 = 5;3031#[frame_support::pallet]32pub mod pallet {33 pub use super::*;34 use crate::eth::ContractHelpersEvents;35 use frame_support::pallet_prelude::*;36 use pallet_evm_coder_substrate::DispatchResult;37 use sp_core::{H160, U256};38 use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};39 use up_data_structs::SponsorshipState;40 use evm_coder::ToLog;4142 #[pallet::config]43 pub trait Config:44 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_evm::account::Config45 {46 /// Overarching event type.47 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;4849 /// Address, under which magic contract will be available50 type ContractAddress: Get<H160>;5152 /// In case of enabled sponsoring, but no sponsoring rate limit set,53 /// this value will be used implicitly54 type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;55 }5657 #[pallet::error]58 pub enum Error<T> {59 /// This method is only executable by contract owner60 NoPermission,6162 /// No pending sponsor for contract.63 NoPendingSponsor,6465 /// Number of methods that sponsored limit is defined for exceeds maximum.66 TooManyMethodsHaveSponsoredLimit,67 }6869 #[pallet::pallet]70 #[pallet::generate_store(pub(super) trait Store)]71 pub struct Pallet<T>(_);7273 /// Store owner for contract.74 ///75 /// * **Key** - contract address.76 /// * **Value** - owner for contract.77 #[pallet::storage]78 pub(super) type Owner<T: Config> =79 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;8081 #[pallet::storage]82 #[deprecated]83 pub(super) type SelfSponsoring<T: Config> =84 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;8586 /// Store for contract sponsorship state.87 ///88 /// * **Key** - contract address.89 /// * **Value** - sponsorship state.90 #[pallet::storage]91 pub(super) type Sponsoring<T: Config> = StorageMap<92 Hasher = Twox64Concat,93 Key = H160,94 Value = SponsorshipState<T::CrossAccountId>,95 QueryKind = ValueQuery,96 >;9798 /// Store for sponsoring mode.99 ///100 /// ### Usage101 /// Prefer to delete collection from storage if mode chaged to [`Disabled`](SponsoringModeT::Disabled).102 ///103 /// * **Key** - contract address.104 /// * **Value** - [`sponsoring mode`](SponsoringModeT).105 #[pallet::storage]106 pub(super) type SponsoringMode<T: Config> =107 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;108109 /// Storage for sponsoring rate limit in blocks.110 ///111 /// * **Key** - contract address.112 /// * **Value** - amount of sponsored blocks.113 #[pallet::storage]114 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<115 Hasher = Twox128,116 Key = H160,117 Value = T::BlockNumber,118 QueryKind = ValueQuery,119 OnEmpty = T::DefaultSponsoringRateLimit,120 >;121122 /// Storage for last sponsored block.123 ///124 /// * **Key1** - contract address.125 /// * **Key2** - sponsored user address.126 /// * **Value** - last sponsored block number.127 #[pallet::storage]128 pub(super) type SponsoringFeeLimit<T: Config> = StorageMap<129 Hasher = Twox128,130 Key = H160,131 Value = BoundedBTreeMap<u32, U256, ConstU32<MAX_FEE_LIMITED_METHODS>>,132 QueryKind = ValueQuery,133 >;134135 #[pallet::storage]136 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<137 Hasher1 = Twox128,138 Key1 = H160,139 Hasher2 = Twox128,140 Key2 = H160,141 Value = T::BlockNumber,142 QueryKind = OptionQuery,143 >;144145 /// Storege for contracts with [`Allowlisted`](SponsoringModeT::Allowlisted) sponsoring mode.146 ///147 /// ### Usage148 /// Prefer to delete collection from storage if mode chaged to non `Allowlisted`, than set **Value** to **false**.149 ///150 /// * **Key** - contract address.151 /// * **Value** - is contract in [`Allowlisted`](SponsoringModeT::Allowlisted) mode.152 #[pallet::storage]153 pub(super) type AllowlistEnabled<T: Config> =154 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;155156 /// Storage for users that allowed for sponsorship.157 ///158 /// ### Usage159 /// Prefer to delete record from storage if user no more allowed for sponsorship.160 ///161 /// * **Key1** - contract address.162 /// * **Key2** - user that allowed for sponsorship.163 /// * **Value** - allowance for sponsorship.164 #[pallet::storage]165 pub(super) type Allowlist<T: Config> = StorageDoubleMap<166 Hasher1 = Twox128,167 Key1 = H160,168 Hasher2 = Twox128,169 Key2 = H160,170 Value = bool,171 QueryKind = ValueQuery,172 >;173174 #[pallet::event]175 #[pallet::generate_deposit(pub fn deposit_event)]176 pub enum Event<T: Config> {177 /// Contract sponsor was set.178 ContractSponsorSet(179 /// Contract address of the affected collection.180 H160,181 /// New sponsor address.182 T::AccountId,183 ),184185 /// New sponsor was confirm.186 ContractSponsorshipConfirmed(187 /// Contract address of the affected collection.188 H160,189 /// New sponsor address.190 T::AccountId,191 ),192193 /// Collection sponsor was removed.194 ContractSponsorRemoved(195 /// Contract address of the affected collection.196 H160,197 ),198 }199200 impl<T: Config> Pallet<T> {201 /// Get contract owner.202 pub fn contract_owner(contract: H160) -> H160 {203 <Owner<T>>::get(contract)204 }205206 /// Set `sponsor` for `contract`.207 ///208 /// `sender` must be owner of contract.209 pub fn set_sponsor(210 sender: &T::CrossAccountId,211 contract: H160,212 sponsor: &T::CrossAccountId,213 ) -> DispatchResult {214 Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;215 Sponsoring::<T>::insert(216 contract,217 SponsorshipState::<T::CrossAccountId>::Unconfirmed(sponsor.clone()),218 );219220 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorSet(221 contract,222 sponsor.as_sub().clone(),223 ));224 <PalletEvm<T>>::deposit_log(225 ContractHelpersEvents::ContractSponsorSet {226 contract_address: contract,227 sponsor: *sponsor.as_eth(),228 }229 .to_log(contract),230 );231 Ok(())232 }233234 /// TO-DO235 ///236 ///237 pub fn force_set_sponsor(238 contract_address: H160,239 sponsor: &T::CrossAccountId,240 ) -> DispatchResult {241 Sponsoring::<T>::insert(242 contract_address,243 SponsorshipState::<T::CrossAccountId>::Confirmed(sponsor.clone()),244 );245246 let eth_sponsor = *sponsor.as_eth();247 let sub_sponsor = sponsor.as_sub().clone();248249 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorSet(250 contract_address,251 sub_sponsor.clone(),252 ));253 <PalletEvm<T>>::deposit_log(254 ContractHelpersEvents::ContractSponsorSet {255 contract_address,256 sponsor: eth_sponsor,257 }258 .to_log(contract_address),259 );260261 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorshipConfirmed(262 contract_address,263 sub_sponsor,264 ));265 <PalletEvm<T>>::deposit_log(266 ContractHelpersEvents::ContractSponsorshipConfirmed {267 contract_address,268 sponsor: eth_sponsor,269 }270 .to_log(contract_address),271 );272273 Ok(())274 }275276 /// Remove sponsor for `contract`.277 ///278 /// `sender` must be owner of contract.279 pub fn remove_sponsor(280 sender: &T::CrossAccountId,281 contract_address: H160,282 ) -> DispatchResult {283 Self::ensure_owner(contract_address, *sender.as_eth())?;284 Self::force_remove_sponsor(contract_address)285 }286287 /// TO-DO288 ///289 ///290 pub fn force_remove_sponsor(contract_address: H160) -> DispatchResult {291 Sponsoring::<T>::remove(contract_address);292293 Self::deposit_event(Event::<T>::ContractSponsorRemoved(contract_address));294 <PalletEvm<T>>::deposit_log(295 ContractHelpersEvents::ContractSponsorRemoved { contract_address }296 .to_log(contract_address),297 );298299 Ok(())300 }301302 /// Confirm sponsorship.303 ///304 /// `sender` must be same that set via [`set_sponsor`].305 pub fn confirm_sponsorship(306 sender: &T::CrossAccountId,307 contract_address: H160,308 ) -> DispatchResult {309 match Sponsoring::<T>::get(contract_address) {310 SponsorshipState::Unconfirmed(sponsor) => {311 ensure!(sponsor == *sender, Error::<T>::NoPermission);312 let eth_sponsor = *sponsor.as_eth();313 let sub_sponsor = sponsor.as_sub().clone();314 Sponsoring::<T>::insert(315 contract_address,316 SponsorshipState::<T::CrossAccountId>::Confirmed(sponsor),317 );318319 <Pallet<T>>::deposit_event(Event::<T>::ContractSponsorshipConfirmed(320 contract_address,321 sub_sponsor,322 ));323 <PalletEvm<T>>::deposit_log(324 ContractHelpersEvents::ContractSponsorshipConfirmed {325 contract_address,326 sponsor: eth_sponsor,327 }328 .to_log(contract_address),329 );330331 Ok(())332 }333 SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => {334 Err(Error::<T>::NoPendingSponsor.into())335 }336 }337 }338339 /// Get sponsor.340 pub fn get_sponsor(contract: H160) -> Option<T::CrossAccountId> {341 match Sponsoring::<T>::get(contract) {342 SponsorshipState::Disabled | SponsorshipState::Unconfirmed(_) => None,343 SponsorshipState::Confirmed(sponsor) => Some(sponsor),344 }345 }346347 /// Get current sponsoring mode, performing lazy migration from legacy storage348 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {349 <SponsoringMode<T>>::get(contract)350 .or_else(|| {351 <SelfSponsoring<T>>::get(contract).then(|| SponsoringModeT::Allowlisted)352 })353 .unwrap_or_default()354 }355356 /// Reconfigure contract sponsoring mode357 pub fn set_sponsoring_mode(contract: H160, mode: SponsoringModeT) {358 if mode == SponsoringModeT::Disabled {359 <SponsoringMode<T>>::remove(contract);360 } else {361 <SponsoringMode<T>>::insert(contract, mode);362 }363 <SelfSponsoring<T>>::remove(contract)364 }365366 /// Set duration between two sponsored contract calls367 pub fn set_sponsoring_rate_limit(contract: H160, rate_limit: T::BlockNumber) {368 <SponsoringRateLimit<T>>::insert(contract, rate_limit);369 }370371 /// Set maximum for gas limit of transaction372 pub fn set_sponsoring_fee_limit(contract: H160, fee_limit: U256) -> DispatchResult {373 <SponsoringFeeLimit<T>>::try_mutate(contract, |limits_map| {374 limits_map375 .try_insert(0xffffffff, fee_limit)376 .map_err(|_| <Error<T>>::TooManyMethodsHaveSponsoredLimit)377 })?;378 Ok(())379 }380381 /// Is user added to allowlist, or he is owner of specified contract382 pub fn allowed(contract: H160, user: H160) -> bool {383 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user384 }385386 /// Toggle contract allowlist access387 pub fn toggle_allowlist(contract: H160, enabled: bool) {388 <AllowlistEnabled<T>>::insert(contract, enabled)389 }390391 /// Toggle user presence in contract's allowlist392 pub fn toggle_allowed(contract: H160, user: H160, allowed: bool) {393 <Allowlist<T>>::insert(contract, user, allowed);394 }395396 /// Throw error if user is not allowed to reconfigure target contract397 pub fn ensure_owner(contract: H160, user: H160) -> DispatchResult {398 ensure!(<Owner<T>>::get(&contract) == user, Error::<T>::NoPermission);399 Ok(())400 }401 }402}403404/// Available contract sponsoring modes405#[derive(Encode, Decode, PartialEq, TypeInfo, MaxEncodedLen, Default)]406pub enum SponsoringModeT {407 /// Sponsoring is disabled408 #[default]409 Disabled,410 /// Only users from allowlist will be sponsored411 Allowlisted,412 /// All users will be sponsored413 Generous,414}415416impl SponsoringModeT {417 fn from_eth(v: u8) -> Option<Self> {418 Some(match v {419 0 => Self::Disabled,420 1 => Self::Allowlisted,421 2 => Self::Generous,422 _ => return None,423 })424 }425 fn to_eth(self) -> u8 {426 match self {427 SponsoringModeT::Disabled => 0,428 SponsoringModeT::Allowlisted => 1,429 SponsoringModeT::Generous => 2,430 }431 }432}runtime/common/config/ethereum.rsdiffbeforeafterboth--- a/runtime/common/config/ethereum.rs
+++ b/runtime/common/config/ethereum.rs
@@ -7,10 +7,8 @@
use sp_runtime::{RuntimeAppPublic, Perbill};
use crate::{
runtime_common::{
- dispatch::CollectionDispatchT,
- ethereum::sponsoring::EvmSponsorshipHandler,
- config::sponsoring::{DefaultSponsoringFeeLimit, DefaultSponsoringRateLimit},
- DealWithFees,
+ dispatch::CollectionDispatchT, ethereum::sponsoring::EvmSponsorshipHandler,
+ config::sponsoring::DefaultSponsoringRateLimit, DealWithFees,
},
Runtime, Aura, Balances, Event, ChainId,
};
@@ -117,7 +115,6 @@
type Event = Event;
type ContractAddress = HelpersContractAddress;
type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit;
- type DefaultSponsoringFeeLimit = DefaultSponsoringFeeLimit;
}
impl pallet_evm_coder_substrate::Config for Runtime {}