Skip to content

Commit

Permalink
Improve getMetadataOfBadgeModel and getEvidenceRequirementsOfBadgeModel
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMadoery committed Aug 21, 2024
1 parent 39d55e5 commit 6a48264
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/business-logic/kleros/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ export type MetadataColumn = {
type: KLEROS_LIST_TYPES
isIdentifier: boolean
}

export type ThirdPartyMetadataColumn = MetadataColumn & {
replacementKey?: string
isAutoFillable: boolean
}
79 changes: 62 additions & 17 deletions src/services/badgeModels/badgeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import {
BadgeModel_Filter as BadgeModel_Filter_DEV,
BadgeModelByIdQuery as BadgeModelByIdQuery_DEV,
BadgeModelsQuery as BadgeModelsQuery_DEV,
BadgeModelMetadataByIdQuery as BadgeModelMetadataByIdQuery_DEV,
BadgeModelKlerosMetadataByIdQuery as BadgeModelMetadataByIdQuery_DEV,
BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_DEV,
} from '@subgraph/dev/generated/subgraph'
import {
BadgeModel_Filter as BadgeModel_Filter_STAGING,
BadgeModelByIdQuery as BadgeModelByIdQuery_STAGING,
BadgeModelsQuery as BadgeModelsQuery_STAGING,
BadgeModelMetadataByIdQuery as BadgeModelMetadataByIdQuery_STAGING,
BadgeModelKlerosMetadataByIdQuery as BadgeModelMetadataByIdQuery_STAGING,
BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_STAGING,
} from '@subgraph/staging/generated/subgraph'
import {
BadgeModel_Filter as BadgeModel_Filter_PROD,
BadgeModelByIdQuery as BadgeModelByIdQuery_PROD,
BadgeModelsQuery as BadgeModelsQuery_PROD,
BadgeModelMetadataByIdQuery as BadgeModelMetadataByIdQuery_PROD,
BadgeModelKlerosMetadataByIdQuery as BadgeModelMetadataByIdQuery_PROD,
BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_PROD,
} from '@subgraph/prod/generated/subgraph'
import { TheBadgeSDKConfig } from '../../config'
import { MetadataColumn } from '@businessLogic/kleros/types'
import { MetadataColumn, ThirdPartyMetadataColumn } from '@businessLogic/kleros/types'
import { getFromIPFS } from '@utils/ipfs'

type BadgeModel_Filter = BadgeModel_Filter_DEV | BadgeModel_Filter_STAGING | BadgeModel_Filter_PROD
Expand All @@ -28,6 +31,11 @@ type BadgeModelMetadataByIdQuery =
| BadgeModelMetadataByIdQuery_STAGING
| BadgeModelMetadataByIdQuery_PROD

type BadgeModelThirdPartyMetadataByIdQuery =
| BadgeModelThirdPartyMetaDataByIdQuery_DEV
| BadgeModelThirdPartyMetaDataByIdQuery_STAGING
| BadgeModelThirdPartyMetaDataByIdQuery_PROD

interface BadgeModelsServiceMethods {
get(searchParams?: { first: number; skip: number; filter?: BadgeModel_Filter }): Promise<BadgeModelsQuery>
getById(badgeModelId: string): Promise<BadgeModelByIdQuery>
Expand Down Expand Up @@ -68,8 +76,18 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels
*
* @param badgeModelId
*/
async getMetadataOfBadgeModel(badgeModelId: string): Promise<BadgeModelMetadataByIdQuery> {
return await this.subgraph.badgeModelMetadataById({ id: badgeModelId })
async getMetadataOfBadgeModel(
badgeModelId: string,
): Promise<BadgeModelMetadataByIdQuery | BadgeModelThirdPartyMetadataByIdQuery> {
const badgeModel = await this.getById(badgeModelId)
if (!badgeModel) {
throw new Error('TheBadge SDK: Missing BadgeModel for the given badge model id, provide a valid model id.')
}
if (badgeModel.badgeModel?.controllerType === 'thirdParty') {
return await this.subgraph.badgeModelThirdPartyMetaDataById({ id: badgeModelId })
} else {
return await this.subgraph.badgeModelKlerosMetadataById({ id: badgeModelId })
}
}

/**
Expand All @@ -78,22 +96,49 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels
* @param badgeModelId
* @returns Array<MetadataColumn>
*/
public async getEvidenceRequirementsOfBadgeModel(badgeModelId: string): Promise<Array<MetadataColumn>> {
public async getEvidenceRequirementsOfBadgeModel(
badgeModelId: string,
): Promise<Array<MetadataColumn | ThirdPartyMetadataColumn>> {
// take ipfs uri from metadata of the badge model
const badgeModelMetadataResponse = await this.getMetadataOfBadgeModel(badgeModelId)
const ipfsDataUri = badgeModelMetadataResponse?.badgeModelKlerosMetaData?.registrationUri
if (!ipfsDataUri) {
throw new Error('TheBadge SDK: Missing registrationUri for the given badge model id, provide a valid model id.')
if ('badgeModelKlerosMetaData' in badgeModelMetadataResponse) {
const ipfsDataUri = badgeModelMetadataResponse?.badgeModelKlerosMetaData?.registrationUri
if (!ipfsDataUri) {
throw new Error('TheBadge SDK: Missing registrationUri for the given badge model id, provide a valid model id.')
}

// obtain evidences required
const { result, error } = await getFromIPFS<{ columns: MetadataColumn[] }>(ipfsDataUri, this.env)
const evidencesList = result?.content?.columns
if (error || !evidencesList) {
throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.')
}

// return the list of evidences required
return evidencesList
}
if ('badgeModelThirdPartyMetaData' in badgeModelMetadataResponse) {
const ipfsDataUri = badgeModelMetadataResponse?.badgeModelThirdPartyMetaData?.requirementsIPFSHash
if (!ipfsDataUri) {
throw new Error(
'TheBadge SDK: Missing requirementsIPFSHash for the given badge model id, provide a valid model id.',
)
}

// obtain evidences required
const { result, error } = await getFromIPFS<{
columns: ThirdPartyMetadataColumn[]
}>(ipfsDataUri, this.env)

const evidencesList = result?.content?.columns
if (error || !evidencesList) {
throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.')
}

// obtain evidences required
const { result, error } = await getFromIPFS<{ metadata: { columns: MetadataColumn[] } }>(ipfsDataUri, this.env)
const evidencesList = result?.content?.metadata?.columns
if (error || !evidencesList) {
throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.')
// return the list of evidences required
return evidencesList
}

// return the list of evidences required
return evidencesList
throw new Error('TheBadge SDK: Error obtaining required evidences for given badgeModelId.')
}
}
12 changes: 10 additions & 2 deletions src/subgraph/dev/queries/badgeModelMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import gql from 'graphql-tag'

// TODO: hardcoded for kleros, fix it.

export const BADGE_MODEL_METADATA_BY_ID = gql`
query badgeModelMetadataById($id: ID!) {
export const BADGE_MODEL_KLEROS_METADATA_BY_ID = gql`
query badgeModelKlerosMetadataById($id: ID!) {
badgeModelKlerosMetaData(id: $id) {
...BadgeModelKlerosMetadata
}
}
`

export const BADGE_MODEL_THIRD_PARTY_METADATA_BY_ID = gql`
query badgeModelThirdPartyMetaDataById($id: ID!) {
badgeModelThirdPartyMetaData(id: $id) {
...BadgeModelThirdPartyMetaData
}
}
`
7 changes: 7 additions & 0 deletions src/subgraph/dev/queries/fragments/badgeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ gql`
challengePeriodDuration
}
`

gql`
fragment BadgeModelThirdPartyMetaData on BadgeModelThirdPartyMetaData {
id
requirementsIPFSHash
}
`
2 changes: 1 addition & 1 deletion src/subgraph/fakeGeneratedSubgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { GraphQLClient } from 'graphql-request'
import * as Dom from 'graphql-request/dist/types.dom'
import * as Dom from 'graphql-request/build/cjs/types.dom'

export function getSdk(client: GraphQLClient) {
return {
Expand Down
12 changes: 10 additions & 2 deletions src/subgraph/prod/queries/badgeModelMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import gql from 'graphql-tag'

// TODO: hardcoded for kleros, fix it.

export const BADGE_MODEL_METADATA_BY_ID = gql`
query badgeModelMetadataById($id: ID!) {
export const BADGE_MODEL_KLEROS_METADATA_BY_ID = gql`
query badgeModelKlerosMetadataById($id: ID!) {
badgeModelKlerosMetaData(id: $id) {
...BadgeModelKlerosMetadata
}
}
`

export const BADGE_MODEL_THIRD_PARTY_METADATA_BY_ID = gql`
query badgeModelThirdPartyMetaDataById($id: ID!) {
badgeModelThirdPartyMetaData(id: $id) {
...BadgeModelThirdPartyMetaData
}
}
`
7 changes: 7 additions & 0 deletions src/subgraph/prod/queries/fragments/badgeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ gql`
challengePeriodDuration
}
`

gql`
fragment BadgeModelThirdPartyMetaData on BadgeModelThirdPartyMetaData {
id
requirementsIPFSHash
}
`
12 changes: 10 additions & 2 deletions src/subgraph/staging/queries/badgeModelMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import gql from 'graphql-tag'

// TODO: hardcoded for kleros, fix it.

export const BADGE_MODEL_METADATA_BY_ID = gql`
query badgeModelMetadataById($id: ID!) {
export const BADGE_MODEL_KLEROS_METADATA_BY_ID = gql`
query badgeModelKlerosMetadataById($id: ID!) {
badgeModelKlerosMetaData(id: $id) {
...BadgeModelKlerosMetadata
}
}
`

export const BADGE_MODEL_THIRD_PARTY_METADATA_BY_ID = gql`
query badgeModelThirdPartyMetaDataById($id: ID!) {
badgeModelThirdPartyMetaData(id: $id) {
...BadgeModelThirdPartyMetaData
}
}
`
7 changes: 7 additions & 0 deletions src/subgraph/staging/queries/fragments/badgeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ gql`
challengePeriodDuration
}
`

gql`
fragment BadgeModelThirdPartyMetaData on BadgeModelThirdPartyMetaData {
id
requirementsIPFSHash
}
`

0 comments on commit 6a48264

Please sign in to comment.