1
1
//! CLI for contract deployment
2
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
+ //!
3
13
//! # Local test
4
14
//! Run `just test-contract-deploy`
5
15
use alloy:: { primitives:: Address , providers:: WalletProvider } ;
6
16
use anyhow:: { Context , Result } ;
7
17
use clap:: Parser ;
8
- use serde:: Deserialize ;
18
+ use serde:: { Deserialize , Serialize } ;
9
19
use std:: { fs, path:: PathBuf } ;
10
20
use timeboost_contract:: provider:: build_provider;
11
21
use timeboost_utils:: types:: logging;
12
- use toml_edit :: { DocumentMut , value } ;
22
+ use tracing :: info ;
13
23
use url:: Url ;
14
24
15
25
#[ derive( Clone , Debug , Parser ) ]
16
26
struct Args {
17
- /// Config file storing `KeyManagerConfig`
18
- #[ clap( short, long, default_value = "./test-configs/keymanager.toml" ) ]
19
- config : PathBuf ,
27
+ #[ clap( short, long) ]
28
+ mnemonic : String ,
29
+
30
+ #[ clap( short, long) ]
31
+ index : u32 ,
32
+
33
+ #[ clap( short, long) ]
34
+ url : Url ,
35
+
36
+ #[ clap( short, long) ]
37
+ output : Option < PathBuf > ,
20
38
}
21
39
22
40
/// Config type for the key manager who has the permission to update the KeyManager contract
23
41
/// See `test-configs/keymanager.toml` for an example
24
- #[ derive( Debug , Deserialize ) ]
42
+ #[ derive( Debug , Deserialize , Serialize ) ]
25
43
struct KeyManagerConfig {
26
44
wallet : LocalWalletConfig ,
27
45
deployments : Deployments ,
28
46
}
29
47
30
- #[ derive( Debug , Deserialize ) ]
48
+ #[ derive( Debug , Deserialize , Serialize ) ]
31
49
struct LocalWalletConfig {
32
50
mnemonic : String ,
33
51
account_index : u32 ,
34
52
}
35
53
36
- #[ derive( Debug , Deserialize ) ]
54
+ #[ derive( Debug , Deserialize , Serialize ) ]
37
55
#[ allow( dead_code) ]
38
56
struct Deployments {
39
57
/// RPC endpoint of the target chain
@@ -47,49 +65,48 @@ async fn main() -> Result<()> {
47
65
logging:: init_logging ( ) ;
48
66
49
67
let args = Args :: parse ( ) ;
50
- let config_path = args. config ;
51
68
52
- tracing:: info!(
53
- "Starting contract deployment with config: {:?}" ,
54
- config_path
55
- ) ;
69
+ info ! ( "Starting contract deployment" ) ;
56
70
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" ) ;
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
+ } ;
63
82
64
83
// Build provider
65
84
let provider = build_provider (
66
- config . wallet . mnemonic ,
67
- config . wallet . account_index ,
68
- config . deployments . chain_url ,
85
+ cfg . wallet . mnemonic . clone ( ) ,
86
+ cfg . wallet . account_index ,
87
+ cfg . deployments . chain_url . clone ( ) ,
69
88
) ;
70
89
71
90
let manager = provider. default_signer_address ( ) ;
72
- tracing :: info!( "Deploying with maanger address: {manager:#x}" ) ;
91
+ info ! ( "Deploying with manager address: {manager:#x}" ) ;
73
92
74
93
// Deploy the KeyManager contract
75
94
let km_addr = timeboost_contract:: deployer:: deploy_key_manager_contract ( & provider, manager)
76
95
. await
77
96
. 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}" ) ;
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) ?;
105
+ info ! ( file=?out, "Config written to file" ) ;
106
+ } else {
107
+ println ! ( "===== OUTPUT ======" ) ;
108
+ println ! ( "{toml}" ) ;
109
+ }
93
110
94
111
Ok ( ( ) )
95
112
}
0 commit comments