Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stats on homepage #722

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
pools(first: ${first}, skip: ${skip}) {
reserve,
assetValue,
totalRepaysAggregatedAmount,
totalBorrowsAggregatedAmount,
totalDebt
}
}`
Expand Down Expand Up @@ -37,7 +37,7 @@
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 @@
})
)
} catch (error) {
return res.status(422).send(JSON.stringify(error))
return res.status(500).send(error)
Dismissed Show dismissed Hide dismissed
}
}
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
Loading