-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement : Refactor Settings (#106)
* feat : added settings * changelog * feat : added env settings fucntion * feat : updated settings module implementation * feat : updated provider config * feat : added arc for aws config * chore : refactoring
- Loading branch information
Showing
31 changed files
with
296 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,19 @@ | ||
use std::str::FromStr; | ||
use serde::{Deserialize, Serialize}; | ||
use utils::settings::Settings; | ||
|
||
use alloy::{network::Ethereum, providers::ProviderBuilder, rpc::client::RpcClient}; | ||
use async_trait::async_trait; | ||
use da_client_interface::DaConfig; | ||
use url::Url; | ||
use utils::env_utils::get_env_var_or_panic; | ||
|
||
use crate::EthereumDaClient; | ||
|
||
#[derive(Clone, Debug)] | ||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct EthereumDaConfig { | ||
pub rpc_url: String, | ||
pub memory_pages_contract: String, | ||
pub private_key: String, | ||
} | ||
|
||
#[async_trait] | ||
impl DaConfig<EthereumDaClient> for EthereumDaConfig { | ||
fn new_from_env() -> Self { | ||
Self { | ||
rpc_url: get_env_var_or_panic("SETTLEMENT_RPC_URL"), | ||
memory_pages_contract: get_env_var_or_panic("MEMORY_PAGES_CONTRACT_ADDRESS"), | ||
private_key: get_env_var_or_panic("PRIVATE_KEY"), | ||
} | ||
} | ||
async fn build_client(&self) -> EthereumDaClient { | ||
let client = | ||
RpcClient::new_http(Url::from_str(self.rpc_url.as_str()).expect("Failed to parse SETTLEMENT_RPC_URL")); | ||
let provider = ProviderBuilder::<_, Ethereum>::new().on_client(client); | ||
|
||
EthereumDaClient { provider } | ||
impl EthereumDaConfig { | ||
pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result<Self> { | ||
Ok(Self { | ||
rpc_url: settings.get_settings("SETTLEMENT_RPC_URL")?, | ||
memory_pages_contract: settings.get_settings("MEMORY_PAGES_CONTRACT_ADDRESS")?, | ||
private_key: settings.get_settings("PRIVATE_KEY")?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use utils::settings::Settings; | ||
|
||
#[derive(Clone, Serialize, Deserialize)] | ||
pub struct AWSSNSConfig { | ||
/// AWS SNS ARN | ||
pub sns_arn: String, | ||
/// AWS SNS region | ||
pub sns_arn_region: String, | ||
} | ||
|
||
impl AWSSNSConfig { | ||
pub fn new_with_settings(settings: &impl Settings) -> color_eyre::Result<Self> { | ||
Ok(Self { | ||
sns_arn: settings.get_settings("AWS_SNS_ARN")?, | ||
sns_arn_region: settings.get_settings("AWS_SNS_REGION")?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
mod config; | ||
|
||
use crate::alerts::aws_sns::config::AWSSNSConfig; | ||
use crate::alerts::Alerts; | ||
use crate::config::ProviderConfig; | ||
use async_trait::async_trait; | ||
use aws_sdk_sns::config::Region; | ||
use aws_sdk_sns::Client; | ||
use utils::env_utils::get_env_var_or_panic; | ||
use utils::settings::Settings; | ||
|
||
pub const AWS_SNS_SETTINGS_NAME: &str = "sns"; | ||
|
||
pub struct AWSSNS { | ||
client: Client, | ||
topic_arn: String, | ||
} | ||
|
||
impl AWSSNS { | ||
/// To create a new SNS client | ||
pub async fn new() -> Self { | ||
let sns_region = get_env_var_or_panic("AWS_SNS_REGION"); | ||
let config = aws_config::from_env().region(Region::new(sns_region)).load().await; | ||
AWSSNS { client: Client::new(&config) } | ||
pub async fn new_with_settings(settings: &impl Settings, provider_config: ProviderConfig) -> Self { | ||
match provider_config { | ||
ProviderConfig::AWS(aws_config) => { | ||
let sns_config = AWSSNSConfig::new_with_settings(settings) | ||
.expect("Not able to get Aws sns config from provided settings"); | ||
Self { client: Client::new(&aws_config), topic_arn: sns_config.sns_arn } | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Alerts for AWSSNS { | ||
async fn send_alert_message(&self, message_body: String) -> color_eyre::Result<()> { | ||
let topic_arn = get_env_var_or_panic("AWS_SNS_ARN"); | ||
self.client.publish().topic_arn(topic_arn).message(message_body).send().await?; | ||
self.client.publish().topic_arn(self.topic_arn.clone()).message(message_body).send().await?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.