Skip to content

Commit

Permalink
Burn events endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 committed Jul 12, 2024
1 parent d64d7b6 commit 17f02a1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 46 deletions.
4 changes: 4 additions & 0 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
import { BluezNftService, INftService } from './services/NftService';
import { NftController } from './controllers/NftController';
import { TokenStatsControllerV2 } from './controllers/TokenStatsControllerV2';
import { BurnService, IBurnService } from './services/BurnService';
import { BurnController } from './controllers/BurnController';

const container = new Container();

Expand Down Expand Up @@ -75,6 +77,7 @@ container.bind<IApiFactory>(ContainerTypes.ApiFactory).to(ApiFactory).inSingleto
container.bind<IStatsService>(ContainerTypes.StatsService).to(StatsService).inSingletonScope();

container.bind<IDappsStakingEvents>(ContainerTypes.DappsStakingEvents).to(DappsStakingEvents).inSingletonScope();
container.bind<IBurnService>(ContainerTypes.BurnService).to(BurnService).inSingletonScope();

container
.bind<IDappsStakingService>(ContainerTypes.DappsStakingService)
Expand Down Expand Up @@ -145,5 +148,6 @@ container.bind<IControllerBase>(ContainerTypes.Controller).to(NodeController);
container.bind<IControllerBase>(ContainerTypes.Controller).to(TxQueryController);
container.bind<IControllerBase>(ContainerTypes.Controller).to(MonthlyActiveWalletsController);
container.bind<IControllerBase>(ContainerTypes.Controller).to(NftController);
container.bind<IControllerBase>(ContainerTypes.Controller).to(BurnController);

export default container;
3 changes: 3 additions & 0 deletions src/containertypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BurnService } from './services/BurnService';

export const ContainerTypes = {
Controller: 'Controller',
StatsService: 'StatsService',
Expand All @@ -16,4 +18,5 @@ export const ContainerTypes = {
DappRadarService: 'DappRadarService',
GiantSquidService: 'GiantSquidService',
BluezNftService: 'BluezNftService',
BurnService: 'BurnService',
};
38 changes: 38 additions & 0 deletions src/controllers/BurnController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import express, { Request, Response } from 'express';
import { inject, injectable } from 'inversify';
import { ControllerBase } from './ControllerBase';
import { IControllerBase } from './IControllerBase';
import { ContainerTypes } from '../containertypes';
import { IBurnService } from '../services/BurnService';
import { NetworkType } from '../networks';

@injectable()
export class BurnController extends ControllerBase implements IControllerBase {
constructor(@inject(ContainerTypes.BurnService) private _burnService: IBurnService) {
super();
}

public register(app: express.Application): void {
/**
* @description Burn events route
*/
app.route('/api/v1/:network/burn/events').get(async (req: Request, res: Response) => {
/*
#swagger.description = 'Retrieves burn events for a given network.'
#swagger.tags = ['Burn']
#swagger.parameters['network'] = {
in: 'path',
description: 'The network name. Supported networks: astar, shiden, shibuya',
required: true,
enum: ['astar', 'shiden', 'shibuya']
}
*/
try {
const network = req.params.network as NetworkType;
res.json(await this._burnService.getBurnEvents(network));
} catch (err) {
this.handleError(res, err as Error);
}
});
}
}
40 changes: 40 additions & 0 deletions src/services/BurnService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { injectable } from 'inversify';
import { NetworkType } from '../networks';
import axios from 'axios';
import { DappStakingV3IndexerBase } from './DappStakingV3IndexerBase';

export type BurnEvent = {
blockNumber: number;
timestamp: number;
amount: bigint;
user: string;
};

export interface IBurnService {
getBurnEvents(network: NetworkType): Promise<BurnEvent[]>;
}

@injectable()
export class BurnService extends DappStakingV3IndexerBase implements IBurnService {
public async getBurnEvents(network: NetworkType): Promise<BurnEvent[]> {
this.GuardNetwork(network);

try {
const result = await axios.post(this.getApiUrl(network), {
query: `query {
burns(orderBy: id_ASC) {
blockNumber
timestamp
user
amount
}
}`,
});

return result.data.data.burns;
} catch (e) {
console.error(e);
return [];
}
}
}
17 changes: 17 additions & 0 deletions src/services/DappStakingV3IndexerBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Guard } from '../guard';
import { NetworkType } from '../networks';
import { ServiceBase } from './ServiceBase';

export class DappStakingV3IndexerBase extends ServiceBase {
protected GuardNetwork(network: NetworkType) {
Guard.ThrowIfUndefined('network', network);
if (!['shibuya', 'shiden', 'astar'].includes(network)) {
throw new Error(`This method is not supported for the network ${network}`);
}
}

protected getApiUrl(network: NetworkType): string {
// For local development: `http://localhost:4350/graphql`;
return `https://astar-network.squids.live/dapps-staking-indexer-${network}/graphql`;
}
}
63 changes: 17 additions & 46 deletions src/services/DappsStakingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axios from 'axios';
import { formatEther } from 'ethers';
import { NetworkType } from '../networks';
import { Guard } from '../guard';
import { TotalAmountCount, Triplet, Pair, PeriodType, ServiceBase, List } from './ServiceBase';
import { TotalAmountCount, Triplet, Pair, PeriodType, List } from './ServiceBase';
import { IApiFactory } from '../client/ApiFactory';
import { ContainerTypes } from '../containertypes';
import {
Expand All @@ -16,6 +16,7 @@ import {
StakerPeriodTotalResponse,
} from './DappStaking/ResponseData';
import { IStatsIndexerService } from './StatsIndexerService';
import { DappStakingV3IndexerBase } from './DappStakingV3IndexerBase';

export interface IDappsStakingEvents {
getDapps(network: NetworkType): Promise<[]>;
Expand Down Expand Up @@ -55,7 +56,7 @@ BigInt.prototype.toJSON = function () {
};

@injectable()
export class DappsStakingEvents extends ServiceBase implements IDappsStakingEvents {
export class DappsStakingEvents extends DappStakingV3IndexerBase implements IDappsStakingEvents {
constructor(
@inject(ContainerTypes.ApiFactory) private _apiFactory: IApiFactory,
@inject(ContainerTypes.StatsIndexerService) private _statsService: IStatsIndexerService,
Expand Down Expand Up @@ -155,9 +156,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

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

const range = this.getDateRange(period);

Expand Down Expand Up @@ -201,9 +200,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
period: PeriodType,
transaction: RewardEventType,
): Promise<Pair[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
}
this.GuardNetwork(network);

const range = this.getDateRange(period);

Expand Down Expand Up @@ -242,9 +239,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
address: string,
period: PeriodType,
): Promise<Pair[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
}
this.GuardNetwork(network);

const range = this.getDateRange(period);

Expand Down Expand Up @@ -283,9 +278,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
contractAddress: string,
period: PeriodType,
): Promise<Pair[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
}
this.GuardNetwork(network);

const range = this.getDateRange(period);

Expand Down Expand Up @@ -320,9 +313,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

public async getDappStakingStakersList(network: NetworkType, contractAddress: string): Promise<List[]> {
if (network !== 'astar' && network !== 'shiden' && network !== 'shibuya') {
return [];
}
this.GuardNetwork(network);

try {
const result = await axios.post(this.getApiUrl(network), {
Expand Down Expand Up @@ -362,9 +353,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

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

const range = this.getDateRange(period);

Expand Down Expand Up @@ -399,9 +388,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

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

const range = this.getDateRange(period);

Expand Down Expand Up @@ -431,9 +418,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

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

const range = this.getDateRange(period);

Expand Down Expand Up @@ -463,9 +448,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
network: NetworkType,
period: PeriodType,
): Promise<TotalAmountCount[]> {
if (!['astar', 'shiden', 'shibuya'].includes(network)) {
return [];
}
this.GuardNetwork(network);

const range = this.getDateRange(period);

Expand Down Expand Up @@ -518,9 +501,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

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

try {
const result = await axios.post(this.getApiUrl(network), {
Expand Down Expand Up @@ -548,9 +529,7 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
}

public async getAggregatedPeriodData(network: NetworkType, period: number): Promise<PeriodDataResponse[]> {
if (!['shibuya', 'shiden', 'astar'].includes(network)) {
throw new Error(`This method is not supported for the network ${network}`);
}
this.GuardNetwork(network);

try {
const result = await axios.post(this.getApiUrl(network), {
Expand All @@ -574,11 +553,8 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
network: NetworkType,
stakerAddress: string,
): Promise<StakerPeriodDataResponse[]> {
Guard.ThrowIfUndefined('network', network);
Guard.ThrowIfUndefined('stakerAddress', stakerAddress);
if (!['shibuya', 'shiden', 'astar'].includes(network)) {
throw new Error(`This method is not supported for the network ${network}`);
}
this.GuardNetwork(network);

try {
const result = await axios.post(this.getApiUrl(network), {
Expand All @@ -604,6 +580,8 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
network: NetworkType,
stakerAddress: string,
): Promise<StakerPeriodTotalResponse> {
this.GuardNetwork(network);

const data = await this.getAggregatedStakerData(network, stakerAddress);
let maxPeriod = -1;
const total = data.reduce(
Expand All @@ -626,11 +604,4 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven

return total;
}

private getApiUrl(network: NetworkType): string {
// For local development: `http://localhost:4350/graphql`;
return ['astar', 'shiden', 'shibuya'].includes(network)
? `https://astar-network.squids.live/dapps-staking-indexer-${network}/graphql`
: '';
}
}

0 comments on commit 17f02a1

Please sign in to comment.