-
Notifications
You must be signed in to change notification settings - Fork 890
feat: Introduce Asset Scale for fractional asset support #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Thanks @Bronek for reviewing the spec., I addressed your comments. |
There was a problem hiding this 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. |
There was a problem hiding this comment.
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` | |
There was a problem hiding this comment.
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 ?)
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.Scaling on Deposit:$\sigma = 10^{\text{AssetScale}}$ ). During a deposit, the asset amount is multiplied by this factor to create a scaled integer representation.
AssetScale
acts as an exponent for a power-of-10 multiplier (AssetScale
of6
, a deposit of 20.3 assets is correctly converted to 20,300,000 shares (20.3 * 10^6
).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.
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
AssetScale
to the vault's core configuration.σ
to convert assets to shares. Subsequent calculations for deposits, redemptions, and withdrawals are updated to operate on these scaled values.