Skip to content

Fix frozen balance #84

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

Merged
merged 3 commits into from
Dec 4, 2024
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
5 changes: 5 additions & 0 deletions .changeset/tiny-geese-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@scio-labs/use-inkathon': patch
---

Fix frozen balance
19 changes: 13 additions & 6 deletions src/helpers/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const getBalance = async (
// Query the chain and parse data
const result: any = await api.query.system.account(address)
const balanceData = parseBalanceData(api, result?.data, formatterOptions)

return balanceData
}

Expand Down Expand Up @@ -84,11 +83,19 @@ const parseBalanceData = (
const balance = reservedBalance.add(freeBalance)

// Calculate the reducible balance (see: https://substrate.stackexchange.com/a/3009/3470)
const miscFrozenBalance: BN = new BN(data?.miscFrozen || 0)
const feeFrozenBalance: BN = new BN(data?.feeFrozen || 0)
const reducibleBalance = freeBalance.sub(
miscFrozenBalance.gt(feeFrozenBalance) ? miscFrozenBalance : feeFrozenBalance,
)
// (https://wiki.polkadot.network/docs/learn-guides-accounts#query-account-data-in-polkadot-js)
let reducibleBalance = new BN(0)

if (data?.frozen) {
const frozenBalance: BN = new BN(data?.frozen || 0)
reducibleBalance = freeBalance.sub(frozenBalance.sub(reservedBalance))
} else {
const miscFrozenBalance: BN = new BN(data?.miscFrozen || 0)
const feeFrozenBalance: BN = new BN(data?.feeFrozen || 0)
reducibleBalance = freeBalance.sub(
miscFrozenBalance.gt(feeFrozenBalance) ? miscFrozenBalance : feeFrozenBalance,
)
}

// Format the balance
const freeBalanceFormatted = formatBalance(api, freeBalance, formatterOptions)
Expand Down