Conversion to/from different fixed point decimals #177
thedavidmeister
started this conversation in
Ideas
Replies: 2 comments 3 replies
-
Thanks for your feature suggestion. I actually have a solution for what you need - it's called But this logic is too high-level to be included in PRBMath. If anything, PRBMath may not be used with ERC-20 tokens. |
Beta Was this translation helpful? Give feedback.
3 replies
-
FYI i moved and improved rescaling logic here https://github.com/rainprotocol/rain.math.fixedpoint/blob/main/src/FixedPointDecimalScale.sol includes better testing and ~200-300 less gas per re-scale, and supports a wider range of inputs without error |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thanks to features like
decimals
in ERC20 we have tokens like USDT with 6 decimals commonly paired against tokens like USDC with 18 decimals.This means that expressing "1 USDT = 1USDC" to humans really looks like "1 USDT = 1e12 USDC" on the contract.
For the sake of contract calculations it can be convenient to first normalise values to 18 decimals then do math on them.
prb-math already has conversions to/from integers (i.e. decimals = 0) but no way to go back and forward between other common cases like token balances
i have some examples here https://github.com/rainprotocol/rain-protocol/blob/develop/contracts/math/LibFixedPointMath.sol
but they use OZ math due to the need to specify rounding in the general case of scaling up/down, because it's relatively easy to e.g. scale "dust" down to literally 0, with potentially unintended consequences
Beta Was this translation helpful? Give feedback.
All reactions