diff --git a/src/business-logic/kleros/types.ts b/src/business-logic/kleros/types.ts index 5686d20..923730c 100644 --- a/src/business-logic/kleros/types.ts +++ b/src/business-logic/kleros/types.ts @@ -32,3 +32,8 @@ export type MetadataColumn = { type: KLEROS_LIST_TYPES isIdentifier: boolean } + +export type ThirdPartyMetadataColumn = MetadataColumn & { + replacementKey?: string + isAutoFillable: boolean +} diff --git a/src/services/badgeModels/badgeModels.ts b/src/services/badgeModels/badgeModels.ts index da68c66..af41166 100644 --- a/src/services/badgeModels/badgeModels.ts +++ b/src/services/badgeModels/badgeModels.ts @@ -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 @@ -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 getById(badgeModelId: string): Promise @@ -68,8 +76,18 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels * * @param badgeModelId */ - async getMetadataOfBadgeModel(badgeModelId: string): Promise { - return await this.subgraph.badgeModelMetadataById({ id: badgeModelId }) + async getMetadataOfBadgeModel( + badgeModelId: string, + ): Promise { + 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 }) + } } /** @@ -78,22 +96,49 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels * @param badgeModelId * @returns Array */ - public async getEvidenceRequirementsOfBadgeModel(badgeModelId: string): Promise> { + public async getEvidenceRequirementsOfBadgeModel( + badgeModelId: string, + ): Promise> { // 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.') } } diff --git a/src/subgraph/dev/queries/badgeModelMetadata.ts b/src/subgraph/dev/queries/badgeModelMetadata.ts index 30e68bf..b15c91c 100644 --- a/src/subgraph/dev/queries/badgeModelMetadata.ts +++ b/src/subgraph/dev/queries/badgeModelMetadata.ts @@ -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 + } + } +` diff --git a/src/subgraph/dev/queries/fragments/badgeModel.ts b/src/subgraph/dev/queries/fragments/badgeModel.ts index 0c5ac4e..fc17bb1 100644 --- a/src/subgraph/dev/queries/fragments/badgeModel.ts +++ b/src/subgraph/dev/queries/fragments/badgeModel.ts @@ -27,3 +27,10 @@ gql` challengePeriodDuration } ` + +gql` + fragment BadgeModelThirdPartyMetaData on BadgeModelThirdPartyMetaData { + id + requirementsIPFSHash + } +` diff --git a/src/subgraph/fakeGeneratedSubgraph.ts b/src/subgraph/fakeGeneratedSubgraph.ts index 6c7b75b..1069e86 100644 --- a/src/subgraph/fakeGeneratedSubgraph.ts +++ b/src/subgraph/fakeGeneratedSubgraph.ts @@ -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 { diff --git a/src/subgraph/prod/queries/badgeModelMetadata.ts b/src/subgraph/prod/queries/badgeModelMetadata.ts index 30e68bf..b15c91c 100644 --- a/src/subgraph/prod/queries/badgeModelMetadata.ts +++ b/src/subgraph/prod/queries/badgeModelMetadata.ts @@ -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 + } + } +` diff --git a/src/subgraph/prod/queries/fragments/badgeModel.ts b/src/subgraph/prod/queries/fragments/badgeModel.ts index 0c5ac4e..fc17bb1 100644 --- a/src/subgraph/prod/queries/fragments/badgeModel.ts +++ b/src/subgraph/prod/queries/fragments/badgeModel.ts @@ -27,3 +27,10 @@ gql` challengePeriodDuration } ` + +gql` + fragment BadgeModelThirdPartyMetaData on BadgeModelThirdPartyMetaData { + id + requirementsIPFSHash + } +` diff --git a/src/subgraph/staging/queries/badgeModelMetadata.ts b/src/subgraph/staging/queries/badgeModelMetadata.ts index 30e68bf..b15c91c 100644 --- a/src/subgraph/staging/queries/badgeModelMetadata.ts +++ b/src/subgraph/staging/queries/badgeModelMetadata.ts @@ -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 + } + } +` diff --git a/src/subgraph/staging/queries/fragments/badgeModel.ts b/src/subgraph/staging/queries/fragments/badgeModel.ts index 0c5ac4e..fc17bb1 100644 --- a/src/subgraph/staging/queries/fragments/badgeModel.ts +++ b/src/subgraph/staging/queries/fragments/badgeModel.ts @@ -27,3 +27,10 @@ gql` challengePeriodDuration } ` + +gql` + fragment BadgeModelThirdPartyMetaData on BadgeModelThirdPartyMetaData { + id + requirementsIPFSHash + } +`