Skip to content

Conversation

Tapanito
Copy link
Collaborator

@Tapanito Tapanito commented Jul 22, 2025

Summary

This pull request introduces a crucial new vault parameter, AssetScale, to enable support for fractional assets. Previously, the vault's accounting logic treated asset quantities as integers, leading to the truncation of any fractional part of a deposit and causing a loss of funds for the user.

This change refactors the core exchange algorithms to use a fixed-point arithmetic approach, where fractional assets are converted to scaled, whole-number "share" equivalents upon deposit. This ensures all internal calculations are precise and prevents value loss from rounding or truncation errors.

The Problem

The previous integer-based logic could not handle assets that are commonly used in fractions (e.g., ETH, WBTC). A deposit of 20.3 WETH would be incorrectly credited as only 20 shares, resulting in an immediate and silent loss of 0.3 WETH for the depositor. This limitation severely restricted the types of assets the vault could support and eroded user trust.

The Solution

This PR implements the AssetScale parameter, which is defined once per vault.

  1. Scaling on Deposit: AssetScale acts as an exponent for a power-of-10 multiplier ($\sigma = 10^{\text{AssetScale}}$). During a deposit, the asset amount is multiplied by this factor to create a scaled integer representation.

    • Example: With an AssetScale of 6, a deposit of 20.3 assets is correctly converted to 20,300,000 shares (20.3 * 10^6).
  2. Precision in all Calculations: All subsequent vault operations (deposits, redemptions, and withdrawals) now use these scaled share values for their calculations. This maintains precision throughout the vault's lifecycle.

  3. Accurate Payouts: The redeem and withdraw functions correctly interpret these scaled share values, converting them back into precise asset amounts for the user.

Key Changes

  • New Vault Parameter: Added AssetScale to the vault's core configuration.
  • Updated Deposit Logic: The initial deposit formula now uses the scaling factor σ to convert assets to shares. Subsequent calculations for deposits, redemptions, and withdrawals are updated to operate on these scaled values.
  • Rounding Consistency: The rounding rules established in the exchange algorithms are preserved:
    • Deposits: Share calculations are rounded down.
    • Redeems/Withdrawals: Calculations involving asset payouts or intermediate share amounts are rounded to the nearest number.

@Tapanito
Copy link
Collaborator Author

Thanks @Bronek for reviewing the spec., I addressed your comments.

@Tapanito Tapanito requested a review from Bronek August 11, 2025 10:48
Copy link

@Bronek Bronek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some fixes.

##### 2.1.6.1.3 `MPT`

When a vault holds **`XRP`**, the `Scale` is fixed at **0**. This aligns with XRP's native structure, where one share represents one "drop" (the smallest unit of XRP), and one XRP equals 1,000,000 drops. Therefore, a deposit of 10 XRP will result in the issuance of 10,000,000 shares ($10 \times 10^6$).
When a vault holds `MPT`, its `Scale` is fixed at **0**. This creates a 1-to-1 relationship between deposited MPT units and the shares issued (for example, depositing 10 MPTs to an empty Vault issues 10 shares). The value of a single MPT is determined at the issuer's discretion. If an MPT is set to represent a large value, the vault owner must be cautious. Because only whole MPT units can be deposited, any value that is not a multiple of a single MPT's value may be lost due to rounding during a transaction.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the vault owner and depositors should be cautious. Since only whole MPT units are used in calculations, any value that is ..."

| `MaximumAmount` | No limit to the number of shares that can be issued. | `0xFFFFFFFFFFFFFFFF` |
| `TransferFee` | The fee paid to transfer the shares. | 0 |
| `MPTokenMetadata` | Arbitrary metadata about the share MPT, in hex format. | - |
| `AssetScale` | Metadata representing the number of orders of magnitude between a standard unit and an MPT unit | `Vault.Scale` |
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would also comment "only if the vault asset is an IOU", somewhere (probably not in the table ?)

@mvadari
Copy link
Collaborator

mvadari commented Oct 3, 2025

@Bronek @Tapanito should this be merged?

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.

3 participants