wagmi-extended 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const tslib_1 = require("tslib");
5
+ tslib_1.__exportStar(require("./hooks/useContractWriteExtended"), exports);
6
+ tslib_1.__exportStar(require("./hooks/useSendTransactionExtended"), exports);
7
+ tslib_1.__exportStar(require("./utils/errorParser"), exports);
8
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/index.ts"],"sourcesContent":["export * from \"./hooks/useContractWriteExtended\";\nexport * from \"./hooks/useSendTransactionExtended\";\nexport * from \"./utils/errorParser\";\n"],"names":[],"mappings":";;;;AAAA,OAAiD,CAAA,YAAA,CAAA,OAAA,CAAA,kCAAA,CAAA,EAAA,OAAA,CAAA;AACjD,OAAmD,CAAA,YAAA,CAAA,OAAA,CAAA,oCAAA,CAAA,EAAA,OAAA,CAAA;AACnD,OAAoC,CAAA,YAAA,CAAA,OAAA,CAAA,qBAAA,CAAA,EAAA,OAAA,CAAA;;"}
@@ -0,0 +1,6 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const tslib_1 = require("tslib");
3
+ tslib_1.__exportStar(require("./hooks/useContractWriteExtended"), exports);
4
+ tslib_1.__exportStar(require("./hooks/useSendTransactionExtended"), exports);
5
+ tslib_1.__exportStar(require("./utils/errorParser"), exports);
6
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/index.ts"],"sourcesContent":["export * from \"./hooks/useContractWriteExtended\";\nexport * from \"./hooks/useSendTransactionExtended\";\nexport * from \"./utils/errorParser\";\n"],"names":[],"mappings":";;AAAA,OAAiD,CAAA,YAAA,CAAA,OAAA,CAAA,kCAAA,CAAA,EAAA,OAAA,CAAA;AACjD,OAAmD,CAAA,YAAA,CAAA,OAAA,CAAA,oCAAA,CAAA,EAAA,OAAA,CAAA;AACnD,OAAoC,CAAA,YAAA,CAAA,OAAA,CAAA,qBAAA,CAAA,EAAA,OAAA,CAAA"}
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "wagmi-extended",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
+ "type": "module",
4
5
  "description": "A library providing extended hooks on top of Wagmi with additional features.",
5
- "main": "dist/index.js",
6
+ "main": "dist/index.cjs.js",
7
+ "module": "dist/index.esm.js",
6
8
  "types": "dist/index.d.ts",
7
9
  "repository": {
8
10
  "type": "git",
@@ -15,10 +17,10 @@
15
17
  "react",
16
18
  "blockchain"
17
19
  ],
18
- "author": "Your Name",
20
+ "author": "Srdjan Rakic <bans.cowboy@gmail.com>",
19
21
  "license": "MIT",
20
22
  "scripts": {
21
- "build": "tsc",
23
+ "build": "rollup -c --bundleConfigAsCjs",
22
24
  "prepublishOnly": "npm run build"
23
25
  },
24
26
  "peerDependencies": {
@@ -28,8 +30,13 @@
28
30
  "wagmi": "^2.0.0"
29
31
  },
30
32
  "devDependencies": {
33
+ "@rollup/plugin-commonjs": "^28.0.3",
34
+ "@rollup/plugin-node-resolve": "^16.0.1",
35
+ "@rollup/plugin-typescript": "^12.1.2",
31
36
  "@types/node": "^22.14.0",
32
37
  "@types/react": "^19.1.0",
38
+ "rollup": "^4.39.0",
39
+ "rollup-plugin-peer-deps-external": "^2.2.4",
33
40
  "typescript": "^5.0.0"
34
41
  }
35
42
  }
@@ -0,0 +1,33 @@
1
+ // rollup.config.js
2
+ import typescript from "@rollup/plugin-typescript";
3
+ import peerDepsExternal from "rollup-plugin-peer-deps-external";
4
+ import resolve from "@rollup/plugin-node-resolve";
5
+ import commonjs from "@rollup/plugin-commonjs";
6
+
7
+ export default {
8
+ input: "src/index.ts",
9
+ output: [
10
+ {
11
+ file: "dist/index.cjs.js",
12
+ format: "cjs",
13
+ sourcemap: true,
14
+ },
15
+ {
16
+ file: "dist/index.esm.js",
17
+ format: "esm",
18
+ sourcemap: true,
19
+ },
20
+ ],
21
+ plugins: [
22
+ // Automatically externalize peerDependencies in package.json
23
+ peerDepsExternal(),
24
+ // Locate and bundle third party dependencies in node_modules
25
+ resolve(),
26
+ // Convert CommonJS modules to ES6, so they can be included in a Rollup bundle
27
+ commonjs(),
28
+ // Compile TypeScript files
29
+ typescript({ tsconfig: "./tsconfig.json" }),
30
+ ],
31
+ // Ensure that critical peer dependencies are marked as external (optional since peerDepsExternal may handle this)
32
+ external: ["react", "@tanstack/react-query", "wagmi", "viem"],
33
+ };
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useContractWriteExtended = useContractWriteExtended;
4
- const wagmi_1 = require("wagmi");
5
- const useHandleTransactionMutation_1 = require("./useHandleTransactionMutation");
6
- /**
7
- * Custom hook for writing to a smart contract using Wagmi.
8
- *
9
- * This hook provides functionality for sending a transaction using Wagmi, handling the asynchronous nature of the operation, waiting for the transaction receipt, and error handling.
10
- *
11
- * @param {WriteExtendedAsyncParams} [settings] - Optional settings for the write operation.
12
- * @param {boolean} [settings.disableWaitingForReceipt] - Disables waiting for the transaction receipt.
13
- * @param {boolean} [settings.disableLogging] - Disables logging the result of the transaction.
14
- * @param {Function} [settings.onSuccess] - Callback function to be called on successful transaction.
15
- * @param {Function} [settings.onError] - Callback function to be called on transaction error.
16
- * @param {Function} [settings.onSettled] - Callback function to be called after the transaction settles (whether success or failure).
17
- * @param {QueryKey[]} [settings.queriesToInvalidate] - Array of query keys to invalidate after the transaction receives a receipt.
18
- * @returns {Object} Object containing the following properties:
19
- * - {boolean} isPending - Indicates whether the transaction is pending.
20
- * - {string|undefined} errorMessage - The error message, if an error occurred during the transaction.
21
- * - {Function} writeContractAsync - Function to trigger the write operation.
22
- */
23
- function useContractWriteExtended(settings) {
24
- const { isPending, errorMessage, onMutate, onSettled } = (0, useHandleTransactionMutation_1.useHandleTransactionMutation)({
25
- settings,
26
- });
27
- const { writeContractAsync, ...rest } = (0, wagmi_1.useWriteContract)({
28
- mutation: {
29
- onMutate,
30
- onSettled,
31
- },
32
- });
33
- return {
34
- ...rest,
35
- isPending,
36
- errorMessage,
37
- writeContractAsync,
38
- };
39
- }
@@ -1,77 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useHandleTransactionMutation = useHandleTransactionMutation;
4
- const actions_1 = require("wagmi/actions");
5
- const useInvalidateQueries_1 = require("./useInvalidateQueries");
6
- const wagmi_1 = require("wagmi");
7
- const react_1 = require("react");
8
- const errorParser_1 = require("../utils/errorParser");
9
- /**
10
- * Custom hook to handle transaction mutations.
11
- *
12
- * @returns {Function} A shared `onSettled` callback for transaction mutations.
13
- */
14
- function useHandleTransactionMutation({ settings, }) {
15
- const wagmiConfig = (0, wagmi_1.useConfig)();
16
- const { invalidateMany } = (0, useInvalidateQueries_1.useInvalidateQueries)();
17
- const [isPending, setIsPending] = (0, react_1.useState)(false);
18
- const [errorMessage, setErrorMessage] = (0, react_1.useState)(undefined);
19
- const onMutate = () => {
20
- setIsPending(true);
21
- setErrorMessage(undefined);
22
- };
23
- const onSettled = async (txHash, error, args) => {
24
- var _a, _b, _c;
25
- try {
26
- if (error)
27
- throw error;
28
- if (!(settings === null || settings === void 0 ? void 0 : settings.disableWaitingForReceipt)) {
29
- // 1. wait for transaction receipt
30
- const txReceipt = await (0, actions_1.waitForTransactionReceipt)(wagmiConfig, {
31
- hash: txHash,
32
- });
33
- // 2. throw if receipt is not valid
34
- if (txReceipt.status === "reverted")
35
- throw new Error("Execution reverted.");
36
- if (txReceipt.status !== "success")
37
- throw new Error("Execution reverted.");
38
- }
39
- // 3. invalidate queries
40
- if (settings === null || settings === void 0 ? void 0 : settings.queriesToInvalidate)
41
- await invalidateMany(settings === null || settings === void 0 ? void 0 : settings.queriesToInvalidate);
42
- // 4. call onSuccess callback
43
- (_a = settings === null || settings === void 0 ? void 0 : settings.onSuccess) === null || _a === void 0 ? void 0 : _a.call(settings, txHash);
44
- if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogging)) {
45
- // 5. log result
46
- // eslint-disable-next-line no-console
47
- console.info("Operation successful:", txHash); // todo: add logging service
48
- }
49
- // 6. return result
50
- return txHash;
51
- }
52
- catch (error) {
53
- const parsedError = (0, errorParser_1.getParsedError)(error);
54
- if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogging)) {
55
- // 1. log error
56
- console.error(`ContractWriteExtended Operation failed with error(parsed): ${parsedError}`, { error }, { args });
57
- console.error({ error });
58
- }
59
- // 2. set error message
60
- setErrorMessage(parsedError);
61
- // 3. call callback
62
- (_b = settings === null || settings === void 0 ? void 0 : settings.onError) === null || _b === void 0 ? void 0 : _b.call(settings, error);
63
- }
64
- finally {
65
- setIsPending(false);
66
- // 1. call callback
67
- (_c = settings === null || settings === void 0 ? void 0 : settings.onSettled) === null || _c === void 0 ? void 0 : _c.call(settings);
68
- }
69
- return undefined;
70
- };
71
- return {
72
- onMutate,
73
- onSettled,
74
- isPending,
75
- errorMessage,
76
- };
77
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useInvalidateQueries = useInvalidateQueries;
4
- const react_query_1 = require("@tanstack/react-query");
5
- /**
6
- * Hook to invalidate multiple queries in the React Query cache.
7
- *
8
- * @returns An object with the invalidateMany function.
9
- */
10
- function useInvalidateQueries() {
11
- const queryClient = (0, react_query_1.useQueryClient)();
12
- const invalidateMany = async (queries) => {
13
- const promises = queries.map((queryKey) => queryClient.invalidateQueries({ queryKey }));
14
- await Promise.all(promises);
15
- };
16
- return { invalidateMany };
17
- }
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSendTransactionExtended = useSendTransactionExtended;
4
- const wagmi_1 = require("wagmi");
5
- const useHandleTransactionMutation_1 = require("./useHandleTransactionMutation");
6
- /**
7
- * Custom hook for sending a transaction using Wagmi.
8
- *
9
- * This hook provides functionality for sending a transaction using Wagmi, handling the asynchronous nature of the operation, waiting for the transaction receipt, and error handling.
10
- *
11
- * @param {SeamlessWriteAsyncParams} [settings] - Optional settings for the write operation.
12
- * @param {boolean} [settings.disableWaitingForReceipt] - Disables waiting for the transaction receipt.
13
- * @param {boolean} [settings.disableLogging] - Disables logging the result of the transaction.
14
- * @param {Function} [settings.onSuccess] - Callback function to be called on successful transaction.
15
- * @param {Function} [settings.onError] - Callback function to be called on transaction error.
16
- * @param {Function} [settings.onSettled] - Callback function to be called after the transaction settles (whether success or failure).
17
- * @param {QueryKey[]} [settings.queriesToInvalidate] - Array of query keys to invalidate after the transaction receives a receipt.
18
- * @returns {Object} Object containing the following properties:
19
- * - {boolean} isPending - Indicates whether the transaction is pending.
20
- * - {string|undefined} errorMessage - The error message, if an error occurred during the transaction.
21
- * - {Function} sendTransactionAsync - Function to trigger the send transaction mutation.
22
- */
23
- function useSendTransactionExtended(settings) {
24
- const { isPending, errorMessage, onMutate, onSettled } = (0, useHandleTransactionMutation_1.useHandleTransactionMutation)({
25
- settings,
26
- });
27
- const { sendTransactionAsync, ...rest } = (0, wagmi_1.useSendTransaction)({
28
- mutation: {
29
- onMutate,
30
- onSettled,
31
- },
32
- });
33
- return {
34
- ...rest,
35
- isPending,
36
- errorMessage,
37
- sendTransactionAsync,
38
- };
39
- }
package/dist/index.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./hooks/useContractWriteExtended"), exports);
18
- __exportStar(require("./hooks/useSendTransactionExtended"), exports);
19
- __exportStar(require("./utils/errorParser"), exports);
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getParsedError = exports.getErrorMapping = exports.resetErrorMapping = exports.setErrorMapping = void 0;
4
- const viem_1 = require("viem");
5
- /**
6
- * Default error mapping that contains a set of error identifiers mapped to user-friendly error messages.
7
- */
8
- const defaultErrorMapping = {
9
- EnforcedPause: "Temporary pause in effect, please check Discord for updates.",
10
- ErrorNotEnoughAllowance: "Not enough allowance, did you approve your tokens first?",
11
- "0xc2139725": "Not enough allowance, did you approve your tokens first?",
12
- SharesReceivedBelowMinimum: "Action exceeded safe slippage parameters, please try again later",
13
- "0xea8d7f02": "Action exceeded safe slippage parameters, please try again later",
14
- MaxSlippageExceeded: "Action exceeded safe slippage parameters, please try again later",
15
- "51": "Supply cap exceeded",
16
- };
17
- /**
18
- * A mutable copy of the default error mapping that can be extended or overridden by users.
19
- */
20
- let currentErrorMapping = { ...defaultErrorMapping };
21
- /**
22
- * Merges a custom error mapping into the current error mapping.
23
- * Custom values override any existing keys.
24
- *
25
- * @param customMapping - An object containing error keys and the corresponding custom messages.
26
- *
27
- * @example
28
- * setErrorMapping({
29
- * ErrorNotEnoughAllowance: "Custom message: Please approve tokens first!",
30
- * NewCustomError: "A custom error occurred."
31
- * });
32
- */
33
- const setErrorMapping = (customMapping) => {
34
- currentErrorMapping = { ...currentErrorMapping, ...customMapping };
35
- };
36
- exports.setErrorMapping = setErrorMapping;
37
- /**
38
- * Resets the current error mapping to the default error mapping.
39
- *
40
- * @example
41
- * resetErrorMapping();
42
- */
43
- const resetErrorMapping = () => {
44
- currentErrorMapping = { ...defaultErrorMapping };
45
- };
46
- exports.resetErrorMapping = resetErrorMapping;
47
- /**
48
- * Retrieves the current error mapping.
49
- *
50
- * @returns The current error mapping object.
51
- *
52
- * @example
53
- * const mapping = getErrorMapping();
54
- * console.log(mapping);
55
- */
56
- const getErrorMapping = () => currentErrorMapping;
57
- exports.getErrorMapping = getErrorMapping;
58
- /**
59
- * Parses an error object and returns a user-friendly error message.
60
- *
61
- * The function checks if the error is a ContractFunctionRevertedError by attempting to walk through
62
- * the error using its `walk` method. If a matching error is found and its error key exists in the
63
- * current error mapping, the corresponding custom message will be returned. Otherwise, it falls back
64
- * to the error's own message properties.
65
- *
66
- * @param error - The error object, potentially including additional error details.
67
- * @returns A user-friendly error message.
68
- *
69
- * @example
70
- * const message = getParsedError(someError);
71
- * console.log(message); // Outputs a custom error message or a default error message.
72
- */
73
- const getParsedError = (error) => {
74
- var _a, _b, _c, _d;
75
- const defaultMessage = "An unknown error occurred. Please contact support.";
76
- let message = defaultMessage;
77
- let errorKey = "";
78
- const revertedError = (error === null || error === void 0 ? void 0 : error.walk)
79
- ? error.walk((err) => err instanceof viem_1.ContractFunctionRevertedError)
80
- : null;
81
- if (revertedError instanceof viem_1.ContractFunctionRevertedError) {
82
- errorKey =
83
- (_d = (_c = (_b = (_a = revertedError.data) === null || _a === void 0 ? void 0 : _a.errorName) !== null && _b !== void 0 ? _b : revertedError.signature) !== null && _c !== void 0 ? _c : revertedError.reason) !== null && _d !== void 0 ? _d : "";
84
- if (currentErrorMapping[errorKey])
85
- return currentErrorMapping[errorKey];
86
- }
87
- message = error.shortMessage || error.details || error.message || message;
88
- return message;
89
- };
90
- exports.getParsedError = getParsedError;