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

difftreelog

feat implement sponsoring primitive for contract helpers

Yaroslav Bolyukin2021-06-24parent: #bc320b6.patch.diff
in: master

2 files changed

addedpallets/contract-helpers/Cargo.tomldiffbeforeafterboth
after · pallets/contract-helpers/Cargo.toml
1[package]2name = "pallet-contract-helpers"3version = "0.1.0"4edition = "2018"56[dependencies.codec]7default-features = false8features = ['derive']9package = 'parity-scale-codec'10version = '2.0.0'1112[dependencies.up-sponsorship]13default-features = false14path = '../../primitives/sponsorship'15version = '0.1.0'1617[dependencies]18frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }19frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }20pallet-contracts = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }21sp-runtime = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }22sp-std = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }2324[features]25default = ["std"]26std = [27    "frame-support/std",28    "frame-system/std",29    "pallet-contracts/std",30    "sp-runtime/std",31    "sp-std/std",32]
modifiedpallets/contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/contract-helpers/src/lib.rs
+++ b/pallets/contract-helpers/src/lib.rs
@@ -227,4 +227,35 @@
 		}
 	}
 
+	pub struct ContractSponsorshipHandler<T>(PhantomData<T>);
+	impl<T, C> SponsorshipHandler<T::AccountId, C> for ContractSponsorshipHandler<T>
+	where
+		T: Config,
+		C: IsSubType<pallet_contracts::Call<T>>,
+		T::AccountId: UncheckedFrom<T::Hash>,
+		T::AccountId: AsRef<[u8]>,
+	{
+		fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {
+			match IsSubType::<pallet_contracts::Call<T>>::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 <SelfSponsoring<T>>::get(&called_contract) {
+						let last_tx_block = SponsorBasket::<T>::get(&called_contract, &who);
+						let block_number =
+							<frame_system::Pallet<T>>::block_number() as T::BlockNumber;
+						let rate_limit = SponsoringRateLimit::<T>::get(&called_contract);
+						let limit_time = last_tx_block + rate_limit;
+
+						if block_number >= limit_time {
+							SponsorBasket::<T>::insert(&called_contract, who, block_number);
+							return Some(called_contract);
+						}
+					}
+				}
+				_ => {}
+			}
+			None
+		}
+	}
 }