Use completed UTC buckets for position analytics#610
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Review limit reached
Next review available in: 5 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (23)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
4bbedee to
32855b5
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a daily analytics calculation mechanism for user positions, fetching daily flows and market snapshots via a new Envio GraphQL query to calculate earnings and APY without relying on heavy transaction history. The UI and hooks have been updated to support completed UTC days analytics periods. The review feedback highlights a potential bug in the daily analytics calculation where unaligned timestamps could break bucket lookups, and identifies type-safety issues where numeric chain IDs are implicitly converted to string keys via Object.fromEntries.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for (let bucketStart = startTimestamp; bucketStart < endTimestamp; bucketStart += SECONDS_PER_DAY) { | ||
| const bucketEnd = bucketStart + SECONDS_PER_DAY; | ||
| const flow = flowByBucket.get(bucketStart); | ||
| const exposure = getCompletedFlowExposure(flow, currentShares, bucketEnd); | ||
| const endMarketSnapshot = marketSnapshotByBucket.get(bucketStart) ?? latestMarketSnapshot; | ||
| const startMarketSnapshot = latestMarketSnapshot ?? endMarketSnapshot; | ||
|
|
||
| if (exposure.weightedSharesSeconds > 0n) { | ||
| if (startMarketSnapshot && endMarketSnapshot) { | ||
| const startWeightedAssets = toWeightedAssetsSeconds(exposure.weightedSharesSeconds, startMarketSnapshot); | ||
| const endWeightedAssets = toWeightedAssetsSeconds(exposure.weightedSharesSeconds, endMarketSnapshot); | ||
| weightedSuppliedAssets += (startWeightedAssets + endWeightedAssets) / 2n; | ||
| } else { | ||
| weightedSuppliedAssets += getFallbackWeightedAssetsSeconds({ | ||
| weightedSharesSeconds: exposure.weightedSharesSeconds, | ||
| startingBalance, | ||
| startingShares, | ||
| endingBalance, | ||
| endingShares, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| effectiveTime += exposure.activeSeconds; | ||
| currentShares = exposure.closingShares; | ||
| latestMarketSnapshot = endMarketSnapshot; | ||
| } |
There was a problem hiding this comment.
The daily analytics calculation loops through days using bucketStart += SECONDS_PER_DAY starting from startTimestamp. If startTimestamp or endTimestamp are not perfectly aligned to UTC midnight (e.g., due to block timestamp estimation or clock drift), bucketStart will not match the keys in flowByBucket and marketSnapshotByBucket (which are strictly aligned to UTC midnight multiples of 86400). This will cause the lookups to return undefined and break the calculations.
Aligning startTimestamp and endTimestamp to UTC midnight boundaries inside the function makes the daily bucket lookups robust against unaligned inputs.
const alignedStart = Math.floor(startTimestamp / SECONDS_PER_DAY) * SECONDS_PER_DAY;
const alignedEnd = Math.floor(endTimestamp / SECONDS_PER_DAY) * SECONDS_PER_DAY;
for (let bucketStart = alignedStart; bucketStart < alignedEnd; bucketStart += SECONDS_PER_DAY) {
const bucketEnd = bucketStart + SECONDS_PER_DAY;
const flow = flowByBucket.get(bucketStart);
const exposure = getCompletedFlowExposure(flow, currentShares, bucketEnd);
const endMarketSnapshot = marketSnapshotByBucket.get(bucketStart) ?? latestMarketSnapshot;
const startMarketSnapshot = latestMarketSnapshot ?? endMarketSnapshot;
if (exposure.weightedSharesSeconds > 0n) {
if (startMarketSnapshot && endMarketSnapshot) {
const startWeightedAssets = toWeightedAssetsSeconds(exposure.weightedSharesSeconds, startMarketSnapshot);
const endWeightedAssets = toWeightedAssetsSeconds(exposure.weightedSharesSeconds, endMarketSnapshot);
weightedSuppliedAssets += (startWeightedAssets + endWeightedAssets) / 2n;
} else {
weightedSuppliedAssets += getFallbackWeightedAssetsSeconds({
weightedSharesSeconds: exposure.weightedSharesSeconds,
startingBalance,
startingShares,
endingBalance,
endingShares,
});
}
}
effectiveTime += exposure.activeSeconds;
currentShares = exposure.closingShares;
latestMarketSnapshot = endMarketSnapshot;
}| const endTimestampsByChain = useMemo( | ||
| () => Object.fromEntries(Object.entries(earningsRangesByChain).map(([chainId, range]) => [chainId, range.endTimestamp])), | ||
| [earningsRangesByChain], | ||
| ); |
There was a problem hiding this comment.
Using Object.fromEntries on Object.entries converts the numeric chain IDs into string keys in the resulting object. This can lead to implicit type conversions or TypeScript type-checking issues when passing endTimestampsByChain to functions expecting Record<number, number>. Constructing the record with explicit numeric keys is safer and cleaner.
const endTimestampsByChain = useMemo(() => {
const result: Record<number, number> = {};
for (const [chainId, range] of Object.entries(earningsRangesByChain)) {
result[Number(chainId)] = range.endTimestamp;
}
return result;
}, [earningsRangesByChain]);
| const endTimestampsByChain = useMemo( | ||
| () => Object.fromEntries(Object.entries(earningsRangesByChain).map(([rangeChainId, range]) => [rangeChainId, range.endTimestamp])), | ||
| [earningsRangesByChain], | ||
| ); |
There was a problem hiding this comment.
Using Object.fromEntries on Object.entries converts the numeric chain IDs into string keys in the resulting object. This can lead to implicit type conversions or TypeScript type-checking issues when passing endTimestampsByChain to functions expecting Record<number, number>. Constructing the record with explicit numeric keys is safer and cleaner.
const endTimestampsByChain = useMemo(() => {
const result: Record<number, number> = {};
for (const [chainId, range] of Object.entries(earningsRangesByChain)) {
result[Number(chainId)] = range.endTimestamp;
}
return result;
}, [earningsRangesByChain]);
32855b5 to
f9bfef0
Compare
f9bfef0 to
1241b0d
Compare
Summary
PositionDailyFlowpaginator to fetch sparse user flows and market-day states together, instead of keeping a second analytics pipeline.Cleanup
Verification
npx ultracite fixnpx ultracite checkpnpm checkpnpm build0xfe6509875528e7ea210127fad61800d1fd0d77bd, Optimism): 303 flow rows + 863 market-day rows, one analytics request, ~381 KB