Skip to content

Commit

Permalink
aggregated endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gluneau committed Oct 16, 2023
1 parent 091d111 commit e2d6fa9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/controllers/DappsStakingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/services/DappStaking/ResponseData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export interface DappStakingEventResponse {
};
}

export interface DappStakingAggregatedResponse {
data: {
groupedStakingEvents: DappStakingAggregatedData[];
};
}

export enum UserTransactionType {
BondAndStake = 'BondAndStake',
UnbondAndUnstake = 'UnbondAndUnstake',
Expand All @@ -21,3 +27,10 @@ export interface DappStakingEventData {
timestamp: bigint;
blockNumber: bigint;
}

export interface DappStakingAggregatedData {
id: string;
transaction: UserTransactionType;
amount: bigint;
timestamp: bigint;
}
3 changes: 0 additions & 3 deletions src/services/DappStaking/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
// export * from './CallParser';
// export * from './Call';
export * from './ResponseData';
// export * from './CallNameMapping';
47 changes: 46 additions & 1 deletion src/services/DappsStakingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DappStakingEventData[]>;
getAggregatedData(network: NetworkType, period: PeriodType): Promise<DappStakingAggregatedData[]>;
}

// Handles events to SubSquid giant squid indexer.
Expand Down Expand Up @@ -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<DappStakingAggregatedData[]> {
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<DappStakingAggregatedResponse>(this.getApiUrl(network), {
operationName: 'MyQuery',
query,
});

return this.parseAggregatedData(result.data.data.groupedStakingEvents);
}

private getApiUrl(network: NetworkType): string {
return `http://localhost:4350/graphql`;
}
Expand All @@ -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;
}
}

0 comments on commit e2d6fa9

Please sign in to comment.