From acca0c4bc3849154e78f3de931e75c81541df678 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 23 Oct 2023 18:27:51 +0300 Subject: [PATCH] Fix parsing balances --- src/connections/networks/index.ts | 12 ++--- src/services/balances/utils.ts | 73 +++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/src/connections/networks/index.ts b/src/connections/networks/index.ts index c65a829..6d48939 100644 --- a/src/connections/networks/index.ts +++ b/src/connections/networks/index.ts @@ -101,7 +101,7 @@ export const kusamaParachains: Networks = { }, heiko: { name: 'Parallel Heiko', - ...resolveOnfinalityUrl('parallel-heiko'), + node: 'wss://heiko-rpc.parallel.fi', icon: 'parallel.svg', nativeToken: 'HKO', paraId: 2085, @@ -241,7 +241,7 @@ export const kusamaParachains: Networks = { }, bitCountry: { name: 'Bit.Country Pioneer', - ...resolveOnfinalityUrl('pioneer'), + node: 'wss://pioneer-rpc-3.bit.country/wss', icon: 'bitcountry.svg', paraId: 2096, vestingMethod: 'vesting.vest', @@ -342,7 +342,7 @@ export const kusamaParachains: Networks = { }, invArch: { name: 'InvArch Tinkernet', - ...resolveOnfinalityUrl('invarch-tinkernet'), + node: 'wss://tinkernet-rpc.dwellir.com', nativeToken: 'TNKR', paraId: 2125, icon: 'invArch.png', @@ -471,7 +471,7 @@ export const polkadotParachains: Networks = { }, parallel: { name: 'Parallel', - ...resolveOnfinalityUrl('parallel'), + node: 'wss://parallel-rpc.dwellir.com', icon: 'parallel.svg', paraId: 2012, vestingMethod: 'vesting.claim', @@ -519,7 +519,7 @@ export const polkadotParachains: Networks = { }, interlay: { name: 'Interlay', - ...resolveOnfinalityUrl('interlay'), + node: 'wss://interlay-rpc.dwellir.com', icon: 'interlay.svg', paraId: 2032, nativeToken: 'INTR', @@ -557,7 +557,7 @@ export const polkadotParachains: Networks = { hydradx: { name: 'HydraDX', icon: 'snakenet.svg', - ...resolveOnfinalityUrl('hydradx'), + node: 'wss://hydradx-rpc.dwellir.com', paraId: 2034, vestingMethod: 'vesting.claim', isTransferable: true, diff --git a/src/services/balances/utils.ts b/src/services/balances/utils.ts index 829a65d..c1620bc 100644 --- a/src/services/balances/utils.ts +++ b/src/services/balances/utils.ts @@ -3,56 +3,87 @@ import BN from 'bn.js' import { getGenshiroTokens } from './genshiro' import { getOrUpdatePropertiesByNetwork } from '../properties' import { asTypesGenerator } from '../../utils' +import { bnMax } from '@polkadot/util' export type TokenBalances = Record const getNativeTokenBalance = async (api: ApiPromise, account: string) => { - const { data: { feeFrozen, free, miscFrozen, reserved } }: any = - await api.query.system.account(account) + const { + data: { free, frozen, reserved } + }: any = await api.query.system.account(account) - return parseBalances(free, reserved, feeFrozen, { frozenMisc: miscFrozen?.toString() }) + const locks = await api.query.balances.locks(account) + + const lockedBalance = locks.length && bnMax(...(locks.map(({ amount }) => amount))) + + const totalBalance = reserved ? free.add(reserved) || 0 : free + + const freeBalance = lockedBalance ? free.sub(lockedBalance) : free + + return { + totalBalance: totalBalance?.toString(), + reservedBalance: reserved?.toString(), + frozenBalance: frozen?.toString(), + freeBalance: freeBalance?.toString(), + lockedBalance: lockedBalance?.toString() + } } type Balances = { freeBalance: string reservedBalance: string - frozenFee: string + frozen: string totalBalance: string } -export const parseBalances = (free?: BN, reserved?: BN, frozen?: BN, other?: Record) => { +export const parseBalances = ( + free?: BN, + reserved?: BN, + frozen?: BN, + other?: Record +) => { const totalBalance = reserved ? free.add(reserved) || 0 : free return { freeBalance: free?.toString(), reservedBalance: reserved?.toString(), - frozenFee: frozen?.toString(), + frozen: frozen?.toString(), totalBalance: totalBalance?.toString(), ...other } } -type GetBalancesType = (api: ApiPromise, network: string, account: string, tokens?: any[]) => Promise +type GetBalancesType = ( + api: ApiPromise, + network: string, + account: string, + tokens?: any[] +) => Promise type TokenBalanceData = { free?: BN frozen?: BN reserved?: BN } -function defaultOrmlTokenGetter (api: ApiPromise, { account, token }: { account: string, token: any }): Promise { +function defaultOrmlTokenGetter( + api: ApiPromise, + { account, token }: { account: string; token: any } +): Promise { return api.query.tokens.accounts(account, token) as any } const customOrmlTokenGetter = asTypesGenerator< - (...params: Parameters) => ReturnType + ( + ...params: Parameters + ) => ReturnType >()({ 'assets.account': async (api, { account, token }) => { const balancesCodec = await api.query.assets.account(token, account) const data = balancesCodec.toPrimitive() - const { isFrozen, balance } = data || {} as any + const { isFrozen, balance } = data || ({} as any) const balanceData: TokenBalanceData = { free: new BN(0), - frozen: new BN(0), + frozen: new BN(0) } balanceData[isFrozen ? 'frozen' : 'free'] = new BN(balance) return balanceData @@ -87,7 +118,8 @@ const ormlTokenGetterNetworkMapper: { [key: string]: keyof typeof customOrmlToke const getOrmlTokens: GetBalancesType = async (api, network, account, tokens) => { const tokenBalances: TokenBalances = {} - const balanceGetter = customOrmlTokenGetter[ormlTokenGetterNetworkMapper[network] ?? ''] ?? defaultOrmlTokenGetter + const balanceGetter = + customOrmlTokenGetter[ormlTokenGetterNetworkMapper[network] ?? ''] ?? defaultOrmlTokenGetter const balancePromise = tokens.map(async (token) => { try { let balances = {} as unknown as Balances @@ -113,10 +145,10 @@ const getOrmlTokens: GetBalancesType = async (api, network, account, tokens) => return tokenBalances } -const getOnlyOrmlTokens = async (api: ApiPromise, account: string, network: string) =>{ +const getOnlyOrmlTokens = async (api: ApiPromise, account: string, network: string) => { const props = await getOrUpdatePropertiesByNetwork(api, network) - const tokenSymbols = props?.tokenSymbols as string[] || [] - const tokens = props?.assetsRegistry as Record || {} + const tokenSymbols = (props?.tokenSymbols as string[]) || [] + const tokens = (props?.assetsRegistry as Record) || {} return getOrmlTokens(api, network, account, [...tokenSymbols, ...Object.values(tokens)]) } @@ -125,8 +157,8 @@ const getNativeAndOrmlTokens = async (api: ApiPromise, account: string, network: const tokenBalances: TokenBalances = {} const props = await getOrUpdatePropertiesByNetwork(api, network) - const tokenSymbols = props?.tokenSymbols as string[] || [] - const tokens = props?.assetsRegistry as Record || {} + const tokenSymbols = (props?.tokenSymbols as string[]) || [] + const tokens = (props?.assetsRegistry as Record) || {} const [native] = tokenSymbols tokenBalances[native] = await getNativeTokenBalance(api, account) @@ -138,7 +170,12 @@ const getNativeAndOrmlTokens = async (api: ApiPromise, account: string, network: } } -type GetOrmlBalancesType = (api: ApiPromise, account: string, network: string, tokens?: any[]) => Promise +type GetOrmlBalancesType = ( + api: ApiPromise, + account: string, + network: string, + tokens?: any[] +) => Promise const customFetchBalancesByNetwork: Record = { kintsugi: getOnlyOrmlTokens,