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

Transferable vs available balance! address #1071 #1078

Merged
merged 9 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default function AccountDetails (): React.ReactElement {
/>
}
<DisplayBalance
amount={balancesToShow?.availableBalance}
amount={getValue('Transferable', balancesToShow)}
decimal={balancesToShow?.decimal}
isDarkTheme={isDarkTheme}
onClick={goToSend}
Expand Down
7 changes: 5 additions & 2 deletions packages/extension-polkagate/src/hooks/useAssetsBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const BN_MEMBERS = [
'vestedClaimable',
'vestingTotal',
'freeBalance',
'frozenBalance',
'frozenFee',
'frozenMisc',
'reservedBalance',
Expand Down Expand Up @@ -96,7 +97,7 @@ const BALANCE_VALIDITY_PERIOD = 2 * 1000 * 60;

const isUpToDate = (date?: number): boolean | undefined => date ? Date.now() - date < BALANCE_VALIDITY_PERIOD : undefined;

function allHexToBN (balances: string | undefined): BalancesDetails | {} {
function allHexToBN (balances: string | undefined): BalancesDetails | unknown {
if (!balances) {
return {};
}
Expand All @@ -105,7 +106,9 @@ function allHexToBN (balances: string | undefined): BalancesDetails | {} {
const _balances = {} as BalancesDetails;

Object.keys(parsedBalances).forEach((item) => {
_balances[item] = isHexToBn(parsedBalances[item] as string);
if (parsedBalances[item] !== 'undefined') {
_balances[item] = isHexToBn(parsedBalances[item] as string);
}
Nick-1979 marked this conversation as resolved.
Show resolved Hide resolved
});

return _balances;
Expand Down
25 changes: 20 additions & 5 deletions packages/extension-polkagate/src/hooks/useBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,24 @@ export default function useBalances (address: string | undefined, refresh?: bool

const ED = api.consts.balances.existentialDeposit as unknown as BN;

formatted && api.derive.balances?.all(formatted).then((b) => {
setNewBalances({ ...b, ED, chainName, date: Date.now(), decimal, genesisHash: api.genesisHash.toString(), token });
setRefresh && setRefresh(false);
isFetching.fetching[String(formatted)].balances = false;
isFetching.set(isFetching.fetching);
formatted && api.derive.balances?.all(formatted).then((allBalances) => {
api.query.system.account(formatted).then(({ data: systemBalance }) => {
const frozenBalance = systemBalance.frozen as BN;

setNewBalances({
ED,
...allBalances,
chainName,
date: Date.now(),
decimal: api.registry.chainDecimals[0],
frozenBalance,
genesisHash: api.genesisHash.toString(),
token: api.registry.chainTokens[0]
});
setRefresh && setRefresh(false);
isFetching.fetching[String(formatted)].balances = false;
isFetching.set(isFetching.fetching);
}).catch(console.error);
}).catch(console.error);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [api, chain?.genesisHash, chainName, formatted, isFetching.fetching[String(formatted)]?.length, setRefresh]);
Expand Down Expand Up @@ -190,6 +203,7 @@ export default function useBalances (address: string | undefined, refresh?: bool
const balances = {
availableBalance: overall.availableBalance.toString(),
freeBalance: overall.freeBalance.toString(),
frozenBalance: overall.frozenBalance.toString(),
lockedBalance: overall.lockedBalance.toString(),
pooledBalance: overall.pooledBalance.toString(),
reservedBalance: overall.reservedBalance.toString(),
Expand Down Expand Up @@ -223,6 +237,7 @@ export default function useBalances (address: string | undefined, refresh?: bool
date: savedBalances[chainName].date,
decimal: savedBalances[chainName].decimal,
freeBalance: new BN(sb.freeBalance),
frozenBalance: new BN(sb.frozenBalance),
lockedBalance: new BN(sb.lockedBalance),
pooledBalance: new BN(sb.pooledBalance),
reservedBalance: new BN(sb.reservedBalance),
Expand Down
10 changes: 4 additions & 6 deletions packages/extension-polkagate/src/popup/account/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ export const getValue = (type: string, balances: BalancesInfo | null | undefined
return balances?.soloTotal ?? BN_ZERO;
case ('balance'):
case ('available'):
case ('transferable'):
case ('available balance'):
return balances.availableBalance;
case ('transferable'):
return balances.reservedBalance.gte(balances.frozenBalance || BN_ZERO)
? balances.freeBalance
: balances.freeBalance.sub(balances.frozenBalance.sub(balances.reservedBalance));
case ('reserved'):
return balances.reservedBalance;
case ('others'):
Expand All @@ -43,10 +46,6 @@ export const getValue = (type: string, balances: BalancesInfo | null | undefined
return balances.freeBalance;
case ('reserved balance'):
return balances.reservedBalance;
// case ('frozen misc'):
// return balances.frozenMisc;
// case ('frozen fee'):
// return balances.frozenFee;
case ('locked'):
case ('locked balance'):
return balances.lockedBalance;
Expand All @@ -64,4 +63,3 @@ export const getValue = (type: string, balances: BalancesInfo | null | undefined
return undefined;
}
};

1 change: 1 addition & 0 deletions packages/extension-polkagate/src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ export interface BalancesInfo extends DeriveBalancesAll {
token: string;
date: number;
pooledBalance?: BN;
frozenBalance: BN;
soloTotal?: BN;
genesisHash: string;
ED: BN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ async function getAssetOnAssetHub (addresses, assetsToBeFetched, chainName) {
availableBalance: isFrozen ? 0 : _balance,
chainName,
decimal,
freeBalance: isFrozen ? 0 : _balance, // JUST to comply with the rule ...
frozenBalance: isFrozen ? balance : 0, // JUST to comply with the rule that total=available + reserve
genesisHash: api.genesisHash.toString(),
isAsset: true,
lockedBalance: isFrozen ? _balance : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { TEST_NETS } from '../constants';
import getPoolAccounts from '../getPoolAccounts';
import { balancify, closeWebsockets, fastestEndpoint, getChainEndpoints } from './utils';


async function getPooledBalance (api, address) {
const response = await api.query.nominationPools.poolMembers(address);
const member = response && response.unwrapOr(undefined);
Expand Down Expand Up @@ -55,6 +54,10 @@ async function getBalances (chainName, addresses) {
if (api.isConnected && api.derive.balances) {
const requests = addresses.map(async (address) => {
const balances = await api.derive.balances.all(address);
const systemBalance = await api.query.system.account(address);

balances.frozenBalance = systemBalance.frozen;

let soloTotal = BN_ZERO;
let pooledBalance = BN_ZERO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function balancify (balances) {
return JSON.stringify({
availableBalance: String(balances.availableBalance),
freeBalance: String(balances.freeBalance),
frozenBalance: String(balances.frozen),
lockedBalance: String(balances.lockedBalance),
reservedBalance: String(balances.reservedBalance),
vestedBalance: String(balances.vestedBalance),
Expand All @@ -18,6 +19,7 @@ export function balancify (balances) {
export function balancifyAsset (balances) {
return JSON.stringify({
availableBalance: String(balances.free),
freeBalance: String(balances.free),
frozenBalance: String(balances.frozen),
reservedBalance: String(balances.reserved)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export async function toGetNativeToken (addresses, api, chainName) {

const balances = await Promise.all(addresses.map((address) => api.derive.balances.all(address)));

const systemBalance = await Promise.all(addresses.map((address) => api.query.system.account(address)));

addresses.forEach((address, index) => {
balances[index].frozenBalance = systemBalance[index].frozen;

const totalBalance = balances[index].freeBalance.add(balances[index].reservedBalance);

if (totalBalance.isZero()) {
Expand All @@ -27,4 +31,4 @@ export async function toGetNativeToken (addresses, api, chainName) {
});

return _result;
}
}
Loading