From 4eb4f45ae221b84773173526342345ada239812b Mon Sep 17 00:00:00 2001 From: Adam Stox Date: Mon, 18 Dec 2023 17:31:54 -0600 Subject: [PATCH] fix subgraph calls --- functions/src/getPoolsData.ts | 6 ++-- functions/src/getTotalAssetsTokenized.ts | 37 +++++++++--------------- functions/src/subgraphUtils.ts | 5 ++-- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/functions/src/getPoolsData.ts b/functions/src/getPoolsData.ts index 87baf693..90d9a2a0 100644 --- a/functions/src/getPoolsData.ts +++ b/functions/src/getPoolsData.ts @@ -8,7 +8,7 @@ function getPoolsQuery({ skip, first }: { skip: number; first: number }) { pools(first: ${first}, skip: ${skip}) { reserve, assetValue, - totalRepaysAggregatedAmount, + totalBorrowsAggregatedAmount, totalDebt } }` @@ -37,7 +37,7 @@ function getTotalValueLocked(pools) { function getTotalAssetsFinanced(pools) { return pools .reduce((sum, pool) => { - return sum.add(new BN(pool.totalRepaysAggregatedAmount)).add(new BN(pool.totalDebt)) + return sum.add(new BN(pool.totalBorrowsAggregatedAmount)) }, new BN(0)) .div(new BN(10).pow(new BN(24))) .toString() @@ -76,6 +76,6 @@ export default async function getPoolsData(req: Request, res: Response) { }) ) } catch (error) { - return res.status(422).send(JSON.stringify(error)) + return res.status(500).send(error) } } diff --git a/functions/src/getTotalAssetsTokenized.ts b/functions/src/getTotalAssetsTokenized.ts index aa87b99b..0408c9c6 100644 --- a/functions/src/getTotalAssetsTokenized.ts +++ b/functions/src/getTotalAssetsTokenized.ts @@ -3,20 +3,18 @@ import { chunkedFetch } from './subgraphUtils' export default async function getTotalAssetsTokenized(req: Request, res: Response) { try { - const poolIds = await chunkedFetch({ - getQuery: getPoolsQuery, - getProperty: (obj) => obj?.pools, + const request = await chunkedFetch({ + getQuery: getLoansQuery, + getProperty: (obj) => { + const loans = obj.pools.reduce((acc, pool) => { + acc.push(pool.loans) + return acc + }, []) + return loans + }, }) - const requests = poolIds.map(({ id }) => - chunkedFetch({ - id, - getQuery: getLoansQuery, - getProperty: (obj) => obj?.pool?.loans, - }) - ) - - let totalAssetsTokenized = await Promise.all(requests).then((results) => results.flat().length) + let totalAssetsTokenized = request.flat().length return res.status(200).send(JSON.stringify({ totalAssetsTokenized })) } catch (error) { @@ -24,20 +22,11 @@ export default async function getTotalAssetsTokenized(req: Request, res: Respons } } -function getPoolsQuery({ skip, first }: { skip: number; first: number }) { - return ` - query { - pools(first: ${first}, skip: ${skip}) { - id - } - }` -} - -function getLoansQuery({ skip, first, id }: { skip: number; first: number; id: string }) { +function getLoansQuery({ skip, first }: { skip: number; first: number; id: string }) { return ` query { - pool(id: "${id}") { - loans(where: {borrowsAggregatedAmount_gt: "0"}, first: ${first}, skip: ${skip}) { + pools { + loans(first: ${first}, skip: ${skip}) { id } } diff --git a/functions/src/subgraphUtils.ts b/functions/src/subgraphUtils.ts index e068a6c8..5b2762d6 100644 --- a/functions/src/subgraphUtils.ts +++ b/functions/src/subgraphUtils.ts @@ -13,7 +13,7 @@ async function fetchSubgraphData(query) { throw new Error(`Failed to retrieve pool data for TVL: ${response.statusText}`) } - const { data } = await response.clone().json() + const { data } = await response.json() return data } @@ -22,7 +22,7 @@ export async function chunkedFetch({ day, getQuery, getProperty, - chunkSize = 100, + chunkSize = 1000, }: { id?: string day?: number @@ -46,7 +46,6 @@ export async function chunkedFetch({ const response = await fetchSubgraphData(query) const property = getProperty(response) - if (property && property.length) { data.push(...property) }