Skip to content

Commit ebad8b6

Browse files
xlctgmichel
authored andcommitted
handle overflow (open-web3-stack#667)
(cherry picked from commit 1c80da4)
1 parent 0a4e46c commit ebad8b6

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::{pallet_prelude::*, require_transactional, traits::Get, trans
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::{convert::TryInto, prelude::*, result::Result};
3232

@@ -384,6 +384,12 @@ pub mod module {
384384
// For now fee and asset id should be identical
385385
// We can relax this assumption in the future
386386
ensure!(fee.id == asset.id, Error::<T>::DistincAssetAndFeeId);
387+
// Workaround issue of https://github.com/paritytech/polkadot/pull/4492
388+
// TODO: remove this on next Substrate version
389+
ensure!(
390+
fungible_amount(&asset).checked_add(fungible_amount(&fee)).is_some(),
391+
ArithmeticError::Overflow
392+
);
387393

388394
let (transfer_kind, dest, reserve, recipient) = Self::transfer_kind(&asset, &dest)?;
389395
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)