Skip to content

Commit

Permalink
feat: Adds lockers count to the tvl endpoint (#142)
Browse files Browse the repository at this point in the history
* adds lockers count to the tvl endpoint

* 3 new endpoints
  • Loading branch information
gluneau authored Feb 19, 2024
1 parent 00df239 commit 7a6168e
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 3 deletions.
132 changes: 131 additions & 1 deletion public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,129 @@
}
}
},
"/api/v3/{network}/dapps-staking/stakers-total/{period}": {
"get": {
"tags": [
"Dapps Staking"
],
"description": "Retrieves dapps staking Total Value Staked and Total Number of Unique Stakers 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/lockers-total/{period}": {
"get": {
"tags": [
"Dapps Staking"
],
"description": "Retrieves dapps staking Total Value Locked and Total Number of Unique Lockers 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/lockers-and-stakers-total/{period}": {
"get": {
"tags": [
"Dapps Staking"
],
"description": "Retrieves dapps staking Total Value Locked & Staked and Total Number of Unique Lockers & Stakers 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/stakerscount/{contractAddress}/{period}": {
"get": {
"tags": [
Expand Down Expand Up @@ -987,7 +1110,14 @@
{
"name": "transaction",
"in": "query",
"type": "string"
"description": "The Reward Event transaction type. Supported values: Reward, BonusReward, DAppReward",
"required": false,
"type": "string",
"enum": [
"Reward",
"BonusReward",
"DAppReward"
]
}
],
"responses": {
Expand Down
79 changes: 78 additions & 1 deletion src/controllers/DappsStakingV3Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,83 @@ export class DappsStakingV3Controller extends ControllerBase implements IControl
);
});

app.route('/api/v3/:network/dapps-staking/stakers-total/:period').get(async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves dapps staking Total Value Staked and Total Number of Unique Stakers 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.getDappStakingStakersTotal(
req.params.network as NetworkType,
req.params.period as PeriodType,
),
);
});

app.route('/api/v3/:network/dapps-staking/lockers-total/:period').get(async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves dapps staking Total Value Locked and Total Number of Unique Lockers 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.getDappStakingLockersTotal(
req.params.network as NetworkType,
req.params.period as PeriodType,
),
);
});

app.route('/api/v3/:network/dapps-staking/lockers-and-stakers-total/:period').get(
async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves dapps staking Total Value Locked & Staked and Total Number of Unique Lockers & Stakers 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.getDappStakingLockersAndStakersTotal(
req.params.network as NetworkType,
req.params.period as PeriodType,
),
);
},
);

app.route('/api/v3/:network/dapps-staking/stakerscount/:contractAddress/:period').get(
async (req: Request, res: Response) => {
/*
Expand Down Expand Up @@ -284,7 +361,7 @@ export class DappsStakingV3Controller extends ControllerBase implements IControl
}
#swagger.parameters['transaction'] = {
in: 'query',
description: 'The Reward Event transaction type. Supported values: Reward', 'BonusReward', 'DAppReward',
description: 'The Reward Event transaction type. Supported values: Reward, BonusReward, DAppReward',
required: false,
type: 'string',
enum: ['Reward', 'BonusReward', 'DAppReward']
Expand Down
124 changes: 123 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, inject } from 'inversify';
import axios from 'axios';
import { NetworkType } from '../networks';
import { Guard } from '../guard';
import { Pair, PeriodType, ServiceBase, List } from './ServiceBase';
import { TotalAmountCount, Triplet, Pair, PeriodType, ServiceBase, List } from './ServiceBase';
import { IApiFactory } from '../client/ApiFactory';
import { ContainerTypes } from '../containertypes';
import {
Expand All @@ -27,6 +27,9 @@ export interface IDappsStakingEvents {
getDappStakingStakersCount(network: NetworkType, contractAddress: string, period: PeriodType): Promise<Pair[]>;
getParticipantStake(network: NetworkType, address: string): Promise<bigint>;
getDappStakingStakersCountTotal(network: NetworkType, period: PeriodType): Promise<Pair[]>;
getDappStakingStakersTotal(network: NetworkType, period: PeriodType): Promise<Triplet[]>;
getDappStakingLockersTotal(network: NetworkType, period: PeriodType): Promise<Triplet[]>;
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[]>;
Expand Down Expand Up @@ -373,6 +376,125 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}
}

public async getDappStakingStakersTotal(network: NetworkType, period: PeriodType): Promise<Triplet[]> {
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()}"
}
) {
date: id
count: stakersCount
amount: stakersAmount
}
}`,
});

const results: Triplet[] = result.data.data.stakersCountAggregatedDailies;
return results;
} catch (e) {
console.error(e);
return [];
}
}

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

const range = this.getDateRange(period);

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

const results: Triplet[] = result.data.data.tvlAggregatedDailies;
return results;
} catch (e) {
console.error(e);
return [];
}
}

public async getDappStakingLockersAndStakersTotal(
network: NetworkType,
period: PeriodType,
): Promise<TotalAmountCount[]> {
if (!['astar', 'shiden', 'shibuya'].includes(network)) {
return [];
}

const range = this.getDateRange(period);

const query = `query {
tvlAggregatedDailies(
orderBy: id_DESC
where: { id_gte: "${range.start.getTime()}", id_lte: "${range.end.getTime()}" }
) {
date: id
count: lockersCount
amount: tvl
}
stakersCountAggregatedDailies(
orderBy: id_DESC
where: { id_gte: "${range.start.getTime()}", id_lte: "${range.end.getTime()}" }
) {
date: id
count: stakersCount
amount: stakersAmount
}
}`;

try {
const result = await axios.post(this.getApiUrl(network), { query });

const combinedData: TotalAmountCount[] = [];

const lockersData: Triplet[] = result.data.data.tvlAggregatedDailies;
const stakersData: Triplet[] = result.data.data.stakersCountAggregatedDailies;
const lockersMap = new Map(lockersData.map((item) => [item.date, item]));
const stakersMap = new Map(stakersData.map((item) => [item.date, item]));

const allIds = new Set([...lockersMap.keys(), ...stakersMap.keys()]);

allIds.forEach((date) => {
combinedData.push({
date,
tvl: lockersMap.has(date) ? lockersMap.get(date)?.amount : undefined,
lockersCount: lockersMap.has(date) ? lockersMap.get(date)?.count : undefined,
tvs: stakersMap.has(date) ? stakersMap.get(date)?.amount : undefined,
stakersCount: stakersMap.has(date) ? stakersMap.get(date)?.count : undefined,
});
});

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

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

0 comments on commit 7a6168e

Please sign in to comment.