From a9bfe04e214ce9b23766ef11770de540321d611f Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 24 Jun 2021 19:43:17 +0000 Subject: [PATCH] feat: implement sponsoring primitive for contract helpers --- --- /dev/null +++ b/pallets/contract-helpers/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "pallet-contract-helpers" +version = "0.1.0" +edition = "2018" + +[dependencies.codec] +default-features = false +features = ['derive'] +package = 'parity-scale-codec' +version = '2.0.0' + +[dependencies.up-sponsorship] +default-features = false +path = '../../primitives/sponsorship' +version = '0.1.0' + +[dependencies] +frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +pallet-contracts = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +sp-runtime = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } +sp-std = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' } + +[features] +default = ["std"] +std = [ + "frame-support/std", + "frame-system/std", + "pallet-contracts/std", + "sp-runtime/std", + "sp-std/std", +] \ No newline at end of file --- a/pallets/contract-helpers/src/lib.rs +++ b/pallets/contract-helpers/src/lib.rs @@ -227,4 +227,35 @@ } } + 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 { + match IsSubType::>::is_sub_type(call) { + Some(pallet_contracts::Call::call(dest, _value, _gas_limit, _data)) => { + let called_contract: T::AccountId = + T::Lookup::lookup((*dest).clone()).unwrap_or_default(); + if >::get(&called_contract) { + 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