diff --git a/contracts/near/admin-controlled/src/lib.rs b/contracts/near/admin-controlled/src/lib.rs index 1c346e20a..9515a41b8 100644 --- a/contracts/near/admin-controlled/src/lib.rs +++ b/contracts/near/admin-controlled/src/lib.rs @@ -1,14 +1,13 @@ +pub mod macros; +pub use macros::*; + use near_sdk::env; pub type Mask = u128; pub trait AdminControlled { fn is_owner(&self) -> bool { - env::current_account_id() == env::signer_account_id() - } - - fn assert_owner(&self) { - assert!(self.is_owner()); + env::current_account_id() == env::predecessor_account_id() } /// Return the current mask representing all paused events. diff --git a/contracts/near/admin-controlled/src/macros.rs b/contracts/near/admin-controlled/src/macros.rs new file mode 100644 index 000000000..6d47ad78f --- /dev/null +++ b/contracts/near/admin-controlled/src/macros.rs @@ -0,0 +1,21 @@ +#[macro_export] +macro_rules! impl_admin_controlled { + ($contract: ident, $paused: ident) => { + use admin_controlled::{AdminControlled as AdminControlledInner, Mask as MaskInner}; + use near_sdk as near_sdk_inner; + + #[near_bindgen] + impl AdminControlledInner for $contract { + #[result_serializer(borsh)] + fn get_paused(&self) -> MaskInner { + self.$paused + } + + #[result_serializer(borsh)] + fn set_paused(&mut self, #[serializer(borsh)] paused: MaskInner) { + near_sdk_inner::assert_self(); + self.$paused = paused; + } + } + }; +} diff --git a/contracts/near/eth-client/src/lib.rs b/contracts/near/eth-client/src/lib.rs index f9b0a3d0e..2f78ed3e8 100644 --- a/contracts/near/eth-client/src/lib.rs +++ b/contracts/near/eth-client/src/lib.rs @@ -1,4 +1,4 @@ -use admin_controlled::{AdminControlled, Mask}; +use admin_controlled::Mask; use borsh::{BorshDeserialize, BorshSerialize}; use eth_types::*; use near_sdk::collections::UnorderedMap; @@ -111,20 +111,6 @@ impl Default for EthClient { } } -#[near_bindgen] -impl AdminControlled for EthClient { - #[result_serializer(borsh)] - fn get_paused(&self) -> Mask { - self.paused - } - - #[result_serializer(borsh)] - fn set_paused(&mut self, #[serializer(borsh)] paused: Mask) { - self.assert_owner(); - self.paused = paused; - } -} - #[near_bindgen] impl EthClient { #[init] @@ -441,3 +427,5 @@ impl EthClient { (H256(pair.0), H256(pair.1)) } } + +admin_controlled::impl_admin_controlled!(EthClient, paused); diff --git a/contracts/near/eth-prover/src/lib.rs b/contracts/near/eth-prover/src/lib.rs index d78c8f557..6d8fff9a1 100644 --- a/contracts/near/eth-prover/src/lib.rs +++ b/contracts/near/eth-prover/src/lib.rs @@ -1,4 +1,4 @@ -use admin_controlled::{AdminControlled, Mask}; +use admin_controlled::Mask; use borsh::{BorshDeserialize, BorshSerialize}; use eth_types::*; use near_sdk::{env, ext_contract, near_bindgen, PromiseOrValue}; @@ -320,16 +320,4 @@ impl EthProver { } } -#[near_bindgen] -impl AdminControlled for EthProver { - #[result_serializer(borsh)] - fn get_paused(&self) -> Mask { - self.paused - } - - #[result_serializer(borsh)] - fn set_paused(&mut self, #[serializer(borsh)] paused: Mask) { - self.assert_owner(); - self.paused = paused; - } -} +admin_controlled::impl_admin_controlled!(EthProver, paused);