Skip to content

Commit

Permalink
util tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Apr 3, 2024
1 parent 751aa77 commit c36d4c9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/pages/purchase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ const Purchase = () => {
</Box>
<Box>
{loading ||
!currentPhase ||
!saleEnd ||
!currentBlockNumber ||
!progress ||
!saleEndTimestamp ? (
!currentPhase ||
!saleEnd ||
!currentBlockNumber ||
!progress ||
!saleEndTimestamp ? (
<>
<Typography variant='h5' align='center'>
Connect your wallet
Expand Down
42 changes: 42 additions & 0 deletions src/utils/functions.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
17 changes: 8 additions & 9 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit c36d4c9

Please sign in to comment.