Skip to content

Commit 1c80da4

Browse files
authored
handle overflow (#667)
1 parent 17a791e commit 1c80da4

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

xtokens/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use frame_support::{log, pallet_prelude::*, require_transactional, traits::Get,
2626
use frame_system::{ensure_signed, pallet_prelude::*};
2727
use sp_runtime::{
2828
traits::{AtLeast32BitUnsigned, Convert, MaybeSerializeDeserialize, Member, Zero},
29-
DispatchError,
29+
ArithmeticError, DispatchError,
3030
};
3131
use sp_std::{prelude::*, result::Result};
3232

@@ -389,6 +389,12 @@ pub mod module {
389389
// For now fee and asset id should be identical
390390
// We can relax this assumption in the future
391391
ensure!(fee.id == asset.id, Error::<T>::DistincAssetAndFeeId);
392+
// Workaround issue of https://github.com/paritytech/polkadot/pull/4492
393+
// TODO: remove this on next Substrate version
394+
ensure!(
395+
fungible_amount(&asset).checked_add(fungible_amount(&fee)).is_some(),
396+
ArithmeticError::Overflow
397+
);
392398

393399
let (transfer_kind, dest, reserve, recipient) = Self::transfer_kind(&asset, &dest)?;
394400
let mut msg = match transfer_kind {

xtokens/src/tests.rs

+33
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,36 @@ fn send_with_insufficient_fee_traps_assets() {
733733
}));
734734
})
735735
}
736+
737+
#[test]
738+
fn send_with_fee_should_handle_overflow() {
739+
TestNet::reset();
740+
741+
ParaA::execute_with(|| {
742+
assert_ok!(ParaTokens::deposit(CurrencyId::A, &ALICE, 1_000));
743+
744+
assert_noop!(
745+
ParaXTokens::transfer_with_fee(
746+
Some(ALICE).into(),
747+
CurrencyId::A,
748+
u128::MAX,
749+
1,
750+
Box::new(
751+
MultiLocation::new(
752+
1,
753+
X2(
754+
Parachain(2),
755+
Junction::AccountId32 {
756+
network: NetworkId::Any,
757+
id: BOB.into(),
758+
}
759+
)
760+
)
761+
.into()
762+
),
763+
40,
764+
),
765+
ArithmeticError::Overflow
766+
);
767+
});
768+
}

0 commit comments

Comments
 (0)