Skip to content

Commit 1b8e70f

Browse files
committed
Refactors computation of the masp sus fee into a function
1 parent 229a70e commit 1b8e70f

File tree

1 file changed

+56
-111
lines changed

1 file changed

+56
-111
lines changed

crates/sdk/src/tx.rs

Lines changed: 56 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,33 +2832,10 @@ pub async fn build_ibc_transfer(
28322832
(&MASP, None) => {
28332833
// NOTE: The frontend fee should NOT account for the masp fee
28342834
// payment amount
2835-
let sus_fee_amt = namada_token::Amount::from_uint(
2836-
validated_amount
2837-
.amount()
2838-
.raw_amount()
2839-
.checked_mul_div(
2840-
percentage.abs(),
2841-
namada_core::uint::Uint::exp10(
2842-
POS_DECIMAL_PRECISION as _,
2843-
),
2844-
)
2845-
.ok_or_else(|| {
2846-
Error::Other(
2847-
"Overflow in masp frontend fee computation"
2848-
.to_string(),
2849-
)
2850-
})?
2851-
.0,
2852-
0,
2853-
)
2854-
.map_err(|e| Error::Other(e.to_string()))?;
2855-
// Validate the amount given
2856-
let validated_fee_amount = validate_amount(
2835+
let validated_fee_amount = compute_masp_frontend_sus_fee(
28572836
context,
2858-
args::InputAmount::Unvalidated(DenominatedAmount::new(
2859-
sus_fee_amt,
2860-
validated_amount.denom(),
2861-
)),
2837+
&validated_amount,
2838+
percentage,
28622839
&args.token,
28632840
args.tx.force,
28642841
)
@@ -3499,6 +3476,45 @@ async fn get_masp_fee_payment_amount<N: Namada>(
34993476
})
35003477
}
35013478

3479+
// Extract the validate amount for the masp frontend sustainability fee
3480+
async fn compute_masp_frontend_sus_fee(
3481+
context: &impl Namada,
3482+
input_amount: &namada_token::DenominatedAmount,
3483+
percentage: &namada_core::dec::Dec,
3484+
token: &Address,
3485+
force: bool,
3486+
) -> Result<namada_token::DenominatedAmount> {
3487+
let sus_fee_amt = namada_token::Amount::from_uint(
3488+
input_amount
3489+
.amount()
3490+
.raw_amount()
3491+
.checked_mul_div(
3492+
percentage.abs(),
3493+
namada_core::uint::Uint::exp10(POS_DECIMAL_PRECISION as _),
3494+
)
3495+
.ok_or_else(|| {
3496+
Error::Other(
3497+
"Overflow in masp frontend fee computation".to_string(),
3498+
)
3499+
})?
3500+
.0,
3501+
0,
3502+
)
3503+
.map_err(|e| Error::Other(e.to_string()))?;
3504+
3505+
// Validate the amount given
3506+
validate_amount(
3507+
context,
3508+
args::InputAmount::Unvalidated(DenominatedAmount::new(
3509+
sus_fee_amt,
3510+
input_amount.denom(),
3511+
)),
3512+
token,
3513+
force,
3514+
)
3515+
.await
3516+
}
3517+
35023518
/// Build a shielding transfer
35033519
pub async fn build_shielding_transfer<N: Namada>(
35043520
context: &N,
@@ -3556,40 +3572,15 @@ pub async fn build_shielding_transfer<N: Namada>(
35563572
percentage,
35573573
)) = &args.frontend_sus_fee
35583574
{
3559-
let sus_fee_amt = namada_token::Amount::from_uint(
3560-
validated_amount
3561-
.amount()
3562-
.raw_amount()
3563-
.checked_mul_div(
3564-
percentage.abs(),
3565-
namada_core::uint::Uint::exp10(
3566-
POS_DECIMAL_PRECISION as _,
3567-
),
3568-
)
3569-
.ok_or_else(|| {
3570-
Error::Other(
3571-
"Overflow in masp frontend fee computation"
3572-
.to_string(),
3573-
)
3574-
})?
3575-
.0,
3576-
0,
3575+
let validated_fee_amount = compute_masp_frontend_sus_fee(
3576+
context,
3577+
&validated_amount,
3578+
percentage,
3579+
token,
3580+
args.tx.force,
35773581
)
3578-
.map_err(|e| Error::Other(e.to_string()))?;
3579-
// Validate the amount given
3580-
Some((
3581-
sus_fee_target,
3582-
validate_amount(
3583-
context,
3584-
args::InputAmount::Unvalidated(DenominatedAmount::new(
3585-
sus_fee_amt,
3586-
validated_amount.denom(),
3587-
)),
3588-
token,
3589-
args.tx.force,
3590-
)
3591-
.await?,
3592-
))
3582+
.await?;
3583+
Some((sus_fee_target, validated_fee_amount))
35933584
} else {
35943585
None
35953586
};
@@ -3809,33 +3800,10 @@ pub async fn build_unshielding_transfer<N: Namada>(
38093800
if let Some((sus_fee_target, percentage)) = &args.frontend_sus_fee {
38103801
// NOTE: The frontend fee should NOT account for the masp fee
38113802
// payment amount
3812-
let sus_fee_amt = namada_token::Amount::from_uint(
3813-
validated_amount
3814-
.amount()
3815-
.raw_amount()
3816-
.checked_mul_div(
3817-
percentage.abs(),
3818-
namada_core::uint::Uint::exp10(
3819-
POS_DECIMAL_PRECISION as _,
3820-
),
3821-
)
3822-
.ok_or_else(|| {
3823-
Error::Other(
3824-
"Overflow in masp frontend fee computation"
3825-
.to_string(),
3826-
)
3827-
})?
3828-
.0,
3829-
0,
3830-
)
3831-
.map_err(|e| Error::Other(e.to_string()))?;
3832-
// Validate the amount given
3833-
let validated_fee_amount = validate_amount(
3803+
let validated_fee_amount = compute_masp_frontend_sus_fee(
38343804
context,
3835-
args::InputAmount::Unvalidated(DenominatedAmount::new(
3836-
sus_fee_amt,
3837-
validated_amount.denom(),
3838-
)),
3805+
&validated_amount,
3806+
percentage,
38393807
token,
38403808
args.tx.force,
38413809
)
@@ -4370,33 +4338,10 @@ pub async fn gen_ibc_shielding_transfer<N: Namada>(
43704338

43714339
let (extra_target, source_amount) = match &args.frontend_sus_fee {
43724340
Some((target, percentage)) => {
4373-
let sus_fee_amt = namada_token::Amount::from_uint(
4374-
validated_amount
4375-
.amount()
4376-
.raw_amount()
4377-
.checked_mul_div(
4378-
percentage.abs(),
4379-
namada_core::uint::Uint::exp10(
4380-
POS_DECIMAL_PRECISION as _,
4381-
),
4382-
)
4383-
.ok_or_else(|| {
4384-
Error::Other(
4385-
"Overflow in masp frontend fee computation"
4386-
.to_string(),
4387-
)
4388-
})?
4389-
.0,
4390-
0,
4391-
)
4392-
.map_err(|e| Error::Other(e.to_string()))?;
4393-
// Validate the amount given
4394-
let validated_fee_amount = validate_amount(
4341+
let validated_fee_amount = compute_masp_frontend_sus_fee(
43954342
context,
4396-
args::InputAmount::Unvalidated(DenominatedAmount::new(
4397-
sus_fee_amt,
4398-
validated_amount.denom(),
4399-
)),
4343+
&validated_amount,
4344+
percentage,
44004345
&token,
44014346
false,
44024347
)

0 commit comments

Comments
 (0)