Skip to content

Commit

Permalink
chore: Add example calls for all the methods the SDK will implement (#…
Browse files Browse the repository at this point in the history
…268)

Co-authored-by: Satyajeet Kolhapure <[email protected]>
  • Loading branch information
satyajeetkolhapure and kolhapuresatyajeet authored Oct 10, 2023
1 parent 1be2180 commit 6f0f3a1
Show file tree
Hide file tree
Showing 23 changed files with 214 additions and 49 deletions.
22 changes: 22 additions & 0 deletions sdk/examples/attestation/attestationExamples.ts
Original file line number Diff line number Diff line change
@@ -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",
}),
);
}
}
7 changes: 0 additions & 7 deletions sdk/examples/attestation/findOneById.ts

This file was deleted.

6 changes: 6 additions & 0 deletions sdk/examples/attestation/index.ts
Original file line number Diff line number Diff line change
@@ -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]);
5 changes: 0 additions & 5 deletions sdk/examples/module/findOneById.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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]);
17 changes: 17 additions & 0 deletions sdk/examples/module/moduleExamples.ts
Original file line number Diff line number Diff line change
@@ -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());
}
}
5 changes: 0 additions & 5 deletions sdk/examples/portal/findOneById.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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]);
34 changes: 34 additions & 0 deletions sdk/examples/portal/portalExamples.ts
Original file line number Diff line number Diff line change
@@ -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());
}
}
5 changes: 0 additions & 5 deletions sdk/examples/schema/findBy.ts

This file was deleted.

5 changes: 0 additions & 5 deletions sdk/examples/schema/findOneById.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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]);
19 changes: 19 additions & 0 deletions sdk/examples/schema/schemaExamples.ts
Original file line number Diff line number Diff line change
@@ -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());
}
}
4 changes: 4 additions & 0 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/dataMapper/AttestationDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BaseDataMapper from "./BaseDataMapper";
import { Attestation } from "../types";

export default class AttestationDataMapper extends BaseDataMapper {
export default class AttestationDataMapper extends BaseDataMapper<Attestation> {
typeName = "attestation";
gqlInterface = `{
id
Expand Down
17 changes: 9 additions & 8 deletions sdk/src/dataMapper/BaseDataMapper.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
protected readonly conf: Conf;
protected readonly web3Client: PublicClient;
protected readonly apolloClient: ApolloClient<object>;
Expand All @@ -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<T> {
const queryResult = await this.apolloClient.query<T>({
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<T>) {
const queryResult = await this.apolloClient.query<Array<T>>({
query: gql(`query GetBy { ${this.typeName}s(where: ${stringifyWhereClause(whereClause)}) ${this.gqlInterface} }`),
});

return JSON.stringify(queryResult.data[`${this.typeName}s`]);
return queryResult.data;
}
}
7 changes: 6 additions & 1 deletion sdk/src/dataMapper/ModuleDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Module } from "../types";
import BaseDataMapper from "./BaseDataMapper";

export default class ModuleDataMapper extends BaseDataMapper {
export default class ModuleDataMapper extends BaseDataMapper<Module> {
typeName = "module";
gqlInterface = `{
id
moduleAddress
name
description
}`;

async register() {
throw new Error("Not implemented");
}
}
35 changes: 34 additions & 1 deletion sdk/src/dataMapper/PortalDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Portal } from "../types";
import BaseDataMapper from "./BaseDataMapper";

export default class PortalDataMapper extends BaseDataMapper {
export default class PortalDataMapper extends BaseDataMapper<Portal> {
typeName = "portal";
gqlInterface = `{
id
Expand All @@ -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");
}
}
7 changes: 6 additions & 1 deletion sdk/src/dataMapper/SchemaDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Schema } from "../types";
import BaseDataMapper from "./BaseDataMapper";

export default class SchemaDataMapper extends BaseDataMapper {
export default class SchemaDataMapper extends BaseDataMapper<Schema> {
typeName = "schema";
gqlInterface = `{
id
Expand All @@ -9,4 +10,8 @@ export default class SchemaDataMapper extends BaseDataMapper {
context
schema
}`;

async create() {
throw new Error("Not implemented");
}
}
2 changes: 1 addition & 1 deletion sdk/src/dataMapper/UtilsDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> {
typeName = "counter";
gqlInterface = `{
attestations
Expand Down
48 changes: 43 additions & 5 deletions sdk/src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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.
};
4 changes: 4 additions & 0 deletions sdk/src/utils/apolloClientHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function stringifyWhereClause(whereClauseObj: Record<string, unknown>) {
const json = JSON.stringify(whereClauseObj);
return json.replace(/"([^"]+)":/g, "$1:");
}
2 changes: 1 addition & 1 deletion sdk/test/dataMapper/AttestationDataMapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit 6f0f3a1

Please sign in to comment.