contracts: add deterministic rounding policy for share conversions#661
Open
Amas-01 wants to merge 1 commit into
Open
contracts: add deterministic rounding policy for share conversions#661Amas-01 wants to merge 1 commit into
Amas-01 wants to merge 1 commit into
Conversation
|
@Amas-01 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! 🚀 |
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 #563
PR:Deterministic Rounding Policy for Mint/Burn Share Conversions
Overview
This document summarizes the implementation of Issue #563, which adds a deterministic rounding policy for all mint and burn share conversions in the YieldVault-RWA contract.
Changes Made
1. New Math Module (
src/math.rs)Created a centralized math module that implements the deterministic rounding policy:
Functions:
ssets) -> i128`
shares_to_assets(shares, total_shares, total_assets) -> i128Rounding Policy:
Test Coverage:
2. Updated Contract Functions (
src/lib.rs)Modified Functions:
calculate_shares()- Now delegates tomath::assets_to_shares()calculate_assets()- Now delegates tomath::shares_to_assets()deposit()- Uses centralized conversion, added rounding documentationwithdraw()- Uses centralized conversion, added rounding documentationexecute_withdrawal()- Uses centralized conversion, added rounding documentationBenefits:
3. Updated Fuzz Tests (
src/fuzz_math.rs)Updated helper functions to reference the centralized math module:
shares_for()- Updated documentationassets_for()- Updated documentationAll existing property-based tests continue to pass (10,000+ iterations).
4. Documentation
Created comprehensive documentation:
ROUNDING_POLICY.md- Complete policy documentation including:Rounding Policy Summary
Policy: Always round DOWN (truncate) for both minting and burning.
Rationale:
Formulas:
Test Results
All tests pass successfully:
Test Breakdown:
math.rsKey Test Coverage:
Safety Guarantees
The implementation ensures:
shares_minted ≤ exact_fractional_sharesassets_returned ≤ exact_fractional_assetstotal_assets ≥ sum(all_user_redemption_values)withdraw(deposit(x)) ≤ xfor all xUser Impact
Rounding Loss:
Protection Against Zero Shares:
VaultError::InvalidAmountTiny Withdrawals:
Compliance with Standards
The implementation aligns with ERC-4626 recommendations:
Our implementation:
Files Changed
New Files:
contracts/vault/src/math.rs- Centralized math modulecontracts/vault/ROUNDING_POLICY.md- Policy documentationcontracts/vault/ISSUE_563_IMPLEMENTATION.md- This fileModified Files:
contracts/vault/src/lib.rs- Updated conversion functionscontracts/vault/src/fuzz_math.rs- Updated helper documentationVerification Steps
To verify the implementation:
Integration Checklist
For integrators and frontend developers:
VaultError::InvalidAmountfor zero-share depositsROUNDING_POLICY.mdFuture Considerations
Potential Enhancements:
Not Recommended: