Skip to content

Commit

Permalink
Print max fee conversion info when using v3 txs (#2797)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2707 

## Introduced changes

<!-- A brief description of the changes -->

Display info about about conversion when using `--max-fee` flag with v3
transactions

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`

---------

Co-authored-by: Piotr Figiela <[email protected]>
  • Loading branch information
franciszekjob and Draggu authored Jan 14, 2025
1 parent 9dca1ce commit 44563cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
17 changes: 17 additions & 0 deletions crates/sncast/src/helpers/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl FeeArgs {
}
}

#[allow(clippy::too_many_lines)]
pub async fn try_into_fee_settings<P: Provider>(
&self,
provider: P,
Expand Down Expand Up @@ -106,6 +107,7 @@ impl FeeArgs {

let max_gas = NonZeroFelt::try_from(Felt::from(max_fee).floor_div(&max_gas_unit_price))
.unwrap_or_else(|_| unreachable!("Calculated max gas must be >= 1 because max_fee >= max_gas_unit_price ensures a positive result"));
print_max_fee_conversion_info(max_fee, max_gas, max_gas_unit_price);
FeeSettings::Strk {
max_gas: Some(
NonZeroU64::try_from_(max_gas).map_err(anyhow::Error::msg)?,
Expand All @@ -123,6 +125,7 @@ impl FeeArgs {

let max_gas_unit_price = NonZeroFelt::try_from(Felt::from(max_fee).floor_div(&max_gas))
.unwrap_or_else(|_| unreachable!("Calculated max gas unit price must be >= 1 because max_fee >= max_gas ensures a positive result"));
print_max_fee_conversion_info(max_fee, max_gas, max_gas_unit_price);
FeeSettings::Strk {
max_gas: Some(
NonZeroU64::try_from_(max_gas).map_err(anyhow::Error::msg)?,
Expand All @@ -144,6 +147,7 @@ impl FeeArgs {
// TODO(#2852)
let max_gas = NonZeroFelt::try_from(Felt::from(max_fee)
.floor_div(&max_gas_unit_price)).context("Calculated max-gas from provided --max-fee and the current network gas price is 0. Please increase --max-fee to obtain a positive gas amount")?;
print_max_fee_conversion_info(max_fee, max_gas, max_gas_unit_price);
FeeSettings::Strk {
max_gas: Some(
NonZeroU64::try_from_(max_gas).map_err(anyhow::Error::msg)?,
Expand Down Expand Up @@ -285,6 +289,19 @@ fn parse_fee_token(s: &str) -> Result<FeeToken, String> {
Ok(parsed_token)
}

fn print_max_fee_conversion_info(
max_fee: impl Into<Felt>,
max_gas: impl Into<Felt>,
max_gas_unit_price: impl Into<Felt>,
) {
let max_fee: Felt = max_fee.into();
let max_gas: Felt = max_gas.into();
let max_gas_unit_price: Felt = max_gas_unit_price.into();
println!(
"Specifying '--max-fee' flag while using v3 transactions results in conversion to '--max-gas' and '--max-gas-unit-price' flags\nConverted {max_fee} max fee to {max_gas} max gas and {max_gas_unit_price} max gas unit price\n",
);
}

fn parse_non_zero_felt(s: &str) -> Result<NonZeroFelt, String> {
let felt: Felt = s.parse().map_err(|_| "Failed to parse value")?;
felt.try_into()
Expand Down
9 changes: 9 additions & 0 deletions crates/sncast/tests/e2e/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ pub async fn test_valid_class_hash() {
let snapbox = runner(&args).current_dir(tempdir.path());

snapbox.assert().success().stdout_matches(indoc! {r"
Specifying '--max-fee' flag while using v3 transactions results in conversion to '--max-gas' and '--max-gas-unit-price' flags
Converted [..] max fee to [..] max gas and [..] max gas unit price
command: account deploy
transaction_hash: [..]
Expand Down Expand Up @@ -616,6 +619,9 @@ pub async fn test_happy_case_keystore(account_type: &str) {
let snapbox = runner(&args).current_dir(tempdir.path());

snapbox.assert().stdout_matches(indoc! {r"
Specifying '--max-fee' flag while using v3 transactions results in conversion to '--max-gas' and '--max-gas-unit-price' flags
Converted [..] max fee to [..] max gas and [..] max gas unit price
command: account deploy
transaction_hash: 0x0[..]
Expand Down Expand Up @@ -889,6 +895,9 @@ pub async fn test_deploy_keystore_other_args() {

let snapbox = runner(&args).current_dir(tempdir.path());
snapbox.assert().stdout_matches(indoc! {r"
Specifying '--max-fee' flag while using v3 transactions results in conversion to '--max-gas' and '--max-gas-unit-price' flags
Converted [..] max fee to [..] max gas and [..] max gas unit price
command: account deploy
transaction_hash: 0x0[..]
Expand Down
12 changes: 10 additions & 2 deletions crates/sncast/tests/e2e/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,19 @@ async fn test_happy_case_strk(class_hash: Felt, account_type: AccountType) {
];

let snapbox = runner(&args).current_dir(tempdir.path());
let output = snapbox.assert().success().get_output().stdout.clone();
let output = snapbox.assert().success();
let stdout = output.get_output().stdout.clone();

let hash = get_transaction_hash(&output);
let hash = get_transaction_hash(&stdout);
let receipt = get_transaction_receipt(hash).await;

assert_stdout_contains(
output,
indoc! {
"Specifying '--max-fee' flag while using v3 transactions results in conversion to '--max-gas' and '--max-gas-unit-price' flags
Converted [..] max fee to [..] max gas and [..] max gas unit price"
},
);
assert!(matches!(receipt, Invoke(_)));
}

Expand Down

0 comments on commit 44563cf

Please sign in to comment.