Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Remove logs from the SDK #623

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 6 additions & 9 deletions sdk/src/dataMapper/AttestationDataMapper.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -207,7 +204,7 @@ export default class AttestationDataMapper extends BaseDataMapper<

return request;
} catch (err) {
handleSimulationError(err);
handleError(ActionType.Simulation, err);
}
}
}
20 changes: 3 additions & 17 deletions sdk/src/dataMapper/BaseDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, TFilter, TOrder> {
protected readonly conf: Conf;
Expand All @@ -23,7 +22,7 @@ export default abstract class BaseDataMapper<T, TFilter, TOrder> {
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}`);
Expand All @@ -46,25 +45,12 @@ export default abstract class BaseDataMapper<T, TFilter, TOrder> {
}
`;

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`);
}

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",
},
},
);
}
}
5 changes: 3 additions & 2 deletions sdk/src/dataMapper/ModuleDataMapper.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -143,7 +144,7 @@ export default class ModuleDataMapper extends BaseDataMapper<Module, Module_filt

return request;
} catch (err) {
handleSimulationError(err);
handleError(ActionType.Simulation, err);
}
}
}
7 changes: 4 additions & 3 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 { ActionType } from "../utils/constants";
import BaseDataMapper from "./BaseDataMapper";
import { abiDefaultPortal } from "../abi/DefaultPortal";
import { Address } from "viem";
import { encode } from "../utils/abiCoder";
import { Portal_filter, Portal_orderBy } from "../../.graphclient";
import { abiPortalRegistry } from "../abi/PortalRegistry";
import { handleSimulationError } from "../utils/simulationErrorHandler";
import { handleError } from "../utils/errorHandler";
import { executeTransaction } from "../utils/transactionSender";

export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filter, Portal_orderBy> {
Expand Down Expand Up @@ -252,7 +253,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt

return request;
} catch (err) {
handleSimulationError(err);
handleError(ActionType.Simulation, err);
}
}

Expand All @@ -275,7 +276,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt

return request;
} catch (err) {
handleSimulationError(err);
handleError(ActionType.Simulation, err);
}
}
}
5 changes: 3 additions & 2 deletions sdk/src/dataMapper/SchemaDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Address, TransactionReceipt } from "viem";
import { Schema_filter, Schema_orderBy } from "../../.graphclient";
import { Schema } from "../types";
import { ActionType } from "../utils/constants";
import BaseDataMapper from "./BaseDataMapper";
import { abiSchemaRegistry } from "../abi/SchemaRegistry";
import { executeTransaction } from "../utils/transactionSender";
import { handleSimulationError } from "../utils/simulationErrorHandler";
import { handleError } from "../utils/errorHandler";

export default class SchemaDataMapper extends BaseDataMapper<Schema, Schema_filter, Schema_orderBy> {
typeName = "schema";
Expand Down Expand Up @@ -94,7 +95,7 @@ export default class SchemaDataMapper extends BaseDataMapper<Schema, Schema_filt

return request;
} catch (err) {
handleSimulationError(err);
handleError(ActionType.Simulation, err);
}
}
}
5 changes: 3 additions & 2 deletions sdk/src/dataMapper/UtilsDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { abiModuleRegistry } from "../abi/ModuleRegistry";
import { abiPortalRegistry } from "../abi/PortalRegistry";
import { abiSchemaRegistry } from "../abi/SchemaRegistry";
import { decode, encode } from "../utils/abiCoder";
import { Hex } from "viem";

export default class UtilsDataMapper extends BaseDataMapper<object, unknown, unknown> {
typeName = "counter";
Expand Down Expand Up @@ -54,11 +55,11 @@ export default class UtilsDataMapper extends BaseDataMapper<object, unknown, unk
});
}

encode(schema: string, values: unknown[]): `0x${string}` {
encode(schema: string, values: unknown[]): Hex {
return encode(schema, values);
}

decode(schema: string, attestationData: `0x${string}`): readonly unknown[] {
decode(schema: string, attestationData: Hex): readonly unknown[] {
return decode(schema, attestationData);
}
}
4 changes: 0 additions & 4 deletions sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export type AttestationPayload = {
attestationData: object[]; // The attestation data.
};

export type AttestationWithDecodeObject = Attestation & {
decodeObject: { [propName: string]: unknown };
};

export type Attestation = OnChainAttestation & {
id: string;
schemaString: string;
Expand Down
12 changes: 6 additions & 6 deletions sdk/src/utils/abiCoder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { AbiParameter, BaseError, decodeAbiParameters, encodeAbiParameters, parseAbiParameters } from "viem";
import { AbiParameter, BaseError, decodeAbiParameters, encodeAbiParameters, Hex, parseAbiParameters } from "viem";

const ENCODED_PARENTHESIS: `0x${string}` = "0x0000000000000000000000000000000000000000000000000000000000000020";
const ENCODED_PARENTHESIS: Hex = "0x0000000000000000000000000000000000000000000000000000000000000020";

export function encode(schema: string, values: unknown[]): `0x${string}` {
export function encode(schema: string, values: unknown[]): Hex {
return encodeAbiParameters(parseAbiParameters(schema), values);
}

export function decode(schema: string, attestationData: `0x${string}`): readonly unknown[] {
export function decode(schema: string, attestationData: Hex): readonly unknown[] {
return decodeAbiParameters(parseAbiParameters(schema), attestationData);
}

export function decodeWithRetry(schema: string, attestationData: `0x${string}`): readonly unknown[] {
export function decodeWithRetry(schema: string, attestationData: Hex): readonly unknown[] {
const wrappedSchema = schema.startsWith("(") ? schema : `(${schema})`;
let result = decodeWrapped(wrappedSchema, attestationData);

Expand All @@ -21,7 +21,7 @@ export function decodeWithRetry(schema: string, attestationData: `0x${string}`):
return result;
}

function decodeWrapped(schema: string, attestationData: `0x${string}`): readonly unknown[] {
function decodeWrapped(schema: string, attestationData: Hex): readonly unknown[] {
try {
const parsedParams = tryParse(schema);
return decodeAbiParameters(parsedParams, attestationData);
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ export enum SDKMode {
BACKEND = "BACKEND",
FRONTEND = "FRONTEND",
}

export enum ActionType {
Simulation = "Simulation",
Transaction = "Transaction",
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { BaseError, ContractFunctionRevertedError } from "viem";
import { ActionType } from "./constants";

export function handleSimulationError(err: unknown): never {
export function handleError(type: ActionType, 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}`);
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");
}
15 changes: 15 additions & 0 deletions sdk/src/utils/graphClientHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import axios from "axios";

export function stringifyWhereClause(whereClauseObj: Record<string, unknown>) {
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",
},
},
);
}
2 changes: 0 additions & 2 deletions sdk/src/utils/ipfsClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import axios from "axios";

export const getIPFSContent = async (ipfsHash: string): Promise<string> => {
// 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;
};
9 changes: 4 additions & 5 deletions sdk/src/utils/transactionSender.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -14,17 +16,14 @@ 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
return await publicClient.waitForTransactionReceipt({ hash });
}

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);
}
}
Loading