Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: StakingV3 list of stakers per dapp with total stake #130

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 53 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,48 @@ 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: {
expiredAt_isNull: true,
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
Loading