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

feat: Prevents Errors from Occuring #1026

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/components/common/themes/DividerNewYear.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ export default defineComponent({
#793b02 104.05%
);
}
</style>
</style>
2 changes: 1 addition & 1 deletion src/components/common/themes/DividerNewYearMobile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export default defineComponent({
#793b02 104.05%
);
}
</style>
</style>
2 changes: 1 addition & 1 deletion src/components/common/themes/LunarPack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ export default defineComponent({
height: 240px;
z-index: 9999;
}
</style>
</style>
2 changes: 1 addition & 1 deletion src/components/common/themes/SnowPack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,4 @@ export default defineComponent({
}
}
}
</style>
</style>
2 changes: 1 addition & 1 deletion src/components/dapp-staking/my-staking/BannerArea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export default defineComponent({
gap: 24px;
}
}
</style>
</style>
12 changes: 11 additions & 1 deletion src/i18n/en-US/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,19 @@ export default {
},
stakingV3: {
title: 'dApp Staking v3',
amountGreater0: 'Staking amount must be greater than 0.',
amountGreater0: 'Amount must be greater than 0.',
periodEndsNextEra: 'Period ends in the next era.',
stakerRewardsExpired: 'Staker rewards expired.',
disabled: 'Pallet is disabled/in maintenance mode.',
unstakeFromPastPeriod:
'Unstaking is rejected since the period in which past stake was active has passed.',
unstakeAmountTooLarge: 'Unstake amount is greater than the staked amount.',
unclaimedRewardsFromPastPeriods:
'There are unclaimed rewards remaining from past periods. They should be claimed before staking again.',
tooManyStakedContracts:
'There are too many contract stake entries for the account. This can be cleaned up by either unstaking or cleaning expired entries.',
unavailableStakeFunds:
'The staking amount surpasses the current balance available for staking.',
successfullyStaked: 'You successfully staked to {contractAddress}',
},
bridge: {
Expand Down
60 changes: 57 additions & 3 deletions src/staking-v3/hooks/useDappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
} from '../logic';
import { Symbols } from 'src/v2/symbols';
import { useStore } from 'src/store';
import { useAccount } from 'src/hooks';
import { useAccount, useBalance } from 'src/hooks';
import { useI18n } from 'vue-i18n';
import { useDapps } from './useDapps';
import { ethers } from 'ethers';
import BN from 'bn.js';

export function useDappStaking() {
const { t } = useI18n();
Expand All @@ -23,14 +25,16 @@ export function useDappStaking() {
const { currentAccount } = useAccount();
const { registeredDapps } = useDapps();

const { balance, isLoadingBalance } = useBalance(currentAccount);

const protocolState = computed<ProtocolState | undefined>(
() => store.getters['stakingV3/getProtocolState']
);
const ledger = computed<AccountLedger | undefined>(() => store.getters['stakingV3/getLedger']);
const rewards = computed<Rewards | undefined>(() => store.getters['stakingV3/getRewards']);

const stake = async (dappAddress: string, amount: number): Promise<void> => {
const [result, error] = canStake(amount);
const [result, error] = await canStake(amount);
if (!result) {
throw error;
}
Expand All @@ -43,6 +47,11 @@ export function useDappStaking() {
};

const unstake = async (dappAddress: string, amount: number): Promise<void> => {
const [result, error] = await canUnStake(amount);
if (!result) {
throw error;
}

const stakingService = container.get<IDappStakingService>(Symbols.DappStakingServiceV3);
await stakingService.unstakeAndUnlock(dappAddress, amount, currentAccount.value, 'success');
};
Expand Down Expand Up @@ -107,10 +116,29 @@ export function useDappStaking() {
)?.chain.address;
};

const canStake = (amount: number): [boolean, string] => {
const canStake = async (amount: number): Promise<[boolean, string]> => {
const stakeAmount = new BN(ethers.utils.parseEther(amount.toString()).toString());
const stakingRepo = container.get<IDappStakingRepository>(Symbols.DappStakingRepositoryV3);
const constants = await stakingRepo.getConstants();

if (amount <= 0) {
// Prevents ZeroAmount
return [false, t('stakingV3.amountGreater0')];
} else if ((ledger.value?.contractStakeCount ?? 0) >= constants.maxNumberOfStakedContracts) {
// Prevents TooManyStakedContracts
return [false, t('stakingV3.tooManyStakedContracts')];
} else if (canClaimDappRewards() || canClaimStakerRewards() || canClaimBonusRewards()) {
// Prevents UnclaimedRewardsFromPastPeriods
// May want to auto claim rewards here
return [false, t('stakingV3.unclaimedRewardsFromPastPeriods')];
} else if (protocolState.value?.maintenance) {
// Prevents Disabled
return [false, t('stakingV3.disabled')];
} else if (stakeAmount.gt(balance.value)) {
// Prevents UnavailableStakeFunds
return [false, t('stakingV3.unavailableStakeFunds')];
} else if (
// Prevents PeriodEndsInNextEra
protocolState.value?.periodInfo.subperiod === PeriodType.BuildAndEarn &&
protocolState.value.periodInfo.subperiodEndEra <= protocolState.value.era + 1
) {
Expand All @@ -120,6 +148,31 @@ export function useDappStaking() {
return [true, ''];
};

const canUnStake = async (amount: number): Promise<[boolean, string]> => {
const stakeAmount = new BN(ethers.utils.parseEther(amount.toString()).toString());
const stakedAmount = new BN(ledger.value?.locked?.toString() ?? 0);

if (amount <= 0) {
// Prevents ZeroAmount
return [false, t('stakingV3.amountGreater0')];
} else if (stakeAmount.gt(stakedAmount)) {
// Prevents UnstakeAmountTooLarge
return [false, t('stakingV3.unstakeAmountTooLarge')];
} else if (canClaimDappRewards() || canClaimStakerRewards() || canClaimBonusRewards()) {
// Prevents UnclaimedRewardsFromPastPeriods
// May want to auto claim rewards here
return [false, t('stakingV3.unclaimedRewardsFromPastPeriods')];
} else if (protocolState.value?.maintenance) {
// Prevents Disabled
return [false, t('stakingV3.disabled')];
} else if (!amount) {
// Prevents UnstakeFromPastPeriod
return [false, t('stakingV3.unstakeFromPastPeriod')];
}

return [true, ''];
};

watch(
currentNetworkIdx,
async () => {
Expand All @@ -140,6 +193,7 @@ export function useDappStaking() {
unstake,
claimStakerRewards,
canStake,
canUnStake,
claimDappRewards,
claimBonusRewards,
getAllRewards,
Expand Down
1 change: 1 addition & 0 deletions src/staking-v3/logic/models/DappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface EraReward {
export interface Constants {
eraRewardSpanLength: number;
rewardRetentionInPeriods: number;
maxNumberOfStakedContracts: number;
}

export interface DAppTierRewards {
Expand Down
3 changes: 3 additions & 0 deletions src/staking-v3/logic/repositories/DappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ export class DappStakingRepository implements IDappStakingRepository {
return {
eraRewardSpanLength: (<u32>api.consts.dappStaking.eraRewardSpanLength).toNumber(),
rewardRetentionInPeriods: (<u32>api.consts.dappStaking.rewardRetentionInPeriods).toNumber(),
maxNumberOfStakedContracts: (<u32>(
api.consts.dappStaking.maxNumberOfStakedContracts
)).toNumber(),
};
}

Expand Down
Loading