From 25ed74bbe04041a4f5d3534b7301e344aba9d99d Mon Sep 17 00:00:00 2001 From: Martin Beckmann Date: Wed, 10 Apr 2024 20:17:12 +0200 Subject: [PATCH] Use BigInt to compute unit (#2605) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Follow up to https://github.com/cowprotocol/services/pull/2603. 10**24 doesn't fit into a `u64` so we were still dividing by the wrong base. # Changes compute base using big int which does not result in an over flow see [here](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6321e0b405f0e6ff0f469b5a8ecedbc1). ## How to test tested using rust playground ☝️ --- crates/shared/src/price_estimation/native/oneinch.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/shared/src/price_estimation/native/oneinch.rs b/crates/shared/src/price_estimation/native/oneinch.rs index 871cdec7a1..2314ecf2ab 100644 --- a/crates/shared/src/price_estimation/native/oneinch.rs +++ b/crates/shared/src/price_estimation/native/oneinch.rs @@ -140,7 +140,8 @@ async fn get_current_prices( tracing::debug!(?token, "could not fetch decimals; discarding spot price"); return None; }; - let unit = num::BigRational::from_integer(10u64.pow(decimals.into()).into()); + let unit = + num::BigRational::from_integer(num::BigInt::from(10u64).pow(decimals.into())); let normalized_price = u256_to_big_rational(&price) / unit; Some((token, normalized_price.to_f64()?)) })