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

Fix orml token balances calculation #77

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Changes from 1 commit
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
49 changes: 35 additions & 14 deletions src/services/balances/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getGenshiroTokens } from './genshiro'
import { getOrUpdatePropertiesByNetwork } from '../properties'
import { asTypesGenerator } from '../../utils'
import { bnMax } from '@polkadot/util'
import { newLogger } from '@subsocial/utils';
import { newLogger } from '@subsocial/utils'

const log = newLogger('Balances')

Expand Down Expand Up @@ -48,13 +48,18 @@ export const parseBalances = (
free?: BN,
reserved?: BN,
frozen?: BN,
locks: any[] = [],
other?: Record<string, any>
) => {
const totalBalance = reserved ? free.add(reserved) || 0 : free
const lockedBalance = locks.length && bnMax(...locks.map(({ amount }) => amount))

const freeBalance = lockedBalance ? free.sub(lockedBalance) : free

return {
freeBalance: free?.toString(),
freeBalance: freeBalance.toString(),
reservedBalance: reserved?.toString(),
lockedBalance: lockedBalance?.toString(),
frozen: frozen?.toString(),
totalBalance: totalBalance?.toString(),
...other
Expand All @@ -73,11 +78,19 @@ type TokenBalanceData = {
frozen?: BN
reserved?: BN
}
function defaultOrmlTokenGetter(
async function defaultOrmlTokenGetter(
api: ApiPromise,
{ account, token }: { account: string; token: any }
): Promise<TokenBalanceData> {
return api.query.tokens.accounts(account, token) as any
): Promise<{ balances: TokenBalanceData; locks?: any[] }> {
const [balances, locks] = await api.queryMulti([
[api.query.tokens.accounts, [account, token]],
[api.query.tokens.locks, [account, token]]
])

return {
balances: balances as any,
locks: locks as any
}
}

const customOrmlTokenGetter = asTypesGenerator<
Expand All @@ -94,13 +107,21 @@ const customOrmlTokenGetter = asTypesGenerator<
frozen: new BN(0)
}
balanceData[isFrozen ? 'frozen' : 'free'] = new BN(balance)
return balanceData
return { balances: balanceData }
},
'ormlTokens.accounts': (api, { account, token }) => {
return api.query.ormlTokens.accounts(account, token) as any
'ormlTokens.accounts': async (api, { account, token }) => {
const [balances, locks] = await api.queryMulti([
[api.query.tokens.accounts, [account, token]],
[api.query.tokens.locks, [account, token]]
])
return { balances: balances as any, locks: locks as any }
},
'tokens.accounts': (api, { account, token }) => {
return api.query.tokens.accounts(account, { XCM: token }) as any
'tokens.accounts': async (api, { account, token }) => {
const [balances, locks] = await api.queryMulti([
[api.query.tokens.accounts, [account, { XCM: token }]],
[api.query.tokens.locks, [account, { XCM: token }]]
])
return { balances: balances as any, locks: locks as any }
}
})
const ormlTokenGetterNetworkMapper: { [key: string]: keyof typeof customOrmlTokenGetter } = {
Expand All @@ -118,9 +139,9 @@ const ormlTokenGetterNetworkMapper: { [key: string]: keyof typeof customOrmlToke
phala: 'assets.account',
khala: 'assets.account',
altair: 'ormlTokens.accounts',
centrifuge: 'ormlTokens.accounts',
centrifuge: 'ormlTokens.accounts', // +
shadow: 'ormlTokens.accounts',
pendulum: 'tokens.accounts'
pendulum: 'tokens.accounts' // +
}

const getOrmlTokens: GetBalancesType = async (api, network, account, tokens) => {
Expand All @@ -138,8 +159,8 @@ const getOrmlTokens: GetBalancesType = async (api, network, account, tokens) =>
account: account,
token: isObject ? token.currency : { Token: token }
})
const { free, frozen, reserved } = balancesCodec as any
balances = parseBalances(free, reserved, frozen)
const { free, frozen, reserved } = balancesCodec.balances as any
balances = parseBalances(free, reserved, frozen, balancesCodec.locks)

tokenBalances[isObject ? token.symbol : token] = {
...balances
Expand Down
Loading