Skip to content

Commit

Permalink
Fix for unstaking max amount (#1421)
Browse files Browse the repository at this point in the history
* 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 <tae.gun7784@gmail.com>
  • Loading branch information
bobo-k2 and gtg7784 authored Dec 20, 2024
1 parent f79c109 commit 9430ca6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
25 changes: 15 additions & 10 deletions src/staking-v3/components/my-staking/ModalUnbondDapp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<span class="text--to--balance">
{{
$t('assets.modals.balance', {
amount: $n(truncate(maxAmount)),
amount: $n(truncate(maxAmountDisplay)),
token: nativeTokenSymbol,
})
}}
Expand Down Expand Up @@ -112,25 +112,26 @@ export default defineComponent({
);
const { constants, unstake, canUnStake, getStakerInfo } = useDappStaking();
const minStakingAmount = computed<number>(() =>
Number(ethers.utils.formatEther(constants.value?.minStakeAmount ?? 0))
);
const minStakingAmount = computed<bigint>(() => constants.value?.minStakeAmount ?? BigInt(0));
const isBelowThanMinStaking = computed<boolean>(() => {
return minStakingAmount.value > Number(maxAmount.value) - Number(amount.value);
});
const maxAmount = computed<string>(() => {
const maxAmount = computed<bigint>(() => {
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<string>(() => {
return ethers.utils.formatEther(maxAmount.value);
});
const amount = ref<string | null>(null);
const errorMessage = ref<string | undefined>();
const toMaxAmount = (): void => {
amount.value = truncate(maxAmount.value).toString();
amount.value = ethers.utils.formatEther(maxAmount.value.toString());
};
const inputHandler = (event: any): void => {
Expand All @@ -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;
Expand All @@ -166,6 +170,7 @@ export default defineComponent({
nativeTokenSymbol,
nativeTokenImg,
maxAmount,
maxAmountDisplay,
amount,
selectedTip,
nativeTipPrice,
Expand Down
35 changes: 19 additions & 16 deletions src/staking-v3/hooks/useDappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')];
Expand All @@ -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] => {
Expand Down

0 comments on commit 9430ca6

Please sign in to comment.