From 7159269f74fad4117b5ddcb8a4133c842b5d3a01 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 02 Feb 2022 11:38:55 +0000 Subject: [PATCH] refactor: remove contract-helpers pallet --- --- a/pallets/contract-helpers/Cargo.toml +++ /dev/null @@ -1,29 +0,0 @@ -[package] -name = "pallet-contract-helpers" -version = "0.1.0" -edition = "2021" - -[dependencies.codec] -default-features = false -features = ['derive'] -package = 'parity-scale-codec' -version = '2.3.0' - -[dependencies] -scale-info = { version = "1.0.0", default-features = false, features = ["derive"] } -frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.15' } -frame-system = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.15' } -pallet-contracts = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.15' } -sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.15' } -sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.15' } -up-sponsorship = { version = "0.1.0", default-features = false, git = "https://github.com/UniqueNetwork/pallet-sponsoring", branch = 'polkadot-v0.9.15' } - -[features] -default = ["std"] -std = [ - "frame-support/std", - "frame-system/std", - "pallet-contracts/std", - "sp-runtime/std", - "sp-std/std", -] --- a/pallets/contract-helpers/src/lib.rs +++ /dev/null @@ -1,285 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -pub use pallet::*; - -#[frame_support::pallet] -pub mod pallet { - use frame_support::sp_runtime::traits::StaticLookup; - use frame_support::{pallet_prelude::*, traits::IsSubType}; - use frame_system::pallet_prelude::*; - use frame_system::Config as SysConfig; - use pallet_contracts::chain_extension::UncheckedFrom; - use sp_runtime::{ - traits::{DispatchInfoOf, Hash, PostDispatchInfoOf, SignedExtension}, - transaction_validity, - }; - use sp_std::vec::Vec; - use up_sponsorship::SponsorshipHandler; - - #[pallet::error] - pub enum Error { - /// Should be contract owner - NoPermission, - } - - #[pallet::config] - pub trait Config: frame_system::Config + pallet_contracts::Config { - type DefaultSponsoringRateLimit: Get; - } - - #[pallet::pallet] - #[pallet::generate_store(pub(super) trait Store)] - pub struct Pallet(_); - - #[pallet::storage] - pub(super) type Owner = StorageMap< - Hasher = Twox128, - Key = T::AccountId, - Value = T::AccountId, - QueryKind = ValueQuery, - >; - - #[pallet::storage] - pub(super) type AllowlistEnabled = - StorageMap; - - #[pallet::storage] - pub(super) type Allowlist = StorageDoubleMap< - Hasher1 = Twox128, - Key1 = T::AccountId, - Hasher2 = Twox64Concat, - Key2 = T::AccountId, - Value = bool, - QueryKind = ValueQuery, - >; - - #[pallet::storage] - pub(super) type SelfSponsoring = - StorageMap; - - #[pallet::storage] - pub(super) type SponsoringRateLimit = StorageMap< - Hasher = Twox128, - Key = T::AccountId, - Value = T::BlockNumber, - QueryKind = ValueQuery, - OnEmpty = T::DefaultSponsoringRateLimit, - >; - - #[pallet::storage] - pub(super) type SponsorBasket = StorageDoubleMap< - Hasher1 = Twox128, - Key1 = T::AccountId, - Hasher2 = Twox128, - Key2 = T::AccountId, - Value = T::BlockNumber, - QueryKind = ValueQuery, - >; - - impl Pallet { - pub fn allowed(contract: T::AccountId, user: T::AccountId, default: bool) -> bool { - if !>::get(&contract) { - return default; - } - >::get(&contract, &user) || >::get(contract) == user - } - } - - #[pallet::call] - impl Pallet { - #[pallet::weight(0)] - pub fn toggle_sponsoring( - origin: OriginFor, - contract: T::AccountId, - sponsoring: bool, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!( - >::get(&contract) == sender, - >::NoPermission - ); - - if sponsoring { - >::insert(contract, true); - } else { - >::remove(contract); - } - Ok(()) - } - - #[pallet::weight(0)] - pub fn toggle_allowlist( - origin: OriginFor, - contract: T::AccountId, - enabled: bool, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!( - >::get(&contract) == sender, - >::NoPermission - ); - - if enabled { - >::insert(contract, true); - } else { - >::remove(contract); - } - Ok(()) - } - - #[pallet::weight(0)] - pub fn toggle_allowed( - origin: OriginFor, - contract: T::AccountId, - user: T::AccountId, - allowed: bool, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!( - >::get(&contract) == sender, - >::NoPermission - ); - - if allowed { - >::insert(contract, user, true); - } else { - >::remove(contract, user); - } - Ok(()) - } - - #[pallet::weight(0)] - pub fn set_sponsoring_rate_limit( - origin: OriginFor, - contract: T::AccountId, - rate_limit: T::BlockNumber, - ) -> DispatchResult { - let sender = ensure_signed(origin)?; - ensure!( - >::get(&contract) == sender, - >::NoPermission - ); - - >::insert(contract, rate_limit); - Ok(()) - } - } - - #[derive(Encode, Decode, Clone, PartialEq, Eq, scale_info::TypeInfo)] - pub struct ContractHelpersExtension(PhantomData); - impl core::fmt::Debug for ContractHelpersExtension { - fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { - fmt.debug_struct("ContractHelpersExtension").finish() - } - } - - type CodeHash = ::Hash; - impl SignedExtension for ContractHelpersExtension - where - T: Config + Send + Sync, - ::Call: sp_runtime::traits::Dispatchable, - ::Call: IsSubType>, - T::AccountId: UncheckedFrom, - T::AccountId: AsRef<[u8]>, - { - const IDENTIFIER: &'static str = "ContractHelpers"; - type AccountId = T::AccountId; - type Call = ::Call; - type AdditionalSigned = (); - type Pre = Option<(Self::AccountId, CodeHash, Vec)>; - - fn additional_signed(&self) -> Result<(), transaction_validity::TransactionValidityError> { - Ok(()) - } - - fn validate( - &self, - who: &T::AccountId, - call: &Self::Call, - _info: &DispatchInfoOf, - _len: usize, - ) -> transaction_validity::TransactionValidity { - if let Some(pallet_contracts::Call::call { - dest, - value: _value, - gas_limit: _gas_limit, - data: _data, - }) = IsSubType::>::is_sub_type(call) - { - let called_contract: T::AccountId = - T::Lookup::lookup((*dest).clone()).unwrap_or_default(); - if !>::allowed(called_contract, who.clone(), true) { - return Err(transaction_validity::InvalidTransaction::Call.into()); - } - } - Ok(transaction_validity::ValidTransaction::default()) - } - - fn pre_dispatch( - self, - who: &Self::AccountId, - call: &Self::Call, - _info: &DispatchInfoOf, - _len: usize, - ) -> Result { - match IsSubType::>::is_sub_type(call) { - Some(pallet_contracts::Call::instantiate { - code_hash, salt, .. - }) => Ok(Some((who.clone(), *code_hash, salt.clone()))), - Some(pallet_contracts::Call::instantiate_with_code { code, salt, .. }) => { - let code_hash = &T::Hashing::hash(code); - Ok(Some((who.clone(), *code_hash, salt.clone()))) - } - _ => Ok(None), - } - } - - fn post_dispatch( - pre: Self::Pre, - _info: &DispatchInfoOf, - _post_info: &PostDispatchInfoOf, - _len: usize, - _result: &DispatchResult, - ) -> Result<(), TransactionValidityError> { - if let Some((who, code_hash, salt)) = pre { - let new_contract_address = - >::contract_address(&who, &code_hash, &salt); - >::insert(&new_contract_address, &who); - } - - Ok(()) - } - } - - pub struct ContractSponsorshipHandler(PhantomData); - impl SponsorshipHandler for ContractSponsorshipHandler - where - T: Config, - C: IsSubType>, - T::AccountId: UncheckedFrom, - T::AccountId: AsRef<[u8]>, - { - fn get_sponsor(who: &T::AccountId, call: &C) -> Option { - if let Some(pallet_contracts::Call::call { dest, .. }) = - IsSubType::>::is_sub_type(call) - { - let called_contract: T::AccountId = - T::Lookup::lookup((*dest).clone()).unwrap_or_default(); - if >::get(&called_contract) - && >::allowed(called_contract.clone(), who.clone(), false) - { - let last_tx_block = SponsorBasket::::get(&called_contract, &who); - let block_number = >::block_number() as T::BlockNumber; - let rate_limit = SponsoringRateLimit::::get(&called_contract); - let limit_time = last_tx_block + rate_limit; - - if block_number >= limit_time { - SponsorBasket::::insert(&called_contract, who, block_number); - return Some(called_contract); - } - } - } - None - } - } -} -- gitstuff