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; +}