Skip to content

Commit

Permalink
feat: added read and write methods from schema registry into SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
kolhapuresatyajeet authored and alainncls committed Oct 23, 2023
1 parent 8b1edf9 commit 1a7a1e9
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 3 deletions.
22 changes: 22 additions & 0 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,28 @@ pnpm module findby '{\"name\": \"Msg Sender Module\"}'
pnpm schema findonebyid "0xce2647ed39aa89e6d1528a56deb6c30667ed2aae1ec2378ec3140c0c5d98a61e"

pnpm schema findby '{\"description\": \"Gitcoin Passport Score\"}'

pnpm schema simulateUpdateRouter "0x980978299e23B8F9B4D11542A83D92C83e781cb6"

pnpm schema updateRouter "0x980978299e23B8F9B4D11542A83D92C83e781cb6"

pnpm schema simulateCreate '{\"name\": \"sampleSchema\", \"description\": \"Example schema\", \"context\": \"Created by example\", \"schemaString\": \"bool isExample\"}'

pnpm schema create '{\"name\": \"sampleSchema\", \"description\": \"Example schema\", \"context\": \"Created by example\", \"schemaString\": \"bool isExample\"}'

pnpm schema simulateUpdateContext '{\"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"context\": \"New context\"}'

pnpm schema updateContext '{\"schemaId\": \"0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738\", \"context\": \"New context\"}'

pnpm schema getIdFromSchemaString 'bool isExample'

pnpm schema getSchema "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738"

pnpm schema getSchemasNumber

pnpm schema isRegistered "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738"

pnpm schema getSchemaIds 0
```

## Other operations
Expand Down
81 changes: 80 additions & 1 deletion sdk/examples/schema/schemaExamples.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address } from "viem";
import VeraxSdk from "../../src/VeraxSdk";

export default class SchemaExamples {
Expand All @@ -18,6 +19,84 @@ export default class SchemaExamples {
console.log(await this.veraxSdk.schema.findBy(2, 0, { description: "Gitcoin Passport Score" }, "name", "desc"));
}

if (methodName.toLowerCase() == "create" || methodName == "") console.log(await this.veraxSdk.schema.create());
if (methodName.toLowerCase() == "simulateUpdateRouter".toLowerCase() || methodName == "") {
const routerAddress: Address = argv === "" ? "0x736c78b2f2cBf4F921E8551b2acB6A5Edc9177D5" : (argv as Address);
console.log(await this.veraxSdk.schema.simulateUpdateRouter(routerAddress));
}

if (methodName.toLowerCase() == "updateRouter".toLowerCase() || methodName == "") {
const routerAddress: Address = argv === "" ? "0x736c78b2f2cBf4F921E8551b2acB6A5Edc9177D5" : (argv as Address);
console.log(await this.veraxSdk.schema.updateRouter(routerAddress));
}

if (methodName.toLowerCase() == "simulateCreate".toLowerCase() || methodName == "") {
let params;
if (argv !== "") params = JSON.parse(argv);
const { name, description, context, schemaString } = params ?? {
name: "test",
description: "example",
context: "test",
schemaString: "(bool isBuidler)",
};
console.log(await this.veraxSdk.schema.simulateCreate(name, description, context, schemaString));
}

if (methodName.toLowerCase() == "create".toLowerCase() || methodName == "") {
let params;
if (argv !== "") params = JSON.parse(argv);
const { name, description, context, schemaString } = params ?? {
name: "test",
description: "example",
context: "test",
schemaString: "(bool isBuidler)",
};
console.log(await this.veraxSdk.schema.create(name, description, context, schemaString));
}

if (methodName.toLowerCase() == "simulateUpdateContext".toLowerCase() || methodName == "") {
let params;
if (argv !== "") params = JSON.parse(argv);
const { schemaId, context } = params ?? {
schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738",
context: "new context",
};
console.log(await this.veraxSdk.schema.simulateUpdateContext(schemaId, context));
}

if (methodName.toLowerCase() == "updateContext".toLowerCase() || methodName == "") {
let params;
if (argv !== "") params = JSON.parse(argv);
const { schemaId, context } = params ?? {
schemaId: "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738",
context: "new context",
};
console.log(await this.veraxSdk.schema.updateContext(schemaId, context));
}

if (methodName.toLowerCase() == "getIdFromSchemaString".toLowerCase() || methodName == "") {
const schemaString: string = argv === "" ? "(bool isBuidler)" : argv;
console.log(await this.veraxSdk.schema.getIdFromSchemaString(schemaString));
}

if (methodName.toLowerCase() == "getSchema".toLowerCase() || methodName == "") {
const schemaId: string =
argv === "" ? "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738" : argv;
console.log(await this.veraxSdk.schema.getSchema(schemaId));
}

if (methodName.toLowerCase() == "getSchemasNumber".toLowerCase() || methodName == "") {
console.log(await this.veraxSdk.schema.getSchemasNumber());
}

if (methodName.toLowerCase() == "isRegistered".toLowerCase() || methodName == "") {
const schemaId: string =
argv === "" ? "0x9ba590dd7fbd5bd1a7d06cdcb4744e20a49b3520560575cd63de17734a408738" : argv;
console.log(await this.veraxSdk.schema.isRegistered(schemaId));
}

if (methodName.toLowerCase() == "getSchemaIds".toLowerCase() || methodName == "") {
const index: number = argv === "" ? 0 : (argv as unknown as number);
console.log(await this.veraxSdk.schema.getSchemaIds(index));
}
}
}
85 changes: 83 additions & 2 deletions sdk/src/dataMapper/SchemaDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Address, Hash } from "viem";
import { Schema_filter, Schema_orderBy } from "../../.graphclient";
import { Schema } from "../types";
import { handleError } from "../utils/errorHandler";
import BaseDataMapper from "./BaseDataMapper";
import { abiSchemaRegistry } from "../abi/SchemaRegistry";

export default class SchemaDataMapper extends BaseDataMapper<Schema, Schema_filter, Schema_orderBy> {
typeName = "schema";
Expand All @@ -12,7 +15,85 @@ export default class SchemaDataMapper extends BaseDataMapper<Schema, Schema_filt
schema
}`;

async create() {
throw new Error("Not implemented");
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 simulateCreate(name: string, description: string, context: string, schemaString: string) {
return await this.simulateContract("createSchema", [name, description, context, schemaString]);
}

async create(name: string, description: string, context: string, schemaString: string) {
const request = await this.simulateCreate(name, description, context, schemaString);
return await this.executeTransaction(request);
}

async simulateUpdateContext(schemaId: string, context: string) {
return await this.simulateContract("updateContext", [schemaId, context]);
}

async updateContext(schemaId: string, context: string) {
const request = await this.simulateUpdateContext(schemaId, context);
return await this.executeTransaction(request);
}

async getIdFromSchemaString(schema: string) {
return await this.executeReadMethod("getIdFromSchemaString", [schema]);
}

async getSchema(schemaId: string) {
return await this.executeReadMethod("getSchema", [schemaId]);
}

async getSchemasNumber() {
return await this.executeReadMethod("getSchemasNumber", []);
}

async isRegistered(schemaId: string) {
return await this.executeReadMethod("isRegistered", [schemaId]);
}

async getSchemaIds(index: number) {
return await this.executeReadMethod("schemaIds", [index]);
}

// TODO: Use correct type for args
private async executeReadMethod(functionName: string, args: unknown[]) {
return await this.web3Client.readContract({
abi: abiSchemaRegistry,
address: this.conf.schemaRegistryAddress,
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.schemaRegistryAddress,
abi: abiSchemaRegistry,
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;
}
}
14 changes: 14 additions & 0 deletions sdk/src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 1a7a1e9

Please sign in to comment.