diff --git a/sdk/package.json b/sdk/package.json index c01d95fd..9c038787 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@verax-attestation-registry/verax-sdk", - "version": "1.8.1", + "version": "1.8.2", "description": "Verax Attestation Registry SDK to interact with the subgraph and the contracts", "keywords": [ "linea-attestation-registry", diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index 901f542c..1fcba99a 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -1,10 +1,10 @@ import BaseDataMapper from "./BaseDataMapper"; import { abiAttestationRegistry } from "../abi/AttestationRegistry"; import { Attestation, AttestationPayload, OffchainData, Schema } from "../types"; +import { ActionType, Constants } from "../utils/constants"; import { Attestation_filter, Attestation_orderBy, OrderDirection } from "../../.graphclient"; -import { Constants } from "../utils/constants"; -import { handleSimulationError } from "../utils/simulationErrorHandler"; -import { Address } from "viem"; +import { handleError } from "../utils/errorHandler"; +import { Address, Hex } from "viem"; import { decodeWithRetry, encode } from "../utils/abiCoder"; import { executeTransaction } from "../utils/transactionSender"; import { getIPFSContent } from "../utils/ipfsClient"; @@ -59,7 +59,7 @@ export default class AttestationDataMapper extends BaseDataMapper< private async enrichAttestation(attestation: Attestation) { const schema = (await this.veraxSdk.schema.findOneById(attestation.schemaId)) as Schema; - attestation.decodedPayload = decodeWithRetry(schema.schema, attestation.attestationData as `0x${string}`); + attestation.decodedPayload = decodeWithRetry(schema.schema, attestation.attestationData as Hex); attestation.attestedDate = Number(attestation.attestedDate); attestation.expirationDate = Number(attestation.expirationDate); @@ -82,10 +82,7 @@ export default class AttestationDataMapper extends BaseDataMapper< const offchainDataSchema = (await this.veraxSdk.schema.findOneById( attestation.offchainData.schemaId, )) as Schema; - attestation.decodedPayload = decodeWithRetry( - offchainDataSchema.schema, - attestation.attestationData as `0x${string}`, - ); + attestation.decodedPayload = decodeWithRetry(offchainDataSchema.schema, attestation.attestationData as Hex); } else { attestation.decodedPayload = response as unknown as object; } @@ -207,7 +204,7 @@ export default class AttestationDataMapper extends BaseDataMapper< return request; } catch (err) { - handleSimulationError(err); + handleError(ActionType.Simulation, err); } } } diff --git a/sdk/src/dataMapper/BaseDataMapper.ts b/sdk/src/dataMapper/BaseDataMapper.ts index 321c5d09..eec22304 100644 --- a/sdk/src/dataMapper/BaseDataMapper.ts +++ b/sdk/src/dataMapper/BaseDataMapper.ts @@ -2,8 +2,7 @@ import { PublicClient, WalletClient } from "viem"; import { Conf } from "../types"; import { OrderDirection } from "../../.graphclient"; import { VeraxSdk } from "../VeraxSdk"; -import { stringifyWhereClause } from "../utils/graphClientHelper"; -import axios from "axios"; +import { stringifyWhereClause, subgraphCall } from "../utils/graphClientHelper"; export default abstract class BaseDataMapper { protected readonly conf: Conf; @@ -23,7 +22,7 @@ export default abstract class BaseDataMapper { async findOneById(id: string) { const query = `query get_${this.typeName} { ${this.typeName}(id: "${id}") ${this.gqlInterface} }`; - const { data, status } = await this.subgraphCall(query); + const { data, status } = await subgraphCall(query, this.conf.subgraphUrl); if (status != 200) { throw new Error(`Error(s) while fetching ${this.typeName}`); @@ -46,7 +45,7 @@ export default abstract class BaseDataMapper { } `; - const { data, status } = await this.subgraphCall(query); + const { data, status } = await subgraphCall(query, this.conf.subgraphUrl); if (status != 200) { throw new Error(`Error(s) while fetching ${this.typeName}s`); @@ -54,17 +53,4 @@ export default abstract class BaseDataMapper { return data?.data ? (data.data[`${this.typeName}s`] as T[]) : []; } - - async subgraphCall(query: string) { - return axios.post( - this.conf.subgraphUrl, - { query }, - { - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - }, - ); - } } diff --git a/sdk/src/dataMapper/ModuleDataMapper.ts b/sdk/src/dataMapper/ModuleDataMapper.ts index 7ba1795b..a4943923 100644 --- a/sdk/src/dataMapper/ModuleDataMapper.ts +++ b/sdk/src/dataMapper/ModuleDataMapper.ts @@ -1,9 +1,10 @@ import { Address } from "viem"; import { Module_filter, Module_orderBy } from "../../.graphclient"; import { AttestationPayload, Module } from "../types"; +import { ActionType } from "../utils/constants"; import BaseDataMapper from "./BaseDataMapper"; import { abiModuleRegistry } from "../abi/ModuleRegistry"; -import { handleSimulationError } from "../utils/simulationErrorHandler"; +import { handleError } from "../utils/errorHandler"; import { executeTransaction } from "../utils/transactionSender"; import { encode } from "../utils/abiCoder"; @@ -143,7 +144,7 @@ export default class ModuleDataMapper extends BaseDataMapper { @@ -252,7 +253,7 @@ export default class PortalDataMapper extends BaseDataMapper { typeName = "schema"; @@ -94,7 +95,7 @@ export default class SchemaDataMapper extends BaseDataMapper { typeName = "counter"; @@ -54,11 +55,11 @@ export default class UtilsDataMapper extends BaseDataMapper err instanceof ContractFunctionRevertedError); if (revertError instanceof ContractFunctionRevertedError) { const errorName = revertError.data?.errorName ?? ""; - console.error(`Failing with ${errorName}`); + throw new Error(`${type} failed with ${errorName}`); } } else { - console.error(err); + throw new Error(`${type} failed with ${err}`); } - throw new Error("Simulation failed"); + throw new Error("${type} failed"); } diff --git a/sdk/src/utils/graphClientHelper.ts b/sdk/src/utils/graphClientHelper.ts index 8df5d6bb..2f989fe7 100644 --- a/sdk/src/utils/graphClientHelper.ts +++ b/sdk/src/utils/graphClientHelper.ts @@ -1,4 +1,19 @@ +import axios from "axios"; + export function stringifyWhereClause(whereClauseObj: Record) { const json = JSON.stringify(whereClauseObj); return json.replace(/"([^"]+)":/g, "$1:"); } + +export function subgraphCall(query: string, url: string) { + return axios.post( + url, + { query }, + { + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + }, + ); +} diff --git a/sdk/src/utils/ipfsClient.ts b/sdk/src/utils/ipfsClient.ts index a47e77b1..5de42e9d 100644 --- a/sdk/src/utils/ipfsClient.ts +++ b/sdk/src/utils/ipfsClient.ts @@ -1,9 +1,7 @@ import axios from "axios"; export const getIPFSContent = async (ipfsHash: string): Promise => { - // Use the Cloudflare IPFS gateway URL const ipfsGatewayUrl = `https://cloudflare-ipfs.com/ipfs/${ipfsHash}`; - // Make a request to the IPFS gateway const response = await axios.get(ipfsGatewayUrl); return response.data; }; diff --git a/sdk/src/utils/transactionSender.ts b/sdk/src/utils/transactionSender.ts index 01c7e300..3757c681 100644 --- a/sdk/src/utils/transactionSender.ts +++ b/sdk/src/utils/transactionSender.ts @@ -1,4 +1,6 @@ import { Hash, PublicClient, TransactionReceipt, WalletClient } from "viem"; +import { handleError } from "./errorHandler"; +import { ActionType } from "./constants"; // TODO: Use correct type for request export async function executeTransaction( @@ -14,7 +16,6 @@ export async function executeTransaction( try { const hash: Hash = await walletClient.writeContract(request); - console.log(`Transaction sent with hash ${hash}`); if (waitForConfirmation) { // Wait for the transaction to be confirmed @@ -22,9 +23,7 @@ export async function executeTransaction( } return { transactionHash: hash }; - } catch (error) { - // Handle errors or rethrow if needed - console.error("Error while executing transaction:", error); - throw error; + } catch (err) { + handleError(ActionType.Transaction, err); } }