-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathinvoke.rs
206 lines (177 loc) · 6.63 KB
/
invoke.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::{sync::Arc, time::Duration};
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use starknet::{
accounts::Account,
core::types::{Call, Felt},
};
use crate::{
account::AccountArgs,
address_book::AddressBookResolver,
decode::FeltDecoder,
error::account_error_mapper,
fee::{FeeArgs, FeeSetting, TokenFeeSetting},
utils::{felt_to_bigdecimal, print_colored_json, watch_tx},
verbosity::VerbosityArgs,
ProviderArgs,
};
#[derive(Debug, Parser)]
pub struct Invoke {
#[clap(flatten)]
provider: ProviderArgs,
#[clap(flatten)]
account: AccountArgs,
#[clap(flatten)]
fee: FeeArgs,
#[clap(long, help = "Simulate the transaction only")]
simulate: bool,
#[clap(long, help = "Provide transaction nonce manually")]
nonce: Option<Felt>,
#[clap(long, short, help = "Wait for the transaction to confirm")]
watch: bool,
#[clap(
long,
env = "STARKNET_POLL_INTERVAL",
default_value = "5000",
help = "Transaction result poll interval in milliseconds"
)]
poll_interval: u64,
#[clap(help = "One or more contract calls. See documentation for more details")]
calls: Vec<String>,
#[clap(flatten)]
verbosity: VerbosityArgs,
}
impl Invoke {
pub async fn run(self) -> Result<()> {
self.verbosity.setup_logging();
let fee_setting = self.fee.into_setting()?;
if self.simulate && fee_setting.is_estimate_only() {
anyhow::bail!("--simulate cannot be used with --estimate-only");
}
let provider = Arc::new(self.provider.into_provider()?);
let felt_decoder = FeltDecoder::new(AddressBookResolver::new(provider.clone()));
// Parses and resolves the calls
let calls = {
// TODO: show more helpful message
let unexpected_end_of_args = || anyhow::anyhow!("unexpected end of arguments");
let mut buffer = vec![];
let mut arg_iter = self.calls.into_iter();
while let Some(first_arg) = arg_iter.next() {
let contract_address = felt_decoder
.decode_single_with_addr_fallback(&first_arg)
.await?;
let next_arg = arg_iter.next().ok_or_else(unexpected_end_of_args)?;
let selector = felt_decoder
.decode_single_with_selector_fallback(&next_arg)
.await?;
let mut calldata = vec![];
for arg in &mut arg_iter {
let mut arg = match arg.as_str() {
"/" | "-" | "\\" => break,
_ => felt_decoder.decode(&arg).await?,
};
calldata.append(&mut arg);
}
buffer.push(Call {
to: contract_address,
selector,
calldata,
});
}
buffer
};
if calls.is_empty() {
anyhow::bail!("empty execution");
}
let account = self.account.into_account(provider.clone()).await?;
let invoke_tx = match fee_setting {
FeeSetting::Eth(fee_setting) => {
let execution = account.execute_v1(calls).fee_estimate_multiplier(1.5f64);
let execution = match self.nonce {
Some(nonce) => execution.nonce(nonce),
None => execution,
};
let execution = match fee_setting {
TokenFeeSetting::EstimateOnly => {
let estimated_fee = execution
.estimate_fee()
.await
.map_err(account_error_mapper)?
.overall_fee;
println!(
"{} ETH",
format!("{}", felt_to_bigdecimal(estimated_fee, 18)).bright_yellow(),
);
return Ok(());
}
TokenFeeSetting::Manual(fee) => execution.max_fee(fee.max_fee),
TokenFeeSetting::None => execution,
};
if self.simulate {
print_colored_json(&execution.simulate(false, false).await?)?;
return Ok(());
}
execution.send().await
}
FeeSetting::Strk(fee_setting) => {
let execution = account.execute_v3(calls);
let execution = match self.nonce {
Some(nonce) => execution.nonce(nonce),
None => execution,
};
let execution = match fee_setting {
TokenFeeSetting::EstimateOnly => {
let estimated_fee = execution
.estimate_fee()
.await
.map_err(account_error_mapper)?;
println!(
"{} STRK",
format!("{}", felt_to_bigdecimal(estimated_fee.overall_fee, 18))
.bright_yellow(),
);
return Ok(());
}
TokenFeeSetting::Manual(fee) => {
let execution = if let Some(gas) = fee.gas {
execution.gas(gas)
} else {
execution
};
if let Some(gas_price) = fee.gas_price {
execution.gas_price(gas_price)
} else {
execution
}
}
TokenFeeSetting::None => execution,
};
if self.simulate {
print_colored_json(&execution.simulate(false, false).await?)?;
return Ok(());
}
execution.send().await
}
}
.map_err(account_error_mapper)?
.transaction_hash;
eprintln!(
"Invoke transaction: {}",
format!("{:#064x}", invoke_tx).bright_yellow()
);
if self.watch {
eprintln!(
"Waiting for transaction {} to confirm...",
format!("{:#064x}", invoke_tx).bright_yellow(),
);
watch_tx(
&provider,
invoke_tx,
Duration::from_millis(self.poll_interval),
)
.await?;
}
Ok(())
}
}