From 4fd8b825f77c07453ad91261540efd044fa63f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lloren=C3=A7=20Muntaner?= Date: Fri, 26 Jan 2024 16:55:00 +0100 Subject: [PATCH] Feat: Add `toE8s` to TokenAmountV2 (#529) # Motivation Support formatting e8s for tokens that have different decimals than 8. # Changes * Add method `toE8s` to `TokenAmountV2`. # Tests * Add tests for new method. # Todos - [x] Add entry to changelog (if necessary). --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- CHANGELOG.md | 1 + packages/utils/README.md | 9 ++++ packages/utils/src/parser/token.spec.ts | 72 +++++++++++++++++++++++++ packages/utils/src/parser/token.ts | 14 +++++ 4 files changed, 96 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 260f08f8..2e915419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add "API Boundary Node Management" topic support. - Add optional field `logo` to `Token` type. - Update `sns-js` candid files with new fields in sns canisters. +- Add public method `toE8s` to `TokenAmountV2`. ## Breaking changes diff --git a/packages/utils/README.md b/packages/utils/README.md index aadabe87..861ee210 100644 --- a/packages/utils/README.md +++ b/packages/utils/README.md @@ -465,6 +465,7 @@ Represents an amount of tokens. - [fromString](#gear-fromstring) - [fromNumber](#gear-fromnumber) - [toUlps](#gear-toulps) +- [toE8s](#gear-toe8s) ##### :gear: fromUlps @@ -525,6 +526,14 @@ Parameters: [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L322) +##### :gear: toE8s + +| Method | Type | +| ------- | -------------- | +| `toE8s` | `() => bigint` | + +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L330) + ### :factory: Canister [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/services/canister.ts#L4) diff --git a/packages/utils/src/parser/token.spec.ts b/packages/utils/src/parser/token.spec.ts index 19f73ce6..a5eda444 100644 --- a/packages/utils/src/parser/token.spec.ts +++ b/packages/utils/src/parser/token.spec.ts @@ -347,6 +347,32 @@ describe("TokenAmountV2 with 18 decimals", () => { FromStringToTokenError.InvalidFormat, ); }); + + it("returns the value in e8s", () => { + expect( + ( + TokenAmountV2.fromString({ amount: "2", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(200_000_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "0.21", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(21_000_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "0.00021", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(21_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "2000", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(200_000_000_000n); + }); }); describe("TokenAmountV2 with 2 decimals", () => { @@ -393,6 +419,26 @@ describe("TokenAmountV2 with 2 decimals", () => { }), ); }); + + it("returns the value in e8s", () => { + expect( + ( + TokenAmountV2.fromString({ amount: "2", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(200_000_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "0.21", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(21_000_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "2000", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(200_000_000_000n); + }); }); describe("TokenAmountV2 with 0 decimals", () => { @@ -472,4 +518,30 @@ describe("TokenAmountV2 with 8 decimals", () => { TokenAmountV2.fromUlps({ token: token, amount: 0n }), ); }); + + it("returns the value in e8s", () => { + expect( + ( + TokenAmountV2.fromString({ amount: "2", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(200_000_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "0.21", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(21_000_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "0.00021", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(21_000n); + + expect( + ( + TokenAmountV2.fromString({ amount: "2000", token }) as TokenAmountV2 + ).toE8s(), + ).toEqual(200_000_000_000n); + }); }); diff --git a/packages/utils/src/parser/token.ts b/packages/utils/src/parser/token.ts index d09b7153..266cc4df 100644 --- a/packages/utils/src/parser/token.ts +++ b/packages/utils/src/parser/token.ts @@ -322,4 +322,18 @@ export class TokenAmountV2 { public toUlps(): bigint { return this.ulps; } + + /** + * + * @returns The amount of ulps in e8s precision + */ + public toE8s(): bigint { + if (this.token.decimals < 8) { + return this.ulps * 10n ** BigInt(8 - this.token.decimals); + } else if (this.token.decimals === 8) { + return this.ulps; + } else { + return this.ulps / 10n ** BigInt(this.token.decimals - 8); + } + } }