Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat(tvl): Use Zapper API as TVL fallback (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
JForsaken authored Jun 6, 2022
1 parent a2e1315 commit 7be18f9
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions src/stats/tvl/tvl.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Axios, { AxiosInstance } from 'axios';

import { AppService } from '~app/app.service';
import { Network } from '~types/network.interface';

import { TvlFetcherRegistry } from './tvl-fetcher.registry';

type AppTvl = {
appId: string;
appName: string;
network: Network;
tvl: number;
};

@Injectable()
export class TvlService {
private readonly axios: AxiosInstance;

constructor(
@Inject(TvlFetcherRegistry) private readonly tvlFetcherRegistry: TvlFetcherRegistry,
@Inject(AppService) private readonly appService: AppService,
) {}
@Inject(ConfigService) private readonly configService: ConfigService,
) {
this.axios = Axios.create({
baseURL: this.configService.get('zapperApi.url'),
params: { api_key: this.configService.get('zapperApi.key') },
});
}

private async getTvlFromApi({ appId, network }: { appId: string; network: Network }) {
try {
const { data } = await this.axios.get<AppTvl>(`/v1/apps/${appId}/tvl`, {
params: {
network,
},
});
return data;
} catch (e) {
return undefined;
}
}

async getTvl({ appId, network }: { appId: string; network: Network }) {
private async getTvlLocally({ appId, network }: { appId: string; network: Network }) {
const app = this.appService.getApp(appId);
const tvlFetcher = this.tvlFetcherRegistry.get({ network, appId: app.id });
const tvl = tvlFetcher ? await tvlFetcher.getTvl() : 0;
const tvl = (await tvlFetcher?.getTvl()) ?? 0;

return { appId: appId, appName: app.name, network, tvl };
}

async getTvl({ appId, network }: { appId: string; network: Network }): Promise<AppTvl> {
try {
return await this.getTvlLocally({ appId, network });
} catch (e) {
const apiTvl = await this.getTvlFromApi({ appId, network });
if (!apiTvl) throw new NotFoundException('No TVL registered on Studio and on Zapper API');
return apiTvl;
}
}
}

0 comments on commit 7be18f9

Please sign in to comment.