feat(wrapper): [Math] Rounding error mitigation (#726) - #832
Open
TochukwuJustice wants to merge 2 commits into
Open
feat(wrapper): [Math] Rounding error mitigation (#726)#832TochukwuJustice wants to merge 2 commits into
TochukwuJustice wants to merge 2 commits into
Conversation
… fee vaults - Define VaultState struct storing fee rates, limits, exchange rate, and fee accumulation metrics - Add DataKey::VaultState preserving storage discriminant ordering - Implement set_vault_state and get_vault_state contract entrypoints with strict invariant validation - Add emit_vault_state_set contract event - Add comprehensive unit test suite covering happy paths and error states - Extend TypeScript SDK WrapperClient with VaultState interface, getter, and setter methods
- Add math module with mul_div_down, mul_div_up, and scale_decimals_down fixed-point arithmetic - Round shares down on deposit/wrap to prevent unbacked fractional share inflation - Round underlying tokens down on withdrawal/unwrap to prevent fractional reserve leakage - Add convert_to_shares, convert_to_assets, preview_deposit, and preview_withdraw contract entrypoints - Reject sub-unit deposits and withdrawals that round down to zero with InvalidAmount - Extend TypeScript SDK with conversion and preview methods - Add comprehensive unit tests covering rounding directions and sub-unit edge cases
|
@TochukwuJustice Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
@TochukwuJustice please fix CI and conflicts |
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 #726
Summary of Changes
This PR implements Issue #726: [Math] Rounding error mitigation under the Yield-Bearing Fee Vaults epic:
contracts/wrapper/src/math.rs):mul_div_down(a, b, d)for overflow-safe floor divisionscale_decimals_downfor floor rounding across mismatched decimal precisions.wrap/convert_to_shares/preview_deposit): Shares are strictly rounded down (floor), preventing share inflation attacks and ensuring depositors never receive unbacked fractional shares.unwrap/convert_to_assets/preview_withdraw): Underlying assets are strictly rounded down (floor), preventing fractional drainage of vault reserves.WrapperError::InvalidAmount.convert_to_shares,convert_to_assets,preview_deposit, andpreview_withdrawonWrapperContract.convertToShares(),convertToAssets(),previewDeposit(), andpreviewWithdraw()toWrapperClientin@bc-forge/sdk.Reason for Changes
In yield-bearing vaults, mathematical rounding must always favor the protocol ("no free money") to maintain pool solvency, protect existing shareholders from share dilution, and prevent attackers from siphoning fractional wei across repeated deposit/withdrawal cycles.