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

Show estimated staker + bonus rewards on vote info banner #1329

Merged
merged 2 commits into from
Jun 20, 2024
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
1 change: 1 addition & 0 deletions src/i18n/en-US/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ export default {
utilityDescription:
'Stake to essential projects that enhance the productivity and functionality of Web3.',
othersDescription: 'Explore a diverse set of blockchain dApps.',
basicAprPlusBonus: 'Basic APR + Bonus',
},
bridge: {
selectBridge: 'Select a Bridge',
Expand Down
12 changes: 8 additions & 4 deletions src/staking-v3/components/PeriodInfoVote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
</div>
<div class="reward-info">
<div class="reward-label">
<div>{{ $t('stakingV3.estimatedRewards') }}</div>
<div>{{ $t('stakingV3.basicAprPlusBonus') }}</div>
<div>{{ $t('stakingV3.ifYouStakeNow') }}</div>
</div>
<div class="percentage">
<span>15<small>%+</small></span>
<span>{{ totalApr.toFixed(1) }}<small>%</small></span>
</div>
</div>
</div>
Expand All @@ -54,8 +54,8 @@
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { useDappStaking, useDappStakingNavigation, useVotingCountdown } from '../hooks';
import { defineComponent, computed } from 'vue';
import { useAprV3, useDappStaking, useDappStakingNavigation, useVotingCountdown } from '../hooks';
import { PeriodType } from 'src/staking-v3/logic';
import TokenBalanceNative from 'src/components/common/TokenBalanceNative.vue';

Expand All @@ -67,12 +67,16 @@ export default defineComponent({
const { protocolState, totalStakerRewards, claimStakerAndBonusRewards } = useDappStaking();
const { navigateToAssets } = useDappStakingNavigation();
const { timeLeftFormatted } = useVotingCountdown();
const { stakerApr, bonusApr } = useAprV3({ isWatch: true });

const totalApr = computed<number>(() => stakerApr.value + bonusApr.value);

return {
protocolState,
timeLeftFormatted,
PeriodType,
totalStakerRewards,
totalApr,
navigateToAssets,
claimStakerAndBonusRewards,
};
Expand Down
23 changes: 19 additions & 4 deletions src/staking-v3/logic/services/DappStakingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
DappInfo,
DappStakeInfo,
DappState,
EraInfo,
EraLengths,
PeriodType,
ProtocolState,
SingularStakingInfo,
StakeAmount,
StakerRewards,
Expand Down Expand Up @@ -701,15 +704,16 @@ export class DappStakingService extends SignerService implements IDappStakingSer

// @inheritdoc
public async getStakerApr(block?: number): Promise<number> {
const [totalIssuance, inflationParams, eraLengths, eraInfo] = await Promise.all([
const [totalIssuance, inflationParams, eraLengths, eraInfo, protocolState] = await Promise.all([
this.balancesRepository.getTotalIssuance(block),
this.inflationRepository.getInflationParams(block),
this.dappStakingRepository.getEraLengths(block),
this.dappStakingRepository.getCurrentEraInfo(block),
this.dappStakingRepository.getProtocolState(block),
]);

const cyclesPerYear = this.getCyclesPerYear(eraLengths);
const currentStakeAmount = weiToToken(eraInfo.currentStakeAmount.totalStake);
const currentStakeAmount = this.getStakeAmount(protocolState, eraInfo);

const stakedPercent = currentStakeAmount / weiToToken(totalIssuance);
const { baseStakersPart, adjustableStakersPart, idealStakingRate, maxInflationRate } =
Expand All @@ -728,18 +732,19 @@ export class DappStakingService extends SignerService implements IDappStakingSer
simulatedVoteAmount: number = 1000,
block?: number
): Promise<{ value: number; simulatedBonusPerPeriod: number }> {
const [inflationConfiguration, eraLengths, eraInfo] = await Promise.all([
const [inflationConfiguration, eraLengths, eraInfo, protocolState] = await Promise.all([
this.inflationRepository.getInflationConfiguration(block),
this.dappStakingRepository.getEraLengths(block),
this.dappStakingRepository.getCurrentEraInfo(block),
this.dappStakingRepository.getProtocolState(block),
]);

const cyclesPerYear = this.getCyclesPerYear(eraLengths);

const formattedBonusRewardsPoolPerPeriod = weiToToken(
inflationConfiguration.bonusRewardPoolPerPeriod
);
const voteAmount = weiToToken(eraInfo.currentStakeAmount.voting);
const voteAmount = this.getStakeAmount(protocolState, eraInfo);
const bonusPercentPerPeriod = formattedBonusRewardsPoolPerPeriod / voteAmount;
const simulatedBonusPerPeriod = simulatedVoteAmount * bonusPercentPerPeriod;
const periodsPerYear = eraLengths.periodsPerCycle * cyclesPerYear;
Expand Down Expand Up @@ -822,4 +827,14 @@ export class DappStakingService extends SignerService implements IDappStakingSer
): boolean {
return stakedPeriod < currentPeriod - rewardRetentionInPeriods;
}

private getStakeAmount(protocolState: ProtocolState, eraInfo: EraInfo): number {
const currentStakeAmount = weiToToken(
protocolState.periodInfo.subperiod === PeriodType.Voting
? eraInfo.nextStakeAmount?.totalStake ?? BigInt(0)
: eraInfo.currentStakeAmount.totalStake
);

return currentStakeAmount;
}
}
Loading