Skip to content

Fix APR math in nextRevenuePoolSettleApr - #2207

Open
latent-9 wants to merge 2 commits into
velocity-exchange:masterfrom
latent-9:fix-insurance-apr
Open

Fix APR math in nextRevenuePoolSettleApr#2207
latent-9 wants to merge 2 commits into
velocity-exchange:masterfrom
latent-9:fix-insurance-apr

Conversation

@latent-9

@latent-9 latent-9 commented Aug 10, 2026

Copy link
Copy Markdown

nextRevenuePoolSettleApr in sdk/src/math/insurance.ts has two bugs:

  1. revenuePoolBN.muln(settlesPerYear).muln(payoutRatio) with payoutRatio = 0.1. BN.muln does not multiply by a fraction; it multiplies each limb and masks, so a fractional argument yields garbage rather than value * 0.1. For example new BN(1_000_000_000).muln(0.1) returns 73156454, not 100000000 (about 27 percent off). Since payoutRatio is the constant 0.1, this is integer divn(10).

  2. amount is declared optional (amount?: BN), but vaultBalance.add(amount) is called unconditionally, so add(undefined) throws when a caller omits it. Defaulting a missing delta to ZERO preserves the intended behavior.

The function is exported from the SDK (export * from './math/insurance'), so both reach downstream consumers.

Summary by CodeRabbit

  • Bug Fixes
    • Improved revenue pool settlement APR calculations for more accurate payout projections.
    • Prevented missing optional amounts from causing invalid APR results.
    • Updated APR calculations to use integer-based payout handling for greater consistency.
    • Improved the reliability of projected revenue and vault balance calculations when no additional amount is provided.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 86ae36da-bcfb-4a77-b232-498271a48502

📥 Commits

Reviewing files that changed from the base of the PR and between ecacdf0 and 1cb9f78.

📒 Files selected for processing (1)
  • sdk/src/math/insurance.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • sdk/src/math/insurance.ts

Walkthrough

The insurance APR calculation now divides projected revenue by an integer payout denominator. The optional amount parameter defaults to zero before balance checks and APR calculation.

Changes

Insurance APR calculation

Layer / File(s) Summary
APR calculation and amount handling
sdk/src/math/insurance.ts
The calculation uses an integer payout denominator. The optional amount parameter defaults to ZERO before balance checks and APR calculation.

Estimated code review effort: 2 (Simple) | ~5 minutes

Poem

A rabbit checks the payout line,
With integer steps, the sums align.
No undefined hops astray,
Zero starts the APR day.
Thump, thump—clean math leads the way!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the primary change to APR calculations in nextRevenuePoolSettleApr.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
sdk/src/math/insurance.ts (1)

20-20: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add regression tests for both APR fixes.

Cover the non-zero payout path and the omitted amount path. Assert that payout is divided by 10 and that amount === undefined uses vaultBalance without throwing. Also cover the zero-total-balance case.

Also applies to: 37-43

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sdk/src/math/insurance.ts` at line 20, Add regression tests for the APR logic
near payoutRatioDenominator and the related amount-handling path: verify
non-zero payouts are divided by 10, omitted amount defaults to vaultBalance
without throwing, and zero total balance is handled correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@sdk/src/math/insurance.ts`:
- Line 20: Add regression tests for the APR logic near payoutRatioDenominator
and the related amount-handling path: verify non-zero payouts are divided by 10,
omitted amount defaults to vaultBalance without throwing, and zero total balance
is handled correctly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e57f5d33-37ed-435c-8586-3f1bc84a88a5

📥 Commits

Reviewing files that changed from the base of the PR and between 13e8e9b and ecacdf0.

📒 Files selected for processing (1)
  • sdk/src/math/insurance.ts

…eApr

The fractional settlesPerYear was passed to BN.muln, which only accepts a
small integer and truncates otherwise, so the projected annual revenue was
wrong for any revenue settle period that does not evenly divide a year.
Multiply then divide with integers to keep the value exact.
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