Skip to content

Commit

Permalink
Feat: Add toE8s to TokenAmountV2 (#529)
Browse files Browse the repository at this point in the history
# 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>
  • Loading branch information
lmuntaner and github-actions[bot] authored Jan 26, 2024
1 parent f89b3d0 commit 4fd8b82
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ Represents an amount of tokens.
- [fromString](#gear-fromstring)
- [fromNumber](#gear-fromnumber)
- [toUlps](#gear-toulps)
- [toE8s](#gear-toe8s)

##### :gear: fromUlps

Expand Down Expand Up @@ -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)
Expand Down
72 changes: 72 additions & 0 deletions packages/utils/src/parser/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
});
});
14 changes: 14 additions & 0 deletions packages/utils/src/parser/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 4fd8b82

Please sign in to comment.