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: Support converting small number to AmountTokenV2 #496

Merged
merged 9 commits into from
Dec 6, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Add retrieveBtcStatus to ckbtc minter canister.
- Make uint8ArrayToHexString also accept `number[]` as input.
- Add a new type TokenAmountV2 which supports `decimals !== 8`.
- Fix issue when converting token amount from small numbers with `TokenAmountV2`.

## Operations

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
{
"name": "@dfinity/ledger-icp",
"path": "./packages/ledger-icp/dist/index.js",
"limit": "15 kB",
"limit": "16 kB",
lmuntaner marked this conversation as resolved.
Show resolved Hide resolved
"ignore": [
"@dfinity/agent",
"@dfinity/candid",
Expand Down
24 changes: 12 additions & 12 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Parameters:

- `amount`: - in string format

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L10)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L12)

#### :gear: defaultAgent

Expand Down Expand Up @@ -376,15 +376,15 @@ Tags after patch version are ignored, e.g. 1.0.0-beta.1 is considered equal to 1
| ---------- | ------- |
| `ICPToken` | `Token` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L113)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L115)

### :factory: TokenAmount

Deprecated. Use TokenAmountV2 instead which supports decimals !== 8.

Represents an amount of tokens.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L127)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L129)

#### Methods

Expand All @@ -406,7 +406,7 @@ Parameters:
- `params.amount`: The amount in bigint format.
- `params.token`: The token type.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L144)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L146)

##### :gear: fromString

Expand All @@ -425,7 +425,7 @@ Parameters:
- `params.amount`: The amount in string format.
- `params.token`: The token type.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L165)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L167)

##### :gear: fromNumber

Expand All @@ -442,21 +442,21 @@ Parameters:
- `params.amount`: The amount in number format.
- `params.token`: The token type.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L195)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L197)

##### :gear: toE8s

| Method | Type |
| ------- | -------------- |
| `toE8s` | `() => bigint` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L221)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L223)

### :factory: TokenAmountV2

Represents an amount of tokens.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L233)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L235)

#### Methods

Expand All @@ -478,7 +478,7 @@ Parameters:
- `params.amount`: The amount in bigint format.
- `params.token`: The token type.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L246)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L248)

##### :gear: fromString

Expand All @@ -497,7 +497,7 @@ Parameters:
- `params.amount`: The amount in string format.
- `params.token`: The token type.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L267)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L269)

##### :gear: fromNumber

Expand All @@ -514,15 +514,15 @@ Parameters:
- `params.amount`: The amount in number format.
- `params.token`: The token type.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L291)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L293)

##### :gear: toUlps

| Method | Type |
| -------- | -------------- |
| `toUlps` | `() => bigint` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L319)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/parser/token.ts#L321)

### :factory: Canister

Expand Down
54 changes: 47 additions & 7 deletions packages/utils/src/parser/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,9 @@ describe("TokenAmountV2 with 18 decimals", () => {
).toBe(FromStringToTokenError.InvalidFormat);
});

it("does not support scientific notation", () => {
const callToNumber = () =>
TokenAmountV2.fromNumber({ token: token, amount: 1e-9 });
expect(callToNumber).toThrow(
expect.objectContaining({
message: "Invalid number 1e-9",
}),
it("does not support scientific notation as string", () => {
expect(TokenAmountV2.fromString({ token: token, amount: "1e-9" })).toEqual(
FromStringToTokenError.InvalidFormat,
);
});

Expand Down Expand Up @@ -433,3 +429,47 @@ describe("TokenAmountV2 with 0 decimals", () => {
).toEqual(FromStringToTokenError.FractionalTooManyDecimals);
});
});

describe("TokenAmountV2 with 8 decimals", () => {
const token = {
symbol: "ICP",
name: "ICP",
decimals: 8,
};
it("can be initialized from a number", () => {
expect(TokenAmountV2.fromNumber({ token: token, amount: 1 })).toEqual(
TokenAmountV2.fromUlps({
token: token,
amount: 100000000n,
}),
);
expect(TokenAmountV2.fromNumber({ token: token, amount: 1234 })).toEqual(
TokenAmountV2.fromUlps({
token: token,
amount: 123400000000n,
}),
);
expect(
TokenAmountV2.fromNumber({ token: token, amount: 0.00000002 }),
).toEqual(
TokenAmountV2.fromUlps({
token: token,
amount: 2n,
}),
);
expect(
TokenAmountV2.fromNumber({ token: token, amount: 0.00000001 }),
).toEqual(
TokenAmountV2.fromUlps({
token: token,
amount: 1n,
}),
);
});

it("truncates small numbers to 8 decimals", () => {
expect(TokenAmountV2.fromNumber({ token: token, amount: 1e-9 })).toEqual(
dskloetd marked this conversation as resolved.
Show resolved Hide resolved
TokenAmountV2.fromUlps({ token: token, amount: 0n }),
);
});
});
4 changes: 3 additions & 1 deletion packages/utils/src/parser/token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { E8S_PER_TOKEN } from "../constants/constants";
import { FromStringToTokenError } from "../enums/token.enums";

const DECIMALS_CONVERSION_SUPPORTED = 8;

/**
* Receives a string representing a number and returns the big int or error.
*
Expand Down Expand Up @@ -296,7 +298,7 @@ export class TokenAmountV2 {
token: Token;
}): TokenAmountV2 {
const tokenAmount = TokenAmountV2.fromString({
amount: amount.toString(),
amount: amount.toFixed(DECIMALS_CONVERSION_SUPPORTED),
token,
});
if (tokenAmount instanceof TokenAmountV2) {
Expand Down
Loading