diff --git a/src/common/utils/balance.ts b/src/common/utils/balance.ts index 44094a27..ff28dd6d 100644 --- a/src/common/utils/balance.ts +++ b/src/common/utils/balance.ts @@ -1,14 +1,9 @@ import BigNumber from 'bignumber.js'; -import { decodeAddress } from '@polkadot/keyring'; import { BN, BN_TEN } from '@polkadot/util'; -import { Address, AssetAccount, ChainAssetAccount, ChainAssetId, ChainId, TrasferAsset } from '../types'; +import { AssetAccount, AssetPrice, ChainAssetAccount, ChainAssetId } from '../types'; import { Chain } from '../chainRegistry/types'; import { IAssetBalance } from '../balances/types'; -import { EstimateFee, ExtrinsicBuilder, SubmitExtrinsic } from '../extrinsicService/types'; -import { Balance } from '@polkadot/types/interfaces'; -import { FAKE_ACCOUNT_ID } from './constants'; -import { getKeyringPairFromSeed } from '../wallet'; const ZERO_BALANCE = '0'; @@ -173,55 +168,16 @@ export const formatAmount = (amount: string, precision: number): string => { return new BN(amount.replace(/\D/g, '')).mul(BN_TEN.pow(bnPrecision)).toString(); }; -export async function handleSend( - submitExtrinsic: SubmitExtrinsic, - { destinationAddress, chainId, amount, transferAll, asset }: TrasferAsset, - giftTransfer?: string, -) { - const decodedAddress = decodeAddress(destinationAddress || giftTransfer); - - return await submitExtrinsic(chainId, (builder) => { - const transferFunction = transferAll - ? builder.api.tx.balances.transferAll(decodedAddress, false) - : builder.api.tx.balances.transferKeepAlive(decodedAddress, formatAmount(amount as string, asset.precision)); - - builder.addCall(transferFunction); - }).then((hash) => { - console.log('Success, Hash:', hash?.toString()); - }); -} +export const getTotalBalance = (assets: AssetAccount[], assetsPrices?: AssetPrice) => { + if (!assets.length || !assetsPrices) return; -export async function handleFee( - estimateFee: EstimateFee, - chainId: ChainId, - precision: number, - isGift?: boolean, -): Promise { - return await estimateFee(chainId, (builder: ExtrinsicBuilder) => - builder.addCall(builder.api.tx.balances.transferAll(decodeAddress(FAKE_ACCOUNT_ID), false)), - ).then((fee: Balance) => { - const finalFee = isGift ? Number(fee) * 2 : fee; - const { formattedValue } = formatBalance(finalFee.toString(), precision); - - return Number(formattedValue); - }); -} + return assets.reduce((acc, asset) => { + if (!asset.asset.priceId) return acc; -export async function claimGift( - seed: string, - address: Address, - chainId: ChainId, - submitExtrinsic: SubmitExtrinsic, -): Promise { - const keyring = getKeyringPairFromSeed(seed); - - return await submitExtrinsic( - chainId, - (builder) => { - builder.addCall(builder.api.tx.balances.transferAll(decodeAddress(address), false)); - }, - keyring, - ).then((hash) => { - console.log('Success, Hash:', hash?.toString()); - }); -} + const price = assetsPrices[asset.asset.priceId].price || 0; + const formatedBalance = Number(formatBalance(asset.totalBalance, asset.asset.precision).formattedValue); + acc += price * formatedBalance; + + return acc; + }, 0); +}; diff --git a/src/common/utils/extrinsics.ts b/src/common/utils/extrinsics.ts new file mode 100644 index 00000000..ea9a85d9 --- /dev/null +++ b/src/common/utils/extrinsics.ts @@ -0,0 +1,61 @@ +import { decodeAddress } from '@polkadot/util-crypto'; + +import { EstimateFee, ExtrinsicBuilder, SubmitExtrinsic } from '../extrinsicService/types'; +import { Address, ChainId, TrasferAsset } from '../types'; +import { FAKE_ACCOUNT_ID } from './constants'; +import { Balance } from '@polkadot/types/interfaces'; +import { formatAmount, formatBalance } from './balance'; +import { getKeyringPairFromSeed } from '../wallet'; + +export async function handleSend( + submitExtrinsic: SubmitExtrinsic, + { destinationAddress, chainId, amount, transferAll, asset }: TrasferAsset, + giftTransfer?: string, +) { + const decodedAddress = decodeAddress(destinationAddress || giftTransfer); + + return await submitExtrinsic(chainId, (builder) => { + const transferFunction = transferAll + ? builder.api.tx.balances.transferAll(decodedAddress, false) + : builder.api.tx.balances.transferKeepAlive(decodedAddress, formatAmount(amount as string, asset.precision)); + + builder.addCall(transferFunction); + }).then((hash) => { + console.log('Success, Hash:', hash?.toString()); + }); +} + +export async function handleFee( + estimateFee: EstimateFee, + chainId: ChainId, + precision: number, + isGift?: boolean, +): Promise { + return await estimateFee(chainId, (builder: ExtrinsicBuilder) => + builder.addCall(builder.api.tx.balances.transferAll(decodeAddress(FAKE_ACCOUNT_ID), false)), + ).then((fee: Balance) => { + const finalFee = isGift ? Number(fee) * 2 : fee; + const { formattedValue } = formatBalance(finalFee.toString(), precision); + + return Number(formattedValue); + }); +} + +export async function claimGift( + seed: string, + address: Address, + chainId: ChainId, + submitExtrinsic: SubmitExtrinsic, +): Promise { + const keyring = getKeyringPairFromSeed(seed); + + return await submitExtrinsic( + chainId, + (builder) => { + builder.addCall(builder.api.tx.balances.transferAll(decodeAddress(address), false)); + }, + keyring, + ).then((hash) => { + console.log('Success, Hash:', hash?.toString()); + }); +} diff --git a/src/components/Assets/AssetBalance.tsx b/src/components/Assets/AssetBalance.tsx index 77feeb06..26da3c1f 100644 --- a/src/components/Assets/AssetBalance.tsx +++ b/src/components/Assets/AssetBalance.tsx @@ -1,9 +1,8 @@ import { cnTw } from '@/common/utils/twMerge'; -import { CaptionText, Icon } from '@/components'; +import { CaptionText, Icon, TokenPrice } from '@/components'; import { PriceItem } from '@/common/types'; import { IconNames } from '../Icon/types'; -import TokenPrice from '../Price/TokenPrice'; import { Asset } from '@/common/chainRegistry/types'; import Balance from './Balance'; @@ -24,7 +23,7 @@ const AssetBalance = ({ balance, asset, name, className, showPrice, showArrow }:
{symbol} - + {showArrow && } diff --git a/src/components/Price/Price.tsx b/src/components/Price/Price.tsx index 2c7f85c3..b90fc57e 100644 --- a/src/components/Price/Price.tsx +++ b/src/components/Price/Price.tsx @@ -1,11 +1,13 @@ import { formatFiatBalance } from '@/common/utils/balance'; +import { Shimmering } from '@/components'; type Props = { - amount: string | number; + amount?: string | number; symbol?: string; }; const Price = ({ amount, symbol = '$' }: Props) => { + if (amount === undefined) return ; const { formattedValue, suffix } = formatFiatBalance(amount); return ( diff --git a/src/components/Price/TokenPrice.tsx b/src/components/Price/TokenPrice.tsx index dfb3937f..8d22f3a5 100644 --- a/src/components/Price/TokenPrice.tsx +++ b/src/components/Price/TokenPrice.tsx @@ -1,5 +1,5 @@ import { useGlobalContext } from '@/common/providers/contextProvider'; -import { BodyText, Price, Shimmering } from '..'; +import { BodyText, Price, Shimmering } from '@/components'; import { formatBalance } from '@/common/utils/balance'; type Props = { diff --git a/src/components/index.ts b/src/components/index.ts index 02aff5a5..58050144 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -11,6 +11,7 @@ import { } from './Typography'; import { Icon, IconButton } from './Icon'; import Price from './Price/Price'; +import TokenPrice from '@/components/Price/TokenPrice'; import AssetBalance from './Assets/AssetBalance'; import AssetsList from './Assets/AssetsList'; import Plate from './Plate/Plate'; @@ -41,4 +42,5 @@ export { Identicon, Layout, GiftPlate, + TokenPrice, }; diff --git a/src/pages/transfer/amount.tsx b/src/pages/transfer/amount.tsx index 9328b47b..7d9fec30 100644 --- a/src/pages/transfer/amount.tsx +++ b/src/pages/transfer/amount.tsx @@ -8,12 +8,22 @@ import { Button, CircularProgress, Input } from '@nextui-org/react'; import { useTelegram } from '@common/providers/telegramProvider'; import { useGlobalContext } from '@/common/providers/contextProvider'; import { Paths } from '@/common/routing'; -import { HeadlineText, Icon, Identicon, CaptionText, LargeTitleText, TextBase, Layout, BodyText } from '@/components'; +import { + HeadlineText, + Icon, + Identicon, + CaptionText, + LargeTitleText, + TextBase, + Layout, + BodyText, + TokenPrice, +} from '@/components'; import { IconNames } from '@/components/Icon/types'; import { useExtrinsicProvider } from '@/common/extrinsicService/ExtrinsicProvider'; -import { formatBalance, handleFee } from '@/common/utils/balance'; +import { formatBalance } from '@/common/utils/balance'; +import { handleFee } from '@/common/utils/extrinsics'; import { ChainId } from '@/common/types'; -import TokenPrice from '@/components/Price/TokenPrice'; export default function AmountPage() { const router = useRouter(); diff --git a/src/pages/transfer/confirmation.tsx b/src/pages/transfer/confirmation.tsx index 8ddaed81..07e9d65d 100644 --- a/src/pages/transfer/confirmation.tsx +++ b/src/pages/transfer/confirmation.tsx @@ -8,6 +8,7 @@ import { Divider } from '@nextui-org/react'; import { useTelegram } from '@common/providers/telegramProvider'; import { useGlobalContext } from '@/common/providers/contextProvider'; import { Paths } from '@/common/routing'; +import { handleSend } from '@/common/utils/extrinsics'; import { HeadlineText, Icon, @@ -21,7 +22,6 @@ import { } from '@/components'; import { IconNames } from '@/components/Icon/types'; import { useExtrinsicProvider } from '@/common/extrinsicService/ExtrinsicProvider'; -import { handleSend } from '@/common/utils/balance'; import { TrasferAsset } from '@/common/types'; export default function ConfirmationPage() { diff --git a/src/pages/transfer/create-gift.tsx b/src/pages/transfer/create-gift.tsx index 0c2695fe..2a3d3d09 100644 --- a/src/pages/transfer/create-gift.tsx +++ b/src/pages/transfer/create-gift.tsx @@ -10,12 +10,12 @@ import { Paths } from '@/common/routing'; import { HeadlineText, Layout } from '@/components'; import GiftDetails from '@/screens/gifts/GiftDetails'; import { useExtrinsicProvider } from '@/common/extrinsicService/ExtrinsicProvider'; -import { handleSend } from '@/common/utils/balance'; import { ChainId, TrasferAsset } from '@/common/types'; import { createGiftWallet } from '@/common/wallet'; import { createTgLink } from '@/common/telegram'; import { TgLink } from '@/common/telegram/types'; import { backupGifts } from '@/common/utils/gift'; +import { handleSend } from '@/common/utils/extrinsics'; export default function CreateGiftPage() { const router = useRouter(); diff --git a/src/screens/dashboard/main/DashboardMain.tsx b/src/screens/dashboard/main/DashboardMain.tsx index cf7301d9..f7704c75 100644 --- a/src/screens/dashboard/main/DashboardMain.tsx +++ b/src/screens/dashboard/main/DashboardMain.tsx @@ -10,11 +10,12 @@ import { useTelegram } from '@/common/providers/telegramProvider'; import { useBalances } from '@common/balances/BalanceProvider'; import { useChainRegistry } from '@common/chainRegistry'; import { getMnemonic, resetWallet } from '@common/wallet'; -import { claimGift, updateAssetsBalance } from '@/common/utils/balance'; +import { getTotalBalance, updateAssetsBalance } from '@/common/utils/balance'; import { ChainAssetAccount } from '@common/types'; import { IAssetBalance } from '@common/balances/types'; import { Paths } from '@/common/routing'; import { getPrice } from '@/common/utils/coingecko'; +import { claimGift } from '@/common/utils/extrinsics'; import { BodyText, CaptionText, Icon, AssetsList, Plate, Price, IconButton, LargeTitleText } from '@/components'; export const DashboardMain = () => { @@ -73,7 +74,7 @@ export const DashboardMain = () => { } subscribeBalance(account, (balance: IAssetBalance) => { - console.info(`${address} ${chain.name} => balance: ${balance.total().toString()}`); + console.info(`${address} ${chain.name} => balance: ${balance.total()}`); setAssets((prevAssets) => updateAssetsBalance(prevAssets, chain, balance)); }); } @@ -103,7 +104,7 @@ export const DashboardMain = () => { Total balance - +
router.push(Paths.TRANSFER)} />