Skip to content

Commit 493c323

Browse files
authored
feat(sequencer): Load configuration parameters from ENV vars and [profile].env files (#15)
1 parent 5100d68 commit 493c323

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

metabased-sequencer/interceptor/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ edition = "2021"
66
[dependencies]
77
async-trait = "0.1"
88
alloy = { version = "0.4.2", features = ["full"] }
9+
dotenv = "0.15"
10+
figment = { version = "0.10", features = ["env"] }
911
jsonrpsee = { version = "0.24.3", features = ["full"] }
1012
tokio = { version = "1.0", features = ["full"] }
1113
anyhow = "1.0"
@@ -31,4 +33,4 @@ reqwest = "0.12.7"
3133
serde = "1.0.210"
3234
hyper-util = "0.1.8"
3335
alloy-primitives = "0.8.7"
34-
url = "2.5.2"
36+
url = { version = "2", features = ["serde"] }

metabased-sequencer/interceptor/src/presentation/cli.rs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use crate::domain::primitives::Address;
22
use crate::presentation::server;
3-
use clap::Parser;
3+
use clap::{Parser, ValueEnum};
4+
use figment::providers::{Env, Serialized};
5+
use figment::Figment;
6+
use serde::{Deserialize, Serialize};
47
use tracing_subscriber::EnvFilter;
58
use url::Url;
69

10+
const DEFAULT_PORT: u16 = 8456;
11+
712
pub fn init_tracing_subscriber() {
813
tracing_subscriber::fmt()
914
.with_env_filter(EnvFilter::from_default_env())
@@ -12,7 +17,7 @@ pub fn init_tracing_subscriber() {
1217
}
1318

1419
pub async fn run() -> anyhow::Result<()> {
15-
let args = Args::parse();
20+
let args = parse_args()?;
1621
let (addr, handle) = server::run(
1722
args.port,
1823
args.chain_contract_address,
@@ -28,11 +33,53 @@ pub async fn run() -> anyhow::Result<()> {
2833
Ok(())
2934
}
3035

36+
fn parse_args() -> anyhow::Result<Args> {
37+
let args = ProfileArgs::parse();
38+
39+
let _ = dotenv::dotenv();
40+
41+
if let Some(profile) = args.profile {
42+
dotenv::from_filename(match profile {
43+
Profile::Mainnet => "mainnet.env",
44+
Profile::Testnet => "testnet.env",
45+
Profile::Devnet => "devnet.env",
46+
})?;
47+
}
48+
49+
let args = Figment::new()
50+
.merge(Serialized::default("port", DEFAULT_PORT))
51+
.merge(Env::prefixed("METABASED_"));
52+
53+
let result = CliArgs::try_parse();
54+
55+
let args: CliArgs = match result {
56+
Ok(cli_args) => args.merge(Serialized::defaults(cli_args)).extract()?,
57+
Err(e) => args.extract().unwrap_or_else(|_| e.exit()),
58+
};
59+
60+
Ok(args.into())
61+
}
62+
63+
#[derive(ValueEnum, Debug, Clone, Serialize, Deserialize)]
64+
enum Profile {
65+
Mainnet,
66+
Testnet,
67+
Devnet,
68+
}
69+
70+
#[derive(Parser, Debug, Serialize, Deserialize)]
71+
#[command(version, about, long_about = None)]
72+
struct ProfileArgs {
73+
/// Profile that chooses which .env file to load
74+
#[arg(short = 'o', long)]
75+
profile: Option<Profile>,
76+
}
77+
3178
/// The Metabased sequencer is a server that exposes a JSON-RPC API that accepts incoming
3279
/// transactions to be run on a layer-3 blockchain.
33-
#[derive(Parser, Debug)]
80+
#[derive(Parser, Debug, Serialize, Deserialize)]
3481
#[command(version, about, long_about = None)]
35-
struct Args {
82+
struct CliArgs {
3683
/// Address of the layer-2 Ethereum smart contract that processes the layer-3 transactions
3784
#[arg(short = 'c', long)]
3885
chain_contract_address: Address,
@@ -42,6 +89,26 @@ struct Args {
4289
chain_rpc_address: Url,
4390

4491
/// Port to listen on
45-
#[arg(short = 'p', long, default_value_t = 8456)]
92+
#[arg(short = 'p', long)]
93+
port: Option<u16>,
94+
95+
/// Profile that chooses which .env file to load
96+
#[arg(short = 'o', long)]
97+
profile: Option<Profile>,
98+
}
99+
100+
struct Args {
101+
chain_contract_address: Address,
102+
chain_rpc_address: Url,
46103
port: u16,
47104
}
105+
106+
impl From<CliArgs> for Args {
107+
fn from(value: CliArgs) -> Self {
108+
Self {
109+
chain_contract_address: value.chain_contract_address,
110+
chain_rpc_address: value.chain_rpc_address,
111+
port: value.port.expect("port should be set by a default value"),
112+
}
113+
}
114+
}

testnet.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
METABASED_CHAIN_CONTRACT_ADDRESS=0x7DEdabB3Db89310B316bA49D96Aa6517aFC44294
2+
METABASED_CHAIN_RPC_ADDRESS=https://rpc.devnet.alchemy.com/4934ab6a-37a9-4431-a473-84b7d6bcef57

0 commit comments

Comments
 (0)