Skip to content

feat: migrate BalanceLedger.amount from Float to Decimal with running totals (#261) - #325

Open
DevSolex wants to merge 3 commits into
ChainForgee:mainfrom
DevSolex:feat/261-balance-ledger-decimal-running-totals
Open

feat: migrate BalanceLedger.amount from Float to Decimal with running totals (#261)#325
DevSolex wants to merge 3 commits into
ChainForgee:mainfrom
DevSolex:feat/261-balance-ledger-decimal-running-totals

Conversation

@DevSolex

Copy link
Copy Markdown
Contributor

Summary

Implements issue #261.

Migrates BalanceLedger.amount from Float to Decimal(20,8) and adds a denormalised BalanceLedgerSnapshot table populated by a scheduled hourly job.

Changes

Schema (prisma/schema.prisma)

  • BalanceLedger.amount: FloatDecimal @db.Decimal(20,8)
  • New BalanceLedgerSnapshot model with totalLocked Decimal(20,8) and snapshotAt (indexed)
  • Campaign relation extended with balanceLedgerSnapshots

Migration (20260715000000_balance_ledger_decimal_and_snapshot)

  • Recreates BalanceLedger via table-copy to rename the column type (SQLite workaround), copying all existing data
  • Creates the BalanceLedgerSnapshot table with all required indexes

Job (src/jobs/balance-ledger-snapshot.job.ts)

  • @Cron(EVERY_HOUR) — groups BalanceLedger rows by campaignId over the last 24 h window and writes one BalanceLedgerSnapshot row per active campaign
  • run() is public for direct invocation from tests and manual triggers

Supporting changes

  • src/jobs/jobs.module.ts — registers BalanceLedgerSnapshotJob and imports PrismaModule
  • src/common/budget/budget.service.tstoNum() handles both Decimal and plain number so existing mocks remain compatible
  • src/campaigns/campaigns.service.tsbalanceLedger.reduce calls .toNumber() on Decimal amounts

Tests

File Tests Result
src/jobs/balance-ledger-snapshot.job.spec.ts 3 unit tests (empty window, two-campaign sum, null fallback) ✅ pass
test/balance-ledger-snapshot.e2e-spec.ts 3 acceptance tests ✅ pass

The e2e test directly asserts the acceptance criterion from the issue:

snapshot.totalLocked === Σ BalanceLedger.amount for the last 24 h, and that entries older than 24 h are excluded.

Closes

Closes #261

Copy link
Copy Markdown
Contributor

Hi 👋 Appreciate this PR! The CI is flagging a failing check — could you take a peek at the failed job and push a fix? Happy to help if you need a hand 🙏

…napshot (ChainForgee#261)

- Change BalanceLedger.amount from Float to Decimal(20,8) in schema
- Add BalanceLedgerSnapshot model with totalLocked Decimal and snapshotAt
- Migration 20260715000000_balance_ledger_decimal_and_snapshot recreates
  the BalanceLedger table with DECIMAL column and creates the snapshot table
- Add BalanceLedgerSnapshotJob (hourly cron) that computes Σ amount for
  the last 24 h per campaign and writes a BalanceLedgerSnapshot row
- Register job in JobsModule
- Fix BudgetService._sum.amount handling for Decimal return type
- Fix CampaignsService balanceLedger.reduce to call toNumber() on Decimal
- Add unit spec (balance-ledger-snapshot.job.spec.ts) – 3 tests pass
- Add e2e spec (balance-ledger-snapshot.e2e-spec.ts) asserting running
  total matches Σ BalanceLedger.amount for the last 24 h

Closes ChainForgee#261
@DevSolex
DevSolex force-pushed the feat/261-balance-ledger-decimal-running-totals branch from d7a83e3 to 8fd7f66 Compare July 24, 2026 17:21
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.

Migrate BalanceLedger.amount from Float to Decimal with running totals

2 participants