1use core::marker::PhantomData;2use evm_coder::{abi::AbiWriter, execution::Result, solidity_interface, types::*};3use pallet_evm_coder_substrate::SubstrateRecorder;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::vec::Vec;1213struct ContractHelpers<T: Config>(SubstrateRecorder<T>);1415#[solidity_interface(name = "ContractHelpers")]16impl<T: Config> ContractHelpers<T> {17 fn contract_owner(&self, contract: address) -> Result<address> {18 self.0.consume_sload()?;19 Ok(<Owner<T>>::get(contract))20 }2122 fn sponsoring_enabled(&self, contract: address) -> Result<bool> {23 self.0.consume_sload()?;24 Ok(<SelfSponsoring<T>>::get(contract))25 }2627 fn toggle_sponsoring(28 &mut self,29 caller: caller,30 contract: address,31 enabled: bool,32 ) -> Result<void> {33 self.0.consume_sload()?;34 <Pallet<T>>::ensure_owner(contract, caller)?;35 self.0.consume_sstore()?;36 <Pallet<T>>::toggle_sponsoring(contract, enabled);37 Ok(())38 }3940 fn set_sponsoring_rate_limit(41 &mut self,42 caller: caller,43 contract: address,44 rate_limit: uint32,45 ) -> Result<void> {46 self.0.consume_sload()?;47 <Pallet<T>>::ensure_owner(contract, caller)?;48 self.0.consume_sstore()?;49 <Pallet<T>>::set_sponsoring_rate_limit(contract, rate_limit.into());50 Ok(())51 }5253 fn allowed(&self, contract: address, user: address) -> Result<bool> {54 self.0.consume_sload()?;55 Ok(<Pallet<T>>::allowed(contract, user, true))56 }5758 fn allowlist_enabled(&self, contract: address) -> Result<bool> {59 self.0.consume_sload()?;60 Ok(<AllowlistEnabled<T>>::get(contract))61 }6263 fn toggle_allowlist(64 &mut self,65 caller: caller,66 contract: address,67 enabled: bool,68 ) -> Result<void> {69 self.0.consume_sload()?;70 <Pallet<T>>::ensure_owner(contract, caller)?;71 self.0.consume_sstore()?;72 <Pallet<T>>::toggle_allowlist(contract, enabled);73 Ok(())74 }7576 fn toggle_allowed(77 &mut self,78 caller: caller,79 contract: address,80 user: address,81 allowed: bool,82 ) -> Result<void> {83 self.0.consume_sload()?;84 <Pallet<T>>::ensure_owner(contract, caller)?;85 self.0.consume_sstore()?;86 <Pallet<T>>::toggle_allowed(contract, user, allowed);87 Ok(())88 }89}9091pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);92impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {93 fn is_reserved(contract: &sp_core::H160) -> bool {94 contract == &T::ContractAddress::get()95 }9697 fn is_used(contract: &sp_core::H160) -> bool {98 contract == &T::ContractAddress::get()99 }100101 fn call(102 source: &sp_core::H160,103 target: &sp_core::H160,104 gas_left: u64,105 input: &[u8],106 value: sp_core::U256,107 ) -> Option<PrecompileOutput> {108 109 if !<Pallet<T>>::allowed(*target, *source, true) {110 return Some(PrecompileOutput {111 exit_status: ExitReason::Revert(ExitRevert::Reverted),112 cost: 0,113 output: {114 let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));115 writer.string("Target contract is allowlisted");116 writer.finish()117 },118 logs: sp_std::vec![],119 });120 }121122 if target != &T::ContractAddress::get() {123 return None;124 }125126 let mut helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));127 let result = pallet_evm_coder_substrate::call_internal(*source, &mut helpers, value, input);128 helpers.0.evm_to_precompile_output(result)129 }130131 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {132 (contract == &T::ContractAddress::get())133 .then(|| include_bytes!("./stubs/ContractHelpers.bin").to_vec())134 }135}136137pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);138impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {139 fn on_create(owner: H160, contract: H160) {140 <Owner<T>>::insert(contract, owner);141 }142}143144pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);145impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {146 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {147 if <SelfSponsoring<T>>::get(&call.0) && <Pallet<T>>::allowed(call.0, *who, false) {148 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;149 if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who) {150 let rate_limit = <SponsoringRateLimit<T>>::get(&call.0);151 let limit_time = last_tx_block + rate_limit;152153 if block_number > limit_time {154 <SponsorBasket<T>>::insert(&call.0, who, block_number);155 return Some(call.0);156 }157 } else {158 <SponsorBasket<T>>::insert(&call.0, who, block_number);159 return Some(call.0);160 }161 }162 None163 }164}