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: Support converting small number to AmountTokenV2 #496

Merged
merged 9 commits into from
Dec 6, 2023

Conversation

lmuntaner
Copy link
Contributor

@lmuntaner lmuntaner commented Dec 5, 2023

Motivation

TokenAmountV2 can't convert small numbers such as 0.00000002 which is 2 e8s of ICP.

The problem was that fromNumber was using toString and toString was converting 0.00000002 to the scientific notation. Which we specifically don't want to support when converting from a string.

Solution, use toFixed(8) instead of toString. We already support only a precision of 8 decimals.

Changes

  • Use toFixed(8) instead of toString in fromNumber of TokenAmountV2.

Tests

  • Change test "does not support scientific notation" to do it fromString.
  • Add tests for 8 decimals.
  • Add a test for fromNumber with very small numbers.
  • Add test for fromNumber to check that precision is 8 decimals.

Todos

  • Add entry to changelog (if necessary).

@lmuntaner
Copy link
Contributor Author

@dskloetd I added a test that it's broken with the current implementation, but I feel it should work.

I propose to support scientific notation like in TokenAmount.

Copy link
Contributor

github-actions bot commented Dec 5, 2023

size-limit report 📦

Path Size
@dfinity/ckbtc 6.88 KB (0%)
@dfinity/cmc 1.01 KB (0%)
@dfinity/ledger-icrc 2.92 KB (0%)
@dfinity/ledger-icp 14.77 KB (0%)
@dfinity/nns 33.85 KB (0%)
@dfinity/nns-proto 76.3 KB (0%)
@dfinity/sns 15.04 KB (0%)
@dfinity/utils 4.34 KB (+0.28% 🔺)
@dfinity/ic-management 1.94 KB (0%)

@dskloetd
Copy link
Collaborator

dskloetd commented Dec 5, 2023

I think the problem is the .toString() here:

amount: amount.toString(),

That should be .toFixed(token.decimals).

@dskloetd
Copy link
Collaborator

dskloetd commented Dec 5, 2023

Hm, now I'm not sure.

(0.123).toFixed(18)
'0.122999999999999998'

On the other hand, if we really want to support 18 decimals, we simply can't use the number for any intermediate value.
Supporting scientific notation for this just doesn't seem right either.

I think I prefer hard-coding .toFixed(8) until we stop using number as the type for the number input.

@lmuntaner
Copy link
Contributor Author

I think I prefer hard-coding .toFixed(8) until we stop using number as the type for the number input.

That's a good idea, we anyway agreed to set the precision to 8 decimals.

I used .toFixed(8) and that also means supporting exponential notation, so I changed a test.

Copy link
Collaborator

@dskloetd dskloetd left a comment

Choose a reason for hiding this comment

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

Can you fix the PR title and description as well?

packages/utils/src/parser/token.spec.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@dskloetd dskloetd left a comment

Choose a reason for hiding this comment

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

Asking again: Can you fix the PR title and description as well?

packages/utils/src/parser/token.spec.ts Show resolved Hide resolved
package.json Show resolved Hide resolved
@lmuntaner
Copy link
Contributor Author

@dskloetd ready for another review

@dskloetd
Copy link
Collaborator

dskloetd commented Dec 5, 2023

Are these not coming through?

Can you fix the PR title and description as well?

Asking again: Can you fix the PR title and description as well?

@lmuntaner
Copy link
Contributor Author

Are these not coming through?

Can you fix the PR title and description as well?

Asking again: Can you fix the PR title and description as well?

Oops, sorry. I wasn't seeing them because they were not in any file and I looked only to the comments in the files.

@lmuntaner
Copy link
Contributor Author

@dskloetd ready for another review

@dskloetd
Copy link
Collaborator

dskloetd commented Dec 5, 2023

When I look, the PR title is still "Fix: Add test that is broken".
You're not actually adding a test that is broken, right? Then it wouldn't pass CI.
Am I reading this wrong, or what do you mean by that?

@lmuntaner lmuntaner changed the title Fix: Add test that is broken Fix: Support converting small number to AmountTokenV2 Dec 5, 2023
@lmuntaner
Copy link
Contributor Author

When I look, the PR title is still "Fix: Add test that is broken". You're not actually adding a test that is broken, right? Then it wouldn't pass CI. Am I reading this wrong, or what do you mean by that?

Sorry, again, my bad. I'm a little bit all over the place this week.

@lmuntaner lmuntaner merged commit 7eafaa0 into main Dec 6, 2023
11 checks passed
@lmuntaner lmuntaner deleted the fix_LM_small-number-conversion branch December 6, 2023 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants