From d930c1070a249697f2a10152a8da4342d4206867 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 1 Jun 2022 19:44:11 +0800 Subject: [PATCH] fix: staking max lock amount (#186) --- src/utils/token/tokenInfo.ts | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/utils/token/tokenInfo.ts b/src/utils/token/tokenInfo.ts index 21828125..f567ba5d 100644 --- a/src/utils/token/tokenInfo.ts +++ b/src/utils/token/tokenInfo.ts @@ -1,9 +1,26 @@ import { AccountData } from '@darwinia/types'; import { ApiPromise } from '@polkadot/api'; import { PalletBalancesBalanceLock } from '@polkadot/types/lookup'; -import { BN_ZERO, bnMax } from '@polkadot/util'; +import { BN_ZERO, bnMax, BN } from '@polkadot/util'; import { waitUntilConnected } from '../network'; +// eslint-disable-next-line +const calcMax = (lockItem: any, current: BN) => { + let max = current; + + if (lockItem.reasons && !lockItem.reasons.isFee) { + max = bnMax(lockItem.amount, max); + } else if (lockItem.lockReasons && !lockItem.lockReasons.isFee) { + if (lockItem.lockFor.isCommon) { + max = bnMax(lockItem.lockFor.asCommon.amount, max); + } else if (lockItem.lockFor.isStaking) { + max = bnMax(lockItem.lockFor.asStaking.stakingAmount, max); + } + } + + return max; +}; + /** * @description other api can get balances: api.derive.balances.all, api.query.system.account; * @see https://github.com/darwinia-network/wormhole-ui/issues/142 @@ -23,22 +40,18 @@ export async function getDarwiniaBalances(api: ApiPromise, account = ''): Promis const locks: PalletBalancesBalanceLock[] = await api.query.balances.locks(account); const ktonLocks: PalletBalancesBalanceLock[] = await api.query.kton.locks(account); - let maxRingLock = BN_ZERO; + let maxLock = BN_ZERO; let maxKtonLock = BN_ZERO; locks.forEach((item) => { - if (!item.reasons.isFee) { - maxRingLock = bnMax(item.amount, maxRingLock); - } + maxLock = calcMax(item, maxLock); }); ktonLocks.forEach((item) => { - if (!item.reasons.isFee) { - maxKtonLock = bnMax(item.amount, maxKtonLock); - } + maxKtonLock = calcMax(item, maxKtonLock); }); - return [free.sub(maxRingLock).toString(), freeKton.sub(maxKtonLock).toString()]; + return [free.sub(maxLock).toString(), freeKton.sub(maxKtonLock).toString()]; } catch (err) { console.error(err); return ['0', '0'];