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

fix: formatDecimals precision #912

Merged
merged 20 commits into from
Jul 29, 2024
Merged

fix: formatDecimals precision #912

merged 20 commits into from
Jul 29, 2024

Conversation

0xAlec
Copy link
Contributor

@0xAlec 0xAlec commented Jul 26, 2024

What changed? Why?

  • previously we cast amount from a string to a javascript number (max 64 bits), which would lose precision for some ERC-20 denominations
  • this would result in a trade being requested for more token units than the user had in their account, which resulted in unexecutable transactions
  • change to using BigInt implementation

Notes to reviewers

How has it been tested?
image
improved test coverage compared to main

unit tested and tested locally

if (inputInDecimals) {
return (Number(amount) / 10 ** decimals).toString();
// If input is already in decimals, convert to readable amount
Copy link
Contributor

Choose a reason for hiding this comment

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

What do we mean by "readable" amount?

@Zizzamia
Copy link
Contributor

@0xAlec could you rebase from main when you have a minute? With all our tests in order, it'll help our CI pass more easily.

package.json Outdated Show resolved Hide resolved
}
if (!/^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(amount)) {
throw new Error(
'Invalid input: amount must be a non-negative number string',
Copy link
Contributor

Choose a reason for hiding this comment

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

Love the care for the messages.

Copy link
Contributor

@Zizzamia Zizzamia left a comment

Choose a reason for hiding this comment

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

So niiiice

@Zizzamia Zizzamia merged commit 5d9b4f8 into main Jul 29, 2024
12 checks passed
@Zizzamia Zizzamia deleted the alec/precision-fix branch July 29, 2024 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants