git.delta.rocks / unique-network / refs/commits / dc1fefd785d4

difftreelog

source

pallets/evm-contract-helpers/src/eth.rs5.0 KiBsourcehistory
1use core::marker::PhantomData;2use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};3use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};4use pallet_evm::{ExitReason, ExitRevert, OnCreate, OnMethodCall, PrecompileOutput};5use sp_core::H160;6use crate::{7	AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,8};9use frame_support::traits::Get;10use up_sponsorship::SponsorshipHandler;11use sp_std::{convert::TryInto, vec::Vec};1213struct ContractHelpers<T: Config>(SubstrateRecorder<T>);14impl<T: Config> WithRecorder<T> for ContractHelpers<T> {15	fn recorder(&self) -> &SubstrateRecorder<T> {16		&self.017	}1819	fn into_recorder(self) -> SubstrateRecorder<T> {20		self.021	}22}2324#[solidity_interface(name = "ContractHelpers")]25impl<T: Config> ContractHelpers<T> {26	fn contract_owner(&self, contract_address: address) -> Result<address> {27		Ok(<Owner<T>>::get(contract_address))28	}2930	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {31		Ok(<SelfSponsoring<T>>::get(contract_address))32	}3334	fn toggle_sponsoring(35		&mut self,36		caller: caller,37		contract_address: address,38		enabled: bool,39	) -> Result<void> {40		<Pallet<T>>::ensure_owner(contract_address, caller)?;41		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);42		Ok(())43	}4445	fn set_sponsoring_rate_limit(46		&mut self,47		caller: caller,48		contract_address: address,49		rate_limit: uint32,50	) -> Result<void> {51		<Pallet<T>>::ensure_owner(contract_address, caller)?;52		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());53		Ok(())54	}5556	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {57		Ok(<SponsoringRateLimit<T>>::get(contract_address)58			.try_into()59			.map_err(|_| "rate limit > u32::MAX")?)60	}6162	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {63		Ok(<Pallet<T>>::allowed(contract_address, user, true))64	}6566	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {67		Ok(<AllowlistEnabled<T>>::get(contract_address))68	}6970	fn toggle_allowlist(71		&mut self,72		caller: caller,73		contract_address: address,74		enabled: bool,75	) -> Result<void> {76		<Pallet<T>>::ensure_owner(contract_address, caller)?;77		<Pallet<T>>::toggle_allowlist(contract_address, enabled);78		Ok(())79	}8081	fn toggle_allowed(82		&mut self,83		caller: caller,84		contract_address: address,85		user: address,86		allowed: bool,87	) -> Result<void> {88		<Pallet<T>>::ensure_owner(contract_address, caller)?;89		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);90		Ok(())91	}92}9394pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);95impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {96	fn is_reserved(contract: &sp_core::H160) -> bool {97		contract == &T::ContractAddress::get()98	}99100	fn is_used(contract: &sp_core::H160) -> bool {101		contract == &T::ContractAddress::get()102	}103104	fn call(105		source: &sp_core::H160,106		target: &sp_core::H160,107		gas_left: u64,108		input: &[u8],109		value: sp_core::U256,110	) -> Option<PrecompileOutput> {111		// TODO: Extract to another OnMethodCall handler112		if !<Pallet<T>>::allowed(*target, *source, true) {113			return Some(PrecompileOutput {114				exit_status: ExitReason::Revert(ExitRevert::Reverted),115				cost: 0,116				output: {117					let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));118					writer.string("Target contract is allowlisted");119					writer.finish()120				},121				logs: sp_std::vec![],122			});123		}124125		if target != &T::ContractAddress::get() {126			return None;127		}128129		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));130		pallet_evm_coder_substrate::call(*source, helpers, value, input)131	}132133	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {134		(contract == &T::ContractAddress::get())135			.then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())136	}137}138139pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);140impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {141	fn on_create(owner: H160, contract: H160) {142		<Owner<T>>::insert(contract, owner);143	}144}145146pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);147impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {148	fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {149		if <SelfSponsoring<T>>::get(&call.0) && <Pallet<T>>::allowed(call.0, *who, false) {150			let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;151			if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who) {152				let rate_limit = <SponsoringRateLimit<T>>::get(&call.0);153				let limit_time = last_tx_block + rate_limit;154155				if block_number > limit_time {156					<SponsorBasket<T>>::insert(&call.0, who, block_number);157					return Some(call.0);158				}159			} else {160				<SponsorBasket<T>>::insert(&call.0, who, block_number);161				return Some(call.0);162			}163		}164		None165	}166}167168generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);169generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);