Skip to content

Commit

Permalink
Fix for Shiden circulation panel
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 committed Feb 7, 2024
1 parent 64bd6b2 commit 4760c5a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/hooks/useTokenDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { TvlModel } from 'src/v2/models';
import { ref, watchEffect } from 'vue';
import { useTokenCirculation } from './useTokenCirculation';
import { container } from 'src/v2/common';
import { IDappStakingService } from 'src/v2/services';
import { Symbols } from 'src/v2/symbols';
import { useBalance } from './useBalance';
import { ethers } from 'ethers';
import { IDappStakingServiceV2V3 } from 'src/staking-v3';

export function useTokenDistribution() {
const treasuryAddress = ref<string>('YQnbw3oWxBnCUarnbePrjFcrSgVPP2jqTZYzWcccmN8fXhd');
Expand All @@ -21,7 +21,7 @@ export function useTokenDistribution() {
const other = ref<number>(0);

const fetchData = async () => {
const dappService = container.get<IDappStakingService>(Symbols.DappStakingService);
const dappService = container.get<IDappStakingServiceV2V3>(Symbols.DappStakingServiceV2V3);
tvlModel.value = await dappService.getTvl();
};

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useTvlHistorical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
formatNumber,
} from '@astar-network/astar-sdk-core';
import { container } from 'src/v2/common';
import { IDappStakingService } from 'src/v2/services';
import { Symbols } from 'src/v2/symbols';
import { IDappStakingServiceV2V3 } from 'src/staking-v3';

export function useTvlHistorical() {
const mergedTvlAmount = ref<string>('');
Expand Down Expand Up @@ -58,7 +58,7 @@ export function useTvlHistorical() {
};

const getTvl = async (): Promise<void> => {
const tvlService = container.get<IDappStakingService>(Symbols.DappStakingService);
const tvlService = container.get<IDappStakingServiceV2V3>(Symbols.DappStakingServiceV2V3);
const tvl = await tvlService.getTvl();
const tvlDefaultUnit = formatNumber(tvl.tvlDefaultUnit, 2);

Expand Down
34 changes: 30 additions & 4 deletions src/staking-v3/logic/services/DappStakingServiceV2V3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// service implementation (v2 or v3) is needed, depending on dApp staking version deployed on a node.
// TODO remove after Astar release.

import { container } from 'src/v2/common';
import { useDappStaking } from '../../hooks';
import { IDappStakingService } from 'src/v2/services';
import { IDappStakingService as IDappStakingServiceV3 } from '.';
import { Symbols } from 'src/v2/symbols';
import { inject, injectable } from 'inversify';
import { IApi } from 'src/v2/integration';
import { IDappStakingRepository as IDappStakingRepositoryV3 } from '../repositories';
import { EditDappItem } from 'src/store/dapp-staking/state';
import { TvlModel } from 'src/v2/models';
import { IDappStakingRepository, IMetadataRepository, IPriceRepository } from 'src/v2/repositories';
import { ethers } from 'ethers';
import { ASTAR_NATIVE_TOKEN, astarMainnetNativeToken } from 'src/config/chain';

export interface IDappStakingServiceV2V3 {
getRegisteredContract(developerAddress: string): Promise<string | undefined>;
Expand All @@ -26,14 +27,19 @@ export interface IDappStakingServiceV2V3 {
network: string,
forEdit?: boolean
): Promise<EditDappItem | undefined>;

getTvl(): Promise<TvlModel>;
}

@injectable()
export class DappStakingServiceV2V3 implements IDappStakingServiceV2V3 {
constructor(
@inject(Symbols.DefaultApi) private api: IApi,
@inject(Symbols.DappStakingService) private stakingV2: IDappStakingService,
@inject(Symbols.DappStakingRepositoryV3) private repositoryV3: IDappStakingRepositoryV3
@inject(Symbols.DappStakingRepositoryV3) private repositoryV3: IDappStakingRepositoryV3,
@inject(Symbols.DappStakingRepository) private repositoryV2: IDappStakingRepository,
@inject(Symbols.MetadataRepository) private metadataRepository: IMetadataRepository,
@inject(Symbols.PriceRepository) private priceRepository: IPriceRepository
) {}

public async getRegisteredContract(developerAddress: string): Promise<string | undefined> {
Expand All @@ -55,6 +61,26 @@ export class DappStakingServiceV2V3 implements IDappStakingServiceV2V3 {
return await this.stakingV2.getDapp(contractAddress, network, forEdit);
}

public async getTvl(): Promise<TvlModel> {
const metadata = await this.metadataRepository.getChainMetadata();
const v3 = await this.isV3();
const [tvl, priceUsd] = await Promise.all([
v3
? (await this.repositoryV3.getCurrentEraInfo()).totalLocked.toString()
: this.repositoryV2.getTvl(),
this.priceRepository.getUsdPrice(metadata.token),
]);

const tvlDefaultUnit = Number(
ethers.utils.formatUnits(BigInt(tvl.toString()), metadata.decimals)
);
const tvlUsd = astarMainnetNativeToken.includes(metadata.token as ASTAR_NATIVE_TOKEN)
? tvlDefaultUnit * priceUsd
: 0;

return new TvlModel(tvl, tvlDefaultUnit, tvlUsd);
}

private async isV3() {
const api = await this.api.getApi();
return api.query.hasOwnProperty('dappStaking');
Expand Down
11 changes: 3 additions & 8 deletions src/store/dapp-staking/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ import { BN } from '@polkadot/util';
import { $api } from 'boot/api';
import { ActionTree, Dispatch } from 'vuex';
import { StateInterface } from '../index';
import { sign } from './../../hooks/helper/wallet';
import { SubstrateAccount } from './../general/state';
import { DappStateInterface as State, NewDappItem, FileInfo } from './state';
import { IAccountUnificationService, IDappStakingService } from 'src/v2/services';
import { container } from 'src/v2/common';
import { Symbols } from 'src/v2/symbols';
import axios, { AxiosError } from 'axios';
import type { Transaction } from 'src/hooks/helper/wallet';
import {
getDappAddressEnum,
TOKEN_API_URL,
DappItem,
isValidEvmAddress,
} from '@astar-network/astar-sdk-core';
import { TOKEN_API_URL, DappItem, isValidEvmAddress } from '@astar-network/astar-sdk-core';
import { IDappStakingServiceV2V3 } from 'src/staking-v3';

const showError = (dispatch: Dispatch, message: string): void => {
dispatch(
Expand Down Expand Up @@ -218,7 +213,7 @@ const actions: ActionTree<State, StateInterface> = {

async getTvl({ commit, dispatch }) {
try {
const dappService = container.get<IDappStakingService>(Symbols.DappStakingService);
const dappService = container.get<IDappStakingServiceV2V3>(Symbols.DappStakingServiceV2V3);
const tvl = await dappService.getTvl();
commit('setTvl', tvl);

Expand Down

0 comments on commit 4760c5a

Please sign in to comment.