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

Commit

Permalink
fix: euler tvl (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpoulin authored Jun 4, 2022
1 parent c86cc15 commit c734b46
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/apps/euler/ethereum/euler.tvl-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject } from '@nestjs/common';
import { gql } from 'graphql-request';
import { sum } from 'lodash';

import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface';
import { Register } from '~app-toolkit/decorators';
Expand All @@ -12,8 +13,9 @@ const appId = EULER_DEFINITION.id;
const network = Network.ETHEREUM_MAINNET;

interface EulerMarket {
totalBalancesEth: string;
totalBorrowsEth: string;
symbol: string;
totalBalances: string;
totalBorrows: string;
}

interface EulerMarketsResponse {
Expand All @@ -26,8 +28,9 @@ const query = gql`
{
eulerMarketStore(id: "euler-market-store") {
markets {
totalBalancesEth
totalBorrowsEth
symbol
totalBalances
totalBorrows
}
}
}
Expand All @@ -38,11 +41,21 @@ export class EthereumEulerTvlFetcher implements TvlFetcher {
constructor(@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit) {}

async getTvl() {
const prices = await this.appToolkit.getBaseTokenPrices(network);
const endpoint = 'https://api.thegraph.com/subgraphs/name/euler-xyz/euler-mainnet';
const data = await this.appToolkit.helpers.theGraphHelper.request<EulerMarketsResponse>({ endpoint, query });
return data.eulerMarketStore.markets.reduce(
(acc, cur) => acc + (Number(cur.totalBalancesEth) - Number(cur.totalBorrowsEth)),
0,
);
const markets = data.eulerMarketStore.markets;

const tvlPerMarket = markets.map(market => {
const baseToken = prices.find(x => x.symbol === market.symbol);
if (!baseToken) return 0;

const totalBalance = Number(market.totalBalances) / 10 ** 18;
const totalBorrows = Number(market.totalBorrows) / 10 ** 18;

return (totalBalance - totalBorrows) * baseToken.price;
});

return sum(tvlPerMarket);
}
}

0 comments on commit c734b46

Please sign in to comment.