Skip to content

Commit 6c91eca

Browse files
committed
Merge branch 'grarco/remove-sdk-fee-panics' (#1878)
* origin/grarco/remove-sdk-fee-panics: changelog: add #1878 Removes fee panics from sdk Adds fee-related errors to sdk
2 parents 13da498 + 63467f7 commit 6c91eca

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Added two new variants to the `TxError` enum, `BalanceToLowForFees` and
2+
`FeeUnshieldingError`, representing possible failures in transactions' fees.
3+
([\#1878](https://github.com/anoma/namada/pull/1878))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Removed gas and fees related panics from the sdk.
2+
([\#1878](https://github.com/anoma/namada/pull/1878))

shared/src/sdk/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ pub enum TxError {
189189
to be transferred. Amount to transfer is {2} and the balance is {3}."
190190
)]
191191
BalanceTooLow(Address, Address, String, String),
192+
/// Balance is too low for fee payment
193+
#[error(
194+
"The balance of the source {0} of token {1} is lower than the amount \
195+
required for fees. Amount of the fees is {2} and the balance is {3}."
196+
)]
197+
BalanceTooLowForFees(Address, Address, String, String),
192198
/// Token Address does not exist on chain
193199
#[error("The token address {0} doesn't exist on chain.")]
194200
TokenDoesNotExist(Address),
@@ -213,6 +219,9 @@ pub enum TxError {
213219
/// No Balance found for token
214220
#[error("{0}")]
215221
MaspError(String),
222+
/// Error in the fee unshielding transaction
223+
#[error("Error in fee unshielding: {0}")]
224+
FeeUnshieldingError(String),
216225
/// Wasm validation failed
217226
#[error("Validity predicate code validation failed with {0}")]
218227
WasmValidationFailure(WasmValidationError),

shared/src/sdk/signing.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,11 @@ pub async fn aux_signing_data<
291291
};
292292

293293
if fee_payer == masp_tx_key().to_public() {
294-
panic!(
294+
other_err(
295295
"The gas payer cannot be the MASP, please provide a different gas \
296296
payer."
297-
);
297+
.to_string(),
298+
)?;
298299
}
299300

300301
Ok(SigningTxData {
@@ -333,7 +334,7 @@ pub async fn wrap_tx<
333334
tx_source_balance: Option<TxSourcePostBalance>,
334335
epoch: Epoch,
335336
fee_payer: common::PublicKey,
336-
) -> Option<Epoch> {
337+
) -> Result<Option<Epoch>, Error> {
337338
let fee_payer_address = Address::from(&fee_payer);
338339
// Validate fee amount and token
339340
let gas_cost_key = parameter_storage::get_gas_cost_key();
@@ -345,17 +346,17 @@ pub async fn wrap_tx<
345346
.and_then(|map| {
346347
map.get(&args.fee_token)
347348
.map(ToOwned::to_owned)
348-
.ok_or_else(|| Error::Other("no fee found".to_string()))
349+
.ok_or_else(|| {
350+
Error::Other(format!(
351+
"Could not retrieve from storage the gas cost for token {}",
352+
args.fee_token
353+
))
354+
})
349355
}) {
350356
Ok(amount) => amount,
351-
Err(_e) => {
352-
edisplay_line!(
353-
IO,
354-
"Could not retrieve the gas cost for token {}",
355-
args.fee_token
356-
);
357+
Err(e) => {
357358
if !args.force {
358-
panic!();
359+
return Err(e);
359360
} else {
360361
token::Amount::default()
361362
}
@@ -481,56 +482,62 @@ pub async fn wrap_tx<
481482
> descriptions_limit
482483
&& !args.force
483484
{
484-
panic!(
485-
"Fee unshielding descriptions exceed the limit"
486-
);
485+
return Err(Error::from(
486+
TxError::FeeUnshieldingError(format!(
487+
"Descriptions exceed the limit: found \
488+
{descriptions}, limit \
489+
{descriptions_limit}"
490+
)),
491+
));
487492
}
488493

489494
updated_balance += total_fee;
490495
(Some(transaction), Some(unshielding_epoch))
491496
}
492497
Ok(None) => {
493-
edisplay_line!(IO, "Missing unshielding transaction");
494498
if !args.force {
495-
panic!();
499+
return Err(Error::from(
500+
TxError::FeeUnshieldingError(
501+
"Missing unshielding transaction"
502+
.to_string(),
503+
),
504+
));
496505
}
497506

498507
(None, None)
499508
}
500509
Err(e) => {
501-
edisplay_line!(
502-
IO,
503-
"Error in fee unshielding generation: {}",
504-
e
505-
);
506510
if !args.force {
507-
panic!();
511+
return Err(Error::from(
512+
TxError::FeeUnshieldingError(e.to_string()),
513+
));
508514
}
509515

510516
(None, None)
511517
}
512518
}
513519
} else {
514520
let token_addr = args.fee_token.clone();
515-
let err_msg = format!(
516-
"The wrapper transaction source doesn't have enough \
517-
balance to pay fee {}, balance: {}.",
518-
format_denominated_amount::<_, IO>(
521+
if !args.force {
522+
let fee_amount = format_denominated_amount::<_, IO>(
519523
client,
520524
&token_addr,
521-
total_fee
525+
total_fee,
522526
)
523-
.await,
524-
format_denominated_amount::<_, IO>(
527+
.await;
528+
529+
let balance = format_denominated_amount::<_, IO>(
525530
client,
526531
&token_addr,
527-
updated_balance
532+
updated_balance,
528533
)
529-
.await,
530-
);
531-
edisplay_line!(IO, "{}", err_msg);
532-
if !args.force {
533-
panic!("{}", err_msg);
534+
.await;
535+
return Err(Error::from(TxError::BalanceTooLowForFees(
536+
fee_payer_address,
537+
token_addr,
538+
fee_amount,
539+
balance,
540+
)));
534541
}
535542

536543
(None, None)
@@ -568,7 +575,7 @@ pub async fn wrap_tx<
568575
unshield_section_hash,
569576
);
570577

571-
unshielding_epoch
578+
Ok(unshielding_epoch)
572579
}
573580

574581
#[allow(clippy::result_large_err)]

shared/src/sdk/tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub async fn prepare_tx<
164164
if !args.dry_run {
165165
let epoch = rpc::query_epoch(client).await?;
166166

167-
Ok(signing::wrap_tx::<_, _, IO>(
167+
signing::wrap_tx::<_, _, IO>(
168168
client,
169169
shielded,
170170
tx,
@@ -173,7 +173,7 @@ pub async fn prepare_tx<
173173
epoch,
174174
fee_payer,
175175
)
176-
.await)
176+
.await
177177
} else {
178178
Ok(None)
179179
}

0 commit comments

Comments
 (0)