Skip to content

Commit 941979d

Browse files
Add --estimate-tip flag (#3636)
<!-- Reference any GitHub issues resolved by this PR --> Closes #3554 --------- Co-authored-by: Franciszek Job <[email protected]>
1 parent 0854320 commit 941979d

File tree

11 files changed

+101
-14
lines changed

11 files changed

+101
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020

2121
- `--test-files` flag to `verify` command to include test files under src/ for verification (only applies to voyager)
2222
- `--tip` flag to `invoke`, `declare`, `deploy`, `multicall run` and `account deploy` commands to set the transaction tip
23+
- `--estimate-tip` flag which automatically adds an estimated tip to the transaction. The tip is calculated based on the current network conditions and added to the transaction fee
2324

2425
#### Changed
2526

crates/sncast/src/helpers/fee.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ pub struct FeeArgs {
3434
#[arg(long)]
3535
pub l1_data_gas_price: Option<u128>,
3636

37-
/// Tip for the transaction. If not provided, it will be set to 0.
38-
#[arg(long)]
37+
/// Tip for the transaction. Defaults to 0 unless `--estimate-tip` is used.
38+
#[arg(long, conflicts_with = "estimate_tip")]
3939
pub tip: Option<u64>,
40+
41+
/// If passed, an estimated tip will be added to pay for the transaction.
42+
#[arg(long)]
43+
pub estimate_tip: bool,
4044
}
4145

4246
impl From<ScriptFeeSettings> for FeeArgs {
@@ -59,6 +63,7 @@ impl From<ScriptFeeSettings> for FeeArgs {
5963
l1_data_gas,
6064
l1_data_gas_price,
6165
tip: Some(0),
66+
estimate_tip: false,
6267
}
6368
}
6469
}
@@ -81,7 +86,7 @@ impl FeeArgs {
8186

8287
let fee_settings = FeeSettings::try_from(fee_estimate.clone())
8388
.expect("Failed to convert FeeEstimate to FeeSettings")
84-
.update_tip(self.tip.unwrap_or(0)); // If a tip is not provided, set it to 0
89+
.with_resolved_tip(self.tip, self.estimate_tip);
8590

8691
Ok(fee_settings)
8792
} else {
@@ -117,11 +122,14 @@ pub struct FeeSettings {
117122

118123
impl FeeSettings {
119124
#[must_use]
120-
pub fn update_tip(&self, tip: u64) -> FeeSettings {
121-
FeeSettings {
122-
tip: Some(tip),
123-
..*self
124-
}
125+
pub fn with_resolved_tip(self, tip: Option<u64>, estimate_tip: bool) -> FeeSettings {
126+
let tip = if estimate_tip {
127+
None // If we leave it as None, the tip will be estimated before sending the transaction
128+
} else {
129+
Some(tip.unwrap_or(0)) // If a tip is not provided, set it to 0
130+
};
131+
132+
FeeSettings { tip, ..self }
125133
}
126134
}
127135

@@ -149,8 +157,9 @@ impl From<FeeArgs> for FeeSettings {
149157
l2_gas_price: fee_args.l2_gas_price,
150158
l1_data_gas: fee_args.l1_data_gas,
151159
l1_data_gas_price: fee_args.l1_data_gas_price,
152-
tip: Some(fee_args.tip.unwrap_or(0)),
160+
tip: None,
153161
}
162+
.with_resolved_tip(fee_args.tip, fee_args.estimate_tip)
154163
}
155164
}
156165

crates/sncast/tests/e2e/declare.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ async fn test_happy_case(class_hash: Felt, account_type: AccountType) {
107107
l2_gas: None,
108108
l2_gas_price: None,
109109
tip: Some(100_000),
110+
estimate_tip: false,
110111
}; "max_fee")]
111112
#[test_case(FeeArgs{
112113
max_fee: None,
@@ -117,6 +118,7 @@ async fn test_happy_case(class_hash: Felt, account_type: AccountType) {
117118
l2_gas: Some(1_000_000_000),
118119
l2_gas_price: Some(100_000_000_000_000_000_000),
119120
tip: None,
121+
estimate_tip: false,
120122
}; "resource_bounds")]
121123
#[tokio::test]
122124
async fn test_happy_case_different_fees(fee_args: FeeArgs) {

crates/sncast/tests/e2e/deploy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ async fn test_happy_case(class_hash: Felt, account_type: AccountType) {
104104
l2_gas: None,
105105
l2_gas_price: None,
106106
tip: None,
107+
estimate_tip: false,
107108
}; "max_fee")]
108109
#[test_case(FeeArgs{
109110
max_fee: None,
@@ -114,6 +115,7 @@ async fn test_happy_case(class_hash: Felt, account_type: AccountType) {
114115
l2_gas: Some(1_000_000_000),
115116
l2_gas_price: Some(100_000_000_000_000_000_000),
116117
tip: Some(100_000),
118+
estimate_tip: false,
117119
}; "resource_bounds")]
118120
#[tokio::test]
119121
async fn test_happy_case_different_fees(fee_args: FeeArgs) {

crates/sncast/tests/e2e/invoke.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ async fn test_happy_case(class_hash: Felt, account_type: AccountType) {
106106
l2_gas: None,
107107
l2_gas_price: None,
108108
tip: None,
109+
estimate_tip: false,
109110
}; "max_fee")]
110111
#[test_case(FeeArgs{
111112
max_fee: None,
@@ -116,6 +117,7 @@ async fn test_happy_case(class_hash: Felt, account_type: AccountType) {
116117
l2_gas: Some(1_000_000_000),
117118
l2_gas_price: Some(100_000_000_000_000_000_000),
118119
tip: Some(100_000_000),
120+
estimate_tip: false,
119121
}; "resource_bounds")]
120122
#[tokio::test]
121123
async fn test_happy_case_different_fees(fee_args: FeeArgs) {

crates/sncast/tests/integration/fee.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async fn test_happy_case() {
1313
l1_data_gas: Some(100),
1414
l1_data_gas_price: Some(200),
1515
tip: None,
16+
estimate_tip: false,
1617
};
1718

1819
let settings = args.try_into_fee_settings(None).unwrap();
@@ -42,6 +43,7 @@ async fn test_max_fee_none() {
4243
l1_data_gas: Some(100),
4344
l1_data_gas_price: Some(100),
4445
tip: Some(100),
46+
estimate_tip: false,
4547
};
4648

4749
let settings = args.try_into_fee_settings(None).unwrap();
@@ -81,6 +83,7 @@ async fn test_max_fee_set() {
8183
l1_data_gas: None,
8284
l1_data_gas_price: None,
8385
tip: None,
86+
estimate_tip: false,
8487
};
8588

8689
let settings = args
@@ -122,6 +125,7 @@ async fn test_max_fee_set_and_fee_estimate_higher() {
122125
l1_data_gas: None,
123126
l1_data_gas_price: None,
124127
tip: None,
128+
estimate_tip: false,
125129
};
126130

127131
let err = args
@@ -150,6 +154,7 @@ async fn test_max_fee_set_and_fee_estimate_none() {
150154
l1_data_gas: None,
151155
l1_data_gas_price: None,
152156
tip: None,
157+
estimate_tip: false,
153158
};
154159

155160
args.try_into_fee_settings(None).unwrap();
@@ -166,6 +171,7 @@ async fn test_all_args_none() {
166171
l1_data_gas: None,
167172
l1_data_gas_price: None,
168173
tip: None,
174+
estimate_tip: false,
169175
};
170176

171177
let settings = args.try_into_fee_settings(None).unwrap();
@@ -183,3 +189,33 @@ async fn test_all_args_none() {
183189
}
184190
);
185191
}
192+
193+
#[tokio::test]
194+
async fn test_estimate_tip() {
195+
let args = FeeArgs {
196+
max_fee: None,
197+
l1_gas: None,
198+
l1_gas_price: None,
199+
l2_gas: None,
200+
l2_gas_price: None,
201+
l1_data_gas: None,
202+
l1_data_gas_price: None,
203+
tip: None,
204+
estimate_tip: true,
205+
};
206+
207+
let settings = args.try_into_fee_settings(None).unwrap();
208+
209+
assert_eq!(
210+
settings,
211+
FeeSettings {
212+
l1_gas: None,
213+
l1_gas_price: None,
214+
l2_gas: None,
215+
l2_gas_price: None,
216+
l1_data_gas: None,
217+
l1_data_gas_price: None,
218+
tip: None,
219+
}
220+
);
221+
}

docs/src/appendix/sncast/account/deploy.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,15 @@ Maximum L1 data gas unit price for the `deploy_account` transaction. When not us
5757

5858
## `--tip <TIP>`
5959
Optional.
60+
Conflicts with: [`--estimate-tip`](#--estimate-tip-estimate_tip)
6061

61-
Tip for the transaction. When not provided, defaults to 0.
62+
Tip for the transaction. When not provided, defaults to 0 unless [`--estimate-tip`](#--estimate-tip-estimate_tip) is used.
63+
64+
## `--estimate-tip <ESTIMATE_TIP>`
65+
Optional.
66+
Conflicts with: [`--tip`](#--tip-tip)
67+
68+
If passed, an estimated tip will be added to pay for the transaction. The tip is estimated based on the current network conditions and added to the transaction fee.
6269

6370
## `--silent`
6471
Optional.

docs/src/appendix/sncast/declare.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,15 @@ Maximum L1 data gas unit price for the `declare` transaction. When not used, def
6161

6262
## `--tip <TIP>`
6363
Optional.
64+
Conflicts with: [`--estimate-tip`](#--estimate-tip-estimate_tip)
6465

65-
Tip for the transaction. When not provided, defaults to 0.
66+
Tip for the transaction. Tip for the transaction. When not provided, defaults to 0 unless [`--estimate-tip`](#--estimate-tip-estimate_tip) is used.
67+
68+
## `--estimate-tip <ESTIMATE_TIP>`
69+
Optional.
70+
Conflicts with: [`--tip`](#--tip-tip)
71+
72+
If passed, an estimated tip will be added to pay for the transaction. The tip is estimated based on the current network conditions and added to the transaction fee.
6673

6774
## `--nonce, -n <NONCE>`
6875
Optional.

docs/src/appendix/sncast/deploy.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,15 @@ Maximum L1 data gas unit price for the `deploy` transaction. When not used, defa
8686

8787
## `--tip <TIP>`
8888
Optional.
89+
Conflicts with: [`--estimate-tip`](#--estimate-tip-estimate_tip)
8990

90-
Tip for the transaction. When not provided, defaults to 0.
91+
Tip for the transaction. When not provided, defaults to 0 unless [`--estimate-tip`](#--estimate-tip-estimate_tip) is used.
92+
93+
## `--estimate-tip <ESTIMATE_TIP>`
94+
Optional.
95+
Conflicts with: [`--tip`](#--tip-tip)
96+
97+
If passed, an estimated tip will be added to pay for the transaction. The tip is estimated based on the current network conditions and added to the transaction fee.
9198

9299
## `--nonce, -n <NONCE>`
93100
Optional.

docs/src/appendix/sncast/invoke.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ Maximum L1 data gas unit price for the `invoke` transaction. When not used, defa
8282

8383
## `--tip <TIP>`
8484
Optional.
85+
Conflicts with: [`--estimate-tip`](#--estimate-tip-estimate_tip)
8586

86-
Tip for the transaction. When not provided, defaults to 0.
87+
Tip for the transaction. When not provided, defaults to 0 unless [`--estimate-tip`](#--estimate-tip-estimate_tip) is used.
88+
89+
## `--estimate-tip <ESTIMATE_TIP>`
90+
Optional.
91+
Conflicts with: [`--tip`](#--tip-tip)
92+
93+
If passed, an estimated tip will be added to pay for the transaction. The tip is estimated based on the current network conditions and added to the transaction fee.
8794

8895
## `--nonce, -n <NONCE>`
8996
Optional.

0 commit comments

Comments
 (0)