Skip to content

Commit

Permalink
feat: As a user, I want to be able to call any method on the `SchemaR…
Browse files Browse the repository at this point in the history
…egistry` via the SDK (#309)

Co-authored-by: Satyajeet Kolhapure <[email protected]>
Co-authored-by: Alain Nicolas <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2023
1 parent 6e593ef commit fff70cc
Show file tree
Hide file tree
Showing 3 changed files with 176 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 @@ -275,6 +275,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));
}
}
}
76 changes: 74 additions & 2 deletions sdk/src/dataMapper/SchemaDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Address } from "viem";
import { Schema_filter, Schema_orderBy } from "../../.graphclient";
import { Schema } from "../types";
import BaseDataMapper from "./BaseDataMapper";
import { abiSchemaRegistry } from "../abi/SchemaRegistry";
import { executeTransaction } from "../utils/transactionSender";
import { handleSimulationError } from "../utils/simulationErrorHandler";

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

async create() {
throw new Error("Not implemented");
async simulateUpdateRouter(routerAddress: Address) {
return this.simulateContract("updateRouter", [routerAddress]);
}

async updateRouter(routerAddress: Address) {
const request = await this.simulateUpdateRouter(routerAddress);
return executeTransaction(this.walletClient, request);
}

async simulateCreate(name: string, description: string, context: string, schemaString: string) {
return 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 executeTransaction(this.walletClient, request);
}

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

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

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

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

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

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

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

private async executeReadMethod(functionName: string, args: unknown[]) {
return this.web3Client.readContract({
abi: abiSchemaRegistry,
address: this.conf.schemaRegistryAddress,
functionName,
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) {
handleSimulationError(err);
}
}
}

0 comments on commit fff70cc

Please sign in to comment.