From 346a9815f1e759581562dc8590cf7505b0ba83d8 Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure <77279246+satyajeetkolhapure@users.noreply.github.com> Date: Fri, 13 Oct 2023 16:08:45 +0100 Subject: [PATCH] chore: accepting the param values for examples (#279) Co-authored-by: Satyajeet Kolhapure --- sdk/README.md | 48 ++++++++++ .../attestation/attestationExamples.ts | 29 +++--- sdk/examples/attestation/index.ts | 5 +- sdk/examples/module/index.ts | 5 +- sdk/examples/module/moduleExamples.ts | 15 ++- sdk/examples/portal/index.ts | 5 +- sdk/examples/portal/portalExamples.ts | 94 +++++++++++-------- sdk/examples/schema/index.ts | 5 +- sdk/examples/schema/schemaExamples.ts | 18 ++-- 9 files changed, 151 insertions(+), 73 deletions(-) diff --git a/sdk/README.md b/sdk/README.md index d85c3b66..fe7c8571 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -169,6 +169,54 @@ console.log(result); // ``` +## CLI examples + +You can use command lines to test all the implemented SDK methods. Here are some examples. + +Note: if your don't pass any parameter (e.g. `pnpm portal findBy`)`, some default values will be used. + +### Portal examples + +```shell +pnpm portal findby '{\"ownerName\": \"Tester\"}' + +pnpm portal findonebyid '0x34798a866f52949208e67fb57ad36244024c50c0' + +pnpm portal simulateattest '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationPayload\" : { \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": \"1693583329\", \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": \"true\" }]}, \"validationPayloads\": []}' + +pnpm portal attest '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationPayload\" : { \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": \"1693583329\", \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": \"true\" }]}, \"validationPayloads\": []}' + +pnpm portal simulaterevoke '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationId\" : \"0x000000000000000000000000000000000000000000000000000000000000109b\" }' + +pnpm portal revoke '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationId\" : \"0x000000000000000000000000000000000000000000000000000000000000109b\" }' +``` + +### Attestation examples + +```shell +pnpm attestation findonebyid "0x000000000000000000000000000000000000000000000000000000000000109b" + +pnpm attestation findby '{\"portal\": \"0x34798a866f52949208e67fb57ad36244024c50c0\"}' + +pnpm attestation getRelatedAttestations "0x000000000000000000000000000000000000000000000000000000000000109b" +``` + +### Module examples + +```shell +pnpm module findonebyid "0x4bb8769e18f1518c35be8405d43d7cc07ecf501c" + +pnpm module findby '{\"name\": \"Msg Sender Module\"}' +``` + +### Schema examples + +```shell +pnpm schema findonebyid "0xce2647ed39aa89e6d1528a56deb6c30667ed2aae1ec2378ec3140c0c5d98a61e" + +pnpm schema findby '{\"description\": \"Gitcoin Passport Score\"}' +``` + ## Other operations [Work in progress] The class `veraxSdk.utils` extends the capabilities: diff --git a/sdk/examples/attestation/attestationExamples.ts b/sdk/examples/attestation/attestationExamples.ts index 5b955756..3a27c89f 100644 --- a/sdk/examples/attestation/attestationExamples.ts +++ b/sdk/examples/attestation/attestationExamples.ts @@ -1,4 +1,5 @@ import VeraxSdk from "../../src/VeraxSdk"; +import { Attestation } from "../../src/types"; export default class AttestationExamples { private veraxSdk: VeraxSdk; @@ -7,29 +8,25 @@ export default class AttestationExamples { this.veraxSdk = _veraxSdk; } - async run(methodName: string = "") { + async run(methodName: string = "", argv: string) { if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") { - console.log( - await this.veraxSdk.attestation.findOneById( - "0x00000000000000000000000000000000000000000000000000000000000007b5", - ), - ); + const attestationId: string = + argv === "" ? "0x00000000000000000000000000000000000000000000000000000000000007b5" : argv; + console.log(await this.veraxSdk.attestation.findOneById(attestationId)); } if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") { - console.log( - await this.veraxSdk.attestation.findBy({ - schemaId: "0xd1664d97bd195df77e3d5fe78c1737ab3adaa38bbe52a680d1aa30fa51f186ba", - }), - ); + const params: Partial = + argv === "" + ? { schemaId: "0xd1664d97bd195df77e3d5fe78c1737ab3adaa38bbe52a680d1aa30fa51f186ba" } + : JSON.parse(argv); + console.log(await this.veraxSdk.attestation.findBy(params)); } if (methodName.toLowerCase() == "getRelatedAttestations".toLowerCase() || methodName == "") { - console.log( - await this.veraxSdk.attestation.getRelatedAttestations( - "0x0000000000000000000000000000000000000000000000000000000000000001", - ), - ); + const attestationId: string = + argv === "" ? "0x0000000000000000000000000000000000000000000000000000000000000001" : argv; + console.log(await this.veraxSdk.attestation.getRelatedAttestations(attestationId)); } } } diff --git a/sdk/examples/attestation/index.ts b/sdk/examples/attestation/index.ts index 28635a16..cc413e37 100644 --- a/sdk/examples/attestation/index.ts +++ b/sdk/examples/attestation/index.ts @@ -1,6 +1,9 @@ import VeraxSdk from "../../src/VeraxSdk"; import AttestationExamples from "./attestationExamples"; +let argv: string | null | undefined = process.argv[3] as string; +argv !== null && argv !== undefined ? (argv = argv.replaceAll("\\", "")) : (argv = ""); + const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -await new AttestationExamples(veraxSdk).run(process.argv[2]); +await new AttestationExamples(veraxSdk).run(process.argv[2], argv); diff --git a/sdk/examples/module/index.ts b/sdk/examples/module/index.ts index 98833344..3d12902b 100644 --- a/sdk/examples/module/index.ts +++ b/sdk/examples/module/index.ts @@ -1,6 +1,9 @@ import VeraxSdk from "../../src/VeraxSdk"; import ModuleExamples from "./moduleExamples"; +let argv: string | null | undefined = process.argv[3] as string; +argv !== null && argv !== undefined ? (argv = argv.replaceAll("\\", "")) : (argv = ""); + const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -await new ModuleExamples(veraxSdk).run(process.argv[2]); +await new ModuleExamples(veraxSdk).run(process.argv[2], argv); diff --git a/sdk/examples/module/moduleExamples.ts b/sdk/examples/module/moduleExamples.ts index e1e069f1..2f73c0da 100644 --- a/sdk/examples/module/moduleExamples.ts +++ b/sdk/examples/module/moduleExamples.ts @@ -1,16 +1,21 @@ import VeraxSdk from "../../src/VeraxSdk"; +import { Module } from "../../src/types"; 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")); + async run(methodName: string = "", argv: string) { + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") { + const moduleId: string = argv === "" ? "0xf75be6f9418710fd516fa82afb3aad07e11a0f1b" : argv; + console.log(await this.veraxSdk.module.findOneById(moduleId)); + } - if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") - console.log(await this.veraxSdk.module.findBy({ name: "SchemaCheckerModule" })); + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") { + const params: Partial = argv === "" ? { name: "SchemaCheckerModule" } : JSON.parse(argv); + console.log(await this.veraxSdk.module.findBy(params)); + } if (methodName.toLowerCase() == "register" || methodName == "") console.log(await this.veraxSdk.module.register()); } diff --git a/sdk/examples/portal/index.ts b/sdk/examples/portal/index.ts index 2a5b0266..c8a010ba 100644 --- a/sdk/examples/portal/index.ts +++ b/sdk/examples/portal/index.ts @@ -1,6 +1,9 @@ import VeraxSdk from "../../src/VeraxSdk"; import PortalExamples from "./portalExamples"; +let argv: string | null | undefined = process.argv[3] as string; +argv !== null && argv !== undefined ? (argv = argv.replaceAll("\\", "")) : (argv = ""); + const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -await new PortalExamples(veraxSdk).run(process.argv[2]); +await new PortalExamples(veraxSdk).run(process.argv[2], argv); diff --git a/sdk/examples/portal/portalExamples.ts b/sdk/examples/portal/portalExamples.ts index c5d2bfff..95763d60 100644 --- a/sdk/examples/portal/portalExamples.ts +++ b/sdk/examples/portal/portalExamples.ts @@ -1,4 +1,6 @@ +import { Address } from "viem"; import VeraxSdk from "../../src/VeraxSdk"; +import { Portal } from "../../src/types"; export default class PortalExamples { private veraxSdk: VeraxSdk; @@ -7,41 +9,47 @@ export default class PortalExamples { this.veraxSdk = _veraxSdk; } - async run(methodName: string = "") { - if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") - console.log(await this.veraxSdk.portal.findOneById("0x1495341ab1019798dd08976f4a3e5ab0e095510b")); + async run(methodName: string = "", argv: string) { + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") { + const portalId: string = argv === "" ? "0x1495341ab1019798dd08976f4a3e5ab0e095510b" : argv; + console.log(await this.veraxSdk.portal.findOneById(portalId)); + } - if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") - console.log(await this.veraxSdk.portal.findBy({ ownerName: "Clique" })); + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") { + const params: Partial = argv === "" ? { ownerName: "Clique" } : JSON.parse(argv); + console.log(await this.veraxSdk.portal.findBy(params)); + } if (methodName.toLowerCase() == "simulateAttest".toLowerCase() || methodName == "") { - console.log( - await this.veraxSdk.portal.simulateAttest( - "0xeea25bc2ec56cae601df33b8fc676673285e12cc", - { - schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", - expirationDate: 1693583329, - subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", - attestationData: [{ isBuidler: true }], - }, - [], - ), - ); + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationData = params?.attestationData ?? { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }; + const validationPayloads = params?.validationPayloads ?? []; + console.log(await this.veraxSdk.portal.simulateAttest(portalAddress, attestationData, validationPayloads)); } if (methodName.toLowerCase() == "attest" || methodName == "") { - console.log( - await this.veraxSdk.portal.attest( - "0xeea25bc2ec56cae601df33b8fc676673285e12cc", - { - schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", - expirationDate: 1693583329, - subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", - attestationData: [{ isBuidler: true }], - }, - [], - ), - ); + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationData = params?.attestationData ?? { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }; + const validationPayloads = params?.validationPayloads ?? []; + console.log(await this.veraxSdk.portal.attest(portalAddress, attestationData, validationPayloads)); } if (methodName.toLowerCase() == "simulateBulkAttest".toLowerCase() || methodName == "") { @@ -93,21 +101,25 @@ export default class PortalExamples { if (methodName.toLowerCase() == "replace" || methodName == "") console.log(await this.veraxSdk.portal.replace()); if (methodName.toLowerCase() == "revoke" || methodName == "") { - console.log( - await this.veraxSdk.portal.revoke( - "0xeea25bc2ec56cae601df33b8fc676673285e12cc", - "0x0000000000000000000000000000000000000000000000000000000000000001", - ), - ); + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationId = + params?.attestationId ?? "0x0000000000000000000000000000000000000000000000000000000000000001"; + console.log(await this.veraxSdk.portal.revoke(portalAddress, attestationId)); } if (methodName.toLowerCase() == "simulateRevoke".toLowerCase() || methodName == "") { - console.log( - await this.veraxSdk.portal.simulateRevoke( - "0xeea25bc2ec56cae601df33b8fc676673285e12cc", - "0x0000000000000000000000000000000000000000000000000000000000000001", - ), - ); + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationId = + params?.attestationId ?? "0x0000000000000000000000000000000000000000000000000000000000000001"; + console.log(await this.veraxSdk.portal.simulateRevoke(portalAddress, attestationId)); } if (methodName.toLowerCase() == "simulateBulkRevoke".toLowerCase() || methodName == "") diff --git a/sdk/examples/schema/index.ts b/sdk/examples/schema/index.ts index 9897b4a3..9b858a23 100644 --- a/sdk/examples/schema/index.ts +++ b/sdk/examples/schema/index.ts @@ -1,6 +1,9 @@ import VeraxSdk from "../../src/VeraxSdk"; import PortalExamples from "./schemaExamples"; +let argv: string | null | undefined = process.argv[3] as string; +argv !== null && argv !== undefined ? (argv = argv.replaceAll("\\", "")) : (argv = ""); + const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_TESTNET); -await new PortalExamples(veraxSdk).run(process.argv[2]); +await new PortalExamples(veraxSdk).run(process.argv[2], argv); diff --git a/sdk/examples/schema/schemaExamples.ts b/sdk/examples/schema/schemaExamples.ts index a5b3dd26..e9bd21f6 100644 --- a/sdk/examples/schema/schemaExamples.ts +++ b/sdk/examples/schema/schemaExamples.ts @@ -1,18 +1,22 @@ import VeraxSdk from "../../src/VeraxSdk"; +import { Schema } from "../../src/types"; 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"), - ); + async run(methodName: string = "", argv: string) { + if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") { + const schemaId: string = + argv === "" ? "0x01f031da36192c34057c764239eb77bb6ec8ebfb808f72a7bb172f37a5bec31f" : argv; + console.log(await this.veraxSdk.schema.findOneById(schemaId)); + } - if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") - console.log(await this.veraxSdk.schema.findBy({ name: "Relationship" })); + if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") { + const params: Partial = argv === "" ? { name: "Relationship" } : JSON.parse(argv); + console.log(await this.veraxSdk.schema.findBy(params)); + } if (methodName.toLowerCase() == "create" || methodName == "") console.log(await this.veraxSdk.schema.create()); }