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

difftreelog

path: Add 'self_sponsored_enable' and 'remove_sponsor'.

Trubnikov Sergey2022-08-05parent: #9d33b05.patch.diff
in: master

2 files changed

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
47 T::AccountId: AsRef<[u8]>,48 T::AccountId: AsRef<[u8]>,
48{49{
49 /// Get contract ovner50 /// Get contract ovner
50 /// 51 ///
51 /// @param Contract_address contract for which the owner is being determined.52 /// @param Contract_address contract for which the owner is being determined.
52 /// @return Contract owner.53 /// @return Contract owner.
53 fn contract_owner(&self, contract_address: address) -> Result<address> {54 fn contract_owner(&self, contract_address: address) -> Result<address> {
54 Ok(<Owner<T>>::get(contract_address))55 Ok(<Owner<T>>::get(contract_address))
55 }56 }
5657
57 /// Set sponsor.58 /// Set sponsor.
58 /// 59 ///
59 /// @param contract_address Contract for which a sponsor is being established.60 /// @param contract_address Contract for which a sponsor is being established.
60 /// @param sponsor User address who set as pending sponsor.61 /// @param sponsor User address who set as pending sponsor.
61 fn set_sponsor(62 fn set_sponsor(
73 Ok(())74 Ok(())
74 }75 }
76
77 /// Set contract as self sponsored.
78 ///
79 /// @param contract_address Contract for which a self sponsoring is being enabled.
80 fn self_sponsored_enable(&mut self, caller: caller, contract_address: address) -> Result<void> {
81 Pallet::<T>::self_sponsored_enable(&T::CrossAccountId::from_eth(caller), contract_address)
82 .map_err(dispatch_to_evm::<T>)?;
83 Ok(())
84 }
85
86 /// Remove sponsor.
87 ///
88 /// @param contract_address Contract for which a sponsorship is being removed.
89 fn remove_sponsor(&mut self, caller: caller, contract_address: address) -> Result<void> {
90 Pallet::<T>::remove_sponsor(&T::CrossAccountId::from_eth(caller), contract_address)
91 .map_err(dispatch_to_evm::<T>)?;
92 Ok(())
93 }
7594
76 /// Confirm sponsorship.95 /// Confirm sponsorship.
77 /// 96 ///
78 /// @dev Caller must be same that set via [`set_sponsor`].97 /// @dev Caller must be same that set via [`set_sponsor`].
79 /// 98 ///
80 /// @param contract_address Сontract for which need to confirm sponsorship.99 /// @param contract_address Сontract for which need to confirm sponsorship.
81 fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {100 fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {
82 Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)101 Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)
85 }104 }
86105
87 /// Get current sponsor.106 /// Get current sponsor.
88 /// 107 ///
89 /// @param contract_address The contract for which a sponsor is requested.108 /// @param contract_address The contract for which a sponsor is requested.
90 /// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.109 /// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
91 fn get_sponsor(&self, contract_address: address) -> Result<(address, uint256)> {110 fn get_sponsor(&self, contract_address: address) -> Result<(address, uint256)> {
96 }115 }
97116
98 /// Check tat contract has confirmed sponsor.117 /// Check tat contract has confirmed sponsor.
99 /// 118 ///
100 /// @param contract_address The contract for which the presence of a confirmed sponsor is checked.119 /// @param contract_address The contract for which the presence of a confirmed sponsor is checked.
101 /// @return **true** if contract has confirmed sponsor.120 /// @return **true** if contract has confirmed sponsor.
102 fn has_sponsor(&self, contract_address: address) -> Result<bool> {121 fn has_sponsor(&self, contract_address: address) -> Result<bool> {
103 Ok(Pallet::<T>::get_sponsor(contract_address).is_some())122 Ok(Pallet::<T>::get_sponsor(contract_address).is_some())
104 }123 }
105124
106 /// Check tat contract has pending sponsor.125 /// Check tat contract has pending sponsor.
107 /// 126 ///
108 /// @param contract_address The contract for which the presence of a pending sponsor is checked.127 /// @param contract_address The contract for which the presence of a pending sponsor is checked.
109 /// @return **true** if contract has pending sponsor.128 /// @return **true** if contract has pending sponsor.
110 fn has_pending_sponsor(&self, contract_address: address) -> Result<bool> {129 fn has_pending_sponsor(&self, contract_address: address) -> Result<bool> {
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -49,10 +49,7 @@
 		NoPendingSponsor,
 	}
 
-	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
-
 	#[pallet::pallet]
-	#[pallet::storage_version(STORAGE_VERSION)]
 	#[pallet::generate_store(pub(super) trait Store)]
 	pub struct Pallet<T>(_);
 
@@ -102,7 +99,7 @@
 	>;
 
 	/// Storage for last sponsored block.
-	/// 
+	///
 	/// * **Key1** - contract address.
 	/// * **Key2** - sponsored user address.
 	/// * **Value** - last sponsored block number.
@@ -144,25 +141,15 @@
 		Value = bool,
 		QueryKind = ValueQuery,
 	>;
-
-	#[pallet::hooks]
-	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
-		fn on_runtime_upgrade() -> Weight {
-			let storage_version = StorageVersion::get::<Pallet<T>>();
-			if storage_version < StorageVersion::new(1) {}
 
-			0
-		}
-	}
-
 	impl<T: Config> Pallet<T> {
 		/// Get contract owner.
 		pub fn contract_owner(contract: H160) -> H160 {
 			<Owner<T>>::get(contract)
 		}
-		
-		/// Set `sponsor` for `contract`. 
-		/// 
+
+		/// Set `sponsor` for `contract`.
+		///
 		/// `sender` must be owner of contract.
 		pub fn set_sponsor(
 			sender: &T::CrossAccountId,
@@ -177,8 +164,31 @@
 			Ok(())
 		}
 
+		/// Set `contract` as self sponsored.
+		///
+		/// `sender` must be owner of contract.
+		pub fn self_sponsored_enable(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
+			Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;
+			Sponsoring::<T>::insert(
+				contract,
+				SponsorshipState::<T::CrossAccountId>::Confirmed(T::CrossAccountId::from_eth(
+					contract,
+				)),
+			);
+			Ok(())
+		}
+
+		/// Remove sponsor for `contract`.
+		///
+		/// `sender` must be owner of contract.
+		pub fn remove_sponsor(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
+			Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;
+			Sponsoring::<T>::remove(contract);
+			Ok(())
+		}
+
 		/// Confirm sponsorship.
-		/// 
+		///
 		/// `sender` must be same that set via [`set_sponsor`].
 		pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
 			match Sponsoring::<T>::get(contract) {