|
| 1 | +//! CLI for contract deployment |
| 2 | +//! |
| 3 | +//! # Local test |
| 4 | +//! Run `just test-contract-deploy` |
| 5 | +use alloy::{primitives::Address, providers::WalletProvider}; |
| 6 | +use anyhow::{Context, Result}; |
| 7 | +use clap::Parser; |
| 8 | +use serde::Deserialize; |
| 9 | +use std::{fs, path::PathBuf}; |
| 10 | +use timeboost_contract::provider::build_provider; |
| 11 | +use timeboost_utils::types::logging; |
| 12 | +use toml_edit::{DocumentMut, value}; |
| 13 | +use url::Url; |
| 14 | + |
| 15 | +#[derive(Clone, Debug, Parser)] |
| 16 | +struct Args { |
| 17 | + /// Config file storing `KeyManagerConfig` |
| 18 | + #[clap(short, long, default_value = "./test-configs/keymanager.toml")] |
| 19 | + config: PathBuf, |
| 20 | +} |
| 21 | + |
| 22 | +/// Config type for the key manager who has the permission to update the KeyManager contract |
| 23 | +/// See `test-configs/keymanager.toml` for an example |
| 24 | +#[derive(Debug, Deserialize)] |
| 25 | +struct KeyManagerConfig { |
| 26 | + wallet: LocalWalletConfig, |
| 27 | + deployments: Deployments, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Deserialize)] |
| 31 | +struct LocalWalletConfig { |
| 32 | + mnemonic: String, |
| 33 | + account_index: u32, |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Debug, Deserialize)] |
| 37 | +#[allow(dead_code)] |
| 38 | +struct Deployments { |
| 39 | + /// RPC endpoint of the target chain |
| 40 | + chain_url: Url, |
| 41 | + /// The contract address of KeyManager.sol proxy |
| 42 | + key_manager: Option<Address>, |
| 43 | +} |
| 44 | + |
| 45 | +#[tokio::main] |
| 46 | +async fn main() -> Result<()> { |
| 47 | + logging::init_logging(); |
| 48 | + |
| 49 | + let args = Args::parse(); |
| 50 | + let config_path = args.config; |
| 51 | + |
| 52 | + tracing::info!( |
| 53 | + "Starting contract deployment with config: {:?}", |
| 54 | + config_path |
| 55 | + ); |
| 56 | + |
| 57 | + // Read and parse config file |
| 58 | + let config_content = fs::read_to_string(&config_path) |
| 59 | + .with_context(|| format!("Failed to read config file: {config_path:?}"))?; |
| 60 | + let config: KeyManagerConfig = toml::from_str(&config_content) |
| 61 | + .with_context(|| format!("Failed to parse config file: {config_path:?}"))?; |
| 62 | + tracing::info!("Config loaded successfully"); |
| 63 | + |
| 64 | + // Build provider |
| 65 | + let provider = build_provider( |
| 66 | + config.wallet.mnemonic, |
| 67 | + config.wallet.account_index, |
| 68 | + config.deployments.chain_url, |
| 69 | + ); |
| 70 | + |
| 71 | + let manager = provider.default_signer_address(); |
| 72 | + tracing::info!("Deploying with maanger address: {manager:#x}"); |
| 73 | + |
| 74 | + // Deploy the KeyManager contract |
| 75 | + let km_addr = timeboost_contract::deployer::deploy_key_manager_contract(&provider, manager) |
| 76 | + .await |
| 77 | + .context("Failed to deploy KeyManager contract")?; |
| 78 | + tracing::info!("KeyManager deployed successfully at: {km_addr:#x}"); |
| 79 | + |
| 80 | + // Update the config file with the deployed address |
| 81 | + let mut doc = config_content |
| 82 | + .parse::<DocumentMut>() |
| 83 | + .with_context(|| format!("Failed to parse TOML in config file: {config_path:?}"))?; |
| 84 | + |
| 85 | + // Set the key_manager address in the deployments section |
| 86 | + doc["deployments"]["key_manager"] = value(format!("{km_addr:#x}")); |
| 87 | + |
| 88 | + // Write back to file |
| 89 | + fs::write(&config_path, doc.to_string()) |
| 90 | + .with_context(|| format!("Failed to write updated config file: {config_path:?}"))?; |
| 91 | + |
| 92 | + tracing::info!("Config file updated with KeyManager address: {km_addr:#x}"); |
| 93 | + |
| 94 | + Ok(()) |
| 95 | +} |
0 commit comments