Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the portal pen balance only for transferrable tokens #594

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/components/Wallet/modals/DisconnectModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ const WalletDropdownMenu = ({

export const DisconnectModal = () => {
const { walletAccount, removeWalletAccount } = useGlobalState();
const { query, balances } = useAccountBalance();
const { ss58Format, tokenSymbol } = useNodeInfoState().state;
const { wallet, address } = walletAccount || {};
const { total: balance } = balances;

const { query, balances } = useAccountBalance();
const { transferable: transferableBalance } = balances;

if (!address) return <></>;

Expand All @@ -89,15 +90,15 @@ export const DisconnectModal = () => {
<WalletButton
wallet={wallet}
query={query}
balance={balance}
balance={transferableBalance}
tokenSymbol={tokenSymbol}
walletAccount={walletAccount}
/>
<WalletDropdownMenu
walletAccount={walletAccount}
ss58Format={ss58Format}
address={address}
balance={balance}
balance={transferableBalance}
tokenSymbol={tokenSymbol}
removeWalletAccount={removeWalletAccount}
/>
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/substrate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import Big from 'big.js';
import { EventRecord } from '@polkadot/types/interfaces';
import { ApiPromise } from '@polkadot/api';
import { nativeToDecimal } from '../shared/parseNumbers/metric';

// Defined [here](https://github.com/pendulum-chain/pendulum/blob/63bd13abbe089308f9702925697dba22d7792eba/runtime/common/src/lib.rs#L55)
const EXISTENTIAL_DEPOSIT = nativeToDecimal(1_000_000_000);

// Calculate the transferable balance. It's calculated as `transferable = free - max(frozen - reserved, ED)`,
// see [here](https://wiki.polkadot.network/docs/learn-guides-accounts#query-account-data-in-polkadot-js).
export function calculateTransferableBalance(free: Big, frozen: Big, reserved: Big) {
// Emulate Math.max
const max = frozen.minus(reserved).gt(EXISTENTIAL_DEPOSIT) ? frozen.minus(reserved) : EXISTENTIAL_DEPOSIT;
return free.minus(max);
}

export function containsError(events: EventRecord[], api: ApiPromise): boolean {
const errorEvents = events
Expand Down
11 changes: 6 additions & 5 deletions src/shared/useAccountBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { emptyCacheKey, QueryOptions } from './helpers';
import { nativeToDecimal, prettyNumbers } from './parseNumbers/metric';
import { useSharedState } from './Provider';
import { cacheKeys } from '../constants/cache';
import { calculateTransferableBalance } from '../helpers/substrate';

export interface UseAccountBalanceResponse {
query: UseQueryResult<FrameSystemAccountInfo | undefined, unknown>;
Expand Down Expand Up @@ -50,11 +51,11 @@ export const useAccountBalance = (

const { free: freeRaw, frozen: frozenRaw, reserved: reservedRaw } = data.data;

const free = nativeToDecimal(freeRaw || 0, decimals).toNumber();
const frozen = nativeToDecimal(frozenRaw || 0, decimals).toNumber();
const reserved = nativeToDecimal(reservedRaw || 0, decimals).toNumber();
const total = prettyNumbers(free);
const transferable = prettyNumbers(free - frozen - reserved);
const free = nativeToDecimal(freeRaw || 0, decimals);
const frozen = nativeToDecimal(frozenRaw || 0, decimals);
const reserved = nativeToDecimal(reservedRaw || 0, decimals);
const total = prettyNumbers(free.toNumber());
const transferable = prettyNumbers(calculateTransferableBalance(free, frozen, reserved).toNumber());
return { total, transferable };
}, [data, decimals]);

Expand Down
Loading