From 9430ca669bf15f7105b3b9db41daa1462b1471c2 Mon Sep 17 00:00:00 2001 From: Bobo Date: Fri, 20 Dec 2024 10:41:00 +0100 Subject: [PATCH] Fix for unstaking max amount (#1421) * update polkadot chain icon (#1415) * Inflation chart fix (#1417) * Inflation chart fix * Current inflation calculation and cleanup * Minor bug fixes * Division by zero fix * Fix for unstaking max amount * Better handling unstake warning messages --------- Co-authored-by: Taegeon Alan Go --- .../components/my-staking/ModalUnbondDapp.vue | 25 +++++++------ src/staking-v3/hooks/useDappStaking.ts | 35 ++++++++++--------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/staking-v3/components/my-staking/ModalUnbondDapp.vue b/src/staking-v3/components/my-staking/ModalUnbondDapp.vue index 1379db174..c1037e8ea 100644 --- a/src/staking-v3/components/my-staking/ModalUnbondDapp.vue +++ b/src/staking-v3/components/my-staking/ModalUnbondDapp.vue @@ -13,7 +13,7 @@ {{ $t('assets.modals.balance', { - amount: $n(truncate(maxAmount)), + amount: $n(truncate(maxAmountDisplay)), token: nativeTokenSymbol, }) }} @@ -112,25 +112,26 @@ export default defineComponent({ ); const { constants, unstake, canUnStake, getStakerInfo } = useDappStaking(); - const minStakingAmount = computed(() => - Number(ethers.utils.formatEther(constants.value?.minStakeAmount ?? 0)) - ); + const minStakingAmount = computed(() => constants.value?.minStakeAmount ?? BigInt(0)); const isBelowThanMinStaking = computed(() => { return minStakingAmount.value > Number(maxAmount.value) - Number(amount.value); }); - const maxAmount = computed(() => { + const maxAmount = computed(() => { const selectedDappStakes = getStakerInfo(props.dapp.chain.address); - return selectedDappStakes - ? String(ethers.utils.formatEther(selectedDappStakes.staked.totalStake.toString())) - : '0'; + return selectedDappStakes ? selectedDappStakes.staked.totalStake : BigInt(0); + }); + + const maxAmountDisplay = computed(() => { + return ethers.utils.formatEther(maxAmount.value); }); + const amount = ref(null); const errorMessage = ref(); const toMaxAmount = (): void => { - amount.value = truncate(maxAmount.value).toString(); + amount.value = ethers.utils.formatEther(maxAmount.value.toString()); }; const inputHandler = (event: any): void => { @@ -146,7 +147,10 @@ export default defineComponent({ }; const canUnbond = () => { - const [result, message] = canUnStake(props.dapp.basic.address, Number(amount.value)); + const [result, message] = canUnStake( + props.dapp.basic.address, + ethers.utils.parseEther(amount.value ?? '0').toBigInt() + ); errorMessage.value = message; return result; @@ -166,6 +170,7 @@ export default defineComponent({ nativeTokenSymbol, nativeTokenImg, maxAmount, + maxAmountDisplay, amount, selectedTip, nativeTipPrice, diff --git a/src/staking-v3/hooks/useDappStaking.ts b/src/staking-v3/hooks/useDappStaking.ts index ec068e863..8697b82d3 100644 --- a/src/staking-v3/hooks/useDappStaking.ts +++ b/src/staking-v3/hooks/useDappStaking.ts @@ -487,15 +487,16 @@ export function useDappStaking() { return [true, '', '']; }; - const canUnStake = (dappAddress: string, amount: number): [boolean, string] => { - const unstakeAmount = BigInt(ethers.utils.parseEther(amount.toString()).toString()); + const canUnStake = (dappAddress: string, amount: bigint): [boolean, string] => { const dappInfo = getStakerInfo(dappAddress); const stakedAmount = dappInfo?.staked.totalStake ?? BigInt(0); const stakeInfo = getStakerInfo(dappAddress); + let message = ''; + if (amount <= 0) { return [false, t('stakingV3.dappStaking.ZeroAmount')]; - } else if (unstakeAmount > stakedAmount) { + } else if (amount > stakedAmount) { return [false, t('stakingV3.dappStaking.UnstakeAmountTooLarge')]; } else if (protocolState.value?.maintenance) { return [false, t('stakingV3.dappStaking.Disabled')]; @@ -508,31 +509,33 @@ export function useDappStaking() { (ledger.value?.unlocking?.length ?? 0) >= constants.value.maxUnlockingChunks ) { return [false, t('stakingV3.dappStaking.TooManyUnlockingChunks')]; - } else if (constants.value && constants.value.minStakeAmount > stakedAmount - unstakeAmount) { - // Handle unstaking all tokens. - return [ - true, - t('stakingV3.willUnstakeAll', { - amount: constants.value.minStakeAmountToken, - }), - ]; - } else if ( + } + + if ( stakeInfo?.loyalStaker && protocolState.value?.periodInfo.subperiod === PeriodType.BuildAndEarn && - stakeInfo.staked.totalStake - unstakeAmount < stakeInfo.staked.voting + stakeInfo.staked.totalStake - amount < stakeInfo.staked.voting ) { // Handle possibility to lose bonus rewards. - const message = + message = stakeInfo.staked.buildAndEarn > BigInt(0) ? t('stakingV3.loyalStakerWarningAmount', { amount: ethers.utils.formatEther(stakeInfo.staked.buildAndEarn), }) : t('stakingV3.loyalStakerWarning'); + } - return [true, message]; + if (constants.value && constants.value.minStakeAmount > stakedAmount - amount) { + // Handle un-staking all tokens. + message = + message + + ' ' + + t('stakingV3.willUnstakeAll', { + amount: constants.value.minStakeAmountToken, + }); } - return [true, '']; + return [true, message]; }; const canUnlock = (amount: number): [boolean, string] => {