Skip to content

feat(farming-pool): surface typed CreditOverflow instead of trapping - #134

Open
Okorie2000-code wants to merge 2 commits into
SmartDropLabs:mainfrom
Okorie2000-code:feat/farming-pool-credit-overflow
Open

feat(farming-pool): surface typed CreditOverflow instead of trapping#134
Okorie2000-code wants to merge 2 commits into
SmartDropLabs:mainfrom
Okorie2000-code:feat/farming-pool-credit-overflow

Conversation

@Okorie2000-code

Copy link
Copy Markdown
Contributor

Closes #62

compute_credits chained three unchecked i128 multiplications with no bound on any input (amount is user-supplied, credit_rate/multiplier are admin-settable with no upper bound, elapsed grows unboundedly). With overflow-checks = true in the release profile, an overflow trapped the whole invocation — permanently bricking checkpoint/get_credits/calculate_credits/unstake/unlock_assets for the affected user with an opaque host error and no recovery path.

Changes

  • Added PoolError::CreditOverflow = 15
  • Replaced unchecked arithmetic with checked_mul/checked_add throughout the accrual path:
  • Propagated Result through checkpoint, checkpoint_position, get_credits, and calculate_credits, and their callers (lock_assets, unlock_assets, stake, unstake, set_boost)
  • An overflowing user now receives a typed, recoverable PoolError::CreditOverflow instead of a permanent host trap; unstake/unlock_assets fail cleanly before any token transfer, preserving the user's stake/position so funds remain recoverable once the admin corrects the rate

Design note

The issue suggested "considering" a degraded mode where unstake/unlock_assets return principal without the overflowing credit computation. I chose strict typed errors instead: predictable behavior, no silent credit loss, and the user's funds stay intact (the failing checkpoint aborts before any transfer). This satisfies the issue's primary requirement — a typed, recoverable error rather than a permanent trap.

Tests

  • Updated existing compute_total_stake / compute_position_credits unit tests for the new Result signatures
  • Added overflow coverage: compute_total_stake (amount*pct and boosted*multiplier), compute_credits (total*rate and *elapsed), compute_position_credits (amount*rate), plus integration tests for calculate_credits, lock_assets checkpoint, get_credits, unstake, and unlock_assets — all asserting PoolError::CreditOverflow, and confirming stake/position are preserved on failed withdrawals

Verification

cargo test --workspace → factory 34, farming-pool 87 (79 prior + 8 new); cargo clippy --workspace --all-targets -- -D warnings clean; cargo fmt --all -- --check clean.

Note: this branch is stacked on fix/farming-pool-shared-position-credits (#133, issue #63 + CI lint fixes). The diff here includes those commits until #133 merges; the #62-specific changes are the three farming-pool files.

@netlify

netlify Bot commented Aug 17, 2026

Copy link
Copy Markdown

Deploy Preview for sdcontracts ready!

Name Link
🔨 Latest commit eaff327
🔍 Latest deploy log https://app.netlify.com/projects/sdcontracts/deploys/6a82c0df65dece00085c6777
😎 Deploy Preview https://deploy-preview-134--sdcontracts.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@prodbycorne

Copy link
Copy Markdown
Contributor

resolve conflicts

Okorie2000-code and others added 2 commits August 17, 2026 07:58
…ition_credits

Extract the amount * credit_rate * elapsed formula into a single
compute_position_credits helper used by both checkpoint_position and
calculate_credits, eliminating the duplicated calculation. Deliberately
kept separate from compute_credits since Positions have no boost or
multiplier semantics. Add unit coverage for the shared accrual path,
adapted to the pool's min_stake_amount gate.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Replace the unchecked i128 multiplication chain in the credit-accrual
path with checked_mul/checked_add and propagate a new typed
PoolError::CreditOverflow (10) instead of trapping the whole invocation
on overflow. Covers compute_total_stake, compute_credits, and the shared
compute_position_credits helper (from SmartDropLabs#63), propagated through
checkpoint, checkpoint_position, get_credits, calculate_credits, and
their callers so an overflowing user receives a recoverable typed error
rather than a permanent host trap. Complements the SmartDropLabs#89 input ceilings as
defense-in-depth for user-supplied amounts.

Adds unit and integration coverage for overflow at each multiplication
step and verifies unstake/unlock_assets preserve the user's stake and
position so funds are recoverable once the admin corrects the rate.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@Okorie2000-code
Okorie2000-code force-pushed the feat/farming-pool-credit-overflow branch from 5d9e900 to eaff327 Compare August 17, 2026 08:05
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.

farming-pool: compute_credits' unchecked i128 multiplication chain traps the whole contract on overflow with no typed error

2 participants