Skip to content

Commit 65c821d

Browse files
grarcomergify[bot]
authored andcommitted
Refactors computation of the masp sus fee into a function
(cherry picked from commit 2757e68)
1 parent b259075 commit 65c821d

File tree

2 files changed

+60
-111
lines changed

2 files changed

+60
-111
lines changed

crates/sdk/src/tx.rs

Lines changed: 56 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,33 +2916,10 @@ pub async fn build_ibc_transfer(
29162916
(&MASP, None) => {
29172917
// NOTE: The frontend fee should NOT account for the masp fee
29182918
// payment amount
2919-
let sus_fee_amt = namada_token::Amount::from_uint(
2920-
validated_amount
2921-
.amount()
2922-
.raw_amount()
2923-
.checked_mul_div(
2924-
percentage.abs(),
2925-
namada_core::uint::Uint::exp10(
2926-
POS_DECIMAL_PRECISION as _,
2927-
),
2928-
)
2929-
.ok_or_else(|| {
2930-
Error::Other(
2931-
"Overflow in masp frontend fee computation"
2932-
.to_string(),
2933-
)
2934-
})?
2935-
.0,
2936-
0,
2937-
)
2938-
.map_err(|e| Error::Other(e.to_string()))?;
2939-
// Validate the amount given
2940-
let validated_fee_amount = validate_amount(
2919+
let validated_fee_amount = compute_masp_frontend_sus_fee(
29412920
context,
2942-
args::InputAmount::Unvalidated(DenominatedAmount::new(
2943-
sus_fee_amt,
2944-
validated_amount.denom(),
2945-
)),
2921+
&validated_amount,
2922+
percentage,
29462923
&args.token,
29472924
args.tx.force,
29482925
)
@@ -3650,6 +3627,45 @@ async fn get_masp_fee_payment_amount<N: Namada>(
36503627
})
36513628
}
36523629

3630+
// Extract the validate amount for the masp frontend sustainability fee
3631+
async fn compute_masp_frontend_sus_fee(
3632+
context: &impl Namada,
3633+
input_amount: &namada_token::DenominatedAmount,
3634+
percentage: &namada_core::dec::Dec,
3635+
token: &Address,
3636+
force: bool,
3637+
) -> Result<namada_token::DenominatedAmount> {
3638+
let sus_fee_amt = namada_token::Amount::from_uint(
3639+
input_amount
3640+
.amount()
3641+
.raw_amount()
3642+
.checked_mul_div(
3643+
percentage.abs(),
3644+
namada_core::uint::Uint::exp10(POS_DECIMAL_PRECISION as _),
3645+
)
3646+
.ok_or_else(|| {
3647+
Error::Other(
3648+
"Overflow in masp frontend fee computation".to_string(),
3649+
)
3650+
})?
3651+
.0,
3652+
0,
3653+
)
3654+
.map_err(|e| Error::Other(e.to_string()))?;
3655+
3656+
// Validate the amount given
3657+
validate_amount(
3658+
context,
3659+
args::InputAmount::Unvalidated(DenominatedAmount::new(
3660+
sus_fee_amt,
3661+
input_amount.denom(),
3662+
)),
3663+
token,
3664+
force,
3665+
)
3666+
.await
3667+
}
3668+
36533669
/// Build a shielding transfer
36543670
pub async fn build_shielding_transfer<N: Namada>(
36553671
context: &N,
@@ -3702,40 +3718,15 @@ pub async fn build_shielding_transfer<N: Namada>(
37023718
percentage,
37033719
)) = &args.frontend_sus_fee
37043720
{
3705-
let sus_fee_amt = namada_token::Amount::from_uint(
3706-
validated_amount
3707-
.amount()
3708-
.raw_amount()
3709-
.checked_mul_div(
3710-
percentage.abs(),
3711-
namada_core::uint::Uint::exp10(
3712-
POS_DECIMAL_PRECISION as _,
3713-
),
3714-
)
3715-
.ok_or_else(|| {
3716-
Error::Other(
3717-
"Overflow in masp frontend fee computation"
3718-
.to_string(),
3719-
)
3720-
})?
3721-
.0,
3722-
0,
3721+
let validated_fee_amount = compute_masp_frontend_sus_fee(
3722+
context,
3723+
&validated_amount,
3724+
percentage,
3725+
token,
3726+
args.tx.force,
37233727
)
3724-
.map_err(|e| Error::Other(e.to_string()))?;
3725-
// Validate the amount given
3726-
Some((
3727-
sus_fee_target,
3728-
validate_amount(
3729-
context,
3730-
args::InputAmount::Unvalidated(DenominatedAmount::new(
3731-
sus_fee_amt,
3732-
validated_amount.denom(),
3733-
)),
3734-
token,
3735-
args.tx.force,
3736-
)
3737-
.await?,
3738-
))
3728+
.await?;
3729+
Some((sus_fee_target, validated_fee_amount))
37393730
} else {
37403731
None
37413732
};
@@ -3964,33 +3955,10 @@ pub async fn build_unshielding_transfer<N: Namada>(
39643955
if let Some((sus_fee_target, percentage)) = &args.frontend_sus_fee {
39653956
// NOTE: The frontend fee should NOT account for the masp fee
39663957
// payment amount
3967-
let sus_fee_amt = namada_token::Amount::from_uint(
3968-
validated_amount
3969-
.amount()
3970-
.raw_amount()
3971-
.checked_mul_div(
3972-
percentage.abs(),
3973-
namada_core::uint::Uint::exp10(
3974-
POS_DECIMAL_PRECISION as _,
3975-
),
3976-
)
3977-
.ok_or_else(|| {
3978-
Error::Other(
3979-
"Overflow in masp frontend fee computation"
3980-
.to_string(),
3981-
)
3982-
})?
3983-
.0,
3984-
0,
3985-
)
3986-
.map_err(|e| Error::Other(e.to_string()))?;
3987-
// Validate the amount given
3988-
let validated_fee_amount = validate_amount(
3958+
let validated_fee_amount = compute_masp_frontend_sus_fee(
39893959
context,
3990-
args::InputAmount::Unvalidated(DenominatedAmount::new(
3991-
sus_fee_amt,
3992-
validated_amount.denom(),
3993-
)),
3960+
&validated_amount,
3961+
percentage,
39943962
token,
39953963
args.tx.force,
39963964
)
@@ -4570,33 +4538,10 @@ pub async fn gen_ibc_shielding_transfer<N: Namada>(
45704538

45714539
let (extra_target, source_amount) = match &args.frontend_sus_fee {
45724540
Some((target, percentage)) => {
4573-
let sus_fee_amt = namada_token::Amount::from_uint(
4574-
validated_amount
4575-
.amount()
4576-
.raw_amount()
4577-
.checked_mul_div(
4578-
percentage.abs(),
4579-
namada_core::uint::Uint::exp10(
4580-
POS_DECIMAL_PRECISION as _,
4581-
),
4582-
)
4583-
.ok_or_else(|| {
4584-
Error::Other(
4585-
"Overflow in masp frontend fee computation"
4586-
.to_string(),
4587-
)
4588-
})?
4589-
.0,
4590-
0,
4591-
)
4592-
.map_err(|e| Error::Other(e.to_string()))?;
4593-
// Validate the amount given
4594-
let validated_fee_amount = validate_amount(
4541+
let validated_fee_amount = compute_masp_frontend_sus_fee(
45954542
context,
4596-
args::InputAmount::Unvalidated(DenominatedAmount::new(
4597-
sus_fee_amt,
4598-
validated_amount.denom(),
4599-
)),
4543+
&validated_amount,
4544+
percentage,
46004545
&token,
46014546
false,
46024547
)

crates/tests/src/e2e/ibc_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,8 @@ fn ibc_unlimited_channel() -> Result<()> {
14281428
None,
14291429
None,
14301430
false,
1431+
None,
1432+
None,
14311433
)?;
14321434
wait_for_packet_relay(
14331435
&hermes_dir,
@@ -1458,6 +1460,8 @@ fn ibc_unlimited_channel() -> Result<()> {
14581460
None,
14591461
None,
14601462
false,
1463+
None,
1464+
None,
14611465
)?;
14621466
// wait for the timeout
14631467
sleep(10);

0 commit comments

Comments
 (0)