diff --git a/src/common/utils/balance/balance.test.tsx b/src/common/utils/balance/balance.test.tsx new file mode 100644 index 00000000..47455776 --- /dev/null +++ b/src/common/utils/balance/balance.test.tsx @@ -0,0 +1,122 @@ +import BigNumber from 'bignumber.js'; +import { AssetAccount, TrasferAsset } from '@/common/types'; +import { formatBalance, formatAmount, getTotalBalance, getTransferDetails, updateAssetsBalance } from './index'; +import { Chain } from '@/common/chainRegistry/types'; + +describe('formatBalance function', () => { + test('should calculate small amount', () => { + const { formattedValue, suffix, decimalPlaces } = formatBalance('592321079928', 12); + + expect(formattedValue).toEqual('0.59232'); + expect(suffix).toEqual(''); + expect(decimalPlaces).toEqual(5); + }); + + test('should calculate thousands', () => { + const { formattedValue, suffix, decimalPlaces } = formatBalance('16172107992822306', 12); + + expect(formattedValue).toEqual('16172.1'); + expect(suffix).toEqual(''); + expect(decimalPlaces).toEqual(2); + }); + + test('should calculate millions', () => { + const { formattedValue, suffix, decimalPlaces } = formatBalance('1617210799282230602', 12); + + expect(formattedValue).toEqual('1.61'); + expect(suffix).toEqual('M'); + expect(decimalPlaces).toEqual(2); + }); + + test('should calculate billion', () => { + const { formattedValue, suffix, decimalPlaces } = formatBalance('8717210799282230602024', 12); + + expect(formattedValue).toEqual('8.71'); + expect(suffix).toEqual('B'); + expect(decimalPlaces).toEqual(2); + }); + + test('should calculate trillion', () => { + const { formattedValue, suffix, decimalPlaces } = formatBalance('91528717210799282230602024', 12); + + expect(formattedValue).toEqual('91.52'); + expect(suffix).toEqual('T'); + expect(decimalPlaces).toEqual(2); + }); +}); + +describe('formatAmount function', () => { + test('should freturn zero value with empty string', () => { + const amount = ''; + const formattedAmount = formatAmount(amount, 12); + + expect(formattedAmount).toBe('0'); + }); + + test('should format amount', () => { + const amount = '5'; + const formattedAmount = formatAmount(amount, 12); + + expect(formattedAmount).toBe('5000000000000'); + }); + test('should format amount with precision', () => { + const amount = '0.92321'; + const formattedAmount = formatAmount(amount, 12); + + expect(formattedAmount).toBe('923210000000'); + }); +}); + +describe('getTotalBalance function', () => { + test('should return total balance', () => { + const assets = [ + { totalBalance: '100', asset: { priceId: '1' } }, + { totalBalance: '200', asset: { priceId: '2' } }, + ]; + const assetsPrices = { '1': { price: 1 }, '2': { price: 2 } }; + const totalBalance = getTotalBalance(assets as AssetAccount[], assetsPrices); + + expect(totalBalance).toBe(500); + }); +}); + +describe('updateAssetsBalance function', () => { + test('should update assets balances', () => { + const prevAssets = [ + { chainId: 'chain1', totalBalance: '100' }, + { chainId: 'chain2', totalBalance: '200' }, + ]; + const chain = { chainId: 'chain1' }; + const balance = { total: () => '300', transferable: () => '250' }; + const updatedAssets = updateAssetsBalance(prevAssets as AssetAccount[], chain as Chain, balance); + + expect(updatedAssets[0].totalBalance).toBe('300'); + expect(updatedAssets[0].transferableBalance).toBe('250'); + expect(updatedAssets[1].totalBalance).toBe('200'); + }); +}); + +describe('getTransferDetails function', () => { + test('should return transfer details', async () => { + const selectedAsset = { + chainId: 'chain1', + transferableBalance: '100000000000', + asset: { precision: 12 }, + isGift: false, + }; + const amount = '50'; + const estimateFee = jest.fn().mockResolvedValue(new BigNumber(0.00001)); + const getExistentialDeposit = jest.fn().mockResolvedValue('10000000000'); + + const transferDetails = await getTransferDetails( + selectedAsset as TrasferAsset, + amount, + estimateFee, + getExistentialDeposit, + ); + + expect(transferDetails.fee).toBe(0.00001); + expect(transferDetails.max).toBe('0.10000'); + expect(transferDetails.formattedDeposit).toBe(0.01); + }); +}); diff --git a/src/common/utils/balance.ts b/src/common/utils/balance/index.ts similarity index 94% rename from src/common/utils/balance.ts rename to src/common/utils/balance/index.ts index c3f801ff..07edf5e8 100644 --- a/src/common/utils/balance.ts +++ b/src/common/utils/balance/index.ts @@ -1,11 +1,11 @@ import BigNumber from 'bignumber.js'; import { BN, BN_TEN } from '@polkadot/util'; -import { AssetAccount, AssetPrice, ChainAssetAccount, ChainAssetId, ChainId, TrasferAsset } from '../types'; -import { Chain } from '../chainRegistry/types'; -import { IAssetBalance } from '../balances/types'; -import { EstimateFee, GetExistentialDeposit } from '../extrinsicService/types'; -import { handleFee } from './extrinsics'; +import { AssetAccount, AssetPrice, ChainAssetAccount, ChainAssetId, ChainId, TrasferAsset } from '../../types'; +import { Chain } from '../../chainRegistry/types'; +import { IAssetBalance } from '../../balances/types'; +import { EstimateFee, GetExistentialDeposit } from '../../extrinsicService/types'; +import { handleFee } from '../extrinsics'; const ZERO_BALANCE = '0'; diff --git a/src/common/utils/extrinsics/extrinsics.test.tsx b/src/common/utils/extrinsics/extrinsics.test.tsx new file mode 100644 index 00000000..c9a1b68d --- /dev/null +++ b/src/common/utils/extrinsics/extrinsics.test.tsx @@ -0,0 +1,42 @@ +import { handleSend, handleSendGift } from './index'; +import { TrasferAsset } from '../../types'; + +const mockSubmitExtrinsic = jest.fn().mockResolvedValue('successHash'); +const mockEstimateFee = jest.fn().mockResolvedValue(10); +console.log = jest.fn(); + +const transferAsset = { + destinationAddress: '0x1234567890123456789012345678901234567890', + chainId: 'chainId', + amount: '100', + transferAll: false, + asset: { precision: 18 }, +}; + +describe('handleSend function', () => { + test('should handle sending of funds', async () => { + await handleSend(mockSubmitExtrinsic, transferAsset as TrasferAsset); + + expect(mockSubmitExtrinsic).toHaveBeenCalledWith('chainId', expect.any(Function)); + expect(console.log).toHaveBeenCalledWith('Success, Hash:', 'successHash'); + }); +}); + +describe('handleSendGift function', () => { + test('should calculate the amount of a gift', async () => { + await handleSendGift( + mockSubmitExtrinsic, + mockEstimateFee, + transferAsset as TrasferAsset, + '0x1234567890123456789012345678901234567890', + ); + expect(mockSubmitExtrinsic).toHaveBeenCalledWith('chainId', expect.any(Function)); + + const transferAmount = (+transferAsset.amount * Math.pow(10, 18)).toString(); + expect(transferAmount).toEqual('100000000000000000000'); + + const fee = 10000000000000; + const giftAmount = (+transferAmount + fee).toString(); + expect(giftAmount).toEqual('100000010000000000000'); + }); +}); diff --git a/src/common/utils/extrinsics.ts b/src/common/utils/extrinsics/index.ts similarity index 94% rename from src/common/utils/extrinsics.ts rename to src/common/utils/extrinsics/index.ts index df4378a0..0089e182 100644 --- a/src/common/utils/extrinsics.ts +++ b/src/common/utils/extrinsics/index.ts @@ -2,10 +2,10 @@ import { decodeAddress } from '@polkadot/util-crypto'; import { KeyringPair } from '@polkadot/keyring/types'; import { Balance } from '@polkadot/types/interfaces'; -import { EstimateFee, ExtrinsicBuilder, SubmitExtrinsic } from '../extrinsicService/types'; -import { formatAmount } from './balance'; -import { Address, ChainId, TrasferAsset } from '../types'; -import { FAKE_ACCOUNT_ID } from './constants'; +import { EstimateFee, ExtrinsicBuilder, SubmitExtrinsic } from '../../extrinsicService/types'; +import { formatAmount } from '../balance'; +import { Address, ChainId, TrasferAsset } from '../../types'; +import { FAKE_ACCOUNT_ID } from '../constants'; const transferExtrinsic = async ( submitExtrinsic: SubmitExtrinsic,