This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tvl): Use Zapper API as TVL fallback (#579)
- Loading branch information
Showing
1 changed file
with
45 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |