Skip to content

Commit

Permalink
fix: async/await handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alainncls committed Oct 23, 2023
1 parent e13ad9b commit 47b0a8a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 75 deletions.
43 changes: 18 additions & 25 deletions sdk/src/dataMapper/AttestationDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[]) {
Expand All @@ -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,
Expand All @@ -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;
}
}
78 changes: 35 additions & 43 deletions sdk/src/dataMapper/PortalDataMapper.ts
Original file line number Diff line number Diff line change
@@ -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<Portal, Portal_filter, Portal_orderBy> {
typeName = "portal";
Expand All @@ -22,15 +23,15 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
async simulateAttest(portalAddress: Address, attestationPayload: AttestationPayload, validationPayloads: string[]) {
const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId);
const attestationData = encode(matchingSchema.schema, attestationPayload.attestationData);
return await this.simulateContract(portalAddress, "attest", [
return this.simulatePortalContract(portalAddress, "attest", [
[attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData],
validationPayloads,
]);
}

async attest(portalAddress: Address, attestationPayload: AttestationPayload, validationPayloads: string[]) {
const request = await this.simulateAttest(portalAddress, attestationPayload, validationPayloads);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async simulateBulkAttest(
Expand All @@ -51,31 +52,30 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationData,
]);
}

return await this.simulateContract(portalAddress, "bulkAttest", [attestationPayloadsArg, validationPayloads]);
return this.simulatePortalContract(portalAddress, "bulkAttest", [attestationPayloadsArg, validationPayloads]);
}

async bulkAttest(portalAddress: Address, attestationPayloads: AttestationPayload[], validationPayloads: string[][]) {
const request = await this.simulateBulkAttest(portalAddress, attestationPayloads, validationPayloads);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async simulateRevoke(portalAddress: Address, attestationId: string) {
return await this.simulateContract(portalAddress, "revoke", [attestationId]);
return this.simulatePortalContract(portalAddress, "revoke", [attestationId]);
}

async revoke(portalAddress: Address, attestationId: string) {
const request = await this.simulateRevoke(portalAddress, attestationId);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async simulateBulkRevoke(portalAddress: Address, attestationIds: string[]) {
return await this.simulateContract(portalAddress, "bulkRevoke", [attestationIds]);
return this.simulatePortalContract(portalAddress, "bulkRevoke", [attestationIds]);
}

async bulkRevoke(portalAddress: Address, attestationIds: string[]) {
const request = await this.simulateBulkRevoke(portalAddress, attestationIds);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async simulateReplace(
Expand All @@ -86,7 +86,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
) {
const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId);
const attestationData = encode(matchingSchema.schema, attestationPayload.attestationData);
return await this.simulateContract(portalAddress, "replace", [
return this.simulatePortalContract(portalAddress, "replace", [
attestationId,
[attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData],
validationPayloads,
Expand All @@ -100,7 +100,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
validationPayloads: string[],
) {
const request = await this.simulateReplace(portalAddress, attestationId, attestationPayload, validationPayloads);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async simulateBulkReplace(
Expand All @@ -121,7 +121,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationData,
]);
}
return await this.simulateContract(portalAddress, "bulkReplace", [
return this.simulatePortalContract(portalAddress, "bulkReplace", [
attestationIds,
attestationPayloadsArg,
validationPayloads,
Expand All @@ -140,23 +140,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
attestationPayloads,
validationPayloads,
);
return await this.executeTransaction(request);
}

private async simulateContract(portalAddress: Address, functionName: string, args: unknown[]) {
try {
const { request } = await this.web3Client.simulateContract({
address: portalAddress,
abi: abiDefaultPortal,
functionName,
account: this.walletClient.account,
args,
});

return request;
} catch (err) {
handleError(err);
}
return executeTransaction(this.walletClient, request);
}

async simulateRegister(id: Address, name: string, description: string, isRevocable: boolean, ownerName: string) {
Expand All @@ -165,7 +149,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt

async register(id: Address, name: string, description: string, isRevocable: boolean, ownerName: string) {
const request = await this.simulateRegister(id, name, description, isRevocable, ownerName);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async simulateDeployDefaultPortal(
Expand All @@ -192,19 +176,19 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
ownerName: string,
) {
const request = await this.simulateDeployDefaultPortal(modules, name, description, isRevocable, ownerName);
return await this.executeTransaction(request);
return executeTransaction(this.walletClient, request);
}

async getPortalByAddress(id: Address) {
return await this.executePortalRegistryReadMethod("getPortalByAddress", [id]);
return this.executePortalRegistryReadMethod("getPortalByAddress", [id]);
}

async isPortalRegistered(id: Address) {
return await this.executePortalRegistryReadMethod("isRegistered", [id]);
return this.executePortalRegistryReadMethod("isRegistered", [id]);
}

private async executePortalRegistryReadMethod(functionName: string, args: unknown[]) {
return await this.web3Client.readContract({
return this.web3Client.readContract({
abi: abiPortalRegistry,
address: this.conf.portalRegistryAddress,
functionName,
Expand All @@ -224,15 +208,23 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt

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;
private async simulatePortalContract(portalAddress: Address, functionName: string, args: unknown[]) {
try {
const { request } = await this.web3Client.simulateContract({
address: portalAddress,
abi: abiDefaultPortal,
functionName,
account: this.walletClient.account,
args,
});

return request;
} catch (err) {
handleSimulationError(err);
}
}
}
10 changes: 5 additions & 5 deletions sdk/src/dataMapper/UtilsDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ export default class UtilsDataMapper extends BaseDataMapper<object, unknown, unk
}`;

async getModulesNumber() {
return await this.web3Client.readContract({
return this.web3Client.readContract({
abi: abiModuleRegistry,
address: this.conf.moduleRegistryAddress,
functionName: "getModulesNumber",
});
}

async getPortalsCount() {
return await this.web3Client.readContract({
return this.web3Client.readContract({
abi: abiPortalRegistry,
address: this.conf.portalRegistryAddress,
functionName: "getPortalsCount",
});
}

async getSchemasNumber() {
return await this.web3Client.readContract({
return this.web3Client.readContract({
abi: abiSchemaRegistry,
address: this.conf.schemaRegistryAddress,
functionName: "getSchemasNumber",
});
}

async getVersionNumber() {
return await this.web3Client.readContract({
return this.web3Client.readContract({
abi: abiAttestationRegistry,
address: this.conf.attestationRegistryAddress,
functionName: "getVersionNumber",
});
}

async getAttestationIdCounter() {
return await this.web3Client.readContract({
return this.web3Client.readContract({
abi: abiAttestationRegistry,
address: this.conf.attestationRegistryAddress,
functionName: "getAttestationIdCounter",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { BaseError, ContractFunctionRevertedError } from "viem";

export function handleError(err: unknown): never {
export function handleSimulationError(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}`);
}
} else {
console.error(err);
}
console.error(err);

throw new Error("Simulation failed");
}
9 changes: 9 additions & 0 deletions sdk/src/utils/transactionSender.ts
Original file line number Diff line number Diff line change
@@ -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<Hash> {
const hash: Hash = await walletClient.writeContract(request);
console.log(`Transaction sent with hash ${hash}`);
return hash;
}

0 comments on commit 47b0a8a

Please sign in to comment.