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 total assets #745

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 24 additions & 13 deletions functions/src/getTotalAssetsTokenized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,41 @@ import { chunkedFetch } from './subgraphUtils'

export default async function getTotalAssetsTokenized(req: Request, res: Response) {
try {
const request = await chunkedFetch({
getQuery: getLoansQuery,
getProperty: (obj) => {
const loans = obj.pools.reduce((acc, pool) => {
acc.push(pool.loans)
return acc
}, [])
return loans
},
const poolIds = await chunkedFetch({
getQuery: getPoolsQuery,
getProperty: (obj) => obj?.pools,
})

let totalAssetsTokenized = request.flat().length
const requests = poolIds.map(({ id }) =>
chunkedFetch({
id,
getQuery: getAssetsQuery,
getProperty: (obj) => obj?.pool?.assets,
})
)

let totalAssetsTokenized = await Promise.all(requests).then((results) => results.flat().length)

return res.status(200).send(JSON.stringify({ totalAssetsTokenized }))
} catch (error) {
return res.status(422).send(JSON.stringify(error))
}
}

function getLoansQuery({ skip, first }: { skip: number; first: number; id: string }) {
function getPoolsQuery({ skip, first }: { skip: number; first: number }) {
return `
query {
pools(first: ${first}, skip: ${skip}) {
id
}
}`
}

function getAssetsQuery({ skip, first, id }: { skip: number; first: number; id: string }) {
return `
query {
pools {
loans(first: ${first}, skip: ${skip}) {
pool(id: "${id}") {
assets (first:1000, filter: { totalBorrowed: { greaterThan: "0" } }, first: ${first}, offset: ${skip}) {
id
}
}
Expand Down
Loading