Skip to content

fix(credits): read stream amounts from Superfluid periods instead of reconstructing them - #30

Closed
blueogin wants to merge 2 commits into
mainfrom
refactor/stream-accounting
Closed

blueogin wants to merge 2 commits into
mainfrom
refactor/stream-accounting

Conversation

@blueogin

Copy link
Copy Markdown
Collaborator

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

About # (link your issue here)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • PR title matches follow: (Feature|Bug|Chore) Task Name
  • My code follows the style guidelines of this project
  • I have followed all the instructions described in the initial task (check Definitions of Done)
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have added reference to a related issue in the repository
  • I have added a detailed description of the changes proposed in the pull request. I am as descriptive as possible, assisting reviewers as much as possible.
  • I have added screenshots related to my pull request (for frontend tasks)
  • I have pasted a gif showing the feature.
  • @mentions of the person or team responsible for reviewing proposed changes

- Introduced new module `superfluid.ts` to handle fetching and processing Superfluid streams.
- Implemented `fetchSuperfluidIncomingStreams` and `fetchSuperfluidStreamsForAccount` functions for retrieving stream data.
- Updated `worker.ts` to integrate Superfluid streams into the crediting process, replacing the previous inline implementation.
- Refactored stream credit calculation logic into `stream-credits.ts` for better modularity and maintainability.
- Adjusted tests in `worker.test.ts` to accommodate changes in the worker module and ensure proper functionality.
Copilot AI balanced review requested due to automatic review settings October 12, 2026 06:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Period truncation can lose credits, and account-level aggregation can fund historical periods to the wrong buyer.

4 open findings
What changed in this PR

Updates stream-credit calculation to use exact Superfluid periods and extracts Worker helpers into focused modules.

Changes:

  • Calculates credits from historical constant-rate periods.
  • Consolidates account-level stream processing.
  • Extracts funding, HTTP, analytics-cron, and Superfluid logic.
File Description
backend/​src/​worker.ts Integrates period-based crediting and extracted helpers.
backend/​src/​superfluid.ts Fetches streams and their periods.
backend/​src/​stream-credits.ts Calculates and schedules stream credits.
backend/​src/​http.ts Houses HTTP utilities.
backend/​src/​funding.ts Houses funding lifecycle logic.
backend/​src/​analytics-cron.ts Houses analytics cron utilities.
backend/​test/​worker.test.ts Adds period-based regression coverage.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +131 to +134
current.periods.push(...stream.periods);
// Rows arrive newest-first, so the first buyer seen is the most recently declared one.
current.buyerAddress ??= stream.buyerAddress;
byAccount.set(account, current);
Comment thread backend/src/superfluid.ts
Comment on lines +146 to +150
streamPeriods(orderBy: startedAtTimestamp, orderDirection: desc, first: ${PERIOD_PAGE_SIZE}) {
startedAtTimestamp
stoppedAtTimestamp
flowRate
}
Comment thread backend/src/worker.ts
Comment on lines +355 to +359
const accountStreams = await fetchSuperfluidStreamsForAccount(event.account, cfg, true);
periodsByAccount.set(
streamCacheKey,
accountStreams.flatMap((accountStream) => accountStream.periods)
);
Comment thread backend/src/worker.ts
Comment on lines +471 to +476
const buyerForOperatorCheck = streams[0]?.buyerAddress || account;
const hasOperatorConsent = antseedFundingVault.enabled ? (await antseedFundingVault.isBuyerOperator(buyerForOperatorCheck)).isOperator : true;
const isValidForBonus = !!rootAccount && hasOperatorConsent;
const isVerified = isValidForBonus;
const { gdAmountWei } = streamCreditAmount({ now, lastStreamCreditAt: profile.lastStreamCreditAt, stream, previousFlowRateWeiPerSecond });
const stream = streams[0];
const gdAmountWei = streamCreditFromPeriods(now, profile.lastStreamCreditAt, accountPeriods);
@sirpy sirpy closed this Oct 14, 2026
@sirpy
sirpy deleted the refactor/stream-accounting branch October 14, 2026 12:52
@sirpy

sirpy commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

dup of #29

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.

3 participants