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 TokenAmountV2.fromNumber for decimals < 8 #629

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading