From 6f0f3a1beb9789199501f98fc00d3edc512a2953 Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure <77279246+satyajeetkolhapure@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:59:31 +0100 Subject: [PATCH] chore: Add example calls for all the methods the SDK will implement (#268) Co-authored-by: Satyajeet Kolhapure --- .../attestation/attestationExamples.ts | 22 +++++++++ sdk/examples/attestation/findOneById.ts | 7 --- sdk/examples/attestation/index.ts | 6 +++ sdk/examples/module/findOneById.ts | 5 -- .../findBy.ts => module/index.ts} | 3 +- sdk/examples/module/moduleExamples.ts | 17 +++++++ sdk/examples/portal/findOneById.ts | 5 -- sdk/examples/portal/{findBy.ts => index.ts} | 3 +- sdk/examples/portal/portalExamples.ts | 34 +++++++++++++ sdk/examples/schema/findBy.ts | 5 -- sdk/examples/schema/findOneById.ts | 5 -- .../{module/findBy.ts => schema/index.ts} | 3 +- sdk/examples/schema/schemaExamples.ts | 19 ++++++++ sdk/package.json | 4 ++ sdk/src/dataMapper/AttestationDataMapper.ts | 3 +- sdk/src/dataMapper/BaseDataMapper.ts | 17 +++---- sdk/src/dataMapper/ModuleDataMapper.ts | 7 ++- sdk/src/dataMapper/PortalDataMapper.ts | 35 +++++++++++++- sdk/src/dataMapper/SchemaDataMapper.ts | 7 ++- sdk/src/dataMapper/UtilsDataMapper.ts | 2 +- sdk/src/types/index.d.ts | 48 +++++++++++++++++-- sdk/src/utils/apolloClientHelper.ts | 4 ++ .../dataMapper/AttestationDataMapper.test.ts | 2 +- 23 files changed, 214 insertions(+), 49 deletions(-) create mode 100644 sdk/examples/attestation/attestationExamples.ts delete mode 100644 sdk/examples/attestation/findOneById.ts create mode 100644 sdk/examples/attestation/index.ts delete mode 100644 sdk/examples/module/findOneById.ts rename sdk/examples/{attestation/findBy.ts => module/index.ts} (50%) create mode 100644 sdk/examples/module/moduleExamples.ts delete mode 100644 sdk/examples/portal/findOneById.ts rename sdk/examples/portal/{findBy.ts => index.ts} (50%) create mode 100644 sdk/examples/portal/portalExamples.ts delete mode 100644 sdk/examples/schema/findBy.ts delete mode 100644 sdk/examples/schema/findOneById.ts rename sdk/examples/{module/findBy.ts => schema/index.ts} (50%) create mode 100644 sdk/examples/schema/schemaExamples.ts create mode 100644 sdk/src/utils/apolloClientHelper.ts diff --git a/sdk/examples/attestation/attestationExamples.ts b/sdk/examples/attestation/attestationExamples.ts new file mode 100644 index 00000000..f2389db2 --- /dev/null +++ b/sdk/examples/attestation/attestationExamples.ts @@ -0,0 +1,22 @@ +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 == "") + console.log( + await this.veraxSdk.attestation.findOneById( + "0x00000000000000000000000000000000000000000000000000000000000007b5", + ), + ); + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") + console.log( + await this.veraxSdk.attestation.findBy({ + schemaId: "0xd1664d97bd195df77e3d5fe78c1737ab3adaa38bbe52a680d1aa30fa51f186ba", + }), + ); + } +} diff --git a/sdk/examples/attestation/findOneById.ts b/sdk/examples/attestation/findOneById.ts deleted file mode 100644 index 2240b195..00000000 --- a/sdk/examples/attestation/findOneById.ts +++ /dev/null @@ -1,7 +0,0 @@ -import VeraxSdk from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); - -console.log( - await veraxSdk.attestation.findOneById("0x00000000000000000000000000000000000000000000000000000000000007b5"), -); diff --git a/sdk/examples/attestation/index.ts b/sdk/examples/attestation/index.ts new file mode 100644 index 00000000..28635a16 --- /dev/null +++ b/sdk/examples/attestation/index.ts @@ -0,0 +1,6 @@ +import VeraxSdk from "../../src/VeraxSdk"; +import AttestationExamples from "./attestationExamples"; + +const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); + +await new AttestationExamples(veraxSdk).run(process.argv[2]); diff --git a/sdk/examples/module/findOneById.ts b/sdk/examples/module/findOneById.ts deleted file mode 100644 index 9f0df275..00000000 --- a/sdk/examples/module/findOneById.ts +++ /dev/null @@ -1,5 +0,0 @@ -import VeraxSdk from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); - -console.log(await veraxSdk.module.findOneById("0xf75be6f9418710fd516fa82afb3aad07e11a0f1b")); diff --git a/sdk/examples/attestation/findBy.ts b/sdk/examples/module/index.ts similarity index 50% rename from sdk/examples/attestation/findBy.ts rename to sdk/examples/module/index.ts index 2e5e5d7e..98833344 100644 --- a/sdk/examples/attestation/findBy.ts +++ b/sdk/examples/module/index.ts @@ -1,5 +1,6 @@ import VeraxSdk from "../../src/VeraxSdk"; +import ModuleExamples from "./moduleExamples"; const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -console.log(await veraxSdk.attestation.findBy()); +await new ModuleExamples(veraxSdk).run(process.argv[2]); diff --git a/sdk/examples/module/moduleExamples.ts b/sdk/examples/module/moduleExamples.ts new file mode 100644 index 00000000..e1e069f1 --- /dev/null +++ b/sdk/examples/module/moduleExamples.ts @@ -0,0 +1,17 @@ +import VeraxSdk from "../../src/VeraxSdk"; + +export default class ModuleExamples { + private veraxSdk: VeraxSdk; + constructor(_veraxSdk: VeraxSdk) { + this.veraxSdk = _veraxSdk; + } + async run(methodName: string = "") { + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.module.findOneById("0xf75be6f9418710fd516fa82afb3aad07e11a0f1b")); + + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.module.findBy({ name: "SchemaCheckerModule" })); + + if (methodName.toLowerCase() == "register" || methodName == "") console.log(await this.veraxSdk.module.register()); + } +} diff --git a/sdk/examples/portal/findOneById.ts b/sdk/examples/portal/findOneById.ts deleted file mode 100644 index fa541db4..00000000 --- a/sdk/examples/portal/findOneById.ts +++ /dev/null @@ -1,5 +0,0 @@ -import VeraxSdk from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); - -console.log(await veraxSdk.portal.findOneById("0x1495341ab1019798dd08976f4a3e5ab0e095510b")); diff --git a/sdk/examples/portal/findBy.ts b/sdk/examples/portal/index.ts similarity index 50% rename from sdk/examples/portal/findBy.ts rename to sdk/examples/portal/index.ts index da0c6896..2a5b0266 100644 --- a/sdk/examples/portal/findBy.ts +++ b/sdk/examples/portal/index.ts @@ -1,5 +1,6 @@ import VeraxSdk from "../../src/VeraxSdk"; +import PortalExamples from "./portalExamples"; const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -console.log(await veraxSdk.portal.findBy()); +await new PortalExamples(veraxSdk).run(process.argv[2]); diff --git a/sdk/examples/portal/portalExamples.ts b/sdk/examples/portal/portalExamples.ts new file mode 100644 index 00000000..a0067f39 --- /dev/null +++ b/sdk/examples/portal/portalExamples.ts @@ -0,0 +1,34 @@ +import VeraxSdk from "../../src/VeraxSdk"; + +export default class PortalExamples { + private veraxSdk: VeraxSdk; + constructor(_veraxSdk: VeraxSdk) { + this.veraxSdk = _veraxSdk; + } + async run(methodName: string = "") { + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.portal.findOneById("0x1495341ab1019798dd08976f4a3e5ab0e095510b")); + + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.portal.findBy({ ownerName: "Clique" })); + + if (methodName.toLowerCase() == "attest" || methodName == "") console.log(await this.veraxSdk.portal.attest()); + + if (methodName.toLowerCase() == "bulkAttest".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.portal.bulkAttest()); + + if (methodName.toLowerCase() == "replace" || methodName == "") console.log(await this.veraxSdk.portal.replace()); + + if (methodName.toLowerCase() == "revoke" || methodName == "") console.log(await this.veraxSdk.portal.revoke()); + + if (methodName.toLowerCase() == "bulkRevoke".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.portal.bulkRevoke()); + + if (methodName.toLowerCase() == "massImport".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.portal.massImport()); + + if (methodName.toLowerCase() == "register" || methodName == "") console.log(await this.veraxSdk.portal.register()); + + if (methodName.toLowerCase() == "clone" || methodName == "") console.log(await this.veraxSdk.portal.clone()); + } +} diff --git a/sdk/examples/schema/findBy.ts b/sdk/examples/schema/findBy.ts deleted file mode 100644 index 8a6cb70a..00000000 --- a/sdk/examples/schema/findBy.ts +++ /dev/null @@ -1,5 +0,0 @@ -import VeraxSdk from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); - -console.log(await veraxSdk.schema.findBy()); diff --git a/sdk/examples/schema/findOneById.ts b/sdk/examples/schema/findOneById.ts deleted file mode 100644 index 37437635..00000000 --- a/sdk/examples/schema/findOneById.ts +++ /dev/null @@ -1,5 +0,0 @@ -import VeraxSdk from "../../src/VeraxSdk"; - -const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); - -console.log(await veraxSdk.schema.findOneById("0x01f031da36192c34057c764239eb77bb6ec8ebfb808f72a7bb172f37a5bec31f")); diff --git a/sdk/examples/module/findBy.ts b/sdk/examples/schema/index.ts similarity index 50% rename from sdk/examples/module/findBy.ts rename to sdk/examples/schema/index.ts index a8a51841..9897b4a3 100644 --- a/sdk/examples/module/findBy.ts +++ b/sdk/examples/schema/index.ts @@ -1,5 +1,6 @@ import VeraxSdk from "../../src/VeraxSdk"; +import PortalExamples from "./schemaExamples"; const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -console.log(await veraxSdk.module.findBy()); +await new PortalExamples(veraxSdk).run(process.argv[2]); diff --git a/sdk/examples/schema/schemaExamples.ts b/sdk/examples/schema/schemaExamples.ts new file mode 100644 index 00000000..a5b3dd26 --- /dev/null +++ b/sdk/examples/schema/schemaExamples.ts @@ -0,0 +1,19 @@ +import VeraxSdk from "../../src/VeraxSdk"; + +export default class SchemaExamples { + private veraxSdk: VeraxSdk; + constructor(_veraxSdk: VeraxSdk) { + this.veraxSdk = _veraxSdk; + } + async run(methodName: string = "") { + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") + console.log( + await this.veraxSdk.schema.findOneById("0x01f031da36192c34057c764239eb77bb6ec8ebfb808f72a7bb172f37a5bec31f"), + ); + + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") + console.log(await this.veraxSdk.schema.findBy({ name: "Relationship" })); + + if (methodName.toLowerCase() == "create" || methodName == "") console.log(await this.veraxSdk.schema.create()); + } +} diff --git a/sdk/package.json b/sdk/package.json index b10ae31e..e7b130ab 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -16,6 +16,10 @@ "author": "Consensys", "type": "module", "scripts": { + "attestation": "ts-node examples/attestation/index.ts", + "module": "ts-node examples/module/index.ts", + "portal": "ts-node examples/portal/index.ts", + "schema": "ts-node examples/schema/index.ts", "attestation:all": "ts-node examples/attestation/findBy.ts", "attestation:one": "ts-node examples/attestation/findOneById.ts", "module:all": "ts-node examples/module/findBy.ts", diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index 801b3c37..e7dd56a7 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -1,6 +1,7 @@ import BaseDataMapper from "./BaseDataMapper"; +import { Attestation } from "../types"; -export default class AttestationDataMapper extends BaseDataMapper { +export default class AttestationDataMapper extends BaseDataMapper { typeName = "attestation"; gqlInterface = `{ id diff --git a/sdk/src/dataMapper/BaseDataMapper.ts b/sdk/src/dataMapper/BaseDataMapper.ts index cd6855e1..0a3d4aa1 100644 --- a/sdk/src/dataMapper/BaseDataMapper.ts +++ b/sdk/src/dataMapper/BaseDataMapper.ts @@ -1,8 +1,9 @@ import { PublicClient } from "viem"; import { ApolloClient, gql } from "@apollo/client/core"; import { Conf } from "../types"; +import { stringifyWhereClause } from "../utils/apolloClientHelper"; -export default abstract class BaseDataMapper { +export default abstract class BaseDataMapper { protected readonly conf: Conf; protected readonly web3Client: PublicClient; protected readonly apolloClient: ApolloClient; @@ -15,20 +16,20 @@ export default abstract class BaseDataMapper { this.apolloClient = _apolloClient; } - async findOneById(id: string) { - const queryResult = await this.apolloClient.query({ + async findOneById(id: string): Promise { + const queryResult = await this.apolloClient.query({ query: gql(`query GetOne($id: ID!) { ${this.typeName}(id: $id) ${this.gqlInterface} }`), variables: { id }, }); - return JSON.stringify(queryResult.data[this.typeName]); + return queryResult.data; } - async findBy() { - const queryResult = await this.apolloClient.query({ - query: gql(`query GetBy { ${this.typeName}s ${this.gqlInterface} }`), + async findBy(whereClause: Partial) { + const queryResult = await this.apolloClient.query>({ + query: gql(`query GetBy { ${this.typeName}s(where: ${stringifyWhereClause(whereClause)}) ${this.gqlInterface} }`), }); - return JSON.stringify(queryResult.data[`${this.typeName}s`]); + return queryResult.data; } } diff --git a/sdk/src/dataMapper/ModuleDataMapper.ts b/sdk/src/dataMapper/ModuleDataMapper.ts index c51ac698..20504697 100644 --- a/sdk/src/dataMapper/ModuleDataMapper.ts +++ b/sdk/src/dataMapper/ModuleDataMapper.ts @@ -1,6 +1,7 @@ +import { Module } from "../types"; import BaseDataMapper from "./BaseDataMapper"; -export default class ModuleDataMapper extends BaseDataMapper { +export default class ModuleDataMapper extends BaseDataMapper { typeName = "module"; gqlInterface = `{ id @@ -8,4 +9,8 @@ export default class ModuleDataMapper extends BaseDataMapper { name description }`; + + async register() { + throw new Error("Not implemented"); + } } diff --git a/sdk/src/dataMapper/PortalDataMapper.ts b/sdk/src/dataMapper/PortalDataMapper.ts index 9cd54840..d76cd7da 100644 --- a/sdk/src/dataMapper/PortalDataMapper.ts +++ b/sdk/src/dataMapper/PortalDataMapper.ts @@ -1,6 +1,7 @@ +import { Portal } from "../types"; import BaseDataMapper from "./BaseDataMapper"; -export default class PortalDataMapper extends BaseDataMapper { +export default class PortalDataMapper extends BaseDataMapper { typeName = "portal"; gqlInterface = `{ id @@ -11,4 +12,36 @@ export default class PortalDataMapper extends BaseDataMapper { description ownerName }`; + + async attest() { + throw new Error("Not implemented"); + } + + async bulkAttest() { + throw new Error("Not implemented"); + } + + async replace() { + throw new Error("Not implemented"); + } + + async revoke() { + throw new Error("Not implemented"); + } + + async bulkRevoke() { + throw new Error("Not implemented"); + } + + async massImport() { + throw new Error("Not implemented"); + } + + async register() { + throw new Error("Not implemented"); + } + + async clone() { + throw new Error("Not implemented"); + } } diff --git a/sdk/src/dataMapper/SchemaDataMapper.ts b/sdk/src/dataMapper/SchemaDataMapper.ts index 039c854a..76293d21 100644 --- a/sdk/src/dataMapper/SchemaDataMapper.ts +++ b/sdk/src/dataMapper/SchemaDataMapper.ts @@ -1,6 +1,7 @@ +import { Schema } from "../types"; import BaseDataMapper from "./BaseDataMapper"; -export default class SchemaDataMapper extends BaseDataMapper { +export default class SchemaDataMapper extends BaseDataMapper { typeName = "schema"; gqlInterface = `{ id @@ -9,4 +10,8 @@ export default class SchemaDataMapper extends BaseDataMapper { context schema }`; + + async create() { + throw new Error("Not implemented"); + } } diff --git a/sdk/src/dataMapper/UtilsDataMapper.ts b/sdk/src/dataMapper/UtilsDataMapper.ts index 79c7b932..07685e52 100644 --- a/sdk/src/dataMapper/UtilsDataMapper.ts +++ b/sdk/src/dataMapper/UtilsDataMapper.ts @@ -5,7 +5,7 @@ import { abiPortalRegistry } from "../abi/PortalRegistry"; import { abiSchemaRegistry } from "../abi/SchemaRegistry"; import { decode, encode } from "../utils/abiCoder"; -export default class UtilsDataMapper extends BaseDataMapper { +export default class UtilsDataMapper extends BaseDataMapper { typeName = "counter"; gqlInterface = `{ attestations diff --git a/sdk/src/types/index.d.ts b/sdk/src/types/index.d.ts index f1e92836..1de13567 100644 --- a/sdk/src/types/index.d.ts +++ b/sdk/src/types/index.d.ts @@ -1,10 +1,48 @@ -import { Chain } from "viem"; +import { Chain, Address } from "viem"; export interface Conf { chain: Chain; subgraphUrl: string; - portalRegistryAddress: `0x${string}`; - moduleRegistryAddress: `0x${string}`; - schemaRegistryAddress: `0x${string}`; - attestationRegistryAddress: `0x${string}`; + portalRegistryAddress: Address; + moduleRegistryAddress: Address; + schemaRegistryAddress: Address; + attestationRegistryAddress: Address; } + +export type Attestation = { + attestationId: string; // The unique identifier of the attestation. + schemaId: string; // The identifier of the schema this attestation adheres to. + replacedBy: string | null; // Whether the attestation was replaced by a new one. + Address: string; // The address issuing the attestation to the subject. + Address: string; // The id of the portal that created the attestation. + attestedDate: number; // The date the attestation is issued. + expirationDate: number; // The expiration date of the attestation. + revocationDate: number | null; // The date when the attestation was revoked. + version: number; // Version of the registry when the attestation was created. + revoked: boolean; // Whether the attestation is revoked or not. + subject: string; // The ID of the attestee, EVM address, DID, URL etc. + attestationData: string; // The attestation data. +}; + +export type Schema = { + name: string; // The name of the schema. + description: string; // A description of the schema. + context: string; // The context of the schema. + schema: string; // The schema definition. +}; + +export type Portal = { + id: Address; // The unique identifier of the portal (address). + ownerAddress: Address; // The address of the owner of this portal. + modules: Address[]; // Addresses of modules implemented by the portal. + isRevocable: boolean; // Whether attestations issued can be revoked. + name: string; // The name of the portal. + description: string; // A description of the portal. + ownerName: string; // The name of the owner of this portal. +}; + +export type Module = { + moduleAddress: Address; // The address of the module. + name: string; // The name of the module. + description: string; // A description of the module. +}; diff --git a/sdk/src/utils/apolloClientHelper.ts b/sdk/src/utils/apolloClientHelper.ts new file mode 100644 index 00000000..8df5d6bb --- /dev/null +++ b/sdk/src/utils/apolloClientHelper.ts @@ -0,0 +1,4 @@ +export function stringifyWhereClause(whereClauseObj: Record) { + const json = JSON.stringify(whereClauseObj); + return json.replace(/"([^"]+)":/g, "$1:"); +} diff --git a/sdk/test/dataMapper/AttestationDataMapper.test.ts b/sdk/test/dataMapper/AttestationDataMapper.test.ts index f5dc4fc6..00b9895d 100644 --- a/sdk/test/dataMapper/AttestationDataMapper.test.ts +++ b/sdk/test/dataMapper/AttestationDataMapper.test.ts @@ -59,7 +59,7 @@ describe("AttestationDataMapper", () => { const result = await attestationDataMapper.findOneById(attestationId); // Assert - expect(result).toBe('{"result":"success"}'); + expect(result).toMatchObject({ attestation: { result: "success" } }); expect(mockApolloClient.query).toHaveBeenCalledWith({ query: gql(`query GetOne($id: ID!) { ${typeName}(id: $id) ${gqlInterface} }`), variables: { id: attestationId },