Skip to content

Commit 9452961

Browse files
committed
fix: decimals retrieval
1 parent cc1bd2d commit 9452961

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pragma-node/src/infra/repositories/entry_repository.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,10 @@ pub async fn get_decimals(
459459
let conn = pool.get().await.map_err(adapt_infra_error)?;
460460

461461
let quote_currency = pair_id.split('/').last().unwrap().to_uppercase();
462+
let base_currency = pair_id.split('/').next().unwrap().to_uppercase();
462463

463464
// Fetch currency in DB
464-
let decimals: BigDecimal = conn
465+
let quote_decimals: BigDecimal = conn
465466
.interact(move |conn| {
466467
currencies::table
467468
.filter(currencies::name.eq(quote_currency))
@@ -471,8 +472,24 @@ pub async fn get_decimals(
471472
.await
472473
.map_err(adapt_infra_error)?
473474
.map_err(adapt_infra_error)?;
475+
let base_decimals: BigDecimal = conn
476+
.interact(move |conn| {
477+
currencies::table
478+
.filter(currencies::name.eq(base_currency))
479+
.select(currencies::decimals)
480+
.first::<BigDecimal>(conn)
481+
})
482+
.await
483+
.map_err(adapt_infra_error)?
484+
.map_err(adapt_infra_error)?;
485+
486+
// Take the minimum of the two
487+
let decimals = std::cmp::min(
488+
quote_decimals.to_u32().unwrap(),
489+
base_decimals.to_u32().unwrap(),
490+
);
474491

475-
Ok(decimals.to_u32().unwrap())
492+
Ok(decimals)
476493
}
477494

478495
#[derive(Debug, Clone, Serialize, Deserialize, Queryable)]

0 commit comments

Comments
 (0)