From 02628e36011f16d97a1fbece992dec00180aaccf Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure Date: Thu, 19 Oct 2023 15:48:22 +0100 Subject: [PATCH 1/3] feat: added contracts methods in for attestation registry with examples --- sdk/README.md | 40 ++++- .../attestation/attestationExamples.ts | 112 ++++++++++++ sdk/examples/portal/portalExamples.ts | 122 ++++++++++++- sdk/src/abi/AttestationRegistry.ts | 48 ++++++ sdk/src/dataMapper/AttestationDataMapper.ts | 110 +++++++++++- sdk/src/dataMapper/PortalDataMapper.ts | 162 +++++++++++------- sdk/src/utils/errorHandler.ts | 14 ++ 7 files changed, 539 insertions(+), 69 deletions(-) create mode 100644 sdk/src/utils/errorHandler.ts diff --git a/sdk/README.md b/sdk/README.md index db9b4507..185f792f 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -185,15 +185,14 @@ Note: if your don't pass any parameter (e.g. `pnpm portal findBy`)`, some defaul ```shell pnpm portal findonebyid '0x34798a866f52949208e67fb57ad36244024c50c0' -pnpm portal findby '{\"ownerName\": \"Satya\"}' - +pnpm portal findby '{\"ownerName\": \"Tester\"}' 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 simulateBulkAttest '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}], \"validationPayloads\": [[],[]]}' -pnpm portal bulkAttest '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}], \"validationPayloads\": [[],[]]}' +pnpm portal bulkAttest '{\"portalAddress\": \"0xBA5bBAe01509311f61Bac8A15dCB4B41bEd8Ecb5\", \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}], \"validationPayloads\": [[],[]]}' pnpm portal simulaterevoke '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationId\" : \"0x00000000000000000000000000000000000000000000000000000000000010a8\" }' @@ -203,6 +202,14 @@ pnpm portal simulateBulkRevoke '{\"portalAddress\": \"0x34798a866f52949208e67fb5 pnpm portal bulkRevoke '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationIds\" : [\"0x00000000000000000000000000000000000000000000000000000000000010a7\", \"0x00000000000000000000000000000000000000000000000000000000000010a6\"] }' +pnpm portal simulateReplace '{\"portalAddress\": \"0xBA5bBAe01509311f61Bac8A15dCB4B41bEd8Ecb5\", \"attestationId\": \"0x0000000000000000000000000000000000000000000000000000000000000006\", \"attestationPayload\" : { \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}, \"validationPayloads\": []}' + +pnpm portal replace '{\"portalAddress\": \"0xBA5bBAe01509311f61Bac8A15dCB4B41bEd8Ecb5\", \"attestationId\": \"0x0000000000000000000000000000000000000000000000000000000000000006\", \"attestationPayload\" : { \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}, \"validationPayloads\": []}' + +pnpm portal simulateBulkReplace '{\"portalAddress\": \"0xBA5bBAe01509311f61Bac8A15dCB4B41bEd8Ecb5\", \"attestationIds\" : [\"0x0000000000000000000000000000000000000000000000000000000000000007\", \"0x0000000000000000000000000000000000000000000000000000000000000008\"], \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}], \"validationPayloads\": [[],[]]}' + +pnpm portal bulkReplace '{\"portalAddress\": \"0xBA5bBAe01509311f61Bac8A15dCB4B41bEd8Ecb5\", \"attestationIds\" : [\"0x0000000000000000000000000000000000000000000000000000000000000007\", \"0x0000000000000000000000000000000000000000000000000000000000000008\"], \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}], \"validationPayloads\": [[],[]]}' + pnpm portal simulateRegister '{\"id\":\"0xD39c439cD3Ae5E1F3c7d13985aDAC90846284904\",\"name\":\"test\",\"description\":\"example\",\"isRevocable\":true,\"ownerName\":\"test\"}' pnpm portal register '{\"id\":\"0xD39c439cD3Ae5E1F3c7d13985aDAC90846284904\",\"name\":\"test\",\"description\":\"example\",\"isRevocable\":true,\"ownerName\":\"test\"}' @@ -216,7 +223,6 @@ pnpm portal getPortalByAddress '{\"portalAddress\":\"0x8b833796869b5debb9b06370d pnpm portal isPortalRegistered '{\"portalAddress\":\"0x8b833796869b5debb9b06370d6d47016f0d7973b\"}' pnpm portal getPortalsCount - ``` ### Attestation examples @@ -227,6 +233,32 @@ pnpm attestation findonebyid "0x000000000000000000000000000000000000000000000000 pnpm attestation findby '{\"portal\": \"0x34798a866f52949208e67fb57ad36244024c50c0\"}' pnpm attestation getRelatedAttestations "0x0000000000000000000000000000000000000000000000000000000000000001" + +pnpm attestation simulateUpdateRouter "0xC825ACA6621597bcb86438346A2538fba85380d9" + +pnpm attestation updateRouter "0xC825ACA6621597bcb86438346A2538fba85380d9" + +pnpm attestation simulateMassImport '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}]}' + +pnpm attestation massImport '{\"portalAddress\": \"0x34798a866f52949208e67fb57ad36244024c50c0\", \"attestationPayloads\" : [{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]},{ \"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"expirationDate\": 1693583329, \"subject\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"attestationData\": [{ \"isBuidler\": true }]}]}' + +pnpm attestation simulateIncrementVersionNumber + +pnpm attestation incrementVersionNumber + +pnpm attestation isRegistered "0x0000000000000000000000000000000000000000000000000000000000000001" + +pnpm attestation isRevocable "0xBA5bBAe01509311f61Bac8A15dCB4B41bEd8Ecb5" + +pnpm attestation getAttestation "0x0000000000000000000000000000000000000000000000000000000000000001" + +pnpm attestation getVersionNumber + +pnpm attestation getAttestationIdCounter + +pnpm attestation balanceof '{\"account\": \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"id\": 1}' + +pnpm attestation balanceOfBatch '{\"accounts\": [\"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\", \"0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47\"], \"ids\": [0, 1]}' ``` ### Module examples diff --git a/sdk/examples/attestation/attestationExamples.ts b/sdk/examples/attestation/attestationExamples.ts index 2dad8c7c..35f0b847 100644 --- a/sdk/examples/attestation/attestationExamples.ts +++ b/sdk/examples/attestation/attestationExamples.ts @@ -1,3 +1,4 @@ +import { Address } from "viem"; import VeraxSdk from "../../src/VeraxSdk"; export default class AttestationExamples { @@ -31,5 +32,116 @@ export default class AttestationExamples { argv === "" ? "0x0000000000000000000000000000000000000000000000000000000000000001" : argv; console.log(await this.veraxSdk.attestation.getRelatedAttestations(attestationId)); } + + if (methodName.toLowerCase() == "simulateUpdateRouter".toLowerCase() || methodName == "") { + const routerAddress: Address = argv === "" ? "0x736c78b2f2cBf4F921E8551b2acB6A5Edc9177D5" : (argv as Address); + console.log(await this.veraxSdk.attestation.simulateUpdateRouter(routerAddress)); + } + + if (methodName.toLowerCase() == "updateRouter".toLowerCase() || methodName == "") { + const routerAddress: Address = argv === "" ? "0x736c78b2f2cBf4F921E8551b2acB6A5Edc9177D5" : (argv as Address); + console.log(await this.veraxSdk.attestation.updateRouter(routerAddress)); + } + + if (methodName.toLowerCase() == "simulateMassImport".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + + const attestationPayloads = params?.attestationPayloads ?? [ + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + ]; + console.log(await this.veraxSdk.attestation.simulateMassImport(portalAddress, attestationPayloads)); + } + + if (methodName.toLowerCase() == "massImport".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + + const attestationPayloads = params?.attestationPayloads ?? [ + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + ]; + console.log(await this.veraxSdk.attestation.massImport(portalAddress, attestationPayloads)); + } + + if (methodName.toLowerCase() == "simulateIncrementVersionNumber".toLowerCase() || methodName == "") { + console.log(await this.veraxSdk.attestation.simulateIncrementVersionNumber()); + } + + if (methodName.toLowerCase() == "incrementVersionNumber".toLowerCase() || methodName == "") { + console.log(await this.veraxSdk.attestation.incrementVersionNumber()); + } + + if (methodName.toLowerCase() == "isRegistered".toLowerCase() || methodName == "") { + const attestationId: string = + argv === "" ? "0x00000000000000000000000000000000000000000000000000000000000007b5" : argv; + console.log(await this.veraxSdk.attestation.isRegistered(attestationId)); + } + + if (methodName.toLowerCase() == "isRevocable".toLowerCase() || methodName == "") { + const portalId: string = argv === "" ? "0xeea25bc2ec56cae601df33b8fc676673285e12cc" : argv; + console.log(await this.veraxSdk.attestation.isRevocable(portalId)); + } + + if (methodName.toLowerCase() == "getAttestation".toLowerCase() || methodName == "") { + const attestationId: string = + argv === "" ? "0x00000000000000000000000000000000000000000000000000000000000007b5" : argv; + console.log(await this.veraxSdk.attestation.getAttestation(attestationId)); + } + + if (methodName.toLowerCase() == "getVersionNumber".toLowerCase() || methodName == "") { + console.log(await this.veraxSdk.attestation.getVersionNumber()); + } + + if (methodName.toLowerCase() == "getAttestationIdCounter".toLowerCase() || methodName == "") { + console.log(await this.veraxSdk.attestation.getAttestationIdCounter()); + } + + if (methodName.toLowerCase() == "balanceOf".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const account = params?.account ? (params.account as Address) : "0x809e815596abeb3764abf81be2dc39fbbacc9949"; + const id = params?.id ? (params.id as unknown as number) : 1; + + console.log(await this.veraxSdk.attestation.balanceOf(account, id)); + } + + if (methodName.toLowerCase() == "balanceOfBatch".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const accounts = params?.accounts + ? (params.accounts as Address[]) + : ["0x809e815596abeb3764abf81be2dc39fbbacc9949", "0x809e815596abeb3764abf81be2dc39fbbacc9949"]; + const ids = params?.ids ? (params.ids as number[]) : [1, 2]; + + console.log(await this.veraxSdk.attestation.balanceOfBatch(accounts as Address[], ids)); + } } } diff --git a/sdk/examples/portal/portalExamples.ts b/sdk/examples/portal/portalExamples.ts index f242a7df..4742be59 100644 --- a/sdk/examples/portal/portalExamples.ts +++ b/sdk/examples/portal/portalExamples.ts @@ -102,8 +102,6 @@ export default class PortalExamples { console.log(await this.veraxSdk.portal.bulkAttest(portalAddress, attestationPayloads, validationPayloads)); } - if (methodName.toLowerCase() == "replace" || methodName == "") console.log(await this.veraxSdk.portal.replace()); - if (methodName.toLowerCase() == "revoke" || methodName == "") { let params; if (argv !== "") params = JSON.parse(argv); @@ -152,6 +150,126 @@ export default class PortalExamples { console.log(await this.veraxSdk.portal.bulkRevoke(portalAddress, attestationIds)); } + if (methodName.toLowerCase() == "simulateReplace".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationId = params?.attestationId + ? (params.attestationId as Address) + : "0x0000000000000000000000000000000000000000000000000000000000000001"; + + const attestationPayload = params?.attestationPayload ?? { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }; + const validationPayloads = params?.validationPayloads ?? []; + console.log( + await this.veraxSdk.portal.simulateReplace( + portalAddress, + attestationId, + attestationPayload, + validationPayloads, + ), + ); + } + + if (methodName.toLowerCase() == "replace".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationId = params?.attestationId + ? (params.attestationId as Address) + : "0x0000000000000000000000000000000000000000000000000000000000000001"; + + const attestationPayload = params?.attestationPayload ?? { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }; + const validationPayloads = params?.validationPayloads ?? []; + console.log( + await this.veraxSdk.portal.replace(portalAddress, attestationId, attestationPayload, validationPayloads), + ); + } + + if (methodName.toLowerCase() == "simulateBulkReplace".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationIds = params?.attestationIds + ? (params.attestationIds as string[]) + : [ + "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x0000000000000000000000000000000000000000000000000000000000000002", + ]; + + const attestationPayloads = params?.attestationPayloads ?? [ + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + ]; + const validationPayloads = params?.validationPayloads ?? [[], []]; + console.log( + await this.veraxSdk.portal.simulateBulkReplace( + portalAddress, + attestationIds, + attestationPayloads, + validationPayloads, + ), + ); + } + + if (methodName.toLowerCase() == "bulkReplace".toLowerCase() || methodName == "") { + let params; + if (argv !== "") params = JSON.parse(argv); + const portalAddress = params?.portalAddress + ? (params.portalAddress as Address) + : "0xeea25bc2ec56cae601df33b8fc676673285e12cc"; + const attestationIds = params?.attestationIds + ? (params.attestationIds as string[]) + : [ + "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x0000000000000000000000000000000000000000000000000000000000000002", + ]; + + const attestationPayloads = params?.attestationPayloads ?? [ + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + { + schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738", + expirationDate: 1693583329, + subject: "0x828c9f04D1a07E3b0aBE12A9F8238a3Ff7E57b47", + attestationData: [{ isBuidler: true }], + }, + ]; + const validationPayloads = params?.validationPayloads ?? [[], []]; + console.log( + await this.veraxSdk.portal.bulkReplace(portalAddress, attestationIds, attestationPayloads, validationPayloads), + ); + } + if (methodName.toLowerCase() == "simulateRegister".toLowerCase() || methodName == "") { let params; if (argv !== "") params = JSON.parse(argv); diff --git a/sdk/src/abi/AttestationRegistry.ts b/sdk/src/abi/AttestationRegistry.ts index 416dc039..198bdaf0 100644 --- a/sdk/src/abi/AttestationRegistry.ts +++ b/sdk/src/abi/AttestationRegistry.ts @@ -184,6 +184,54 @@ export const abiAttestationRegistry = [ stateMutability: "nonpayable", type: "function", }, + { + inputs: [ + { + internalType: "address", + name: "account", + type: "address", + }, + { + internalType: "uint256", + name: "id", + type: "uint256", + }, + ], + name: "balanceOf", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { + internalType: "address[]", + name: "accounts", + type: "address[]", + }, + { + internalType: "uint256[]", + name: "ids", + type: "uint256[]", + }, + ], + name: "balanceOfBatch", + outputs: [ + { + internalType: "uint256[]", + name: "", + type: "uint256[]", + }, + ], + stateMutability: "view", + type: "function", + }, { inputs: [ { diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index c4cf8a57..a27e5651 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -1,7 +1,11 @@ import BaseDataMapper from "./BaseDataMapper"; -import { Attestation } from "../types"; +import { abiAttestationRegistry } from "../abi/AttestationRegistry"; +import { Attestation, AttestationPayload } from "../types"; import { Attestation_filter, Attestation_orderBy } from "../../.graphclient"; import { Constants } from "../utils/constants"; +import { handleError } from "../utils/errorHandler"; +import { Address, Hash } from "viem"; +import { encode } from "../utils/abiCoder"; export default class AttestationDataMapper extends BaseDataMapper< Attestation, @@ -38,4 +42,108 @@ export default class AttestationDataMapper extends BaseDataMapper< undefined, ); } + + async simulateUpdateRouter(routerAddress: Address) { + return await this.simulateContract("updateRouter", [routerAddress]); + } + + async updateRouter(routerAddress: Address) { + const request = await this.simulateUpdateRouter(routerAddress); + return await this.executeTransaction(request); + } + + async simulateMassImport(portalAddress: Address, attestationPayloads: AttestationPayload[]) { + const attestationPayloadsArg = []; + + for (const attestationPayload of attestationPayloads) { + const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId); + const attestationData = encode(matchingSchema.schema, attestationPayload.attestationData); + + attestationPayloadsArg.push([ + attestationPayload.schemaId, + attestationPayload.expirationDate, + attestationPayload.subject, + attestationData, + ]); + } + + return await this.simulateContract("massImport", [attestationPayloadsArg, portalAddress]); + } + + async massImport(portalAddress: Address, attestationPayloads: AttestationPayload[]) { + const request = await this.simulateMassImport(portalAddress, attestationPayloads); + return await this.executeTransaction(request); + } + + async simulateIncrementVersionNumber() { + return await this.simulateContract("incrementVersionNumber", []); + } + + async incrementVersionNumber() { + const request = await this.simulateIncrementVersionNumber(); + return await this.executeTransaction(request); + } + + async isRegistered(attestationId: string) { + return await this.executeReadMethod("isRegistered", [attestationId]); + } + + async isRevocable(portalId: string) { + return await this.executeReadMethod("isRevocable", [portalId]); + } + + async getAttestation(attestationId: string) { + return await this.executeReadMethod("getAttestation", [attestationId]); + } + + async getVersionNumber() { + return await this.executeReadMethod("getVersionNumber", []); + } + + async getAttestationIdCounter() { + return await this.executeReadMethod("getAttestationIdCounter", []); + } + + async balanceOf(account: Address, id: number) { + return await this.executeReadMethod("balanceOf", [account, id]); + } + + async balanceOfBatch(accounts: Address[], ids: number[]) { + return await this.executeReadMethod("balanceOfBatch", [accounts, ids]); + } + + // TODO: Use correct type for args + private async executeReadMethod(functionName: string, args: unknown[]) { + return await this.web3Client.readContract({ + abi: abiAttestationRegistry, + address: this.conf.attestationRegistryAddress, + functionName, + args, + }); + } + + // TODO: Use correct type for args + private async simulateContract(functionName: string, args: unknown[]) { + try { + const { request } = await this.web3Client.simulateContract({ + address: this.conf.attestationRegistryAddress, + abi: abiAttestationRegistry, + functionName, + account: this.walletClient.account, + args, + }); + + return request; + } catch (err) { + handleError(err); + } + } + + // TODO: Use correct type for request + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private async executeTransaction(request: any) { + const hash: Hash = await this.walletClient.writeContract(request); + console.log(`Transaction sent with hash ${hash}`); + return hash; + } } diff --git a/sdk/src/dataMapper/PortalDataMapper.ts b/sdk/src/dataMapper/PortalDataMapper.ts index 957994eb..90613d1c 100644 --- a/sdk/src/dataMapper/PortalDataMapper.ts +++ b/sdk/src/dataMapper/PortalDataMapper.ts @@ -1,10 +1,11 @@ import { AttestationPayload, Portal } from "../types"; import BaseDataMapper from "./BaseDataMapper"; import { abiDefaultPortal } from "../abi/DefaultPortal"; -import { Address, BaseError, ContractFunctionRevertedError, Hash } from "viem"; +import { Address, Hash } from "viem"; import { encode } from "../utils/abiCoder"; import { Portal_filter, Portal_orderBy } from "../../.graphclient"; import { abiPortalRegistry } from "../abi/PortalRegistry"; +import { handleError } from "../utils/errorHandler"; export default class PortalDataMapper extends BaseDataMapper { typeName = "portal"; @@ -21,23 +22,10 @@ export default class PortalDataMapper extends BaseDataMapper err instanceof ContractFunctionRevertedError); - if (revertError instanceof ContractFunctionRevertedError) { - const errorName = revertError.data?.errorName ?? ""; - console.error(`Failing with ${errorName}`); - } - } - console.error(err); - - throw new Error("Simulation failed"); + // TODO: Use correct type for request + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private async executeTransaction(request: any) { + const hash: Hash = await this.walletClient.writeContract(request); + console.log(`Transaction sent with hash ${hash}`); + return hash; } } diff --git a/sdk/src/utils/errorHandler.ts b/sdk/src/utils/errorHandler.ts new file mode 100644 index 00000000..147880af --- /dev/null +++ b/sdk/src/utils/errorHandler.ts @@ -0,0 +1,14 @@ +import { BaseError, ContractFunctionRevertedError } from "viem"; + +export function handleError(err: unknown): never { + if (err instanceof BaseError) { + const revertError = err.walk((err) => err instanceof ContractFunctionRevertedError); + if (revertError instanceof ContractFunctionRevertedError) { + const errorName = revertError.data?.errorName ?? ""; + console.error(`Failing with ${errorName}`); + } + } + console.error(err); + + throw new Error("Simulation failed"); +} From 59b842229ed862aaedeb6f81b1399273aeac44dd Mon Sep 17 00:00:00 2001 From: Alain Nicolas Date: Mon, 23 Oct 2023 19:03:41 +0200 Subject: [PATCH 2/3] fix: Resolve conflicts --- sdk/src/dataMapper/AttestationDataMapper.ts | 2 -- sdk/src/dataMapper/PortalDataMapper.ts | 23 +-------------------- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index a27e5651..0d917165 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -112,7 +112,6 @@ export default class AttestationDataMapper extends BaseDataMapper< return await this.executeReadMethod("balanceOfBatch", [accounts, ids]); } - // TODO: Use correct type for args private async executeReadMethod(functionName: string, args: unknown[]) { return await this.web3Client.readContract({ abi: abiAttestationRegistry, @@ -122,7 +121,6 @@ export default class AttestationDataMapper extends BaseDataMapper< }); } - // TODO: Use correct type for args private async simulateContract(functionName: string, args: unknown[]) { try { const { request } = await this.web3Client.simulateContract({ diff --git a/sdk/src/dataMapper/PortalDataMapper.ts b/sdk/src/dataMapper/PortalDataMapper.ts index 90613d1c..45e8b8a0 100644 --- a/sdk/src/dataMapper/PortalDataMapper.ts +++ b/sdk/src/dataMapper/PortalDataMapper.ts @@ -143,15 +143,6 @@ export default class PortalDataMapper extends BaseDataMapper Date: Mon, 23 Oct 2023 19:21:29 +0200 Subject: [PATCH 3/3] fix: async/await handling --- sdk/src/dataMapper/AttestationDataMapper.ts | 43 +++++----- sdk/src/dataMapper/PortalDataMapper.ts | 78 +++++++++---------- sdk/src/dataMapper/UtilsDataMapper.ts | 10 +-- ...orHandler.ts => simulationErrorHandler.ts} | 5 +- sdk/src/utils/transactionSender.ts | 9 +++ 5 files changed, 70 insertions(+), 75 deletions(-) rename sdk/src/utils/{errorHandler.ts => simulationErrorHandler.ts} (81%) create mode 100644 sdk/src/utils/transactionSender.ts diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index 0d917165..5ede1579 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -3,9 +3,10 @@ import { abiAttestationRegistry } from "../abi/AttestationRegistry"; import { Attestation, AttestationPayload } from "../types"; import { Attestation_filter, Attestation_orderBy } from "../../.graphclient"; import { Constants } from "../utils/constants"; -import { handleError } from "../utils/errorHandler"; -import { Address, Hash } from "viem"; +import { handleSimulationError } from "../utils/simulationErrorHandler"; +import { Address } from "viem"; import { encode } from "../utils/abiCoder"; +import { executeTransaction } from "../utils/transactionSender"; export default class AttestationDataMapper extends BaseDataMapper< Attestation, @@ -44,12 +45,12 @@ export default class AttestationDataMapper extends BaseDataMapper< } async simulateUpdateRouter(routerAddress: Address) { - return await this.simulateContract("updateRouter", [routerAddress]); + return this.simulateContract("updateRouter", [routerAddress]); } async updateRouter(routerAddress: Address) { const request = await this.simulateUpdateRouter(routerAddress); - return await this.executeTransaction(request); + return executeTransaction(this.walletClient, request); } async simulateMassImport(portalAddress: Address, attestationPayloads: AttestationPayload[]) { @@ -67,53 +68,53 @@ export default class AttestationDataMapper extends BaseDataMapper< ]); } - return await this.simulateContract("massImport", [attestationPayloadsArg, portalAddress]); + return this.simulateContract("massImport", [attestationPayloadsArg, portalAddress]); } async massImport(portalAddress: Address, attestationPayloads: AttestationPayload[]) { const request = await this.simulateMassImport(portalAddress, attestationPayloads); - return await this.executeTransaction(request); + return executeTransaction(this.walletClient, request); } async simulateIncrementVersionNumber() { - return await this.simulateContract("incrementVersionNumber", []); + return this.simulateContract("incrementVersionNumber", []); } async incrementVersionNumber() { const request = await this.simulateIncrementVersionNumber(); - return await this.executeTransaction(request); + return executeTransaction(this.walletClient, request); } async isRegistered(attestationId: string) { - return await this.executeReadMethod("isRegistered", [attestationId]); + return this.executeReadMethod("isRegistered", [attestationId]); } async isRevocable(portalId: string) { - return await this.executeReadMethod("isRevocable", [portalId]); + return this.executeReadMethod("isRevocable", [portalId]); } async getAttestation(attestationId: string) { - return await this.executeReadMethod("getAttestation", [attestationId]); + return this.executeReadMethod("getAttestation", [attestationId]); } async getVersionNumber() { - return await this.executeReadMethod("getVersionNumber", []); + return this.executeReadMethod("getVersionNumber", []); } async getAttestationIdCounter() { - return await this.executeReadMethod("getAttestationIdCounter", []); + return this.executeReadMethod("getAttestationIdCounter", []); } async balanceOf(account: Address, id: number) { - return await this.executeReadMethod("balanceOf", [account, id]); + return this.executeReadMethod("balanceOf", [account, id]); } async balanceOfBatch(accounts: Address[], ids: number[]) { - return await this.executeReadMethod("balanceOfBatch", [accounts, ids]); + return this.executeReadMethod("balanceOfBatch", [accounts, ids]); } private async executeReadMethod(functionName: string, args: unknown[]) { - return await this.web3Client.readContract({ + return this.web3Client.readContract({ abi: abiAttestationRegistry, address: this.conf.attestationRegistryAddress, functionName, @@ -133,15 +134,7 @@ export default class AttestationDataMapper extends BaseDataMapper< return request; } catch (err) { - handleError(err); + handleSimulationError(err); } } - - // TODO: Use correct type for request - // eslint-disable-next-line @typescript-eslint/no-explicit-any - private async executeTransaction(request: any) { - const hash: Hash = await this.walletClient.writeContract(request); - console.log(`Transaction sent with hash ${hash}`); - return hash; - } } diff --git a/sdk/src/dataMapper/PortalDataMapper.ts b/sdk/src/dataMapper/PortalDataMapper.ts index 45e8b8a0..87c187ed 100644 --- a/sdk/src/dataMapper/PortalDataMapper.ts +++ b/sdk/src/dataMapper/PortalDataMapper.ts @@ -1,11 +1,12 @@ import { AttestationPayload, Portal } from "../types"; import BaseDataMapper from "./BaseDataMapper"; import { abiDefaultPortal } from "../abi/DefaultPortal"; -import { Address, Hash } from "viem"; +import { Address } from "viem"; import { encode } from "../utils/abiCoder"; import { Portal_filter, Portal_orderBy } from "../../.graphclient"; import { abiPortalRegistry } from "../abi/PortalRegistry"; -import { handleError } from "../utils/errorHandler"; +import { handleSimulationError } from "../utils/simulationErrorHandler"; +import { executeTransaction } from "../utils/transactionSender"; export default class PortalDataMapper extends BaseDataMapper { typeName = "portal"; @@ -22,7 +23,7 @@ export default class PortalDataMapper extends BaseDataMapper err instanceof ContractFunctionRevertedError); if (revertError instanceof ContractFunctionRevertedError) { const errorName = revertError.data?.errorName ?? ""; console.error(`Failing with ${errorName}`); } + } else { + console.error(err); } - console.error(err); throw new Error("Simulation failed"); } diff --git a/sdk/src/utils/transactionSender.ts b/sdk/src/utils/transactionSender.ts new file mode 100644 index 00000000..ca20401b --- /dev/null +++ b/sdk/src/utils/transactionSender.ts @@ -0,0 +1,9 @@ +import { Hash, WalletClient } from "viem"; + +// TODO: Use correct type for request +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function executeTransaction(walletClient: WalletClient, request: any): Promise { + const hash: Hash = await walletClient.writeContract(request); + console.log(`Transaction sent with hash ${hash}`); + return hash; +}