Skip to content

Commit

Permalink
Fix TokenAmountV2.fromNumber for decimals < 8 (#629)
Browse files Browse the repository at this point in the history
# Motivation

When creating a `TokenAmountV2` from a `number`, we first convert it to
a string with 8 decimals.
When the number has more decimals, they are just truncated. But number
input components in gix-components don't allow more than 8 decimals.
But when the token allows fewer than 8 decimals, this results in an
error.
The happened to be one of the few combinations without a unit test but
will be needed for ckUSDC.

# Changes

When converting the number to a string with a fixed number of decimals,
instead of always using 8 decimals, use the minimum of 8 and the number
of decimals the token allows.

# Tests

1. Added a unit test.
2. Tested in NNS dapp that this error no longer appears:

<img width="356" alt="image"
src="https://github.com/dfinity/ic-js/assets/122978264/a1002c5e-234b-4fee-af8b-44fafff5343b">

# Todos

- [x] Add entry to changelog (if necessary).

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
dskloetd and github-actions[bot] authored May 16, 2024
1 parent 65e23d1 commit 86a58da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ The current status of the libraries at the time of the release is as follows:
- Update Candid definition in ckBTC, ckETH, CMC, ICP and ICRC Ledgers, Nns and Sns.
- Add "Subnet Rental" to the list of `NnsFunction` and to topic support.

## Fix

- Fixed `TokenAmountV2.fromNumber` for tokens with fewer than 8 decimals.

## Build

- Upgrade `agent-js` dependencies to `v1.3.0` and revert the default retry times value to 10, given that the issue is fixed.
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,15 @@ Parameters:
| -------- | -------------- |
| `toUlps` | `() => bigint` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L322)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L324)

##### :gear: toE8s

| Method | Type |
| ------- | -------------- |
| `toE8s` | `() => bigint` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L330)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L332)

### :factory: Canister

Expand Down
14 changes: 14 additions & 0 deletions packages/utils/src/parser/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,20 @@ describe("TokenAmountV2 with 2 decimals", () => {
);
});

it("can be initialized from a number", () => {
expect(
TokenAmountV2.fromNumber({
token: token,
amount: 100000000.91,
}),
).toEqual(
TokenAmountV2.fromUlps({
token: token,
amount: 10000000091n,
}),
);
});

it("returns the value in e8s", () => {
expect(
(
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/parser/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ export class TokenAmountV2 {
token: Token;
}): TokenAmountV2 {
const tokenAmount = TokenAmountV2.fromString({
amount: amount.toFixed(DECIMALS_CONVERSION_SUPPORTED),
amount: amount.toFixed(
Math.min(DECIMALS_CONVERSION_SUPPORTED, token.decimals),
),
token,
});
if (tokenAmount instanceof TokenAmountV2) {
Expand Down

0 comments on commit 86a58da

Please sign in to comment.