Skip to content

Commit 70985b3

Browse files
committed
Improve numeric conversions in Balancer V2 fixed point math
- Replace lossy unwrap_or fallbacks with direct unwrap() in Bfp Debug impl - Fix to_f64_lossy() to use string parsing for accurate large value conversion - Update test helper functions to use string-based U256/f64 conversions instead of lossy casts - Simplify into_legacy() calls by using method syntax instead of fully qualified paths
1 parent 270db68 commit 70985b3

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

crates/shared/src/sources/balancer_v2/swap/fixed_point.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,22 @@ impl FromStr for Bfp {
8989
impl Debug for Bfp {
9090
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
9191
let remainder = self.0 % *ONE_18;
92-
let low: u128 = remainder.try_into().unwrap_or(0);
92+
let low: u128 = remainder.try_into().unwrap();
9393
write!(formatter, "{}.{:0>18}", self.0 / *ONE_18, low)
9494
}
9595
}
9696

9797
impl Bfp {
9898
#[cfg(test)]
9999
pub fn to_f64_lossy(self) -> f64 {
100-
// Convert U256 to f64 by taking the high bits
101-
let val: u128 = self.as_uint256().try_into().unwrap_or(u128::MAX);
102-
(val as f64) / 1e18
100+
// NOTE: This conversion is lossy and may not be accurate for large values.
101+
// It is only intended for use in tests.
102+
let val: f64 = self
103+
.as_uint256()
104+
.to_string()
105+
.parse()
106+
.unwrap_or(f64::INFINITY);
107+
val / 1e18
103108
}
104109

105110
pub fn as_uint256(self) -> U256 {

crates/shared/src/sources/balancer_v2/swap/stable_math.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,11 @@ mod tests {
229229
};
230230

231231
fn u256_from_f64_lossy(val: f64) -> U256 {
232-
U256::from(val as u64)
232+
U256::from_str_radix(&val.round().to_string(), 10).unwrap_or(U256::MAX)
233233
}
234234

235235
fn u256_to_f64_lossy(val: U256) -> f64 {
236-
let v: u128 = val.try_into().unwrap_or(u128::MAX);
237-
v as f64
236+
val.to_string().parse().unwrap_or(f64::INFINITY)
238237
}
239238

240239
// interpreted from

crates/solvers/src/boundary/liquidity/stable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use shared::sources::balancer_v2::pool_fetching::StablePool as Pool;
22
use {
33
crate::domain::{eth, liquidity},
44
alloy::primitives::{Address, B256, U256},
5+
ethrpc::alloy::conversions::IntoLegacy,
56
shared::sources::balancer_v2::{
67
pool_fetching::{AmplificationParameter, CommonPoolState, TokenState},
78
swap::fixed_point::Bfp,
@@ -39,7 +40,7 @@ pub fn to_boundary_pool(address: Address, pool: &liquidity::stable::Pool) -> Opt
3940

4041
Some(Pool {
4142
common: CommonPoolState {
42-
id: ethrpc::alloy::conversions::IntoLegacy::into_legacy(id),
43+
id: id.into_legacy(),
4344
address,
4445
swap_fee,
4546
paused: false,

crates/solvers/src/boundary/liquidity/weighted_product.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use shared::sources::balancer_v2::pool_fetching::WeightedPool as Pool;
22
use {
33
crate::domain::{eth, liquidity},
44
alloy::primitives::{Address, B256, U256},
5+
ethrpc::alloy::conversions::IntoLegacy,
56
shared::sources::balancer_v2::{
67
pool_fetching::{CommonPoolState, TokenState, WeightedPoolVersion, WeightedTokenState},
78
swap::fixed_point::Bfp,
@@ -40,7 +41,7 @@ pub fn to_boundary_pool(
4041

4142
Some(Pool {
4243
common: CommonPoolState {
43-
id: ethrpc::alloy::conversions::IntoLegacy::into_legacy(id),
44+
id: id.into_legacy(),
4445
address,
4546
swap_fee,
4647
paused: false,

0 commit comments

Comments
 (0)