Skip to content

Commit

Permalink
fix subgraph calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AStox committed Dec 18, 2023
1 parent 76b1b9e commit 4eb4f45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
6 changes: 3 additions & 3 deletions functions/src/getPoolsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getPoolsQuery({ skip, first }: { skip: number; first: number }) {
pools(first: ${first}, skip: ${skip}) {
reserve,
assetValue,
totalRepaysAggregatedAmount,
totalBorrowsAggregatedAmount,
totalDebt
}
}`
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Check warning

Code scanning / CodeQL

Information exposure through a stack trace Medium

This information exposed to the user depends on
stack trace information
.
}
}
37 changes: 13 additions & 24 deletions functions/src/getTotalAssetsTokenized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,30 @@ 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) {
return res.status(422).send(JSON.stringify(error))
}
}

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
}
}
Expand Down
5 changes: 2 additions & 3 deletions functions/src/subgraphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -22,7 +22,7 @@ export async function chunkedFetch({
day,
getQuery,
getProperty,
chunkSize = 100,
chunkSize = 1000,
}: {
id?: string
day?: number
Expand All @@ -46,7 +46,6 @@ export async function chunkedFetch({
const response = await fetchSubgraphData(query)

const property = getProperty(response)

if (property && property.length) {
data.push(...property)
}
Expand Down

0 comments on commit 4eb4f45

Please sign in to comment.