feat(farming-pool): surface typed CreditOverflow instead of trapping - #134
Open
Okorie2000-code wants to merge 2 commits into
Open
feat(farming-pool): surface typed CreditOverflow instead of trapping#134Okorie2000-code wants to merge 2 commits into
Okorie2000-code wants to merge 2 commits into
Conversation
✅ Deploy Preview for sdcontracts ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Contributor
|
resolve conflicts |
…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
force-pushed
the
feat/farming-pool-credit-overflow
branch
from
August 17, 2026 08:05
5d9e900 to
eaff327
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #62
compute_creditschained three uncheckedi128multiplications with no bound on any input (amountis user-supplied,credit_rate/multiplierare admin-settable with no upper bound,elapsedgrows unboundedly). Withoverflow-checks = truein the release profile, an overflow trapped the whole invocation — permanently brickingcheckpoint/get_credits/calculate_credits/unstake/unlock_assetsfor the affected user with an opaque host error and no recovery path.Changes
PoolError::CreditOverflow = 15checked_mul/checked_addthroughout the accrual path:compute_total_stake→Result<i128, PoolError>compute_credits→Result<i128, PoolError>compute_position_credits(the shared helper from farming-pool: checkpoint_position and calculate_credits duplicate the credit-accrual formula instead of sharing one implementation #63) →Result<i128, PoolError>Resultthroughcheckpoint,checkpoint_position,get_credits, andcalculate_credits, and their callers (lock_assets,unlock_assets,stake,unstake,set_boost)PoolError::CreditOverflowinstead of a permanent host trap;unstake/unlock_assetsfail cleanly before any token transfer, preserving the user's stake/position so funds remain recoverable once the admin corrects the rateDesign note
The issue suggested "considering" a degraded mode where
unstake/unlock_assetsreturn 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
compute_total_stake/compute_position_creditsunit tests for the newResultsignaturescompute_total_stake(amount*pctandboosted*multiplier),compute_credits(total*rateand*elapsed),compute_position_credits(amount*rate), plus integration tests forcalculate_credits,lock_assetscheckpoint,get_credits,unstake, andunlock_assets— all assertingPoolError::CreditOverflow, and confirming stake/position are preserved on failed withdrawalsVerification
cargo test --workspace→ factory 34, farming-pool 87 (79 prior + 8 new);cargo clippy --workspace --all-targets -- -D warningsclean;cargo fmt --all -- --checkclean.