Skip to content

Commit

Permalink
Release hotfix to main (#1391)
Browse files Browse the repository at this point in the history
* Remove Age of Chronos promotion (#1386)

* Fetch block time from chain (#1384)

* Fetch block time to store

* Apr calculation fix

* Type fix

* PR comments fix and code cleanup

* Fix/selected dapp index (#1387)

* Voting wizard search bug fix

* Keep selected dApps while switching categories

* APR calculation fix to use block time for a given block (#1389)

* APR calculation fix to use block time for a given block

* Small fix
  • Loading branch information
bobo-k2 authored Sep 30, 2024
1 parent 0192225 commit 2d6d473
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 74 deletions.
Binary file removed public/images/dapp_promotions/age_of_chronos.png
Binary file not shown.
5 changes: 4 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
import { setCurrentWallet } from 'src/v2/app.container';
import { container } from 'src/v2/common';
import { Symbols } from 'src/v2/symbols';
import { useAccount, useAppRouter } from 'src/hooks';
import { useAccount, useAppRouter, useBlockTime } from 'src/hooks';
import { ETHEREUM_EXTENSION } from 'src/modules/account';
import { LOCAL_STORAGE } from 'src/config/localStorage';
import {
Expand Down Expand Up @@ -98,6 +98,7 @@ export default defineComponent({
} = useDappStaking();
const { fetchStakeAmountsToStore, fetchDappsToStore } = useDapps();
const { fetchActiveConfigurationToStore, fetchInflationParamsToStore } = useInflation();
const { fetchBlockTimeToStore } = useBlockTime();
const isLoading = computed(() => store.getters['general/isLoading']);
const showAlert = computed(() => store.getters['general/showAlert']);
const isEthWallet = computed<boolean>(() => store.getters['general/isEthWallet']);
Expand Down Expand Up @@ -150,6 +151,8 @@ export default defineComponent({
.get<IDappStakingRepositoryV3>(Symbols.DappStakingRepositoryV3)
.startProtocolStateSubscription();
}
fetchBlockTimeToStore();
});
eventAggregator.subscribe(ProtocolStateChangedMessage.name, async (m) => {
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export enum HttpCodes {
NotFound = 404,
}

export const ORIGINAL_BLOCK_TIME = 12;

export const PERIOD1_START_BLOCKS = new Map<string, number>([
['astar', 5514935],
['shiden', 5876079],
Expand Down
6 changes: 0 additions & 6 deletions src/data/dapp_promotions.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@
"link": "https://www.apexchimpz.com/dapp-on-astar",
"img": "images/dapp_promotions/apeXchimpz.jpg"
},
{
"name": "Age Of Chronos",
"shortDescription": "Stake and earn FREE $Astr and nfts! Play and craft NFTs 2.0!",
"link": "https://twitter.com/SFY_Labs",
"img": "images/dapp_promotions/age_of_chronos.png"
},
{
"name": "Algem - Liquid Staking",
"shortDescription": "Receive one liquid nASTR token for every ASTR token you stake. Then use nASTR to yield farm on the ecosystem to maximize your earnings.",
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export * from './bridge/useLayerZeroBridge';
export * from './useTokenDistribution';
export * from './useTokenCirculation';
export * from './useInflation';
export * from './useBlockTime';
20 changes: 20 additions & 0 deletions src/hooks/useBlockTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { computed } from 'vue';
import { useStore } from 'src/store';
import { container } from 'src/v2/common';
import { ISystemRepository } from 'src/v2/repositories';
import { Symbols } from 'src/v2/symbols';

export function useBlockTime() {
const store = useStore();
const blockTimeInSeconds = computed<number | undefined>(
() => store.getters['general/getBlockTimeInSeconds']
);

const fetchBlockTimeToStore = async () => {
const systemRepository = container.get<ISystemRepository>(Symbols.SystemRepository);
const blockTime = await systemRepository.getBlockTimeInSeconds();
store.commit('general/setBlockTime', blockTime);
};

return { blockTimeInSeconds, fetchBlockTimeToStore };
}
12 changes: 6 additions & 6 deletions src/staking-v3/components/vote/VotingWizard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@
<script lang="ts">
import { defineComponent, ref, computed, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { WizardItem } from './types';
import { DappVote, mapToDappVote } from '../../logic';
import type { WizardItem } from './types';
import { type DappVote, mapToDappVote } from '../../logic';
import { useSelectableComponent, useVote, useDapps, useDappStaking } from 'src/staking-v3/hooks';
import ChooseAmountsPanel, { StakeInfo } from './enter-amount/ChooseAmountsPanel.vue';
import ChooseAmountsPanel, { type StakeInfo } from './enter-amount/ChooseAmountsPanel.vue';
import ReviewPanel from './review/ReviewPanel.vue';
import ChooseDappsPanel from './choose-dapps/ChooseDappsPanel.vue';
import WizardSteps from './WizardSteps.vue';
Expand All @@ -87,9 +87,9 @@ import ModalRestake from './re-stake/ModalRestake.vue';
import { useNetworkInfo } from 'src/hooks';
enum Steps {
ChooseDapps,
AddAmount,
Review,
ChooseDapps = 0,
AddAmount = 1,
Review = 2,
}
export default defineComponent({
Expand Down
12 changes: 6 additions & 6 deletions src/staking-v3/components/vote/choose-dapps/ChooseDappsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<div class="main-container">
<dapp-search :title="title" :search-term="searchTerm" :on-search="handleSearch" />
<choose-category
v-if="currentView === View.Category"
v-show="currentView === View.Category"
:on-category-selected="handleCategorySelected"
/>
<dapps-list
v-if="currentView === View.Dapps"
v-show="currentView === View.Dapps"
:dapps="dapps"
:category="currentCategory"
:filter="searchTerm"
Expand All @@ -23,18 +23,18 @@
</div>
</template>
<script lang="ts">
import { defineComponent, computed, ref, PropType } from 'vue';
import { defineComponent, computed, ref, type PropType } from 'vue';
import DappsList from './DappsList.vue';
import ChooseCategory from './ChooseCategory.vue';
import DappSearch from './DappSearch.vue';
import GoBackButton from '../GoBackButton.vue';
import { DappVote, mapToDappVote } from '../../../logic';
import { type DappVote, mapToDappVote } from '../../../logic';
import { useDapps } from 'src/staking-v3/hooks';
import { useI18n } from 'vue-i18n';
enum View {
Category,
Dapps,
Category = 0,
Dapps = 1,
}
export default defineComponent({
Expand Down
2 changes: 1 addition & 1 deletion src/staking-v3/components/vote/choose-dapps/DappSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue';
import { defineComponent, type PropType } from 'vue';
export default defineComponent({
props: {
Expand Down
29 changes: 18 additions & 11 deletions src/staking-v3/components/vote/choose-dapps/DappsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
</template>
<script lang="ts">
import { defineComponent, PropType, ref, computed } from 'vue';
import { defineComponent, type PropType, ref, computed } from 'vue';
import { useDappStaking } from 'src/staking-v3/hooks';
import DappIcon from '../DappIcon.vue';
import { DappVote } from '../../../logic';
import type { DappVote } from '../../../logic';
import FormatBalance from 'src/components/common/FormatBalance.vue';
// Import Swiper
Expand Down Expand Up @@ -97,13 +97,16 @@ export default defineComponent({
}
if (props.category) {
return props.dapps.filter((dapp) => categoryFilter(dapp, props.category));
} else if (props.filter) {
}
if (props.filter) {
return props.dapps.filter((dapp) =>
dapp.name.toLowerCase().includes(props.filter.toLowerCase())
);
} else {
return props.dapps;
}
return props.dapps;
});
const selectedIndexes = ref<number[]>([]);
Expand All @@ -112,29 +115,33 @@ export default defineComponent({
() => constants.value?.maxNumberOfStakedContracts ?? 0
);
const globalDappIndex = (filteredIndex: number): number =>
props.dapps.findIndex((dapp) => dapp === filteredDapps.value[filteredIndex]);
const handleItemSelected = (index: number): void => {
const indexToRemove = selectedIndexes.value.indexOf(index);
const dappIndex = globalDappIndex(index);
const indexToRemove = selectedIndexes.value.indexOf(dappIndex);
if (indexToRemove >= 0) {
selectedIndexes.value.splice(indexToRemove, 1);
} else {
if (selectedIndexes.value.length <= maxDappsToSelect.value) {
selectedIndexes.value.push(index);
selectedIndexes.value.push(dappIndex);
}
}
if (props.onDappsSelected) {
props.onDappsSelected(
selectedIndexes.value.map((selectedIndex) => filteredDapps.value[selectedIndex])
selectedIndexes.value.map((selectedIndex) => props.dapps[selectedIndex])
);
}
};
const getSelectionOrder = (index: number): number | undefined => {
const number = selectedIndexes.value.indexOf(index) + 1;
const number = selectedIndexes.value.indexOf(globalDappIndex(index)) + 1;
return number === 0 ? undefined : number;
};
const isItemSelected = (index: number): boolean => selectedIndexes.value.includes(index);
const isItemSelected = (index: number): boolean => selectedIndexes.value.includes(globalDappIndex(index));
return {
modules: [Grid, Navigation],
Expand Down
19 changes: 1 addition & 18 deletions src/staking-v3/hooks/useAprV3.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { u128 } from '@polkadot/types';
import { $api } from 'boot/api';
import { ethers } from 'ethers';
import { container } from 'src/v2/common';
import { IInflationRepository } from 'src/v2/repositories';
import { Symbols } from 'src/v2/symbols';
import { computed, ref, watch } from 'vue';
import { EraInfo, EraLengths, IDappStakingService, InflationParam } from '../logic';
import { IDappStakingService } from '../logic';
import { useDappStaking } from './useDappStaking';
import { weiToToken } from 'src/token-utils';

export const useAprV3 = ({ isWatch }: { isWatch: boolean }) => {
const stakerApr = ref<number>(0);
Expand All @@ -16,19 +12,6 @@ export const useAprV3 = ({ isWatch }: { isWatch: boolean }) => {

const periodsPerCycle = computed<number>(() => eraLengths.value.periodsPerCycle);

const getCyclePerYear = (eraLength: EraLengths): number => {
const secBlockProductionRate = 12;
const secsOneYear = 365 * 24 * 60 * 60;
const periodLength =
eraLength.standardErasPerBuildAndEarnPeriod + eraLength.standardErasPerVotingPeriod;

const eraPerCycle = periodLength * periodsPerCycle.value;
const blocksStandardEraLength = eraLength.standardEraLength;
const blockPerCycle = blocksStandardEraLength * eraPerCycle;
const cyclePerYear = secsOneYear / secBlockProductionRate / blockPerCycle;
return cyclePerYear;
};

const getApr = async (): Promise<{ stakerApr: number; bonusApr: number }> => {
try {
const stakingService = container.get<IDappStakingService>(Symbols.DappStakingServiceV3);
Expand Down
6 changes: 4 additions & 2 deletions src/staking-v3/hooks/useVotingCountdown.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { computed } from 'vue';
import { useDappStaking } from './useDappStaking';
import { PeriodType } from '../logic';
import { useBlockTime } from 'src/hooks';

export function useVotingCountdown() {
const { protocolState, currentBlock } = useDappStaking();
const blockTimeInSeconds = 12;
const { blockTimeInSeconds } = useBlockTime();

const secondsLeft = computed<number>(() => {
if (
protocolState.value === undefined ||
currentBlock.value === undefined ||
blockTimeInSeconds.value === undefined ||
protocolState.value.periodInfo.subperiod === PeriodType.BuildAndEarn
) {
return 0;
}

return (protocolState.value.nextEraStart - currentBlock.value) * blockTimeInSeconds;
return (protocolState.value.nextEraStart - currentBlock.value) * blockTimeInSeconds.value;
});

const timeLeftFormatted = computed<string>(() => {
Expand Down
46 changes: 26 additions & 20 deletions src/staking-v3/logic/services/DappStakingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
IBalancesRepository,
IInflationRepository,
IMetadataRepository,
ISystemRepository,
ITokenApiRepository,
} from 'src/v2/repositories';
import { weiToToken } from 'src/token-utils';
Expand All @@ -42,7 +43,8 @@ export class DappStakingService extends SignerService implements IDappStakingSer
@inject(Symbols.MetadataRepository) protected metadataRepository: IMetadataRepository,
@inject(Symbols.TokenApiRepository) protected priceRepository: ITokenApiRepository,
@inject(Symbols.BalancesRepository) protected balancesRepository: IBalancesRepository,
@inject(Symbols.InflationRepository) protected inflationRepository: IInflationRepository
@inject(Symbols.InflationRepository) protected inflationRepository: IInflationRepository,
@inject(Symbols.SystemRepository) protected systemRepository: ISystemRepository
) {
super(walletFactory);
}
Expand Down Expand Up @@ -708,15 +710,17 @@ export class DappStakingService extends SignerService implements IDappStakingSer

// @inheritdoc
public async getStakerApr(block?: number): Promise<number> {
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 [totalIssuance, inflationParams, eraLengths, eraInfo, protocolState, blockTime] =
await Promise.all([
this.balancesRepository.getTotalIssuance(block),
this.inflationRepository.getInflationParams(block),
this.dappStakingRepository.getEraLengths(block),
this.dappStakingRepository.getCurrentEraInfo(block),
this.dappStakingRepository.getProtocolState(block),
this.systemRepository.getBlockTimeInSeconds(block),
]);

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

const stakedPercent = currentStakeAmount / weiToToken(totalIssuance);
Expand All @@ -736,14 +740,16 @@ export class DappStakingService extends SignerService implements IDappStakingSer
simulatedVoteAmount: number = 1000,
block?: number
): Promise<{ value: number; simulatedBonusPerPeriod: number }> {
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 [inflationConfiguration, eraLengths, eraInfo, protocolState, blockTime] =
await Promise.all([
this.inflationRepository.getInflationConfiguration(block),
this.dappStakingRepository.getEraLengths(block),
this.dappStakingRepository.getCurrentEraInfo(block),
this.dappStakingRepository.getProtocolState(block),
this.systemRepository.getBlockTimeInSeconds(block),
]);

const cyclesPerYear = this.getCyclesPerYear(eraLengths);
const cyclesPerYear = this.getCyclesPerYear(eraLengths, blockTime);

const formattedBonusRewardsPoolPerPeriod = weiToToken(
inflationConfiguration.bonusRewardPoolPerPeriod
Expand All @@ -758,17 +764,17 @@ export class DappStakingService extends SignerService implements IDappStakingSer
return { value: bonusApr, simulatedBonusPerPeriod };
}

private getCyclesPerYear(eraLength: EraLengths): number {
private getCyclesPerYear(eraLength: EraLengths, blockTimeInSeconds: number): number {
// TODO read from chain
const secBlockProductionRate = 12;
const secsOneYear = 365 * 24 * 60 * 60;
const periodLength =
eraLength.standardErasPerBuildAndEarnPeriod + eraLength.standardErasPerVotingPeriod;

const eraPerCycle = periodLength * eraLength.periodsPerCycle;
const blocksStandardEraLength = eraLength.standardEraLength;
const blockPerCycle = blocksStandardEraLength * eraPerCycle;
const cyclePerYear = secsOneYear / secBlockProductionRate / blockPerCycle;
const cyclePerYear = secsOneYear / blockTimeInSeconds / blockPerCycle;

return cyclePerYear;
}

Expand Down
7 changes: 5 additions & 2 deletions src/staking-v3/logic/services/DappStakingServiceEvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IBalancesRepository,
IInflationRepository,
IMetadataRepository,
ISystemRepository,
ITokenApiRepository,
} from 'src/v2/repositories';
import { Guard } from 'src/v2/common';
Expand All @@ -30,7 +31,8 @@ export class DappStakingServiceEvm extends DappStakingService implements IDappSt
@inject(Symbols.MetadataRepository) metadataRepository: IMetadataRepository,
@inject(Symbols.TokenApiRepository) priceRepository: ITokenApiRepository,
@inject(Symbols.BalancesRepository) protected balancesRepository: IBalancesRepository,
@inject(Symbols.InflationRepository) protected inflationRepository: IInflationRepository
@inject(Symbols.InflationRepository) protected inflationRepository: IInflationRepository,
@inject(Symbols.SystemRepository) protected systemRepository: ISystemRepository
) {
super(
dappStakingRepository,
Expand All @@ -39,7 +41,8 @@ export class DappStakingServiceEvm extends DappStakingService implements IDappSt
metadataRepository,
priceRepository,
balancesRepository,
inflationRepository
inflationRepository,
systemRepository
);
this.wallet = walletFactory();
}
Expand Down
Loading

0 comments on commit 2d6d473

Please sign in to comment.