Ensure Amount uses checked ops

This commit is contained in:
Luke Parker 2023-01-20 08:08:20 -05:00
parent 8ca90e7905
commit 27f5881553
No known key found for this signature in database

View file

@ -1,6 +1,4 @@
use core::{ use core::ops::{Add, Sub, Mul};
ops::{Add, Mul},
};
use scale::{Encode, Decode, MaxEncodedLen}; use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo; use scale_info::TypeInfo;
@ -21,13 +19,21 @@ pub const COIN: Amount = Amount(1_000_000_00);
impl Add for Amount { impl Add for Amount {
type Output = Amount; type Output = Amount;
fn add(self, other: Amount) -> Amount { fn add(self, other: Amount) -> Amount {
Amount(self.0 + other.0) // Explicitly use checked_add so even if range checks are disabled, this is still checked
Amount(self.0.checked_add(other.0).unwrap())
}
}
impl Sub for Amount {
type Output = Amount;
fn sub(self, other: Amount) -> Amount {
Amount(self.0.checked_sub(other.0).unwrap())
} }
} }
impl Mul for Amount { impl Mul for Amount {
type Output = Amount; type Output = Amount;
fn mul(self, other: Amount) -> Amount { fn mul(self, other: Amount) -> Amount {
Amount(self.0 * other.0) Amount(self.0.checked_mul(other.0).unwrap())
} }
} }