From e2735f296ec85ed9855d6b1bea7aebd93c4645c5 Mon Sep 17 00:00:00 2001 From: Tidus91 <65631603+Tidus91@users.noreply.github.com> Date: Wed, 27 Dec 2023 14:49:57 +0100 Subject: [PATCH] feat: add interfaces for each abstract contract used by the Starknet core contract #4 (#6) --- .../src/clients/sovereign.rs | 18 ++++- .../src/interfaces/governance.rs | 72 +++++++++++++++++++ .../src/interfaces/governed_finalizable.rs | 40 +++++++++++ .../src/interfaces/mod.rs | 4 ++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 crates/starknet-core-contract-client/src/interfaces/governance.rs create mode 100644 crates/starknet-core-contract-client/src/interfaces/governed_finalizable.rs diff --git a/crates/starknet-core-contract-client/src/clients/sovereign.rs b/crates/starknet-core-contract-client/src/clients/sovereign.rs index acd35db..9287420 100644 --- a/crates/starknet-core-contract-client/src/clients/sovereign.rs +++ b/crates/starknet-core-contract-client/src/clients/sovereign.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use crate::{ - interfaces::{Operator, ProxySupport, StarknetMessaging, StarknetSovereignContract}, + interfaces::{Operator, ProxySupport, StarknetMessaging, StarknetSovereignContract, StarknetGovernance, GovernedFinalizable}, LocalMiddleware, }; use ethers::types::Address; @@ -12,6 +12,8 @@ pub struct StarknetSovereignContractClient { messaging: StarknetMessaging, operator: Operator, proxy_support: ProxySupport, + governance: StarknetGovernance, + governed_finalizable: GovernedFinalizable } impl StarknetSovereignContractClient { @@ -20,7 +22,9 @@ impl StarknetSovereignContractClient { core_contract: StarknetSovereignContract::new(address, client.clone()), messaging: StarknetMessaging::new(address, client.clone()), operator: Operator::new(address, client.clone()), - proxy_support: ProxySupport::new(address, client), + proxy_support: ProxySupport::new(address, client.clone()), + governance: StarknetGovernance::new(address, client.clone()), + governed_finalizable: GovernedFinalizable::new(address, client.clone()) } } } @@ -45,3 +49,13 @@ impl AsRef> for StarknetSovereignContractClient { &self.operator } } +impl AsRef> for StarknetSovereignContractClient { + fn as_ref(&self) -> &StarknetGovernance { + &self.governance + } +} +impl AsRef> for StarknetSovereignContractClient { + fn as_ref(&self) -> &GovernedFinalizable { + &self.governed_finalizable + } +} diff --git a/crates/starknet-core-contract-client/src/interfaces/governance.rs b/crates/starknet-core-contract-client/src/interfaces/governance.rs new file mode 100644 index 0000000..9aa9ec2 --- /dev/null +++ b/crates/starknet-core-contract-client/src/interfaces/governance.rs @@ -0,0 +1,72 @@ +use async_trait::async_trait; +use ethers::{prelude::abigen, providers::Middleware, types::H160}; + +use crate::Error; + +type Adress = H160; + +abigen!( + StarknetGovernance, + r#"[ + function starknetIsGovernor(address user) external view returns (bool) + function starknetNominateNewGovernor(address newGovernor) external + function starknetRemoveGovernor(address governorForRemoval) external + function starknetAcceptGovernance() external + function starknetCancelNomination() external + ]"#, +); + +#[async_trait] +pub trait StarknetGovernanceTrait { + async fn starknet_is_governor(&self, user: Adress) -> Result>; + async fn starknet_nominate_new_governor(&self, new_governor: Adress) -> Result<(), Error>; + async fn starknet_remove_governor(&self, governor_for_removal: Adress) -> Result<(), Error>; + async fn starknet_accept_governance(&self) -> Result<(), Error>; + async fn starknet_cancel_nomination(&self) -> Result<(), Error>; +} + +#[async_trait] +impl StarknetGovernanceTrait for T +where + T: AsRef> + Send + Sync, +{ + async fn starknet_is_governor(&self, user: Adress) -> Result> { + self.as_ref() + .starknet_is_governor(user) + .call() + .await + .map_err(Into::into) + } + + async fn starknet_nominate_new_governor(&self, new_governor: Adress) -> Result<(), Error> { + self.as_ref() + .starknet_nominate_new_governor(new_governor) + .call() + .await + .map_err(Into::into) + } + + async fn starknet_remove_governor(&self, governor_for_removal: Adress) -> Result<(), Error> { + self.as_ref() + .starknet_remove_governor(governor_for_removal) + .call() + .await + .map_err(Into::into) + } + + async fn starknet_accept_governance(&self) -> Result<(), Error> { + self.as_ref() + .starknet_accept_governance() + .call() + .await + .map_err(Into::into) + } + + async fn starknet_cancel_nomination(&self) -> Result<(), Error> { + self.as_ref() + .starknet_cancel_nomination() + .call() + .await + .map_err(Into::into) + } +} diff --git a/crates/starknet-core-contract-client/src/interfaces/governed_finalizable.rs b/crates/starknet-core-contract-client/src/interfaces/governed_finalizable.rs new file mode 100644 index 0000000..da627fe --- /dev/null +++ b/crates/starknet-core-contract-client/src/interfaces/governed_finalizable.rs @@ -0,0 +1,40 @@ +use async_trait::async_trait; +use ethers::{prelude::abigen, providers::Middleware}; + +use crate::Error; + +abigen!( + GovernedFinalizable, + r#"[ + function isFinalized() public view returns (bool) + function finalize() external onlyGovernance notFinalized + ]"#, +); + +#[async_trait] +pub trait GovernedFinalizableTrait { + async fn is_finalized(&self) -> Result>; + async fn finalize(&self) -> Result<(), Error>; +} + +#[async_trait] +impl GovernedFinalizableTrait for T +where + T: AsRef> + Send + Sync, +{ + async fn is_finalized(&self) -> Result> { + self.as_ref() + .is_finalized() + .call() + .await + .map_err(Into::into) + } + + async fn finalize(&self) -> Result<(), Error> { + self.as_ref() + .finalize() + .call() + .await + .map_err(Into::into) + } +} diff --git a/crates/starknet-core-contract-client/src/interfaces/mod.rs b/crates/starknet-core-contract-client/src/interfaces/mod.rs index 4fa0907..422128e 100644 --- a/crates/starknet-core-contract-client/src/interfaces/mod.rs +++ b/crates/starknet-core-contract-client/src/interfaces/mod.rs @@ -3,9 +3,13 @@ mod operator; mod proxy_support; mod sovereign_core_contract; mod validity_core_contract; +mod governance; +mod governed_finalizable; pub use messaging::{StarknetMessaging, StarknetMessagingTrait}; pub use operator::{Operator, OperatorTrait}; pub use proxy_support::{ProxySupport, ProxySupportTrait}; pub use sovereign_core_contract::{StarknetSovereignContract, StarknetSovereignContractTrait}; pub use validity_core_contract::{StarknetValidityContract, StarknetValidityContractTrait}; +pub use governance::{StarknetGovernance, StarknetGovernanceTrait}; +pub use governed_finalizable::{GovernedFinalizable, GovernedFinalizableTrait};