Skip to content

Commit

Permalink
Provide EVM address on stakers list
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 committed Nov 11, 2024
1 parent 9f240a1 commit 4a2ae6e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/services/DappStakingV3IndexerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export class DappStakingV3IndexerBase extends ServiceBase {

protected getApiUrl(network: NetworkType): string {
// For local development: `http://localhost:4350/graphql`;
return `https://astar-network.squids.live/dapps-staking-indexer-${network}/graphql`;
return `https://astar-network.squids.live/dapps-staking-indexer-${network}/v/v14/graphql`;
}
}
38 changes: 22 additions & 16 deletions src/services/DappsStakingEvents.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { injectable, inject } from 'inversify';
import axios from 'axios';
import { formatEther } from 'ethers';
import { NetworkType } from '../networks';
import type { NetworkType } from '../networks';
import { Guard } from '../guard';
import { TotalAmountCount, Triplet, Pair, PeriodType, List } from './ServiceBase';
import { IApiFactory } from '../client/ApiFactory';
import type { TotalAmountCount, Triplet, Pair, PeriodType, StakerAmount } from './ServiceBase';
import type { IApiFactory } from '../client/ApiFactory';
import { ContainerTypes } from '../containertypes';
import {
import type {
DappStakingEventData,
DappStakingEventResponse,
DappStakingAggregatedData,
Expand All @@ -15,7 +15,7 @@ import {
StakerPeriodDataResponse,
StakerPeriodTotalResponse,
} from './DappStaking/ResponseData';
import { IStatsIndexerService } from './StatsIndexerService';
import type { IStatsIndexerService } from './StatsIndexerService';
import { DappStakingV3IndexerBase } from './DappStakingV3IndexerBase';

export interface IDappsStakingEvents {
Expand All @@ -38,7 +38,7 @@ export interface IDappsStakingEvents {
getDappStakingLockersAndStakersTotal(network: NetworkType, period: PeriodType): Promise<TotalAmountCount[]>;
getDappStakingRewards(network: NetworkType, period: PeriodType, transaction: RewardEventType): Promise<Pair[]>;
getDappStakingRewardsAggregated(network: NetworkType, address: string, period: PeriodType): Promise<Pair[]>;
getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<List[]>;
getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<StakerAmount[]>;
getAggregatedPeriodData(network: NetworkType, period: number): Promise<PeriodDataResponse[]>;
getAggregatedStakerData(network: NetworkType, stakerAddress: string): Promise<StakerPeriodDataResponse[]>;
getTotalAggregatedStakerData(network: NetworkType, stakerAddress: string): Promise<StakerPeriodTotalResponse>;
Expand Down Expand Up @@ -312,7 +312,7 @@ export class DappsStakingEvents extends DappStakingV3IndexerBase implements IDap
}
}

public async getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<List[]> {
public async getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<StakerAmount[]> {
this.GuardNetwork(network);
Guard.ThrowIfUndefined('contractAddress', contractAddress);

Expand All @@ -331,25 +331,31 @@ export class DappsStakingEvents extends DappStakingV3IndexerBase implements IDap
}
) {
stakerAddress
stakerAddressEvm
amount
}
}`,
});

const sumsByStaker: { [key: string]: bigint } = result.data.data.stakes.reduce(
(acc: { [key: string]: bigint }, { stakerAddress, amount }: List) => {
acc[stakerAddress] = (acc[stakerAddress] || BigInt(0)) + BigInt(amount);
const sumsByStaker: {
[key: string]: { amount: bigint; stakerAddress: string; stakerAddressEvm?: string };
} = result.data.data.stakes.reduce(
(
acc: { [key: string]: { amount: bigint; stakerAddress: string; stakerAddressEvm?: string } },
{ stakerAddress, stakerAddressEvm, amount }: StakerAmount,
) => {
if (!acc[stakerAddress]) {
acc[stakerAddress] = { amount: BigInt(0), stakerAddress, stakerAddressEvm };
}
acc[stakerAddress].amount += BigInt(amount);
return acc;
},
{},
);

const stakersList: List[] = Object.entries(sumsByStaker)
.map(([stakerAddress, amount]) => ({
stakerAddress,
amount,
}))
.filter((staker) => staker.amount !== BigInt(0));
const stakersList: StakerAmount[] = Object.entries(sumsByStaker)
.map(([_, stake]) => stake)
.filter((s) => s.amount !== BigInt(0));

return stakersList;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/ServiceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type PeriodType = '1 day' | '7 days' | '30 days' | '90 days' | '1 year';
export type PeriodTypeEra = '7 eras' | '30 eras' | '90 eras' | 'all';
export type Pair = { date: number; value: number };
export type Triplet = { date: string; count: number; amount: number };
export type List = { stakerAddress: string; amount: bigint };
export type StakerAmount = { stakerAddress: string; amount: bigint; stakerAddressEvm?: string };
export type DateRange = { start: Date; end: Date };
export type TotalAmountCount = {
date: string;
Expand Down

0 comments on commit 4a2ae6e

Please sign in to comment.