Skip to content

Commit

Permalink
get reports
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored and hieronx committed Jun 27, 2023
1 parent 26f3ba7 commit a286aed
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
49 changes: 49 additions & 0 deletions centrifuge-js/src/modules/pod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { u8aToHex } from '@polkadot/util'
import { decodeAddress } from '@polkadot/util-crypto'
import { PoolMetadata } from './pools'

type JobResponse = {
JobID: string
Expand Down Expand Up @@ -106,6 +107,53 @@ export function getPodModule() {
return res as T
}

async function callIndexer<T = any>(indexerUrl: string, query: string, variables?: any) {
const res = await fetch(indexerUrl, {
method: 'POST',
body: JSON.stringify({ query, variables }),
headers: {
'Content-Type': 'application/json',
},
}).then(async (res) => {
const { data, errors } = await res.json()
if (errors?.length) {
throw errors
}
return data as T
})
return res as T
}

async function getReports(
args: [
indexerUrl: string,
poolMetadata: Required<Pick<PoolMetadata, 'aggregates' | 'reports'>>,
page?: keyof Exclude<PoolMetadata['reports'], undefined>
]
) {
const [indexerUrl, poolMetadata, page] = args
const aggregateNames = Array.from(
new Set(
(page ? [poolMetadata.reports[page]] : Object.values(poolMetadata.reports)).flatMap((page) =>
page.sections.map((s) => s.aggregate)
)
)
)
const res = await callIndexer(
indexerUrl,
`query {
aggregations {
${aggregateNames.map(
(n) => `${n}
`
)}
}
}`,
{}
)
return res.query.aggregations
}

async function getJob(args: [podUrl: string, token: string, jobId: string]) {
const [podUrl, token, jobId] = args
const res = await callPod<JobResponse>(podUrl, `v2/jobs/${jobId}`, 'get', token)
Expand Down Expand Up @@ -220,5 +268,6 @@ export function getPodModule() {
getCommittedDocument,
awaitJob,
getSelf,
getReports,
}
}
11 changes: 11 additions & 0 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,17 @@ export type PoolMetadata = {
id: string
createdAt: string
}[]
aggregates?: Record<string, Record<string, object>[]>
reports?: {
poolOverview: {
sections: {
name: string
aggregate: string
view: 'chart' | 'table' | 'counter'
viewData: any
}[]
}
}
adminMultisig?: {
signers: string[]
threshold: number
Expand Down

0 comments on commit a286aed

Please sign in to comment.