diff --git a/sdk/examples/attestation/attestationExamples.ts b/sdk/examples/attestation/attestationExamples.ts index f2389db2..5b955756 100644 --- a/sdk/examples/attestation/attestationExamples.ts +++ b/sdk/examples/attestation/attestationExamples.ts @@ -2,21 +2,34 @@ import VeraxSdk from "../../src/VeraxSdk"; export default class AttestationExamples { private veraxSdk: VeraxSdk; + constructor(_veraxSdk: VeraxSdk) { this.veraxSdk = _veraxSdk; } + async run(methodName: string = "") { - if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") { console.log( await this.veraxSdk.attestation.findOneById( "0x00000000000000000000000000000000000000000000000000000000000007b5", ), ); - if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") + } + + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") { console.log( await this.veraxSdk.attestation.findBy({ schemaId: "0xd1664d97bd195df77e3d5fe78c1737ab3adaa38bbe52a680d1aa30fa51f186ba", }), ); + } + + if (methodName.toLowerCase() == "getRelatedAttestations".toLowerCase() || methodName == "") { + console.log( + await this.veraxSdk.attestation.getRelatedAttestations( + "0x0000000000000000000000000000000000000000000000000000000000000001", + ), + ); + } } } diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index e7dd56a7..d4b16d05 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -1,5 +1,6 @@ import BaseDataMapper from "./BaseDataMapper"; import { Attestation } from "../types"; +import { Constants } from "../utils/constants"; export default class AttestationDataMapper extends BaseDataMapper { typeName = "attestation"; @@ -19,4 +20,11 @@ export default class AttestationDataMapper extends BaseDataMapper { schemaString decodedData }`; + + async getRelatedAttestations(id: string) { + return this.findBy({ + attestationData_contains: id, + schemaId_in: [Constants.RELATIONSHIP_SCHEMA_ID, Constants.NAMED_GRAPH_RELATIONSHIP_SCHEMA_ID], + }); + } } diff --git a/sdk/src/dataMapper/BaseDataMapper.ts b/sdk/src/dataMapper/BaseDataMapper.ts index 0a3d4aa1..d679612b 100644 --- a/sdk/src/dataMapper/BaseDataMapper.ts +++ b/sdk/src/dataMapper/BaseDataMapper.ts @@ -1,6 +1,6 @@ import { PublicClient } from "viem"; import { ApolloClient, gql } from "@apollo/client/core"; -import { Conf } from "../types"; +import { Conf, FilterMap } from "../types"; import { stringifyWhereClause } from "../utils/apolloClientHelper"; export default abstract class BaseDataMapper { @@ -25,7 +25,7 @@ export default abstract class BaseDataMapper { return queryResult.data; } - async findBy(whereClause: Partial) { + async findBy(whereClause: Partial) { const queryResult = await this.apolloClient.query>({ query: gql(`query GetBy { ${this.typeName}s(where: ${stringifyWhereClause(whereClause)}) ${this.gqlInterface} }`), }); diff --git a/sdk/src/types/index.d.ts b/sdk/src/types/index.d.ts index 1de13567..a2201f0c 100644 --- a/sdk/src/types/index.d.ts +++ b/sdk/src/types/index.d.ts @@ -1,4 +1,4 @@ -import { Chain, Address } from "viem"; +import { Address, Chain } from "viem"; export interface Conf { chain: Chain; @@ -46,3 +46,18 @@ export type Module = { name: string; // The name of the module. description: string; // A description of the module. }; + +export type FilterMap = { + Attestation: FilterAttestation; + Module: FilterModule; + Schema: FilterSchema; + Portal: FilterPortal; +}; + +export type FilterAttestation = Attestation & { schemaId_in: string[]; attestationData_contains: string }; + +export type FilterModule = Module; + +export type FilterSchema = Schema; + +export type FilterPortal = Portal; diff --git a/sdk/src/utils/constants.ts b/sdk/src/utils/constants.ts new file mode 100644 index 00000000..f7ea3773 --- /dev/null +++ b/sdk/src/utils/constants.ts @@ -0,0 +1,5 @@ +export class Constants { + static readonly RELATIONSHIP_SCHEMA_ID = "0x89bd76e17fd84df8e1e448fa1b46dd8d97f7e8e806552b003f8386a5aebcb9f0"; + static readonly NAMED_GRAPH_RELATIONSHIP_SCHEMA_ID = + "0x5003a7832fa2734780a5bf6a1f3940b84c0c66a398e62dd4e7f183fdbc7da6ee"; +} diff --git a/sdk/test/dataMapper/AttestationDataMapper.test.ts b/sdk/test/dataMapper/AttestationDataMapper.test.ts index 00b9895d..602f18c5 100644 --- a/sdk/test/dataMapper/AttestationDataMapper.test.ts +++ b/sdk/test/dataMapper/AttestationDataMapper.test.ts @@ -1,8 +1,9 @@ import AttestationDataMapper from "../../src/dataMapper/AttestationDataMapper"; import VeraxSdk from "../../src/VeraxSdk"; -import { createPublicClient, PublicClient, http } from "viem"; -import { ApolloClient, InMemoryCache, ApolloQueryResult, gql } from "@apollo/client/core"; -//TODO : This is a basic test example. mock data and assertions should be more precise +import { createPublicClient, http, PublicClient } from "viem"; +import { ApolloClient, ApolloQueryResult, gql, InMemoryCache } from "@apollo/client/core"; +import { Constants } from "../../src/utils/constants"; + describe("AttestationDataMapper", () => { let mockApolloClient: ApolloClient; let web3Client: PublicClient; @@ -66,4 +67,27 @@ describe("AttestationDataMapper", () => { }); }); }); + + describe("getRelatedAttestations", () => { + it("should return the expected attestations", async () => { + const attestationId = "0x0000000000000000000000000000000000000000000000000000000000000001"; + // Mock the behavior of the query method + const queryMock = jest.spyOn(mockApolloClient, "query"); + queryMock.mockResolvedValueOnce({ + data: { + attestations: { result: "success" }, + }, + } as ApolloQueryResult); + + const result = await attestationDataMapper.getRelatedAttestations(attestationId); + + // Assert + expect(result).toMatchObject({ attestations: { result: "success" } }); + expect(mockApolloClient.query).toHaveBeenCalledWith({ + query: gql( + `query GetBy { ${typeName}s(where: {attestationData_contains:"${attestationId}",schemaId_in:["${Constants.RELATIONSHIP_SCHEMA_ID}","${Constants.NAMED_GRAPH_RELATIONSHIP_SCHEMA_ID}"]}) ${gqlInterface} }`, + ), + }); + }); + }); });