make math safer

This commit is contained in:
akildemir
2024-04-08 12:08:48 +03:00
parent 207f2bd28a
commit c32040240c

View File

@@ -65,7 +65,7 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: BlockNumberFor<T>) { fn on_finalize(n: BlockNumberFor<T>) {
// Distribute the genesis sri to pools after a month // Distribute the genesis sri to pools after a month
if n > BLOCKS_PER_MONTH.into() { if n == BLOCKS_PER_MONTH.into() {
// mint the SRI // mint the SRI
Coins::<T>::mint( Coins::<T>::mint(
GENESIS_LIQUIDITY_ACCOUNT.into(), GENESIS_LIQUIDITY_ACCOUNT.into(),
@@ -84,19 +84,19 @@ pub mod pallet {
account_values.insert(coin, vec![]); account_values.insert(coin, vec![]);
let mut pool_amount: u64 = 0; let mut pool_amount: u64 = 0;
for (account, amount) in Liquidity::<T>::iter_prefix(coin) { for (account, amount) in Liquidity::<T>::iter_prefix(coin) {
pool_amount += amount; pool_amount = pool_amount.saturating_add(amount);
let value_this_addr = amount * value; let value_this_addr = amount.saturating_mul(value);
account_values.get_mut(&coin).unwrap().push((account, value_this_addr)) account_values.get_mut(&coin).unwrap().push((account, value_this_addr))
} }
let pool_value = pool_amount * value; let pool_value = pool_amount.saturating_mul(value);
total_value += pool_value; total_value = total_value.saturating_add(pool_value);
pool_values.insert(coin, (pool_amount, pool_value)); pool_values.insert(coin, (pool_amount, pool_value));
} }
// add the liquidity per pool // add the liquidity per pool
for (coin, (amount, value)) in &pool_values { for (coin, (amount, value)) in &pool_values {
let sri_amount = (GENESIS_SRI * value) / total_value; let sri_amount = GENESIS_SRI.saturating_mul(*value) / total_value;
let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into()); let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into());
Dex::<T>::add_liquidity( Dex::<T>::add_liquidity(
origin.into(), origin.into(),
@@ -117,8 +117,8 @@ pub mod pallet {
let tokens = LiquidityTokens::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), *coin).0; let tokens = LiquidityTokens::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), *coin).0;
let mut total_tokens_this_coin: u64 = 0; let mut total_tokens_this_coin: u64 = 0;
for (acc, value) in account_values.get(coin).unwrap() { for (acc, value) in account_values.get(coin).unwrap() {
let liq_tokens_this_acc = (tokens * value) / pool_values.get(coin).unwrap().1; let liq_tokens_this_acc = tokens.saturating_mul(*value) / pool_values.get(coin).unwrap().1;
total_tokens_this_coin += liq_tokens_this_acc; total_tokens_this_coin = total_tokens_this_coin.saturating_add(liq_tokens_this_acc);
LiquidityTokensPerAddress::<T>::set(coin, acc, Some(liq_tokens_this_acc)); LiquidityTokensPerAddress::<T>::set(coin, acc, Some(liq_tokens_this_acc));
} }
assert_eq!(tokens, total_tokens_this_coin); assert_eq!(tokens, total_tokens_this_coin);
@@ -195,10 +195,10 @@ pub mod pallet {
let current_coin = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), balance.coin); let current_coin = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), balance.coin);
// burn the SRI if necessary // burn the SRI if necessary
let mut sri = current_sri.0 - prev_sri.0; let mut sri = current_sri.0.saturating_sub(prev_sri.0);
let burn_sri_amount = (sri * let burn_sri_amount = sri.saturating_mul(
(GENESIS_SRI_TRICKLE_FEED - Self::blocks_since_ec_security(&balance.coin))) / GENESIS_SRI_TRICKLE_FEED - Self::blocks_since_ec_security(&balance.coin),
GENESIS_SRI_TRICKLE_FEED; ) / GENESIS_SRI_TRICKLE_FEED;
Coins::<T>::burn( Coins::<T>::burn(
origin.clone().into(), origin.clone().into(),
Balance { coin: Coin::Serai, amount: Amount(burn_sri_amount) }, Balance { coin: Coin::Serai, amount: Amount(burn_sri_amount) },