Skip to content

Commit 0b413ac

Browse files
authored
pindexer: insights: correct calculation of total value shielded (#4932)
## Describe your changes This corrects the calculation of the total value shielded. Previously, were were adding the absolute value of outbound transfers, as well as refunds to the total. Instead, we just need to consider deposits. To test, we should look at current mainnet data, and make sure that the total for USDC is changing appropriately. ## Issue ticket number and link Closes #4925 ## Checklist before requesting a review - [x] I have added guiding text to explain how a reviewer should test these changes. - [x] If this code contains consensus-breaking changes, I have added the "consensus-breaking" label. Otherwise, I declare my belief that there are not consensus-breaking changes, for the following reason: > REPLACE THIS TEXT WITH RATIONALE (CAN BE BRIEF)
1 parent 744e253 commit 0b413ac

File tree

1 file changed

+21
-4
lines changed
  • crates/bin/pindexer/src/insights

1 file changed

+21
-4
lines changed

crates/bin/pindexer/src/insights/mod.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ async fn asset_flow(
151151
asset_id: asset::Id,
152152
height: u64,
153153
flow: i128,
154+
refund: bool,
154155
depositor_existed: DepositorExisted,
155156
) -> anyhow::Result<()> {
156157
let asset_pool: Option<(String, String, i32)> = sqlx::query_as("SELECT total_value, current_value, unique_depositors FROM insights_shielded_pool WHERE asset_id = $1 ORDER BY height DESC LIMIT 1").bind(asset_id.to_bytes()).fetch_optional(dbtx.as_mut()).await?;
@@ -164,7 +165,7 @@ async fn asset_flow(
164165
})
165166
.transpose()?
166167
.unwrap_or((0i128, 0i128, 0i32));
167-
asset_pool.0 += flow.abs();
168+
asset_pool.0 += if refund { 0 } else { flow.max(0) };
168169
asset_pool.1 += flow;
169170
asset_pool.2 += match depositor_existed {
170171
DepositorExisted::Yes => 0,
@@ -431,19 +432,35 @@ impl Component {
431432
if e.value.asset_id != *STAKING_TOKEN_ASSET_ID {
432433
let existed = register_depositor(dbtx, e.value.asset_id, &e.sender).await?;
433434
let flow = i128::try_from(e.value.amount.value())?;
434-
asset_flow(dbtx, e.value.asset_id, height, flow, existed).await?;
435+
asset_flow(dbtx, e.value.asset_id, height, flow, false, existed).await?;
435436
}
436437
} else if let Ok(e) = EventOutboundFungibleTokenTransfer::try_from_event(&event.event) {
437438
if e.value.asset_id != *STAKING_TOKEN_ASSET_ID {
438439
let flow = i128::try_from(e.value.amount.value())?;
439440
// For outbound transfers, never increment unique count
440-
asset_flow(dbtx, e.value.asset_id, height, -flow, DepositorExisted::No).await?;
441+
asset_flow(
442+
dbtx,
443+
e.value.asset_id,
444+
height,
445+
-flow,
446+
false,
447+
DepositorExisted::No,
448+
)
449+
.await?;
441450
}
442451
} else if let Ok(e) = EventOutboundFungibleTokenRefund::try_from_event(&event.event) {
443452
if e.value.asset_id != *STAKING_TOKEN_ASSET_ID {
444453
let flow = i128::try_from(e.value.amount.value())?;
445454
// For outbound transfers, never increment unique count.
446-
asset_flow(dbtx, e.value.asset_id, height, flow, DepositorExisted::No).await?;
455+
asset_flow(
456+
dbtx,
457+
e.value.asset_id,
458+
height,
459+
flow,
460+
true,
461+
DepositorExisted::No,
462+
)
463+
.await?;
447464
}
448465
} else if let Ok(e) = EventCandlestickData::try_from_event(&event.event) {
449466
if let Some(pn) = self.price_numeraire {

0 commit comments

Comments
 (0)