diff --git a/src/pages/purchase.tsx b/src/pages/purchase.tsx index accdc9c6..ab930614 100644 --- a/src/pages/purchase.tsx +++ b/src/pages/purchase.tsx @@ -213,11 +213,11 @@ const Purchase = () => { {loading || - !currentPhase || - !saleEnd || - !currentBlockNumber || - !progress || - !saleEndTimestamp ? ( + !currentPhase || + !saleEnd || + !currentBlockNumber || + !progress || + !saleEndTimestamp ? ( <> Connect your wallet diff --git a/src/utils/functions.test.ts b/src/utils/functions.test.ts new file mode 100644 index 00000000..0988c3d5 --- /dev/null +++ b/src/utils/functions.test.ts @@ -0,0 +1,42 @@ +import { CORETIME_TOKEN_UNIT } from '@/models'; +import { + extractRegionIdFromRaw, + formatBalance, + parseHNString, + parseHNStringToString, +} from './functions'; +import { CoreMask, RegionId } from 'coretime-utils'; + +describe('Util functions', () => { + describe('parseHNString', () => { + it('works', () => { + const a = '100,305'; + expect(parseHNString(a)).toBe(100305); + const b = '42'; + expect(parseHNString(b)).toBe(42); + expect(parseHNString('')).toBe(NaN); + }); + }); + + describe('parseHNStringToString', () => { + it('works', () => { + const a = '100,305'; + expect(parseHNStringToString(a)).toBe('100305'); + const b = '42'; + expect(parseHNStringToString(b)).toBe('42'); + expect(parseHNStringToString('')).toBe(''); + }); + }); + + describe('extractRegionIdFromRaw', () => { + it('works', () => { + const raw = '316913858982876965003350507519'; + const regionId: RegionId = { + begin: 4, + core: 0, + mask: CoreMask.completeMask(), + }; + expect(extractRegionIdFromRaw(BigInt(raw))).toStrictEqual(regionId); + }); + }); +}); diff --git a/src/utils/functions.ts b/src/utils/functions.ts index bb135ea6..8f8147be 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -1,5 +1,6 @@ import { ApiPromise } from '@polkadot/api'; import { CoreMask, RegionId } from 'coretime-utils'; +import { formatBalance as polkadotFormatBalance } from '@polkadot/util'; import Decimal from 'decimal.js'; import { @@ -82,18 +83,16 @@ export const formatBalance = (balance: string, contractChain: boolean) => { Decimal.config({ rounding: Decimal.ROUND_DOWN }); const decimals = contractChain ? CONTRACT_DECIMALS : CORETIME_DECIMALS; - if (new Decimal(balance).lt(new Decimal(10).pow(CONTRACT_DECIMALS))) { - return new Decimal(balance) - .dividedBy(new Decimal(10).pow(decimals)) - .toPrecision(2); - } else { - return new Decimal(balance) - .dividedBy(new Decimal(10).pow(decimals)) - .toFixed(2); - } + return polkadotFormatBalance(balance, { + decimals, + withUnit: false, + withSiFull: true, + }); }; // TODO: should be queried from runtime api instead. +// +// https://github.com/paritytech/polkadot-sdk/pull/3485 export const leadinFactorAt = (when: number) => { return 2 - when; };