@@ -8,7 +8,12 @@ use ethers::{
8
8
signers:: { LocalWallet , Signer } ,
9
9
types:: { TransactionReceipt , H256 , U64 } ,
10
10
} ;
11
- use std:: { path:: PathBuf , str:: FromStr , sync:: Arc , time:: Duration } ;
11
+ use std:: {
12
+ path:: { Path , PathBuf } ,
13
+ str:: FromStr ,
14
+ sync:: Arc ,
15
+ time:: Duration ,
16
+ } ;
12
17
13
18
const DEFAULT_GAS_LIMIT : u64 = 30_000_000 ;
14
19
const DEFAULT_GAS_PRICE : u64 = 0 ;
@@ -91,16 +96,16 @@ pub(super) async fn execute(args: &DeployArgs) -> Result<(), Error> {
91
96
92
97
fn validate_wasm_file ( wasm_file : & PathBuf ) -> Result < ( ) , Error > {
93
98
if !wasm_file. exists ( ) {
94
- return Err ( Error :: DeploymentError ( format ! (
99
+ return Err ( Error :: Deployment ( format ! (
95
100
"WASM file not found: {}" ,
96
101
wasm_file. display( )
97
102
) ) ) ;
98
103
}
99
104
100
105
let wasm_bytes = std:: fs:: read ( wasm_file)
101
- . map_err ( |e| Error :: DeploymentError ( format ! ( "Failed to read WASM file: {}" , e) ) ) ?;
102
- if wasm_bytes. len ( ) < 4 || & wasm_bytes[ 0 ..4 ] != & [ 0x00 , 0x61 , 0x73 , 0x6d ] {
103
- return Err ( Error :: DeploymentError (
106
+ . map_err ( |e| Error :: Deployment ( format ! ( "Failed to read WASM file: {}" , e) ) ) ?;
107
+ if wasm_bytes. len ( ) < 4 || wasm_bytes[ 0 ..4 ] != [ 0x00 , 0x61 , 0x73 , 0x6d ] {
108
+ return Err ( Error :: Deployment (
104
109
"Invalid WASM file: missing magic number" . to_string ( ) ,
105
110
) ) ;
106
111
}
@@ -110,13 +115,13 @@ fn validate_wasm_file(wasm_file: &PathBuf) -> Result<(), Error> {
110
115
fn create_wallet ( private_key : & str , chain_id : u64 ) -> Result < LocalWallet , Error > {
111
116
let clean_key = private_key. trim_start_matches ( "0x" ) ;
112
117
if clean_key. len ( ) != 64 {
113
- return Err ( Error :: DeploymentError (
118
+ return Err ( Error :: Deployment (
114
119
"Private key must be 64 hex characters." . to_string ( ) ,
115
120
) ) ;
116
121
}
117
122
118
123
LocalWallet :: from_str ( clean_key)
119
- . map_err ( |e| Error :: DeploymentError ( format ! ( "Invalid private key: {}" , e) ) )
124
+ . map_err ( |e| Error :: Deployment ( format ! ( "Invalid private key: {}" , e) ) )
120
125
. map ( |wallet| wallet. with_chain_id ( chain_id) )
121
126
}
122
127
@@ -127,18 +132,18 @@ async fn prepare_deploy_transaction(
127
132
gas_price : u64 ,
128
133
) -> Result < TransactionRequest , Error > {
129
134
let provider = Provider :: < Http > :: try_from ( & network_config. endpoint )
130
- . map_err ( |e| Error :: NetworkError ( format ! ( "Failed to create provider: {}" , e) ) ) ?;
135
+ . map_err ( |e| Error :: Network ( format ! ( "Failed to create provider: {}" , e) ) ) ?;
131
136
132
137
let wasm_bytes = std:: fs:: read ( wasm_file)
133
- . map_err ( |e| Error :: DeploymentError ( format ! ( "Failed to read WASM file: {}" , e) ) ) ?;
138
+ . map_err ( |e| Error :: Deployment ( format ! ( "Failed to read WASM file: {}" , e) ) ) ?;
134
139
println ! ( "📦 WASM file size: {} bytes" , wasm_bytes. len( ) ) ;
135
140
136
141
let gas_price = if gas_price == 0 {
137
142
println ! ( "⛽ Estimating gas price..." ) ;
138
143
provider
139
144
. get_gas_price ( )
140
145
. await
141
- . map_err ( |e| Error :: NetworkError ( format ! ( "Failed to fetch gas price: {}" , e) ) ) ?
146
+ . map_err ( |e| Error :: Network ( format ! ( "Failed to fetch gas price: {}" , e) ) ) ?
142
147
} else {
143
148
U256 :: from ( gas_price)
144
149
} ;
@@ -161,23 +166,23 @@ async fn send_tx(
161
166
) -> Result < TransactionReceipt , Error > {
162
167
let gas_limit = tx. gas ;
163
168
let provider = Provider :: < Http > :: try_from ( & network_config. endpoint )
164
- . map_err ( |e| Error :: NetworkError ( format ! ( "Failed to create provider: {}" , e) ) ) ?;
169
+ . map_err ( |e| Error :: Network ( format ! ( "Failed to create provider: {}" , e) ) ) ?;
165
170
let client = Arc :: new ( SignerMiddleware :: new ( provider. clone ( ) , wallet) ) ;
166
171
167
172
println ! ( "🚀 Sending transaction..." ) ;
168
173
let pending_tx = client
169
174
. send_transaction ( tx, None )
170
175
. await
171
- . map_err ( |e| Error :: DeploymentError ( format ! ( "Failed to send transaction: {}" , e) ) ) ?;
176
+ . map_err ( |e| Error :: Deployment ( format ! ( "Failed to send transaction: {}" , e) ) ) ?;
172
177
173
178
let receipt = pending_tx
174
179
. await
175
- . map_err ( |e| Error :: DeploymentError ( format ! ( "Transaction failed: {}" , e) ) ) ?
176
- . ok_or_else ( || Error :: DeploymentError ( "Transaction receipt not found" . to_string ( ) ) ) ?;
180
+ . map_err ( |e| Error :: Deployment ( format ! ( "Transaction failed: {}" , e) ) ) ?
181
+ . ok_or_else ( || Error :: Deployment ( "Transaction receipt not found" . to_string ( ) ) ) ?;
177
182
178
183
if receipt. status != Some ( U64 :: from ( 1 ) ) {
179
184
print_deployment_result ( & receipt, gas_limit) ;
180
- return Err ( Error :: DeploymentError ( "Transaction failed" . to_string ( ) ) ) ;
185
+ return Err ( Error :: Deployment ( "Transaction failed" . to_string ( ) ) ) ;
181
186
}
182
187
183
188
if confirmations > 0 {
@@ -197,12 +202,10 @@ async fn wait_for_confirmations(
197
202
if let Some ( receipt) = provider
198
203
. get_transaction_receipt ( tx_hash)
199
204
. await
200
- . map_err ( |e| {
201
- Error :: DeploymentError ( format ! ( "Failed to get transaction receipt: {}" , e) )
202
- } ) ?
205
+ . map_err ( |e| Error :: Deployment ( format ! ( "Failed to get transaction receipt: {}" , e) ) ) ?
203
206
{
204
207
let current_block = provider. get_block_number ( ) . await . map_err ( |e| {
205
- Error :: DeploymentError ( format ! ( "Failed to get current block number: {}" , e) )
208
+ Error :: Deployment ( format ! ( "Failed to get current block number: {}" , e) )
206
209
} ) ?;
207
210
208
211
if let Some ( block_number) = receipt. block_number {
@@ -219,7 +222,7 @@ async fn wait_for_confirmations(
219
222
fn print_deployment_start (
220
223
wallet : & LocalWallet ,
221
224
network : & NetworkConfig ,
222
- wasm_file : & PathBuf ,
225
+ wasm_file : & Path ,
223
226
) -> Result < ( ) , Error > {
224
227
println ! ( "\n 🚀 Starting Deployment" ) ;
225
228
println ! ( "====================" ) ;
@@ -305,7 +308,7 @@ impl NetworkConfig {
305
308
chain_id,
306
309
} )
307
310
} else {
308
- Err ( Error :: NetworkError (
311
+ Err ( Error :: Network (
309
312
"Please specify either --local, --dev, or both --rpc and --chain-id." . to_string ( ) ,
310
313
) )
311
314
}
0 commit comments