Skip to content
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

feat: normalize native token decimals #100

Open
wants to merge 62 commits into
base: develop
Choose a base branch
from
Open

Conversation

gzeoneth
Copy link
Member

Was #65 but have decimal stored in bridge instead of inbox & outbox, also rebased the PR on main for fast-track release.

There is a scenario where custom fee token has number of decimals different than 18. That's not ideal because a lot of tooling assumes that native currency (on child chain) is using 18 decimals. For that reason we do a normalization, and de-normalization, when depositing and withdrawing native asset through native bridge.

Ie. let's say deposit amount is 752.

  • if token has 16 decimals normalized amount which is going to be minted on child chain is 75200
  • if token has 20 decimals normalized amount is 7
  • if token uses no decimals normalized amount is 752*10^18

When withdrawing, opposite process is done. There can be loss of precision, due to the fact that we have to round down amount to the nearest supported value.

We also add 2 requirements on the custom fee token in order to avoid overflow:

  • fee token can use at most 36 decimals (enforced at inbox/outbox initialization)
  • max amount that can be bridged is type(uint256).max / 10**18 which is ~1.1*10^59 tokens

@cla-bot cla-bot bot added the s label Dec 11, 2023
@gvladika gvladika changed the base branch from main to develop May 15, 2024 08:27
Comment on lines +319 to +333
function _scaleDownToNativeDecimals(uint256 amount, uint8 decimals)
internal
pure
returns (uint256)
{
uint256 scaledAmount = amount;
if (decimals < 18) {
scaledAmount = amount / (10**(18 - decimals));
// round up if necessary
if (scaledAmount * (10**(18 - decimals)) < amount) {
scaledAmount++;
}
}
return scaledAmount;
}

Check warning

Code scanning / Slither

Divide before multiply Medium

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants