-
Notifications
You must be signed in to change notification settings - Fork 77
feat: fix128 bit math docs #1514
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: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for GitHub.
|
let finalAmount = output / someRatio // Even more precision lost | ||
``` | ||
|
||
After 3-4 sequential operations, the cumulative rounding error can be significant, especially when dealing with large amounts. |
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.
Here though give a specific example. "After 100 operations of 1 million dollars USDF, the protocol is losing XXX,XXX in revenue from this lack of precision".
|
||
These constants ensure consistent scaling across all operations. | ||
|
||
## Rounding Modes |
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.
Smart Rounding Modes? We want to make sure Smart Rounding surfaces in search I think, so it needs to be a heading
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 section below doesn't really sound like smart rounding or explain what it is. Should the comment above be something like "Final results with rounding strategy applied"?
} | ||
``` | ||
|
||
### When to Use Each Mode |
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.
This section is great to have but feels a little light. Can you please add a sentence or two for when and why a developer might pick each of these? I can guess, but I don't want to guess with defi.
**RoundDown** - Conservative for user payouts: | ||
|
||
```cadence | ||
// When calculating how much to pay out to users |
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.
Maybe just leave this comment here but also add it to the sentence above. Adding a suggestion
|
||
### When to Use Each Mode | ||
|
||
**RoundDown** - Conservative for user payouts: |
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.
**RoundDown** - Conservative for user payouts: | |
**RoundDown** - Select for user payouts to ensure the amount the protocol pays is never greater than what is owed: |
**RoundEven** - Reduces cumulative rounding bias: | ||
|
||
```cadence | ||
// For repeated operations where bias matters |
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.
When does bias matter? Can you link to an internal or external resource?
// result = 1500.0 | ||
``` | ||
|
||
**Important:** The multiplication uses `UInt256` internally to prevent overflow: |
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.
This should probably be an info admonition.
} | ||
``` | ||
|
||
### Flexible Rounding Strategies |
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.
Why the duplicate section on rounding?
let display = DeFiActionsMathUtils.toUFix64Round(calculatedValue) | ||
``` | ||
|
||
### Gas Efficiency |
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.
This feels like a random ai addition
let afterFee: UFix64 = amount - fee | ||
let output: UFix64 = afterFee * price | ||
``` | ||
|
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.
Can we show a consequence here? What's below is much more complicated, we should demonstarte why it's worth it.
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.
One line though, not a complex example.
// XX.XX fee lost to the protocol @ amount of 1MM
|
||
## More Resources | ||
|
||
- [View the DeFiActionsMathUtils source code](https://github.com/onflow/FlowActions/blob/main/cadence/contracts/utils/DeFiActionsMathUtils.cdc) |
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.
Please use reference-style links
|
||
## Conclusion | ||
|
||
`DeFiActionsMathUtils` provides the mathematical foundation for building accurate and secure DeFi applications on Flow. By using 24-decimal fixed-point arithmetic with `UInt128`, you can perform complex financial calculations without the precision loss that would occur with native `UFix64` operations alone. No newline at end of file |
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.
Restate advantages over other platforms
No description provided.