Skip to content

Commit 18a2fb7

Browse files
committed
Fix token amount parsing
1 parent b8e6589 commit 18a2fb7

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

packages/sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949

5050
## 10.0.0-alpha.? (Unreleased)
5151

52+
### Fixed
53+
54+
- Fix conversion in `TokenAmount.fromDecimals` function when used with large `tokenAmount` values with small `decimal` values.
55+
5256
## 10.0.0-alpha.15
5357

5458
### Fixed

packages/sdk/src/plt/TokenAmount.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ export function fromDecimal(amount: BigSource | bigint, decimals: number): Token
163163
}
164164

165165
const bigAmount = Big(parsed);
166-
const parsedDecimals = bigAmount.toString().split('.')[1]?.length ?? 0;
167-
166+
const parsedDecimals = bigAmount.toFixed(0).split('.')[1]?.length ?? 0;
168167
if (parsedDecimals > decimals) {
169168
throw new Error('The amount has more decimal places than the specified decimals.');
170169
}

packages/sdk/test/ci/plt/TokenAmount.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ describe('PLT TokenAmount', () => {
2626

2727
test('Token amounts with invalid values throws', () => {
2828
expect(() => TokenAmount.create(-504n, 0)).toThrow(TokenAmount.Err.negative());
29+
expect(() => TokenAmount.create(-504n, 1)).toThrow(TokenAmount.Err.negative());
30+
expect(() => TokenAmount.fromDecimal(-504n, 0)).toThrow(TokenAmount.Err.negative());
31+
expect(() => TokenAmount.fromDecimal(-504n, 1)).toThrow(TokenAmount.Err.negative());
2932
expect(() => TokenAmount.create(MAX_U64 + 1n, 0)).toThrow(TokenAmount.Err.exceedsMaxValue());
33+
expect(() => TokenAmount.create(99999999999999999999999999n, 1)).toThrow(TokenAmount.Err.exceedsMaxValue());
34+
expect(() => TokenAmount.fromDecimal(MAX_U64 + 1n, 0)).toThrow(TokenAmount.Err.exceedsMaxValue());
35+
expect(() => TokenAmount.fromDecimal(99999999999999999999999999n, 1)).toThrow(
36+
TokenAmount.Err.exceedsMaxValue()
37+
);
3038
});
3139

3240
test('Returns expected amount', () => {

0 commit comments

Comments
 (0)