From 1908f7fd6f646aa5d44dad5baa7c6c807efe4483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lloren=C3=A7?= Date: Fri, 26 Jan 2024 16:22:03 +0100 Subject: [PATCH] Feat: Add `toE8s` to TokenAmountV2 --- packages/utils/src/parser/token.spec.ts | 72 +++++++++++++++++++++++++ packages/utils/src/parser/token.ts | 14 +++++ 2 files changed, 86 insertions(+) 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); + } + } }