diff --git a/CHANGELOG.md b/CHANGELOG.md index 260f08f8f..2e9154193 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 aadabe87a..861ee2102 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 19f73ce68..a5eda4449 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 d09b7153c..266cc4df4 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); + } + } }