Skip to content

Commit

Permalink
fix: calculate spendable balance properly
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosala committed Aug 28, 2024
1 parent da101a1 commit 2a32b6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.18306352226980812337",
"version": "0.1.0-autogenerated.18031542027992032156",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
38 changes: 26 additions & 12 deletions src/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
dotAh,
} from "@polkadot-api/descriptors"
import { Binary, Enum, SS58String } from "polkadot-api"
import { map } from "rxjs"
import { combineLatest, from, map } from "rxjs"

const encodeAccount = AccountId().enc

Expand Down Expand Up @@ -90,20 +90,34 @@ export const fromAssetHubToForeign = (

type GenericApi = TypedApi<typeof dotAh>

export const watchAccoutFreeBalance =
(api: {
query: {
System: {
Account: {
watchValue: GenericApi["query"]["System"]["Account"]["watchValue"]
}
export const watchAccoutFreeBalance = (api: {
constants: {
Balances: {
ExistentialDeposit: () => Promise<bigint>
}
}
query: {
System: {
Account: {
watchValue: GenericApi["query"]["System"]["Account"]["watchValue"]
}
}
}) =>
(account: SS58String) =>
api.query.System.Account.watchValue(account, "best").pipe(
map(({ data }) => data.free - data.frozen),
}
}) => {
const edPromise = api.constants.Balances.ExistentialDeposit()
return (account: SS58String) =>
combineLatest([
from(edPromise),
api.query.System.Account.watchValue(account, "best"),
]).pipe(
map(([ed, { data }]) => {
const spendableBalance =
data.free -
(data.frozen - data.reserved > ed ? data.frozen - data.reserved : ed)
return spendableBalance > 0n ? spendableBalance : 0n
}),
)
}

export const watchForeingAssetAccoutFreeBalance =
(
Expand Down
2 changes: 2 additions & 0 deletions whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const dotWhitelist: DotWhitelistEntry[] = [
"tx.XcmPallet.limited_teleport_assets",
"tx.XcmPallet.limited_reserve_transfer_assets",
"query.System.Account",
"const.Balances.ExistentialDeposit",
]

const ahWhitelist: DotAhWhitelistEntry[] = [
"tx.PolkadotXcm.limited_teleport_assets",
"tx.PolkadotXcm.limited_reserve_transfer_assets",
"query.ForeignAssets.Account",
"const.Balances.ExistentialDeposit",
]

export const whitelist = [...dotWhitelist, ...ahWhitelist]

0 comments on commit 2a32b6f

Please sign in to comment.