Skip to content

Commit

Permalink
feat: total number of v3 stakers (#126)
Browse files Browse the repository at this point in the history
* stakerscount-total

* lint spaces
  • Loading branch information
gluneau authored Jan 26, 2024
1 parent 8ee5f8a commit be56ca7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
41 changes: 41 additions & 0 deletions public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,47 @@
}
}
},
"/api/v3/{network}/dapps-staking/stakerscount-total/{period}": {
"get": {
"tags": [
"Dapps Staking"
],
"description": "Retrieves total stakers count 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": "period",
"in": "path",
"required": true,
"type": "string",
"description": "The period type. Supported values: 1 day, 7 days, 30 days, 90 days, 1 year",
"enum": [
"1 day",
"7 days",
"30 days",
"90 days",
"1 year"
]
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v3/{network}/dapps-staking/chaindapps": {
"get": {
"tags": [
Expand Down
27 changes: 27 additions & 0 deletions src/controllers/DappsStakingV3Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ export class DappsStakingV3Controller extends ControllerBase implements IControl
},
);

app.route('/api/v3/:network/dapps-staking/stakerscount-total/:period').get(
async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves total stakers count 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['period'] = {
in: 'path',
description: 'The period type. Supported values: 1 day, 7 days, 30 days, 90 days, 1 year',
required: true,
enum: ['1 day', '7 days', '30 days', '90 days', '1 year']
}
*/
res.json(
await this._dappsStakingEvents.getDappStakingStakersCountTotal(
req.params.network as NetworkType,
req.params.period as PeriodType,
),
);
},
);

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
37 changes: 37 additions & 0 deletions src/services/DappsStakingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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[]>;
getDappStakingStakersCountTotal(network: NetworkType, period: PeriodType): Promise<Pair[]>;
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[]>;
Expand Down Expand Up @@ -313,6 +314,42 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}
}

public async getDappStakingStakersCountTotal(network: NetworkType, period: PeriodType): Promise<Pair[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
}

const range = this.getDateRange(period);

try {
const result = await axios.post(this.getApiUrl(network), {
query: `query {
stakersCountAggregatedDailies(
orderBy: id_DESC
where: {
id_gte: "${range.start.getTime()}"
id_lte: "${range.end.getTime()}"
}
) {
stakersCount
id
}
}`,
});

const stakersCount = result.data.data.stakersCountAggregatedDailies.map(
(node: { id: string; stakersCount: number }) => {
return [node.id, node.stakersCount];
},
);

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

public async getDapps(network: NetworkType): Promise<[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
Expand Down

0 comments on commit be56ca7

Please sign in to comment.