|
| 1 | +//! CLI for contract deployment |
| 2 | +//! |
| 3 | +//! # Usage |
| 4 | +//! |
| 5 | +//! ``` |
| 6 | +//! # Write config to stdout |
| 7 | +//! cargo run --bin deploy -- -m "your mnemonic here" -i 0 -u http://localhost:8545 |
| 8 | +//! |
| 9 | +//! # Write config to a file |
| 10 | +//! cargo run --bin deploy -- -m "your mnemonic here" -i 0 -u http://localhost:8545 -o output.toml |
| 11 | +//! ``` |
| 12 | +//! |
| 13 | +//! # Local test |
| 14 | +//! Run `just test-contract-deploy` |
| 15 | +use alloy::{primitives::Address, providers::WalletProvider}; |
| 16 | +use anyhow::{Context, Result}; |
| 17 | +use clap::Parser; |
| 18 | +use serde::Serialize; |
| 19 | +use std::path::PathBuf; |
| 20 | +use timeboost_contract::provider::build_provider; |
| 21 | +use timeboost_utils::types::logging; |
| 22 | +use tokio::fs; |
| 23 | +use tracing::info; |
| 24 | +use url::Url; |
| 25 | + |
| 26 | +#[derive(Clone, Debug, Parser)] |
| 27 | +struct Args { |
| 28 | + #[clap(short, long)] |
| 29 | + mnemonic: String, |
| 30 | + |
| 31 | + #[clap(short, long)] |
| 32 | + index: u32, |
| 33 | + |
| 34 | + #[clap(short, long)] |
| 35 | + url: Url, |
| 36 | + |
| 37 | + #[clap(short, long)] |
| 38 | + output: Option<PathBuf>, |
| 39 | +} |
| 40 | + |
| 41 | +/// Config type for the key manager who has the permission to update the KeyManager contract |
| 42 | +/// See `test-configs/keymanager.toml` for an example |
| 43 | +#[derive(Debug, Serialize)] |
| 44 | +struct KeyManagerConfig { |
| 45 | + wallet: LocalWalletConfig, |
| 46 | + deployments: Deployments, |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Debug, Serialize)] |
| 50 | +struct LocalWalletConfig { |
| 51 | + mnemonic: String, |
| 52 | + account_index: u32, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Serialize)] |
| 56 | +struct Deployments { |
| 57 | + /// RPC endpoint of the target chain |
| 58 | + chain_url: Url, |
| 59 | + /// The contract address of KeyManager.sol proxy |
| 60 | + key_manager: Option<Address>, |
| 61 | +} |
| 62 | + |
| 63 | +#[tokio::main] |
| 64 | +async fn main() -> Result<()> { |
| 65 | + logging::init_logging(); |
| 66 | + |
| 67 | + let args = Args::parse(); |
| 68 | + |
| 69 | + info!("Starting contract deployment"); |
| 70 | + |
| 71 | + // Construct the config from command-line arguments |
| 72 | + let mut cfg = KeyManagerConfig { |
| 73 | + wallet: LocalWalletConfig { |
| 74 | + mnemonic: args.mnemonic, |
| 75 | + account_index: args.index, |
| 76 | + }, |
| 77 | + deployments: Deployments { |
| 78 | + chain_url: args.url, |
| 79 | + key_manager: None, |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + // Build provider |
| 84 | + let provider = build_provider( |
| 85 | + cfg.wallet.mnemonic.clone(), |
| 86 | + cfg.wallet.account_index, |
| 87 | + cfg.deployments.chain_url.clone(), |
| 88 | + )?; |
| 89 | + |
| 90 | + let manager = provider.default_signer_address(); |
| 91 | + info!("Deploying with manager address: {manager:#x}"); |
| 92 | + |
| 93 | + // Deploy the KeyManager contract |
| 94 | + let km_addr = timeboost_contract::deployer::deploy_key_manager_contract(&provider, manager) |
| 95 | + .await |
| 96 | + .context("Failed to deploy KeyManager contract")?; |
| 97 | + info!("KeyManager deployed successfully at: {km_addr:#x}"); |
| 98 | + |
| 99 | + // Update the address and deliver the final config |
| 100 | + cfg.deployments.key_manager = Some(km_addr); |
| 101 | + let toml = toml::to_string_pretty(&cfg)?; |
| 102 | + |
| 103 | + if let Some(out) = &args.output { |
| 104 | + fs::write(out, &toml).await?; |
| 105 | + info!(file=?out, "Config written to file"); |
| 106 | + } else { |
| 107 | + println!("{toml}"); |
| 108 | + } |
| 109 | + |
| 110 | + Ok(()) |
| 111 | +} |
0 commit comments