Skip to content

Commit

Permalink
list of stakers per dapp with total stake
Browse files Browse the repository at this point in the history
  • Loading branch information
gluneau committed Jan 19, 2024
1 parent 423bd5e commit 683ed3b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
34 changes: 34 additions & 0 deletions public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,40 @@
}
}
},
"/api/v3/{network}/dapps-staking/stakerslist/{contractAddress}": {
"get": {
"tags": [
"Dapps Staking"
],
"description": "Retrieves dapps staking stakers list for a given network and period.",
"parameters": [
{
"name": "network",
"in": "path",
"required": true,
"type": "string",
"description": "The network name. Supported networks: astar",
"enum": [
"astar",
"shiden",
"shibuya"
]
},
{
"name": "contractAddress",
"in": "path",
"required": true,
"type": "string",
"description": "Contract address to get stats for"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v3/{network}/dapps-staking/chaindapps": {
"get": {
"tags": [
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/DappsStakingV3Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ export class DappsStakingV3Controller extends ControllerBase implements IControl
},
);

app.route('/api/v3/:network/dapps-staking/stakerslist/:contractAddress').get(
async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves dapps staking stakers list for a given network and period.'
#swagger.tags = ['Dapps Staking']
#swagger.parameters['network'] = {
in: 'path',
description: 'The network name. Supported networks: astar',
required: true,
enum: ['astar', 'shiden', 'shibuya']
}
#swagger.parameters['contractAddress'] = {
in: 'path',
description: 'Contract address to get stats for',
required: true
}
*/
res.json(
await this._dappsStakingEvents.getDappStakingStakersList(
req.params.network as NetworkType,
req.params.contractAddress as string,
),
);
},
);

app.route('/api/v3/:network/dapps-staking/chaindapps').get(async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves list of dapps (basic info) registered for dapps staking'
Expand Down
53 changes: 52 additions & 1 deletion src/services/DappsStakingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { injectable } from 'inversify';
import axios from 'axios';
import { NetworkType } from '../networks';
import { Guard } from '../guard';
import { Pair, PeriodType, ServiceBase } from './ServiceBase';
import { Pair, PeriodType, ServiceBase, List } from './ServiceBase';
import {
DappStakingEventData,
DappStakingEventResponse,
Expand All @@ -23,8 +23,18 @@ export interface IDappsStakingEvents {
getAggregatedData(network: NetworkType, period: PeriodType): Promise<DappStakingAggregatedData[]>;
getDappStakingTvl(network: NetworkType, period: PeriodType): Promise<Pair[]>;
getDappStakingStakersCount(network: NetworkType, contractAddress: string, period: PeriodType): Promise<Pair[]>;
getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<List[]>;
}

declare global {
interface BigInt {
toJSON: () => string;
}
}
BigInt.prototype.toJSON = function () {
return this.toString();
};

@injectable()
export class DappsStakingEvents extends ServiceBase implements IDappsStakingEvents {
public async getStakingEvents(
Expand Down Expand Up @@ -175,6 +185,47 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}
}

public async getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<List[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
}

try {
const result = await axios.post(this.getApiUrl(network), {
query: `query {
stakes(
where: {
dappAddress_eq: "${contractAddress}"
}
) {
stakerAddress
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);
return acc;
},
{},
);

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

return stakersList;
} catch (e) {
console.error(e);
return [];
}
}

public async getDapps(network: NetworkType): Promise<[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
Expand Down
1 change: 1 addition & 0 deletions src/services/ServiceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { injectable } from 'inversify';
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 List = { stakerAddress: string; amount: bigint };
export type DateRange = { start: Date; end: Date };

const DEFAULT_RANGE_LENGTH_DAYS = 7;
Expand Down

0 comments on commit 683ed3b

Please sign in to comment.