diff --git a/programs/uxd/src/mercurial_utils/calculate_lp_tokens_value.rs b/programs/uxd/src/mercurial_utils/calculate_lp_tokens_value.rs index d9895ea84..ba754f896 100644 --- a/programs/uxd/src/mercurial_utils/calculate_lp_tokens_value.rs +++ b/programs/uxd/src/mercurial_utils/calculate_lp_tokens_value.rs @@ -1,4 +1,5 @@ use crate::error::UxdError; +use crate::utils::checked_as_u64; use anchor_lang::{ prelude::{Account, Clock, SolanaSysvar}, Result, @@ -10,9 +11,7 @@ pub fn calculate_lp_tokens_value( mercurial_vault_lp_mint_supply: u64, lp_token_amount: u64, ) -> Result { - let current_time = u64::try_from(Clock::get()?.unix_timestamp) - .ok() - .ok_or(UxdError::MathOverflow)?; + let current_time = checked_as_u64(Clock::get()?.unix_timestamp)?; Ok(mercurial_vault .get_amount_by_share( diff --git a/programs/uxd/src/mercurial_utils/calculate_possible_lp_token_precision_loss_collateral_value.rs b/programs/uxd/src/mercurial_utils/calculate_possible_lp_token_precision_loss_collateral_value.rs index 76e480b71..0faef25f2 100644 --- a/programs/uxd/src/mercurial_utils/calculate_possible_lp_token_precision_loss_collateral_value.rs +++ b/programs/uxd/src/mercurial_utils/calculate_possible_lp_token_precision_loss_collateral_value.rs @@ -1,4 +1,6 @@ -use crate::{error::UxdError, utils::compute_value_for_single_share_ceil}; +use crate::error::UxdError; +use crate::utils::checked_as_u64; +use crate::utils::compute_value_for_single_share_ceil; use anchor_lang::{ prelude::{Account, Clock, SolanaSysvar}, Result, @@ -11,9 +13,7 @@ pub fn calculate_possible_lp_token_precision_loss_collateral_value( mercurial_vault: &Account, mercurial_vault_lp_mint_supply: u64, ) -> Result { - let current_time = u64::try_from(Clock::get()?.unix_timestamp) - .ok() - .ok_or(UxdError::MathOverflow)?; + let current_time = checked_as_u64(Clock::get()?.unix_timestamp)?; // Calculate the price of 1 native LP token // Do not use mercurial_vault.get_amount_by_share because it does not handle precision loss diff --git a/programs/uxd/src/utils/calculate_depositories_mint_collateral_amount.rs b/programs/uxd/src/utils/calculate_depositories_mint_collateral_amount.rs index c6e773a60..03b31e7a5 100644 --- a/programs/uxd/src/utils/calculate_depositories_mint_collateral_amount.rs +++ b/programs/uxd/src/utils/calculate_depositories_mint_collateral_amount.rs @@ -4,6 +4,7 @@ use anchor_lang::require; use crate::error::UxdError; use crate::utils::calculate_depositories_sum_value; use crate::utils::checked_as_u64; +use crate::utils::checked_sub; use crate::ROUTER_DEPOSITORIES_COUNT; use super::compute_amount_less_fraction_floor; @@ -36,10 +37,10 @@ pub fn calculate_depositories_mint_collateral_amount( { return Ok(0); } - Ok(depository - .target_redeemable_amount - .checked_sub(depository_redeemable_amount_under_management) - .ok_or(UxdError::MathOverflow)?) + checked_sub( + depository.target_redeemable_amount, + depository_redeemable_amount_under_management, + ) }) .collect::>>()?; @@ -65,10 +66,10 @@ pub fn calculate_depositories_mint_collateral_amount( let depositories_mint_collateral_amount = depositories_maximum_mintable_collateral_amount .iter() .map(|depository_mintable_collateral_amount| { - let other_depositories_maximum_mintable_collateral_amount = - total_maximum_mintable_collateral_amount - .checked_sub(*depository_mintable_collateral_amount) - .ok_or(UxdError::MathOverflow)?; + let other_depositories_maximum_mintable_collateral_amount = checked_sub( + total_maximum_mintable_collateral_amount, + *depository_mintable_collateral_amount, + )?; compute_amount_less_fraction_floor( requested_mint_collateral_amount, other_depositories_maximum_mintable_collateral_amount, diff --git a/programs/uxd/src/utils/calculate_depositories_redeemable_amount.rs b/programs/uxd/src/utils/calculate_depositories_redeemable_amount.rs index 2bef7a897..eafff0eaf 100644 --- a/programs/uxd/src/utils/calculate_depositories_redeemable_amount.rs +++ b/programs/uxd/src/utils/calculate_depositories_redeemable_amount.rs @@ -3,7 +3,9 @@ use anchor_lang::require; use crate::error::UxdError; use crate::utils::calculate_depositories_sum_value; +use crate::utils::checked_add; use crate::utils::checked_as_u64; +use crate::utils::checked_sub; use crate::ROUTER_DEPOSITORIES_COUNT; use super::compute_amount_less_fraction_floor; @@ -42,9 +44,10 @@ pub fn calculate_depositories_redeemable_amount( { return Ok(0); } - Ok(depository_redeemable_amount_under_management - .checked_sub(depository.target_redeemable_amount) - .ok_or(UxdError::MathOverflow)?) + checked_sub( + depository_redeemable_amount_under_management, + depository.target_redeemable_amount, + ) }) .collect::>>()?; @@ -82,9 +85,10 @@ pub fn calculate_depositories_redeemable_amount( // -- to be able to fullfill the user's redeemable requested amount // --------------------------------------------------------------------- - let total_overall_redeemable_amount = total_over_target_redeemable_amount - .checked_add(total_under_target_redeemable_amount) - .ok_or(UxdError::MathOverflow)?; + let total_overall_redeemable_amount = checked_add( + total_over_target_redeemable_amount, + total_under_target_redeemable_amount, + )?; require!( total_overall_redeemable_amount >= requested_redeemable_amount, UxdError::InvalidRedeemableAmount @@ -109,15 +113,16 @@ pub fn calculate_depositories_redeemable_amount( requested_redeemable_amount, total_over_target_redeemable_amount, ); - let requested_second_redeemable_amount = requested_redeemable_amount - .checked_sub(requested_first_redeemable_amount) - .ok_or(UxdError::MathOverflow)?; + let requested_second_redeemable_amount = checked_sub( + requested_redeemable_amount, + requested_first_redeemable_amount, + )?; // First step, try to use the over_target amounts, weighted for each depository let depository_first_redeemable_amount = if total_over_target_redeemable_amount > 0 { - let other_depositories_over_target_redeemable_amount = - total_over_target_redeemable_amount - .checked_sub(*depository_over_target_redeemable_amount) - .ok_or(UxdError::MathOverflow)?; + let other_depositories_over_target_redeemable_amount = checked_sub( + total_over_target_redeemable_amount, + *depository_over_target_redeemable_amount, + )?; compute_amount_less_fraction_floor( requested_first_redeemable_amount, other_depositories_over_target_redeemable_amount, @@ -128,10 +133,10 @@ pub fn calculate_depositories_redeemable_amount( }; // Second step, anything under_target must be taken as backup let depository_second_redeemable_amount = if total_under_target_redeemable_amount > 0 { - let other_depositories_under_target_redeemable_amount = - total_under_target_redeemable_amount - .checked_sub(*depository_under_target_redeemable_amount) - .ok_or(UxdError::MathOverflow)?; + let other_depositories_under_target_redeemable_amount = checked_sub( + total_under_target_redeemable_amount, + *depository_under_target_redeemable_amount, + )?; compute_amount_less_fraction_floor( requested_second_redeemable_amount, other_depositories_under_target_redeemable_amount, @@ -141,9 +146,10 @@ pub fn calculate_depositories_redeemable_amount( 0 }; // The combo of the two gives our depository amount - Ok(depository_first_redeemable_amount - .checked_add(depository_second_redeemable_amount) - .ok_or(UxdError::MathOverflow)?) + checked_add( + depository_first_redeemable_amount, + depository_second_redeemable_amount, + ) }, ) .collect::>>()?; @@ -158,27 +164,24 @@ pub fn calculate_depositories_redeemable_amount( let total_redeemable_amount = calculate_depositories_sum_value(&depositories_redeemable_amount)?; - let mut rounding_errors = requested_redeemable_amount - .checked_sub(total_redeemable_amount) - .ok_or(UxdError::MathOverflow)?; + let mut rounding_errors = checked_sub(requested_redeemable_amount, total_redeemable_amount)?; for i in 0..depositories_info.len() { let depository = &depositories_info[i]; if !depository.is_liquid { continue; } - let depository_remaining_after_redeem = - checked_as_u64(depository.redeemable_amount_under_management)? - .checked_sub(depositories_redeemable_amount[i]) - .ok_or(UxdError::MathOverflow)?; + let depository_remaining_after_redeem = checked_sub( + checked_as_u64(depository.redeemable_amount_under_management)?, + depositories_redeemable_amount[i], + )?; let depository_rounding_correction = std::cmp::min(depository_remaining_after_redeem, rounding_errors); - depositories_redeemable_amount[i] = depositories_redeemable_amount[i] - .checked_add(depository_rounding_correction) - .ok_or(UxdError::MathOverflow)?; - rounding_errors = rounding_errors - .checked_sub(depository_rounding_correction) - .ok_or(UxdError::MathOverflow)?; + depositories_redeemable_amount[i] = checked_add( + depositories_redeemable_amount[i], + depository_rounding_correction, + )?; + rounding_errors = checked_sub(rounding_errors, depository_rounding_correction)?; } // Done diff --git a/programs/uxd/src/utils/calculate_depositories_sum_value.rs b/programs/uxd/src/utils/calculate_depositories_sum_value.rs index fcb6097bd..e7193f5c0 100644 --- a/programs/uxd/src/utils/calculate_depositories_sum_value.rs +++ b/programs/uxd/src/utils/calculate_depositories_sum_value.rs @@ -2,6 +2,7 @@ use anchor_lang::prelude::Result; use anchor_lang::require; use crate::error::UxdError; +use crate::utils::checked_add; use crate::ROUTER_DEPOSITORIES_COUNT; /** @@ -15,9 +16,7 @@ pub fn calculate_depositories_sum_value(depositories_values: &Vec) -> Resul let sum = depositories_values .iter() .try_fold(0u64, |accumulator: u64, value: &u64| { - accumulator - .checked_add(*value) - .ok_or(UxdError::MathOverflow) + checked_add(accumulator, *value) })?; Ok(sum) } diff --git a/programs/uxd/src/utils/calculate_depositories_target_redeemable_amount.rs b/programs/uxd/src/utils/calculate_depositories_target_redeemable_amount.rs index 3961f37ef..2f8a6feb3 100644 --- a/programs/uxd/src/utils/calculate_depositories_target_redeemable_amount.rs +++ b/programs/uxd/src/utils/calculate_depositories_target_redeemable_amount.rs @@ -3,7 +3,9 @@ use anchor_lang::require; use crate::error::UxdError; use crate::utils::calculate_depositories_sum_value; +use crate::utils::checked_add; use crate::utils::checked_as_u64; +use crate::utils::checked_sub; use crate::BPS_POWER; use crate::ROUTER_DEPOSITORIES_COUNT; @@ -76,9 +78,10 @@ pub fn calculate_depositories_target_redeemable_amount( if depository_raw_target_redeemable_amount <= depository_hard_cap_amount { return Ok(0); } - Ok(depository_raw_target_redeemable_amount - .checked_sub(*depository_hard_cap_amount) - .ok_or(UxdError::MathOverflow)?) + checked_sub( + *depository_raw_target_redeemable_amount, + *depository_hard_cap_amount, + ) }, ) .collect::>>()?; @@ -93,9 +96,10 @@ pub fn calculate_depositories_target_redeemable_amount( if depository_raw_target_redeemable_amount >= depository_hard_cap_amount { return Ok(0); } - Ok(depository_hard_cap_amount - .checked_sub(*depository_raw_target_redeemable_amount) - .ok_or(UxdError::MathOverflow)?) + checked_sub( + *depository_hard_cap_amount, + *depository_raw_target_redeemable_amount, + ) }, ) .collect::>>()?; @@ -150,11 +154,13 @@ pub fn calculate_depositories_target_redeemable_amount( } else { 0 }; - let final_target = depository_raw_target_redeemable_amount - .checked_add(overflow_amount_reallocated_from_other_depositories) - .ok_or(UxdError::MathOverflow)? - .checked_sub(*depository_overflow_amount) - .ok_or(UxdError::MathOverflow)?; + let final_target = checked_sub( + checked_add( + *depository_raw_target_redeemable_amount, + overflow_amount_reallocated_from_other_depositories, + )?, + *depository_overflow_amount, + )?; Ok(final_target) }, ) diff --git a/programs/uxd/src/utils/math/checked_add_u128_and_i128.rs b/programs/uxd/src/utils/math/checked_add_u128_and_i128.rs index e28597c88..99b348a96 100644 --- a/programs/uxd/src/utils/math/checked_add_u128_and_i128.rs +++ b/programs/uxd/src/utils/math/checked_add_u128_and_i128.rs @@ -1,30 +1,21 @@ use crate::error::UxdError; +use crate::utils::checked_add; +use crate::utils::checked_as_u128; +use crate::utils::checked_sub; use anchor_lang::prelude::*; pub fn checked_add_u128_and_i128(value_before: u128, change_delta: i128) -> Result { // In case of a simple positive change (increase), add the two positive values if change_delta >= 0 { - let increase: u128 = u128::try_from(change_delta) - .ok() - .ok_or(UxdError::MathOverflow)?; - return Ok(value_before - .checked_add(increase) - .ok_or(UxdError::MathOverflow)?); + let increase: u128 = checked_as_u128(change_delta)?; + return checked_add(value_before, increase); } // In case of a negative change, substract the absolute value of the delta (decrease) let decrease: u128 = if change_delta == i128::MIN { // special case: i128::MIN does not have an i128 absolute value - u128::try_from(i128::MAX) - .ok() - .ok_or(UxdError::MathOverflow)? - .checked_add(1) - .ok_or(UxdError::MathOverflow)? + checked_add(checked_as_u128(i128::MAX)?, 1)? } else { - u128::try_from(change_delta.checked_abs().ok_or(UxdError::MathOverflow)?) - .ok() - .ok_or(UxdError::MathOverflow)? + checked_as_u128(change_delta.checked_abs().ok_or(UxdError::MathOverflow)?)? }; - Ok(value_before - .checked_sub(decrease) - .ok_or(UxdError::MathOverflow)?) + checked_sub(value_before, decrease) } diff --git a/programs/uxd/src/utils/math/compute_amount_fraction_ceil.rs b/programs/uxd/src/utils/math/compute_amount_fraction_ceil.rs index 837df33ea..cef010737 100644 --- a/programs/uxd/src/utils/math/compute_amount_fraction_ceil.rs +++ b/programs/uxd/src/utils/math/compute_amount_fraction_ceil.rs @@ -1,4 +1,7 @@ use crate::error::UxdError; +use crate::utils::checked_as_u64; +use crate::utils::checked_ceil_div; +use crate::utils::checked_mul; use anchor_lang::prelude::*; // Rounding error may increase the returned amount. @@ -12,19 +15,9 @@ pub fn compute_amount_fraction_ceil( if fraction_numerator == 0 || amount == 0 { return Ok(0); } - let amount: u128 = amount.into(); - let fraction_numerator: u128 = fraction_numerator.into(); - let fraction_denominator: u128 = fraction_denominator.into(); - let amount_fraction_ceil: u128 = amount - .checked_mul(fraction_numerator) - .ok_or(UxdError::MathOverflow)? - .checked_sub(1) - .ok_or(UxdError::MathOverflow)? - .checked_div(fraction_denominator) - .ok_or(UxdError::MathOverflow)? - .checked_add(1) - .ok_or(UxdError::MathOverflow)?; - Ok(u64::try_from(amount_fraction_ceil) - .ok() - .ok_or(UxdError::MathOverflow)?) + let amount_fraction_ceil: u128 = checked_ceil_div::( + checked_mul::(u128::from(amount), u128::from(fraction_numerator))?, + u128::from(fraction_denominator), + )?; + checked_as_u64(amount_fraction_ceil) } diff --git a/programs/uxd/src/utils/math/compute_amount_less_fraction_floor.rs b/programs/uxd/src/utils/math/compute_amount_less_fraction_floor.rs index ea136b8ff..731969239 100644 --- a/programs/uxd/src/utils/math/compute_amount_less_fraction_floor.rs +++ b/programs/uxd/src/utils/math/compute_amount_less_fraction_floor.rs @@ -1,4 +1,8 @@ use crate::error::UxdError; +use crate::utils::checked_as_u64; +use crate::utils::checked_div; +use crate::utils::checked_mul; +use crate::utils::checked_sub; use anchor_lang::prelude::*; // Precision loss may lower the returned amount. @@ -9,19 +13,19 @@ pub fn compute_amount_less_fraction_floor( fraction_denominator: u64, ) -> Result { require!(fraction_denominator > 0, UxdError::MathOverflow); - let amount: u128 = amount.into(); - let fraction_numerator: u128 = fraction_numerator.into(); - let fraction_denominator: u128 = fraction_denominator.into(); - let amount_less_fraction: u128 = amount - .checked_mul( - fraction_denominator - .checked_sub(fraction_numerator) - .ok_or(UxdError::MathOverflow)?, - ) - .ok_or(UxdError::MathOverflow)? - .checked_div(fraction_denominator) - .ok_or(UxdError::MathOverflow)?; - Ok(u64::try_from(amount_less_fraction) - .ok() - .ok_or(UxdError::MathOverflow)?) + require!( + fraction_denominator >= fraction_numerator, + UxdError::MathOverflow + ); + let amount_less_fraction: u128 = checked_div::( + checked_mul::( + u128::from(amount), + checked_sub::( + u128::from(fraction_denominator), + u128::from(fraction_numerator), + )?, + )?, + u128::from(fraction_denominator), + )?; + checked_as_u64(amount_less_fraction) } diff --git a/programs/uxd/src/utils/math/compute_decrease.rs b/programs/uxd/src/utils/math/compute_decrease.rs index 5121c549e..ea05ca522 100644 --- a/programs/uxd/src/utils/math/compute_decrease.rs +++ b/programs/uxd/src/utils/math/compute_decrease.rs @@ -1,6 +1,6 @@ -use crate::error::UxdError; +use crate::utils::checked_sub; use anchor_lang::prelude::*; pub fn compute_decrease(before: u64, after: u64) -> Result { - Ok(before.checked_sub(after).ok_or(UxdError::MathOverflow)?) + checked_sub(before, after) } diff --git a/programs/uxd/src/utils/math/compute_increase.rs b/programs/uxd/src/utils/math/compute_increase.rs index 9d15bc155..73f5be833 100644 --- a/programs/uxd/src/utils/math/compute_increase.rs +++ b/programs/uxd/src/utils/math/compute_increase.rs @@ -1,6 +1,6 @@ -use crate::error::UxdError; +use crate::utils::checked_sub; use anchor_lang::prelude::*; pub fn compute_increase(before: u64, after: u64) -> Result { - Ok(after.checked_sub(before).ok_or(UxdError::MathOverflow)?) + checked_sub(after, before) } diff --git a/programs/uxd/src/utils/math/compute_shares_amount_for_value_floor.rs b/programs/uxd/src/utils/math/compute_shares_amount_for_value_floor.rs index 7d93dedb0..6c875d6b8 100644 --- a/programs/uxd/src/utils/math/compute_shares_amount_for_value_floor.rs +++ b/programs/uxd/src/utils/math/compute_shares_amount_for_value_floor.rs @@ -1,4 +1,7 @@ use crate::error::UxdError; +use crate::utils::checked_as_u64; +use crate::utils::checked_div; +use crate::utils::checked_mul; use anchor_lang::prelude::*; // Precision loss may lower the returned shares amount. @@ -13,15 +16,9 @@ pub fn compute_shares_amount_for_value_floor( } require!(total_shares_supply > 0, UxdError::MathOverflow); require!(total_shares_value > 0, UxdError::MathOverflow); - let value: u128 = value.into(); - let total_shares_supply: u128 = total_shares_supply.into(); - let total_shares_value: u128 = total_shares_value.into(); - let shares_amount: u128 = value - .checked_mul(total_shares_supply) - .ok_or(UxdError::MathOverflow)? - .checked_div(total_shares_value) - .ok_or(UxdError::MathOverflow)?; - Ok(u64::try_from(shares_amount) - .ok() - .ok_or(UxdError::MathOverflow)?) + let shares_amount: u128 = checked_div::( + checked_mul::(u128::from(value), u128::from(total_shares_supply))?, + u128::from(total_shares_value), + )?; + checked_as_u64(shares_amount) } diff --git a/programs/uxd/src/utils/math/compute_value_for_shares_amount_floor.rs b/programs/uxd/src/utils/math/compute_value_for_shares_amount_floor.rs index bdba47f04..2ac6b1204 100644 --- a/programs/uxd/src/utils/math/compute_value_for_shares_amount_floor.rs +++ b/programs/uxd/src/utils/math/compute_value_for_shares_amount_floor.rs @@ -1,4 +1,7 @@ use crate::error::UxdError; +use crate::utils::checked_as_u64; +use crate::utils::checked_div; +use crate::utils::checked_mul; use anchor_lang::prelude::*; // Precision loss may lower the returned value amount. @@ -13,13 +16,9 @@ pub fn compute_value_for_shares_amount_floor( } require!(total_shares_supply > 0, UxdError::MathOverflow); require!(total_shares_value > 0, UxdError::MathOverflow); - let shares_amount: u128 = shares_amount.into(); - let total_shares_supply: u128 = total_shares_supply.into(); - let total_shares_value: u128 = total_shares_value.into(); - let value: u128 = shares_amount - .checked_mul(total_shares_value) - .ok_or(UxdError::MathOverflow)? - .checked_div(total_shares_supply) - .ok_or(UxdError::MathOverflow)?; - Ok(u64::try_from(value).ok().ok_or(UxdError::MathOverflow)?) + let value: u128 = checked_div::( + checked_mul::(u128::from(shares_amount), u128::from(total_shares_value))?, + u128::from(total_shares_supply), + )?; + checked_as_u64(value) } diff --git a/programs/uxd/src/utils/math/compute_value_for_single_share_ceil.rs b/programs/uxd/src/utils/math/compute_value_for_single_share_ceil.rs index c70ffba0f..2d99e96ce 100644 --- a/programs/uxd/src/utils/math/compute_value_for_single_share_ceil.rs +++ b/programs/uxd/src/utils/math/compute_value_for_single_share_ceil.rs @@ -1,19 +1,12 @@ use crate::error::UxdError; +use crate::utils::checked_ceil_div; use anchor_lang::prelude::*; pub fn compute_value_for_single_share_ceil( total_shares_value: u64, total_shares_supply: u64, ) -> Result { - // ceil ( total_shares_value / total_shares_supply ) - // is equivalent to (total_shares_value - 1) / total_shares_supply + 1 require!(total_shares_value > 0, UxdError::MathOverflow); require!(total_shares_supply > 0, UxdError::MathOverflow); - Ok(total_shares_value - .checked_sub(1) - .ok_or(UxdError::MathOverflow)? - .checked_div(total_shares_supply) - .ok_or(UxdError::MathOverflow)? - .checked_add(1) - .ok_or(UxdError::MathOverflow)?) + checked_ceil_div(total_shares_value, total_shares_supply) } diff --git a/programs/uxd/src/utils/maths.rs b/programs/uxd/src/utils/maths.rs index cedf27e61..42bc63e9c 100644 --- a/programs/uxd/src/utils/maths.rs +++ b/programs/uxd/src/utils/maths.rs @@ -1,6 +1,7 @@ //! Common math routines. #![allow(dead_code)] +#![allow(clippy::redundant_clone)] // the solana target macro triggers this warning on non-solana builds use {crate::error::UxdError, anchor_lang::prelude::*, std::fmt::Display}; @@ -11,6 +12,7 @@ where if let Some(res) = arg1.checked_add(&arg2) { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} + {}", arg1, arg2); err!(UxdError::MathOverflow) } @@ -23,6 +25,7 @@ where if let Some(res) = arg1.checked_sub(&arg2) { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} - {}", arg1, arg2); err!(UxdError::MathOverflow) } @@ -35,6 +38,7 @@ where if let Some(res) = arg1.checked_div(&arg2) { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", arg1, arg2); err!(UxdError::MathOverflow) } @@ -45,11 +49,13 @@ where T: num_traits::Float + Display, { if arg2 == T::zero() { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", arg1, arg2); return err!(UxdError::MathOverflow); } let res = arg1 / arg2; if !res.is_finite() { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", arg1, arg2); err!(UxdError::MathOverflow) } else { @@ -68,12 +74,14 @@ where if let Some(res) = (arg1 - T::one()).checked_div(&arg2) { Ok(res + T::one()) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", arg1, arg2); err!(UxdError::MathOverflow) } } else if let Some(res) = arg1.checked_div(&arg2) { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", arg1, arg2); err!(UxdError::MathOverflow) } @@ -87,6 +95,7 @@ pub fn checked_decimal_div( target_exponent: i32, ) -> Result { if coefficient2 == 0 { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", coefficient1, coefficient2); return err!(UxdError::MathOverflow); } @@ -137,6 +146,7 @@ pub fn checked_decimal_ceil_div( target_exponent: i32, ) -> Result { if coefficient2 == 0 { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} / {}", coefficient1, coefficient2); return err!(UxdError::MathOverflow); } @@ -205,6 +215,7 @@ where if let Some(res) = arg1.checked_mul(&arg2) { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} * {}", arg1, arg2); err!(UxdError::MathOverflow) } @@ -216,6 +227,7 @@ where { let res = arg1 * arg2; if !res.is_finite() { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} * {}", arg1, arg2); err!(UxdError::MathOverflow) } else { @@ -297,6 +309,7 @@ where if let Some(res) = num_traits::checked_pow(arg, exp) { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} ^ {}", arg, exp); err!(UxdError::MathOverflow) } @@ -307,6 +320,7 @@ pub fn checked_powf(arg: f64, exp: f64) -> Result { if res.is_finite() { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} ^ {}", arg, exp); err!(UxdError::MathOverflow) } @@ -322,6 +336,7 @@ pub fn checked_powi(arg: f64, exp: i32) -> Result { if res.is_finite() { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} ^ {}", arg, exp); err!(UxdError::MathOverflow) } @@ -335,6 +350,7 @@ where if let Some(res) = option { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} as usize", arg); err!(UxdError::MathOverflow) } @@ -348,6 +364,7 @@ where if let Some(res) = option { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} as u64", arg); err!(UxdError::MathOverflow) } @@ -361,6 +378,7 @@ where if let Some(res) = option { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} as u128", arg); err!(UxdError::MathOverflow) } @@ -374,6 +392,7 @@ where if let Some(res) = option { Ok(res) } else { + #[cfg(target_os = "solana")] msg!("Error: Overflow in {} as f64", arg); err!(UxdError::MathOverflow) } diff --git a/programs/uxd/tests/integration_tests/api/program_alloyx/binaries/executable-devnet.so b/programs/uxd/tests/integration_tests/api/program_alloyx/binaries/executable-devnet.so index 4ecaa9fb6..b5b5bdba5 100644 Binary files a/programs/uxd/tests/integration_tests/api/program_alloyx/binaries/executable-devnet.so and b/programs/uxd/tests/integration_tests/api/program_alloyx/binaries/executable-devnet.so differ diff --git a/programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-mainnet-beta.so b/programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-mainnet-beta.so index 8318ce919..0a53d74bc 100644 Binary files a/programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-mainnet-beta.so and b/programs/uxd/tests/integration_tests/api/program_credix/binaries/executable-mainnet-beta.so differ