Skip to content

Commit

Permalink
fix: Improve badgemodel metadata query (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Madoery <[email protected]>
  • Loading branch information
Agupane and FedeMadoery authored Aug 21, 2024
1 parent 5067e76 commit 0f24467
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thebadge/sdk",
"version": "0.2.15",
"version": "0.2.16",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down
41 changes: 41 additions & 0 deletions src/business-logic/kleros/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,47 @@ export enum KLEROS_LIST_TYPES {
LONG_TEXT = 'long text',
}

export type BadgeModelKlerosMetadata = {
ipfsHash: string
ipfsUrl: string
s3Url: string
extension: string
content: {
name: string
title: string
description: string
rulingOptions: { titles: Record<string, string>; descriptions: Record<string, string> }
category: string
question: string
fileURI: {
ipfsHash: string
ipfsUrl: string
s3Url: string
extension: string
}
fileTypeExtension: string
evidenceDisplayInterfaceURI: string
dynamicScriptURI: {
ipfsHash: string
ipfsUrl: string
s3Url: string
extension: string
}
dynamicScriptRequiredParams: string[]
metadata: {
tcrTitle: string
tcrDescription: string
columns: MetadataColumn[]
itemName: string
itemNamePlural: string
logoURI: string
requireRemovalEvidence: boolean
isTCRofTCRs: boolean
relTcrDisabled: boolean
}
}
}

export type MetadataColumn = {
label: string
description: string
Expand Down
14 changes: 14 additions & 0 deletions src/services/badgeModels/badgeModels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ describe('BadgeModelsService', () => {
expect('get' in badgeModelsService).toBeTruthy()
expect('notExistingMethod' in badgeModelsService).toBeFalsy()
})
it('should return the evidences list for tpBadgeModel', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const badgeModelsService = new BadgeModelsService(11155111, {
rpcProviderConfig: { name: RPCProvider.infura, apiKey: '' },
})

const metadata = await badgeModelsService.getEvidenceRequirementsOfBadgeModel(2)

expect(metadata.length).toBe(1)
expect(badgeModelsService).not.toBeUndefined()
expect('get' in badgeModelsService).toBeTruthy()
expect('notExistingMethod' in badgeModelsService).toBeFalsy()
})
})
95 changes: 56 additions & 39 deletions src/services/badgeModels/badgeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_PROD,
} from '@subgraph/prod/generated/subgraph'
import { TheBadgeSDKConfig } from '../../config'
import { MetadataColumn, ThirdPartyMetadataColumn } from '@businessLogic/kleros/types'
import { BadgeModelKlerosMetadata, MetadataColumn, ThirdPartyMetadataColumn } from '@businessLogic/kleros/types'
import { getFromIPFS } from '@utils/ipfs'

type BadgeModel_Filter = BadgeModel_Filter_DEV | BadgeModel_Filter_STAGING | BadgeModel_Filter_PROD
Expand Down Expand Up @@ -90,55 +90,72 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels
}
}

private async getKlerosEvidenceRequirementsOfBadgeModel(
evidencesListIpfsHash?: string,
): Promise<Array<MetadataColumn>> {
if (!evidencesListIpfsHash) {
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(evidencesListIpfsHash, this.env)

const evidencesList = (result as BadgeModelKlerosMetadata)?.content?.metadata.columns

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

return evidencesList
}

private async getThirdPartyEvidenceRequirementsOfBadgeModel(
evidencesListIpfsHash?: string,
): Promise<Array<ThirdPartyMetadataColumn>> {
if (!evidencesListIpfsHash) {
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<{
requirementsColumns: ThirdPartyMetadataColumn[]
}>(evidencesListIpfsHash, this.env)

const evidencesList = result?.content?.requirementsColumns

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

return evidencesList
}

/**
* Get evidence requirements of a badge model giving its id
*
* @param badgeModelId
* @returns Array<MetadataColumn>
*/
public async getEvidenceRequirementsOfBadgeModel(
badgeModelId: string,
): Promise<Array<MetadataColumn | ThirdPartyMetadataColumn>> {
public async getEvidenceRequirementsOfBadgeModel(badgeModelId: string): Promise<Array<MetadataColumn>> {
// take ipfs uri from metadata of the badge model
let evidencesList

const badgeModelMetadataResponse = await this.getMetadataOfBadgeModel(badgeModelId)
if ('badgeModelThirdPartyMetaData' in badgeModelMetadataResponse) {
evidencesList = await this.getThirdPartyEvidenceRequirementsOfBadgeModel(
badgeModelMetadataResponse?.badgeModelThirdPartyMetaData?.requirementsIPFSHash,
)
}
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
evidencesList = await this.getKlerosEvidenceRequirementsOfBadgeModel(
badgeModelMetadataResponse?.badgeModelKlerosMetaData?.registrationUri,
)
}
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.')
}

// return the list of evidences required
return evidencesList

if (!evidencesList) {
throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.')
}

throw new Error('TheBadge SDK: Error obtaining required evidences for given badgeModelId.')
return evidencesList
}
}

0 comments on commit 0f24467

Please sign in to comment.