Skip to content

fix(provider-rewards): delta formula parity + remove misleading estimates - #31

Merged
nimrod-teich merged 6 commits into
mainfrom
fix/provider-rewards-delta-parity
Apr 16, 2026
Merged

nimrod-teich merged 6 commits into
mainfrom
fix/provider-rewards-delta-parity

Conversation

@nimrod-teich

@nimrod-teich nimrod-teich commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • QosCus fix: use MV's qosCu (QoS-filtered CU) instead of total CU — delta only counts CU from rows with valid QoS
  • Averaging fix: switch from relay-weighted (qosSyncW/qosWeight) to unweighted row-level averages (qosSyncSum/qosCount), matching delta's AVG() semantics
  • End-date boundary: lessThanOrEqualTo → lessThan so the to date is exclusive, matching delta's datetime < to
  • Missing response fields: add qosCus, avgLatency, avgAvailability, avgSync, avgLatencyExc, avgAvailabilityExc, avgSyncExc — cross-chain aggregation uses raw sums/counts for exact averaging
  • Remove misleading estimates: drop estimatedRewardsLava/Usd, lavaUsdPrice, providerPoolLava — pool balance snapshot ≠ actual distribution; consumers should use /provider-estimated-rewards for real chain-sourced values

Depends on: Magma-Devs/lava-indexer#7 (new MV columns must be deployed first)

Test plan

  • Unit tests pass (11/11 updated for new field names and removed estimate fields)
  • Deploy indexer MV changes first, then deploy info
  • Call /provider-rewards?from=2026-03-17&to=2026-04-17&specs[]=LAVA and verify response includes new QoS fields
  • Compare adjustedRewards values against delta's WebSocket output for the same date range + spec
  • Verify /provider-estimated-rewards still serves USD estimates independently

Delta's adjustedRewards formula sums CU only from relay_payments rows
where all 3 QoS metrics are non-null and > 0. The endpoint was using
total CU (all rows) whenever any QoS data existed, inflating QosCus for
providers with mixed QoS/non-QoS traffic. Use the new qos_cu MV column
that applies delta's exact AND filter.
Delta computes AVG(qos_latency) etc. as unweighted row-level averages
where each relay_payments record counts equally regardless of relay count.
The endpoint was using relay-weighted averages from the MV (qosSyncW /
qosWeight), which over-represents high-relay records. Switch to the new
qos{Sync,Avail,Latency}Sum / qosCount columns that reproduce delta's
exact AVG() + AND-filter semantics.
Delta filters blocks with datetime >= from AND datetime < to (exclusive
end). The endpoint was using lessThanOrEqualTo which includes the full
end date, adding an extra day of data. Switch to lessThan for parity.
Add qosCus, avgLatency, avgAvailability, avgSync and their excellence
variants to the response, matching delta's CSV columns. When results span
multiple chains, QoS averages are correctly combined using raw sums and
counts (not pre-averaged values) so cross-chain aggregation is exact.
The estimatedRewardsUsd/Lava fields used share × poolBalance × price,
but pool balance is a point-in-time snapshot that doesn't represent the
actual monthly distribution. After a payout the pools are near-empty;
before one they're full — the number was arbitrary. The existing
/provider-estimated-rewards endpoint already calls the chain's
estimated_provider_rewards RPC for accurate per-provider values.

Remove: estimatedRewardsLava, estimatedRewardsUsd, lavaUsdPrice,
providerPoolLava. Keep: rewardShare (pure QoS metric). Drop the
CoinGecko price and reward pool fetches that only served these fields.
- Strict calendar-date validation: reject 2025-02-30 / 2025-04-31 etc.
  via Date.UTC round-trip instead of naive `new Date()` parsing.
- Conditional cacheTTL: 86400s for immutable past windows (to < today),
  300s for live windows. Cache plugin now honors request.cacheTTL as a
  per-request override of route config.
- Clarify BigInt rule in CLAUDE.md and api-conventions.md — the
  weighted-QoS rule has an explicit exception for /provider-rewards
  (unweighted row-level averaging for delta parity), and an inline
  comment at the aggregation loop explains the choice plus why CU
  stays as Number (CU is not ulava; totals stay within safe-int).
- Add tests for the two new calendar-invalid cases.
@nimrod-teich
nimrod-teich force-pushed the fix/provider-rewards-delta-parity branch from e481654 to d16861f Compare April 16, 2026 17:36
@nimrod-teich
nimrod-teich merged commit f837f71 into main Apr 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant