Skip to content

Commit

Permalink
Concatenate TVL from dApp staking v2 and v3
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 committed May 22, 2024
1 parent 4c66c15 commit 1e8c8f4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/controllers/DappsStakingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IStatsIndexerService } from '../services/StatsIndexerService';
import { ControllerBase } from './ControllerBase';
import { IControllerBase } from './IControllerBase';
import { IGiantSquidService } from '../services/GiantSquidService';
import { IDappsStakingEvents } from '../services/DappsStakingEvents';

@injectable()
export class DappsStakingController extends ControllerBase implements IControllerBase {
Expand All @@ -23,6 +24,7 @@ export class DappsStakingController extends ControllerBase implements IControlle
@inject(ContainerTypes.DappsStakingStatsService) private _statsService: IDappsStakingStatsService,
@inject(ContainerTypes.DappRadarService) private _dappRadarService: IDappRadarService,
@inject(ContainerTypes.GiantSquidService) private _giantSquidService: IGiantSquidService,
@inject(ContainerTypes.DappsStakingEvents) private _eventsService: IDappsStakingEvents,
) {
super();
}
Expand Down Expand Up @@ -101,7 +103,7 @@ export class DappsStakingController extends ControllerBase implements IControlle
}
*/
res.json(
await this._indexerService.getDappStakingTvl(
await this._eventsService.getDappStakingTvl(
req.params.network as NetworkType,
req.params.period as PeriodType,
),
Expand Down
44 changes: 36 additions & 8 deletions src/services/DappsStakingEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { injectable, inject } from 'inversify';
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';
Expand All @@ -11,6 +12,7 @@ import {
DappStakingAggregatedData,
DappStakingAggregatedResponse,
} from './DappStaking/ResponseData';
import { IStatsIndexerService } from './StatsIndexerService';

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

@injectable()
export class DappsStakingEvents extends ServiceBase implements IDappsStakingEvents {
constructor(@inject(ContainerTypes.ApiFactory) private _apiFactory: IApiFactory) {
constructor(
@inject(ContainerTypes.ApiFactory) private _apiFactory: IApiFactory,
@inject(ContainerTypes.StatsIndexerService) private _statsService: IStatsIndexerService,
) {
super();
}

Expand Down Expand Up @@ -159,15 +164,26 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven
) {
id
tvl
usdPrice
}
}`,
});

const indexedTvl = result.data.data.tvlAggregatedDailies.map((node: { id: string; tvl: number }) => {
return [node.id, node.tvl];
});
const indexedTvl = result.data.data.tvlAggregatedDailies.map(
(node: { id: string; tvl: number; usdPrice: number }) => {
return [node.id, node.usdPrice * Number(formatEther(node.tvl.toString()))];
},
);

return indexedTvl;
// Concat TVL from staking v2 if needed.
const missingDays = this.getPeriodDurationInDays(period) - indexedTvl.length;
let v2tvl: Pair[] = [];
if (missingDays > 0) {
const v2data = await this._statsService.getDappStakingTvl(network, period);
v2tvl = v2data.slice(-Math.min(missingDays, v2data.length));
}

return v2tvl.concat(indexedTvl);
} catch (e) {
console.error(e);
return [];
Expand Down Expand Up @@ -527,8 +543,20 @@ export class DappsStakingEvents extends ServiceBase implements IDappsStakingEven

private getApiUrl(network: NetworkType): string {
// For local development: `http://localhost:4350/graphql`;
return ['astar', 'shiden', 'shibuya'].includes(network)
? `https://squid.subsquid.io/dapps-staking-indexer-${network}/graphql`
: '';
switch (network) {
case 'astar':
// Latest indexer version is not deployed to production yet, so we are using staging deployment for now.
return `https://squid.subsquid.io/dapps-staking-indexer-${network}/v/v4/graphql`;
case 'shiden':
case 'shibuya':
return `https://squid.subsquid.io/dapps-staking-indexer-${network}/graphql`;
default:
return '';
}

// TODO revert to below after deployment of astar indexer
// return ['astar', 'shiden', 'shibuya'].includes(network)
// ? `https://squid.subsquid.io/dapps-staking-indexer-${network}/v/v4/graphql`
// : '';
}
}
7 changes: 0 additions & 7 deletions src/services/StatsIndexerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ export class StatsIndexerService extends ServiceBase implements IStatsIndexerSer
return [node.timestamp, node.tvlUsd];
});

// Add current TVL to the result, so we provide up to date TVL info.
try {
indexedTvl.push(await this.getCurrentTvlInUsd(network));
} catch (err) {
console.error(`Unable to fetch current TVL ${err}`);
}

return indexedTvl;
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 1e8c8f4

Please sign in to comment.