mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-10-29 05:39:49 +00:00
When an amount had more decimals than the selected unit allows, the function set result to -1 and then overwrote it, so the conversion went on with a negative exponent. BN computes 10 ** -n as 10 ** n: with 12 chain decimals, "1.1234567890" in milli became 13345678900 instead of 1123456789. Such input is now invalid. The decimals were also cut to the chain's decimals, which silently dropped digits of an asset with more decimals than the chain; the cut is removed, and with it the api argument it was the only use of.
34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/react-components authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
|
|
import { formatBalance } from '@pezkuwi/util';
|
|
|
|
import { inputToBn } from './InputNumber.js';
|
|
|
|
function convert (input: string, unit: string, decimals: number): [string, boolean] {
|
|
const [bn, isValid] = inputToBn(input, formatBalance.findSi(unit), 128, false, true, undefined, decimals);
|
|
|
|
return [bn.toString(), isValid];
|
|
}
|
|
|
|
describe('inputToBn', (): void => {
|
|
it('converts a decimal in the base unit', (): void => {
|
|
expect(convert('1.5', '-', 12)).toEqual(['1500000000000', true]);
|
|
});
|
|
|
|
it('converts every decimal a smaller unit can carry', (): void => {
|
|
expect(convert('1.123456789', 'm', 12)).toEqual(['1123456789', true]);
|
|
});
|
|
|
|
it('rejects more decimals than the unit can carry instead of scaling them up', (): void => {
|
|
// the exponent would be 9 - 10 = -1, and BN computes 10 ** -1 as 10
|
|
expect(convert('1.1234567890', 'm', 12)).toEqual(['-1', false]);
|
|
});
|
|
|
|
it('keeps decimals beyond the chain decimals for an asset with more', (): void => {
|
|
expect(convert('1.0000000000001', '-', 18)).toEqual(['1000000000000100000', true]);
|
|
});
|
|
});
|