From 6a4826405b09c84c9258b4c551400c424c12a67d Mon Sep 17 00:00:00 2001 From: Federico Madoery Date: Tue, 20 Aug 2024 21:53:31 -0300 Subject: [PATCH 1/4] Improve getMetadataOfBadgeModel and getEvidenceRequirementsOfBadgeModel --- src/business-logic/kleros/types.ts | 5 ++ src/services/badgeModels/badgeModels.ts | 79 +++++++++++++++---- .../dev/queries/badgeModelMetadata.ts | 12 ++- .../dev/queries/fragments/badgeModel.ts | 7 ++ src/subgraph/fakeGeneratedSubgraph.ts | 2 +- .../prod/queries/badgeModelMetadata.ts | 12 ++- .../prod/queries/fragments/badgeModel.ts | 7 ++ .../staging/queries/badgeModelMetadata.ts | 12 ++- .../staging/queries/fragments/badgeModel.ts | 7 ++ 9 files changed, 119 insertions(+), 24 deletions(-) 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 + } +` From 8977875d624f76849acd6586bb31e6f40f6d2f1b Mon Sep 17 00:00:00 2001 From: Federico Madoery Date: Mon, 2 Sep 2024 14:05:38 -0300 Subject: [PATCH 2/4] add badgeModelByIdWithMetadata --- src/services/badgeModels/badgeModels.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/services/badgeModels/badgeModels.ts b/src/services/badgeModels/badgeModels.ts index 64a9a3a..428a7ee 100644 --- a/src/services/badgeModels/badgeModels.ts +++ b/src/services/badgeModels/badgeModels.ts @@ -4,6 +4,7 @@ import { BadgeModelsQuery as BadgeModelsQuery_DEV, BadgeModelKlerosMetadataByIdQuery as BadgeModelMetadataByIdQuery_DEV, BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_DEV, + BadgeModelByIdWithMetadataQuery as BadgeModelByIdWithMetadataQuery_DEV, } from '@subgraph/dev/generated/subgraph' import { BadgeModel_Filter as BadgeModel_Filter_STAGING, @@ -11,6 +12,7 @@ import { BadgeModelsQuery as BadgeModelsQuery_STAGING, BadgeModelKlerosMetadataByIdQuery as BadgeModelMetadataByIdQuery_STAGING, BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_STAGING, + BadgeModelByIdWithMetadataQuery as BadgeModelByIdWithMetadataQuery_STAGING, } from '@subgraph/staging/generated/subgraph' import { BadgeModel_Filter as BadgeModel_Filter_PROD, @@ -18,6 +20,7 @@ import { BadgeModelsQuery as BadgeModelsQuery_PROD, BadgeModelKlerosMetadataByIdQuery as BadgeModelMetadataByIdQuery_PROD, BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_PROD, + BadgeModelByIdWithMetadataQuery as BadgeModelByIdWithMetadataQuery_PROD, } from '@subgraph/prod/generated/subgraph' import { TheBadgeSDKConfig } from '../../config' import { MetadataColumn, BadgeModelKlerosMetadata, ThirdPartyMetadataColumn } from '@businessLogic/kleros/types' @@ -26,6 +29,10 @@ import { getFromIPFS } from '@utils/ipfs' type BadgeModel_Filter = BadgeModel_Filter_DEV | BadgeModel_Filter_STAGING | BadgeModel_Filter_PROD type BadgeModelByIdQuery = BadgeModelByIdQuery_DEV | BadgeModelByIdQuery_STAGING | BadgeModelByIdQuery_PROD type BadgeModelsQuery = BadgeModelsQuery_DEV | BadgeModelsQuery_STAGING | BadgeModelsQuery_PROD +type BadgeModelByIdWithMetadataQuery = + | BadgeModelByIdWithMetadataQuery_DEV + | BadgeModelByIdWithMetadataQuery_STAGING + | BadgeModelByIdWithMetadataQuery_PROD type BadgeModelMetadataByIdQuery = | BadgeModelMetadataByIdQuery_DEV | BadgeModelMetadataByIdQuery_STAGING @@ -71,6 +78,15 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels return await this.subgraph.badgeModelById({ id: badgeModelId }) } + /** + * Obtain a badge model giving its id and also contains its metadata, depending on + * the controllerType you would get badgeModelKleros or badgeModelThirdParty + * @param badgeModelId + */ + async getByIdWithMetadata(badgeModelId: string): Promise { + return await this.subgraph.badgeModelByIdWithMetadata({ id: badgeModelId }) + } + /** * Obtain the metadata of a badge model giving its id * From a0ad656a55cb2c750799771ba40d27e107a17844 Mon Sep 17 00:00:00 2001 From: Federico Madoery Date: Mon, 2 Sep 2024 14:07:17 -0300 Subject: [PATCH 3/4] add badgeModelByIdWithMetadata on class --- src/services/badgeModels/badgeModels.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/badgeModels/badgeModels.ts b/src/services/badgeModels/badgeModels.ts index 428a7ee..939ef4b 100644 --- a/src/services/badgeModels/badgeModels.ts +++ b/src/services/badgeModels/badgeModels.ts @@ -46,6 +46,7 @@ type BadgeModelThirdPartyMetadataByIdQuery = interface BadgeModelsServiceMethods { get(searchParams?: { first: number; skip: number; filter?: BadgeModel_Filter }): Promise getById(badgeModelId: string): Promise + getByIdWithMetadata(badgeModelId: string): Promise getMetadataOfBadgeModel(badgeModelId: string): Promise getEvidenceRequirementsOfBadgeModel(badgeModelId: string): Promise> // create(userAddress: string, params: BadgeModelCreationParams) TODO coming soon From cf669062274597122d73aca46e66ffdde8783fd9 Mon Sep 17 00:00:00 2001 From: Federico Madoery Date: Mon, 2 Sep 2024 14:17:12 -0300 Subject: [PATCH 4/4] 0.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 38e3b5d..bd7381e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thebadge/sdk", - "version": "0.2.16", + "version": "0.3.0", "license": "MIT", "main": "dist/index.js", "module": "dist/index.es.js",