diff --git a/src/controllers/DappsStakingController.ts b/src/controllers/DappsStakingController.ts index 41a3d62..a1774dd 100644 --- a/src/controllers/DappsStakingController.ts +++ b/src/controllers/DappsStakingController.ts @@ -353,6 +353,24 @@ export class DappsStakingController extends ControllerBase implements IControlle }, ); + app.route('/api/v3/:network/dapps-staking/stats/aggregated/:period').get( + async (req: Request, res: Response) => { + /* + #swagger.ignore = true + */ + try { + res.json( + await this._dappsStakingEvents.getAggregatedData( + req.params.network as NetworkType, + req.params.period as PeriodType, + ), + ); + } catch (err) { + this.handleError(res, err as Error); + } + }, + ); + app.route('/api/v1/:network/dapps-staking/stats/transactions').get(async (req: Request, res: Response) => { /* #swagger.ignore = true diff --git a/src/services/DappStaking/ResponseData.ts b/src/services/DappStaking/ResponseData.ts index e9d689a..0366e34 100644 --- a/src/services/DappStaking/ResponseData.ts +++ b/src/services/DappStaking/ResponseData.ts @@ -4,6 +4,12 @@ export interface DappStakingEventResponse { }; } +export interface DappStakingAggregatedResponse { + data: { + groupedStakingEvents: DappStakingAggregatedData[]; + }; +} + export enum UserTransactionType { BondAndStake = 'BondAndStake', UnbondAndUnstake = 'UnbondAndUnstake', @@ -21,3 +27,10 @@ export interface DappStakingEventData { timestamp: bigint; blockNumber: bigint; } + +export interface DappStakingAggregatedData { + id: string; + transaction: UserTransactionType; + amount: bigint; + timestamp: bigint; +} diff --git a/src/services/DappStaking/index.ts b/src/services/DappStaking/index.ts index 9d52fa4..9bf91f4 100644 --- a/src/services/DappStaking/index.ts +++ b/src/services/DappStaking/index.ts @@ -1,4 +1 @@ -// export * from './CallParser'; -// export * from './Call'; export * from './ResponseData'; -// export * from './CallNameMapping'; diff --git a/src/services/DappsStakingEvents.ts b/src/services/DappsStakingEvents.ts index fba9271..a05e287 100644 --- a/src/services/DappsStakingEvents.ts +++ b/src/services/DappsStakingEvents.ts @@ -6,11 +6,17 @@ import { decodeAddress } from '@polkadot/util-crypto'; import { u8aToHex } from '@polkadot/util'; import { PeriodType, ServiceBase } from './ServiceBase'; import { UserEvent } from '../models/DappStaking'; -import { DappStakingEventData, DappStakingEventResponse } from './DappStaking/ResponseData'; +import { + DappStakingEventData, + DappStakingEventResponse, + DappStakingAggregatedData, + DappStakingAggregatedResponse, +} from './DappStaking/ResponseData'; import container from '../container'; export interface IDappsStakingEvents { getStakingEvents(network: NetworkType, address: string, period: PeriodType): Promise; + getAggregatedData(network: NetworkType, period: PeriodType): Promise; } // Handles events to SubSquid giant squid indexer. @@ -54,6 +60,35 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven return this.parseStakingEvents(result.data.data.stakingEvents); } + public async getAggregatedData(network: NetworkType, period: PeriodType): Promise { + Guard.ThrowIfUndefined('network', network); + + if (network !== 'astar') { + return []; + } + + const range = this.getDateRange(period); + + const query = `query MyQuery { + groupedStakingEvents(where: { + timestamp_gte: "${range.start.getTime()}", + timestamp_lte: "${range.end.getTime()}" + }, orderBy: timestamp_DESC) { + amount + id + timestamp + transaction + } + }`; + + const result = await axios.post(this.getApiUrl(network), { + operationName: 'MyQuery', + query, + }); + + return this.parseAggregatedData(result.data.data.groupedStakingEvents); + } + private getApiUrl(network: NetworkType): string { return `http://localhost:4350/graphql`; } @@ -67,4 +102,14 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven return result; } + + private parseAggregatedData(events: DappStakingAggregatedData[]): DappStakingAggregatedData[] { + const result: DappStakingAggregatedData[] = []; + + for (const event of events) { + result.push(event); + } + + return result; + } }