difftreelog
feat(contract-helpers) generous sponsoring mode
in: master
7 files changed
crates/evm-coder/src/abi.rsdiffbeforeafterboth95 string::from_utf8(self.bytes()?).map_err(|_| Error::Error(ExitError::InvalidRange))95 string::from_utf8(self.bytes()?).map_err(|_| Error::Error(ExitError::InvalidRange))96 }96 }9798 pub fn uint8(&mut self) -> Result<u8> {99 Ok(self.read_padleft::<1>()?[0])100 }9710198 pub fn uint32(&mut self) -> Result<u32> {102 pub fn uint32(&mut self) -> Result<u32> {99 Ok(u32::from_be_bytes(self.read_padleft()?))103 Ok(u32::from_be_bytes(self.read_padleft()?))243 };247 };244}248}245249250impl_abi_readable!(u8, uint8);246impl_abi_readable!(u32, uint32);251impl_abi_readable!(u32, uint32);247impl_abi_readable!(u64, uint64);252impl_abi_readable!(u64, uint64);248impl_abi_readable!(u128, uint128);253impl_abi_readable!(u128, uint128);pallets/evm-contract-helpers/exp.rsdiffbeforeafterbothno changes
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth4use pallet_evm::{ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure};4use pallet_evm::{ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure};5use sp_core::H160;5use sp_core::H160;6use crate::{6use crate::{7 AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,7 AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsoringMode, SponsorBasket,8 SponsoringRateLimit, SponsoringModeT,8};9};9use frame_support::traits::Get;10use frame_support::traits::Get;10use up_sponsorship::SponsorshipHandler;11use up_sponsorship::SponsorshipHandler;31 Ok(<SelfSponsoring<T>>::get(contract_address))32 Ok(<SelfSponsoring<T>>::get(contract_address))32 }33 }333435 /// Deprecated34 fn toggle_sponsoring(36 fn toggle_sponsoring(35 &mut self,37 &mut self,36 caller: caller,38 caller: caller,42 Ok(())44 Ok(())43 }45 }4647 fn set_sponsoring_mode(48 &mut self,49 caller: caller,50 contract_address: address,51 mode: uint8,52 ) -> Result<void> {53 <Pallet<T>>::ensure_owner(contract_address, caller)?;54 let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;55 <Pallet<T>>::set_sponsoring_mode(contract_address, mode);56 Ok(())57 }5859 fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {60 Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())61 }446245 fn set_sponsoring_rate_limit(63 fn set_sponsoring_rate_limit(46 &mut self,64 &mut self,147pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);165pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);148impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {166impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {149 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {167 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {150 if !<SelfSponsoring<T>>::get(&call.0) {168 let mode = <Pallet<T>>::sponsoring_mode(call.0);151 return None;169 if mode == SponsoringModeT::Disabled {152 }170 return None;171 }153 if !<Pallet<T>>::allowed(call.0, *who) {172 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, *who) {154 return None;173 return None;155 }174 }156 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;175 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]1#![cfg_attr(not(feature = "std"), no_std)]223use codec::{Decode, Encode};3pub use pallet::*;4pub use pallet::*;4pub use eth::*;5pub use eth::*;6use scale_info::TypeInfo;5pub mod eth;7pub mod eth;687#[frame_support::pallet]9#[frame_support::pallet]8pub mod pallet {10pub mod pallet {11 pub use super::*;9 use evm_coder::execution::Result;12 use evm_coder::execution::Result;10 use frame_support::pallet_prelude::*;13 use frame_support::pallet_prelude::*;11 use sp_core::H160;14 use sp_core::H160;31 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;34 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;323533 #[pallet::storage]36 #[pallet::storage]37 #[deprecated]34 pub(super) type SelfSponsoring<T: Config> =38 pub(super) type SelfSponsoring<T: Config> =35 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;39 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;4041 #[pallet::storage]42 pub(super) type SponsoringMode<T: Config> =43 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;364437 #[pallet::storage]45 #[pallet::storage]38 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<46 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<68 >;76 >;697770 impl<T: Config> Pallet<T> {78 impl<T: Config> Pallet<T> {79 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {80 <SponsoringMode<T>>::get(contract)81 .or_else(|| {82 <SelfSponsoring<T>>::get(contract).then(|| SponsoringModeT::Allowlisted)83 })84 .unwrap_or_default()85 }86 pub fn set_sponsoring_mode(contract: H160, mode: SponsoringModeT) {87 if mode == SponsoringModeT::Disabled {88 <SponsoringMode<T>>::remove(contract);89 } else {90 <SponsoringMode<T>>::insert(contract, mode);91 }92 <SelfSponsoring<T>>::remove(contract)93 }9471 pub fn toggle_sponsoring(contract: H160, enabled: bool) {95 pub fn toggle_sponsoring(contract: H160, enabled: bool) {72 <SelfSponsoring<T>>::insert(contract, enabled);96 Self::set_sponsoring_mode(97 contract,98 if enabled {99 SponsoringModeT::Allowlisted100 } else {101 SponsoringModeT::Disabled102 },103 )73 }104 }7410595 }126 }96}127}128129#[derive(Encode, Decode, PartialEq, TypeInfo)]130pub enum SponsoringModeT {131 Disabled,132 Allowlisted,133 Generous,134}135136impl SponsoringModeT {137 fn from_eth(v: u8) -> Option<Self> {138 Some(match v {139 0 => Self::Disabled,140 1 => Self::Allowlisted,141 2 => Self::Generous,142 _ => return None,143 })144 }145 fn to_eth(self) -> u8 {146 match self {147 SponsoringModeT::Disabled => 0,148 SponsoringModeT::Allowlisted => 1,149 SponsoringModeT::Generous => 2,150 }151 }152}153154impl Default for SponsoringModeT {155 fn default() -> Self {156 Self::Disabled157 }158}97159pallets/evm-contract-helpers/src/stubs/ContractHelpers.rawdiffbeforeafterbothbinary blob — no preview
pallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth21 }21 }22}22}232324// Selector: 31acb1fe24// Selector: 7b4866f925contract ContractHelpers is Dummy, ERC165 {25contract ContractHelpers is Dummy, ERC165 {26 // Selector: contractOwner(address) 5152b14c26 // Selector: contractOwner(address) 5152b14c27 function contractOwner(address contractAddress)27 function contractOwner(address contractAddress)47 return false;47 return false;48 }48 }494950 // Deprecated51 //50 // Selector: toggleSponsoring(address,bool) fcac6d8652 // Selector: toggleSponsoring(address,bool) fcac6d8651 function toggleSponsoring(address contractAddress, bool enabled) public {53 function toggleSponsoring(address contractAddress, bool enabled) public {52 require(false, stub_error);54 require(false, stub_error);55 dummy = 0;57 dummy = 0;56 }58 }5960 // Selector: setSponsoringMode(address,uint8) fde8a56061 function setSponsoringMode(address contractAddress, uint8 mode) public {62 require(false, stub_error);63 contractAddress;64 mode;65 dummy = 0;66 }6768 // Selector: sponsoringMode(address) b70c726769 function sponsoringMode(address contractAddress)70 public71 view72 returns (uint8)73 {74 require(false, stub_error);75 contractAddress;76 dummy;77 return 0;78 }577958 // Selector: setSponsoringRateLimit(address,uint32) 77b6c90880 // Selector: setSponsoringRateLimit(address,uint32) 77b6c90859 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)81 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)tests/src/eth/api/ContractHelpers.soldiffbeforeafterboth12 function supportsInterface(bytes4 interfaceID) external view returns (bool);12 function supportsInterface(bytes4 interfaceID) external view returns (bool);13}13}141415// Selector: 31acb1fe15// Selector: 7b4866f916interface ContractHelpers is Dummy, ERC165 {16interface ContractHelpers is Dummy, ERC165 {17 // Selector: contractOwner(address) 5152b14c17 // Selector: contractOwner(address) 5152b14c18 function contractOwner(address contractAddress)18 function contractOwner(address contractAddress)26 view26 view27 returns (bool);27 returns (bool);282829 // Deprecated30 //29 // Selector: toggleSponsoring(address,bool) fcac6d8631 // Selector: toggleSponsoring(address,bool) fcac6d8630 function toggleSponsoring(address contractAddress, bool enabled) external;32 function toggleSponsoring(address contractAddress, bool enabled) external;3334 // Selector: setSponsoringMode(address,uint8) fde8a56035 function setSponsoringMode(address contractAddress, uint8 mode) external;3637 // Selector: sponsoringMode(address) b70c726738 function sponsoringMode(address contractAddress)39 external40 view41 returns (uint8);314232 // Selector: setSponsoringRateLimit(address,uint32) 77b6c90843 // Selector: setSponsoringRateLimit(address,uint32) 77b6c90833 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)44 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)