Skip to content

Unit-test the Bond withdrawal penalty math: getPenaltyRate and computeWithdrawBreakdown #439

Description

@Baskarayelu

📋 Description

src/pages/Bond.tsx computes early-withdrawal slash penalties via getPenaltyRate(status) (0.2 for locked, 0.1 for grace-period, 0 for active) and computeWithdrawBreakdown(bond), which derives penaltyPercent, penaltyAmount, resultingBalance, and a raw penaltyUsdc. This money-affecting logic — surfaced both in the slash-exposure banner and the ConfirmDialog breakdown — currently has no unit tests.

Why this matters: the penalty breakdown is the number a user sees before irreversibly slashing funds. A rounding or rate error here is a financial-trust bug. These pure functions are trivially testable and must be locked down.

🎯 Requirements & Context

Functional requirements

  • Extract getPenaltyRate and computeWithdrawBreakdown (and the formatUsdc helper) into a testable module if they are not already exported — prefer src/lib/bondPenalty.ts and import them back into Bond.tsx.
  • Add a Vitest suite covering every BondStatus branch and the rate→amount→resulting-balance chain.
  • Assert the formatting: penaltyPercent is an integer percent; penaltyAmount/resultingBalance use the formatUsdc thousands-grouped + USDC suffix.
  • Assert the invariant penaltyUsdc + resultingUsdc === bondAmount (within floating-point tolerance) across representative amounts.

Context & constraints

  • Keep behavior identical; this is test-and-extract only, no penalty-policy change.
  • TypeScript strict; the extracted module must be fully typed and TSDoc'd.

🛠️ Suggested Execution

1. Fork & branch

git checkout -b test/bond-penalty-math

2. Implement changes

  • Move the pure helpers to src/lib/bondPenalty.ts, re-import into Bond.tsx, add TSDoc.
  • Cover edge cases: zero-amount bond, fractional USDC amounts, active status yielding zero penalty, very large amounts (no precision loss in the displayed string).

3. Test & commit

npm run test
npm run lint
npm run build
  • Paste the Vitest run summary into the PR.

Example commit message

test(bond): extract and unit-test penalty rate + withdraw breakdown math

Moves getPenaltyRate/computeWithdrawBreakdown to src/lib/bondPenalty.ts and
covers every status branch plus the penalty+resulting==amount invariant.

✅ Acceptance Criteria & Guidelines

Requirement Target
Penalty helpers extracted + TSDoc'd Required
All status branches covered Required
Conservation invariant asserted Required
Vitest coverage of the module ≥ 95%
npm run lint + npm run build + npm run test clean Required
Timeframe 96 hours from assignment

💬 Community & Support

Questions and design discussion — join the Credence contributor community on Discord: https://discord.gg/nmnPBkBge

Please comment to ask to be assigned before you start so we can avoid duplicate work and unblock you fast. 🚀

Metadata

Metadata

Type

Fields

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions