wagmi-extended 0.8.0 → 1.0.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.
- package/README.md +220 -1
- package/dist/config/defaults.d.ts +40 -0
- package/dist/fetch-functions/fetchAllowanceX.d.ts +2 -0
- package/dist/fetch-functions/fetchTokenX.d.ts +24 -0
- package/dist/hooks/{useContractWriteExtended.d.ts → useContractWriteX.d.ts} +43 -3
- package/dist/hooks/useERC20ApproveX.d.ts +40 -0
- package/dist/hooks/{useToken.d.ts → useFetchAssetAllowanceX.d.ts} +50 -37
- package/dist/hooks/{useHandleTransactionMutation.d.ts → useHandleTransactionMutationX.d.ts} +1 -1
- package/dist/hooks/{useSendTransactionExtended.d.ts → useSendTransactionX.d.ts} +31 -10
- package/dist/hooks/{useFetchAssetAllowance.d.ts → useTokenX.d.ts} +62 -108
- package/dist/index.cjs.js +353 -141
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +8 -4
- package/dist/index.esm.js +341 -138
- package/dist/index.esm.js.map +1 -1
- package/dist/utils/{errorParser.d.ts → errorParserX.d.ts} +1 -1
- package/package.json +2 -2
- package/src/config/defaults.ts +60 -0
- package/src/fetch-functions/fetchAllowanceX.ts +22 -0
- package/src/{hooks/useToken.ts → fetch-functions/fetchTokenX.ts} +42 -30
- package/src/hooks/useContractWriteX.ts +84 -0
- package/src/hooks/{useERC20Approve.ts → useERC20ApproveX.ts} +42 -38
- package/src/hooks/useFetchAssetAllowanceX.ts +60 -0
- package/src/hooks/{useHandleTransactionMutation.ts → useHandleTransactionMutationX.ts} +3 -3
- package/src/hooks/{useSendTransactionExtended.ts → useSendTransactionX.ts} +35 -13
- package/src/hooks/useTokenX.ts +65 -0
- package/src/index.ts +27 -4
- package/src/utils/{errorParser.ts → errorParserX.ts} +1 -1
- package/dist/hooks/useERC20Approve.d.ts +0 -28
- package/src/hooks/useContractWriteExtended.ts +0 -44
- package/src/hooks/useFetchAssetAllowance.ts +0 -94
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useConfig, useWriteContract, useSendTransaction, useAccount } from 'wagmi';
|
|
2
2
|
import { waitForTransactionReceipt } from 'wagmi/actions';
|
|
3
3
|
import { useState, useEffect } from 'react';
|
|
4
|
-
import { ContractFunctionRevertedError,
|
|
4
|
+
import { ContractFunctionRevertedError, erc20Abi, maxUint256, zeroAddress } from 'viem';
|
|
5
5
|
import { useQueryClient, useQuery } from '@tanstack/react-query';
|
|
6
6
|
import { readContract } from 'viem/actions';
|
|
7
7
|
import { readContractQueryOptions } from 'wagmi/query';
|
|
@@ -71,7 +71,7 @@ const getErrorMapping = () => currentErrorMapping;
|
|
|
71
71
|
* const message = getParsedError(someError);
|
|
72
72
|
* console.log(message); // Outputs a custom error message or a default error message.
|
|
73
73
|
*/
|
|
74
|
-
const
|
|
74
|
+
const getParsedErrorX = (error) => {
|
|
75
75
|
var _a, _b, _c, _d;
|
|
76
76
|
const defaultMessage = "An unknown error occurred. Please contact support.";
|
|
77
77
|
let message = defaultMessage;
|
|
@@ -108,7 +108,7 @@ function useInvalidateQueries() {
|
|
|
108
108
|
*
|
|
109
109
|
* @returns {Function} A shared `onSettled` callback for transaction mutations.
|
|
110
110
|
*/
|
|
111
|
-
function
|
|
111
|
+
function useHandleTransactionMutationX({ settings, }) {
|
|
112
112
|
const wagmiConfig = useConfig();
|
|
113
113
|
const { invalidateMany } = useInvalidateQueries();
|
|
114
114
|
const [isPending, setIsPending] = useState(false);
|
|
@@ -147,7 +147,7 @@ function useHandleTransactionMutation({ settings, }) {
|
|
|
147
147
|
return txHash;
|
|
148
148
|
}
|
|
149
149
|
catch (error) {
|
|
150
|
-
const parsedError =
|
|
150
|
+
const parsedError = getParsedErrorX(error);
|
|
151
151
|
if (!(settings === null || settings === void 0 ? void 0 : settings.disableLogging)) {
|
|
152
152
|
// 1. log error
|
|
153
153
|
console.error(`ContractWriteExtended Operation failed with error(parsed): ${parsedError}`, { error }, { args });
|
|
@@ -176,7 +176,7 @@ function useHandleTransactionMutation({ settings, }) {
|
|
|
176
176
|
/**
|
|
177
177
|
* Custom hook for writing to a smart contract using Wagmi.
|
|
178
178
|
*
|
|
179
|
-
* This hook provides functionality for
|
|
179
|
+
* This hook provides functionality for writing a contract using Wagmi, handling the asynchronous nature of the operation, waiting for the transaction receipt, and error handling.
|
|
180
180
|
*
|
|
181
181
|
* @param {WriteExtendedAsyncParams} [settings] - Optional settings for the write operation.
|
|
182
182
|
* @param {boolean} [settings.disableWaitingForReceipt] - Disables waiting for the transaction receipt.
|
|
@@ -189,9 +189,49 @@ function useHandleTransactionMutation({ settings, }) {
|
|
|
189
189
|
* - {boolean} isPending - Indicates whether the transaction is pending.
|
|
190
190
|
* - {string|undefined} errorMessage - The error message, if an error occurred during the transaction.
|
|
191
191
|
* - {Function} writeContractAsync - Function to trigger the write operation.
|
|
192
|
+
*
|
|
193
|
+
/**
|
|
194
|
+
* Custom hook for writing a contract using Wagmi with extended functionality.
|
|
195
|
+
*
|
|
196
|
+
* This hook wraps Wagmi’s `useContractWriteX` with additional handling for
|
|
197
|
+
* waiting for a transaction receipt, logging control, and invalidation of specified queries.
|
|
198
|
+
*
|
|
199
|
+
* @param {WriteExtendedAsyncParams} [settings] - Optional settings for handling the transaction.
|
|
200
|
+
* @returns {Object} An object containing:
|
|
201
|
+
* - `isPending`: {boolean} indicating if the transaction is in progress.
|
|
202
|
+
* - `errorMessage`: {string|undefined} a potential error message.
|
|
203
|
+
* - `writeContractAsync`: {Function} a function to trigger the transaction.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* // In your component:
|
|
207
|
+
* function MyTransactionComponent() {
|
|
208
|
+
* const { writeContractAsync, isPending, errorMessage } = useContractWriteX({
|
|
209
|
+
* onSuccess: (txHash) => console.log("Transaction successful:", txHash),
|
|
210
|
+
* onError: (error) => console.error("Transaction error:", error),
|
|
211
|
+
* queriesToInvalidate: [["userBalance"], ["userActivity"]],
|
|
212
|
+
* });
|
|
213
|
+
*
|
|
214
|
+
* const handleWrite = async () => {
|
|
215
|
+
* try {
|
|
216
|
+
* const txHash = await writeContractAsync({ transaction params here.. });
|
|
217
|
+
* console.log("Received txHash:", txHash);
|
|
218
|
+
* } catch (err) {
|
|
219
|
+
* console.error("Failed writing transaction:", err);`
|
|
220
|
+
* }
|
|
221
|
+
* };
|
|
222
|
+
*
|
|
223
|
+
* return (
|
|
224
|
+
* <div>
|
|
225
|
+
* <button onClick={handleWrite} disabled={isPending}>
|
|
226
|
+
* {isPending ? "Processing..." : "Write Transaction"}
|
|
227
|
+
* </button>
|
|
228
|
+
* {errorMessage && <p>Error: {errorMessage}</p>}
|
|
229
|
+
* </div>
|
|
230
|
+
* );
|
|
231
|
+
* }
|
|
192
232
|
*/
|
|
193
|
-
function
|
|
194
|
-
const { isPending, errorMessage, onMutate, onSettled } =
|
|
233
|
+
function useContractWriteX(settings) {
|
|
234
|
+
const { isPending, errorMessage, onMutate, onSettled } = useHandleTransactionMutationX({
|
|
195
235
|
settings,
|
|
196
236
|
});
|
|
197
237
|
const { writeContractAsync, ...rest } = useWriteContract({
|
|
@@ -213,7 +253,7 @@ function useContractWriteExtended(settings) {
|
|
|
213
253
|
*
|
|
214
254
|
* 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.
|
|
215
255
|
*
|
|
216
|
-
* @param {
|
|
256
|
+
* @param {WriteExtendedAsyncParams} [settings] - Optional settings for the write operation.
|
|
217
257
|
* @param {boolean} [settings.disableWaitingForReceipt] - Disables waiting for the transaction receipt.
|
|
218
258
|
* @param {boolean} [settings.disableLogging] - Disables logging the result of the transaction.
|
|
219
259
|
* @param {Function} [settings.onSuccess] - Callback function to be called on successful transaction.
|
|
@@ -224,9 +264,37 @@ function useContractWriteExtended(settings) {
|
|
|
224
264
|
* - {boolean} isPending - Indicates whether the transaction is pending.
|
|
225
265
|
* - {string|undefined} errorMessage - The error message, if an error occurred during the transaction.
|
|
226
266
|
* - {Function} sendTransactionAsync - Function to trigger the send transaction mutation.
|
|
267
|
+
|
|
268
|
+
* @example
|
|
269
|
+
* // In your component:
|
|
270
|
+
* function MyTransactionComponent() {
|
|
271
|
+
* const { sendTransactionAsync, isPending, errorMessage } = useSendTransactionX({
|
|
272
|
+
* onSuccess: (txHash) => console.log("Transaction successful:", txHash),
|
|
273
|
+
* onError: (error) => console.error("Transaction error:", error),
|
|
274
|
+
* queriesToInvalidate: [["userBalance"], ["userActivity"]],
|
|
275
|
+
* });
|
|
276
|
+
*
|
|
277
|
+
* const handleSend = async () => {
|
|
278
|
+
* try {
|
|
279
|
+
* const txHash = await sendTransactionAsync({ transaction params here.. });
|
|
280
|
+
* console.log("Received txHash:", txHash);
|
|
281
|
+
* } catch (err) {
|
|
282
|
+
* console.error("Failed sending transaction:", err);`
|
|
283
|
+
* }
|
|
284
|
+
* };
|
|
285
|
+
*
|
|
286
|
+
* return (
|
|
287
|
+
* <div>
|
|
288
|
+
* <button onClick={handleSend} disabled={isPending}>
|
|
289
|
+
* {isPending ? "Processing..." : "Send Transaction"}
|
|
290
|
+
* </button>
|
|
291
|
+
* {errorMessage && <p>Error: {errorMessage}</p>}
|
|
292
|
+
* </div>
|
|
293
|
+
* );
|
|
294
|
+
* }
|
|
227
295
|
*/
|
|
228
|
-
function
|
|
229
|
-
const { isPending, errorMessage, onMutate, onSettled } =
|
|
296
|
+
function useSendTransactionX(settings) {
|
|
297
|
+
const { isPending, errorMessage, onMutate, onSettled } = useHandleTransactionMutationX({
|
|
230
298
|
settings,
|
|
231
299
|
});
|
|
232
300
|
const { sendTransactionAsync, ...rest } = useSendTransaction({
|
|
@@ -252,12 +320,210 @@ const queryConfig = {
|
|
|
252
320
|
},
|
|
253
321
|
};
|
|
254
322
|
|
|
323
|
+
const fetchAllowance = async (asset, spender, userAddress, config) => {
|
|
324
|
+
const allowance = await readContract(config, {
|
|
325
|
+
address: asset,
|
|
326
|
+
abi: erc20Abi,
|
|
327
|
+
functionName: "allowance",
|
|
328
|
+
args: [userAddress, spender],
|
|
329
|
+
});
|
|
330
|
+
if (allowance == null) {
|
|
331
|
+
throw new Error("Failed to fetch token data or allowance");
|
|
332
|
+
}
|
|
333
|
+
return allowance;
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const HookFetchAssetAllowanceQK = (asset, spender, userAddress) => ["HookFetchAllowance", asset, spender, userAddress];
|
|
337
|
+
/**
|
|
338
|
+
* Custom hook for fetching asset allowance.
|
|
339
|
+
*
|
|
340
|
+
* @param {Address} asset - The address of the ERC20 token contract.
|
|
341
|
+
* @param {Address} spender - The address of the spender to check allowance for.
|
|
342
|
+
*
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* // In your component:
|
|
346
|
+
* function AllowanceDisplay() {
|
|
347
|
+
* const { data: allowance, isLoading, error } = useFetchAssetAllowanceX({
|
|
348
|
+
* asset: "0xTokenAddressExample",
|
|
349
|
+
* spender: "0xSpenderAddressExample",
|
|
350
|
+
* });
|
|
351
|
+
*
|
|
352
|
+
* if (isLoading) return <div>Loading allowance...</div>;
|
|
353
|
+
* if (error) return <div>Error loading allowance</div>;
|
|
354
|
+
*
|
|
355
|
+
* return (
|
|
356
|
+
* <div>
|
|
357
|
+
* Current Allowance: {allowance}
|
|
358
|
+
* </div>
|
|
359
|
+
* );
|
|
360
|
+
* }
|
|
361
|
+
*/
|
|
362
|
+
const useFetchAssetAllowanceX = ({ asset, spender, }) => {
|
|
363
|
+
const config = useConfig();
|
|
364
|
+
const { address: userAddress } = useAccount();
|
|
365
|
+
const { data, ...rest } = useQuery({
|
|
366
|
+
queryKey: HookFetchAssetAllowanceQK(asset, spender, userAddress),
|
|
367
|
+
queryFn: () => fetchAllowance(asset, spender, userAddress, config),
|
|
368
|
+
enabled: Boolean(asset) && Boolean(spender) && Boolean(userAddress),
|
|
369
|
+
...queryConfig.sensitiveDataQueryConfig,
|
|
370
|
+
});
|
|
371
|
+
return {
|
|
372
|
+
...rest,
|
|
373
|
+
data,
|
|
374
|
+
queryKey: HookFetchAssetAllowanceQK(asset, spender, userAddress),
|
|
375
|
+
};
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Custom hook for approving ERC20 token transfers.
|
|
380
|
+
*
|
|
381
|
+
* This hook provides functionality for approving ERC20 token transfers, checking the current allowance, and handling the approval transaction using Wagmi.
|
|
382
|
+
*
|
|
383
|
+
* @param {Address} tokenAddress - The address of the ERC20 token contract (the transfer from).
|
|
384
|
+
* @param {Address} spenderAddress - The address of the spender to approve the transfer to.
|
|
385
|
+
* @param {bigint} [amount=BigInt(0)] - The amount to approve for transfer. Defaults to undefined.
|
|
386
|
+
* @param {boolean} [approveMax=false] - Indicates whether to approve the maximum amount or a specific amount.
|
|
387
|
+
* @returns {Object} Object containing the following properties:
|
|
388
|
+
* - {boolean} isApproved - Indicates whether the spender is already approved to transfer the specified amount of tokens.
|
|
389
|
+
* - {boolean} isApproving - Indicates whether an approval transaction is currently pending.
|
|
390
|
+
* - {Function} approveAsync - Function to trigger the approval transaction.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* // In your component:
|
|
394
|
+
* function ApproveTokenButton(amountToApprove) {
|
|
395
|
+
* const tokenAddress = "0xTokenAddressExample";
|
|
396
|
+
* const spenderAddress = "0xSpenderAddressExample";
|
|
397
|
+
*
|
|
398
|
+
* const { isApproved, isApproving, justApproved, approveAsync } = useERC20ApproveX(
|
|
399
|
+
* tokenAddress,
|
|
400
|
+
* spenderAddress,
|
|
401
|
+
* parseUnits(amountToApprove.toString(), 18),
|
|
402
|
+
* );
|
|
403
|
+
*
|
|
404
|
+
* return (
|
|
405
|
+
* <button onClick={approveAsync} disabled={isApproving || isApproved}>
|
|
406
|
+
* {isApproving ? "Approving..." : isApproved ? "Approved" : "Approve Token"}
|
|
407
|
+
* </button>
|
|
408
|
+
* );
|
|
409
|
+
* }
|
|
410
|
+
*/
|
|
411
|
+
const useERC20ApproveX = (tokenAddress, spenderAddress, amount, approveMax) => {
|
|
412
|
+
const [isApproved, setIsApproved] = useState(false);
|
|
413
|
+
const [justApproved, setJustApproved] = useState(false);
|
|
414
|
+
const { data: allowance, queryKey: allowanceKQ } = useFetchAssetAllowanceX({
|
|
415
|
+
asset: tokenAddress,
|
|
416
|
+
spender: spenderAddress,
|
|
417
|
+
});
|
|
418
|
+
const { writeContractAsync: approveTokenAsync, isPending } = useContractWriteX({
|
|
419
|
+
queriesToInvalidate: [allowanceKQ],
|
|
420
|
+
});
|
|
421
|
+
useEffect(() => {
|
|
422
|
+
if (amount == null) {
|
|
423
|
+
setIsApproved(false);
|
|
424
|
+
}
|
|
425
|
+
else if (allowance && allowance >= amount) {
|
|
426
|
+
setIsApproved(true);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
setIsApproved(false);
|
|
430
|
+
}
|
|
431
|
+
}, [allowance, amount]);
|
|
432
|
+
const approveAsync = async () => {
|
|
433
|
+
const amountToApprove = approveMax ? maxUint256 : amount;
|
|
434
|
+
try {
|
|
435
|
+
if (!spenderAddress) {
|
|
436
|
+
throw new Error("spenderAddress is undefined!");
|
|
437
|
+
}
|
|
438
|
+
if (!tokenAddress) {
|
|
439
|
+
throw new Error("tokenAddress is undefined!");
|
|
440
|
+
}
|
|
441
|
+
if (amountToApprove == null) {
|
|
442
|
+
throw new Error("amountToApprove is undefined!");
|
|
443
|
+
}
|
|
444
|
+
await approveTokenAsync({
|
|
445
|
+
address: tokenAddress,
|
|
446
|
+
abi: erc20Abi,
|
|
447
|
+
functionName: "approve",
|
|
448
|
+
args: [spenderAddress, amountToApprove],
|
|
449
|
+
}, {
|
|
450
|
+
onSuccess: () => {
|
|
451
|
+
setJustApproved(true);
|
|
452
|
+
},
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
catch (e) {
|
|
456
|
+
console.error("Error approving token:", e);
|
|
457
|
+
throw e;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
return {
|
|
461
|
+
isApproved,
|
|
462
|
+
isApproving: isPending,
|
|
463
|
+
justApproved,
|
|
464
|
+
approveAsync,
|
|
465
|
+
};
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
// You can adjust the type for wagmiConfig to match your needs.
|
|
469
|
+
let defaultQueryClient = null;
|
|
470
|
+
let defaultWagmiConfig = null;
|
|
471
|
+
/**
|
|
472
|
+
* Sets the default configuration values.
|
|
473
|
+
*
|
|
474
|
+
* @param queryClient - The default QueryClient instance.
|
|
475
|
+
* @param wagmiConfig - The default Wagmi configuration.
|
|
476
|
+
* @example
|
|
477
|
+
* //In your application initialization (e.g., index.tsx or App.tsx):
|
|
478
|
+
* import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
479
|
+
* import { wagmiConfig } from "/path/to/wagmi-config";
|
|
480
|
+
* import { setDefaults } from "wagmi-extended";
|
|
481
|
+
*
|
|
482
|
+
* const queryClient = new QueryClient();
|
|
483
|
+
*
|
|
484
|
+
* //Set defaults for the extended library functions.
|
|
485
|
+
* setDefaults(queryClient, wagmiConfig);
|
|
486
|
+
*
|
|
487
|
+
* //Now helper functions like fetchTokenX can use these defaults if no explicit parameters are provided.
|
|
488
|
+
*/
|
|
489
|
+
function setDefaults(queryClient, wagmiConfig) {
|
|
490
|
+
defaultQueryClient = queryClient;
|
|
491
|
+
defaultWagmiConfig = wagmiConfig;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Retrieves the currently set default configurations.
|
|
495
|
+
*
|
|
496
|
+
* @throws Will throw an error if defaults are not initialized.
|
|
497
|
+
* @returns An object containing the default queryClient and wagmiConfig.
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* // Usage in a helper function:
|
|
501
|
+
* import { getDefaults } from "wagmi-extended";
|
|
502
|
+
*
|
|
503
|
+
* function exampleFunction() {
|
|
504
|
+
* const { queryClient, wagmiConfig } = getDefaults();
|
|
505
|
+
* // Use queryClient and wagmiConfig as needed...
|
|
506
|
+
* }
|
|
507
|
+
*/
|
|
508
|
+
function getDefaults() {
|
|
509
|
+
if (!defaultQueryClient || !defaultWagmiConfig) {
|
|
510
|
+
throw new Error("Default configuration not set. Please call setDefaults() first.");
|
|
511
|
+
}
|
|
512
|
+
return { queryClient: defaultQueryClient, wagmiConfig: defaultWagmiConfig };
|
|
513
|
+
}
|
|
514
|
+
|
|
255
515
|
const EthTokenData = {
|
|
256
516
|
symbol: "ETH",
|
|
257
517
|
decimals: 18,
|
|
258
518
|
name: "Ethereum",
|
|
259
519
|
};
|
|
260
|
-
async function
|
|
520
|
+
async function fetchDecimalsX(token, queryClient, wagmiConfig) {
|
|
521
|
+
if (!queryClient || !wagmiConfig) {
|
|
522
|
+
({ queryClient, wagmiConfig } = getDefaults());
|
|
523
|
+
}
|
|
524
|
+
if (!queryClient || !wagmiConfig) {
|
|
525
|
+
throw new Error("Could not find queryClient or wagmiConfig, either pass them as arguments or set them using setDefaults()");
|
|
526
|
+
}
|
|
261
527
|
if (token === zeroAddress)
|
|
262
528
|
return EthTokenData.decimals;
|
|
263
529
|
const decimals = await queryClient.fetchQuery({
|
|
@@ -270,7 +536,13 @@ async function fetchDecimals(token, queryClient, wagmiConfig) {
|
|
|
270
536
|
});
|
|
271
537
|
return decimals;
|
|
272
538
|
}
|
|
273
|
-
async function
|
|
539
|
+
async function fetchSymbolX(token, queryClient, wagmiConfig) {
|
|
540
|
+
if (!queryClient || !wagmiConfig) {
|
|
541
|
+
({ queryClient, wagmiConfig } = getDefaults());
|
|
542
|
+
}
|
|
543
|
+
if (!queryClient || !wagmiConfig) {
|
|
544
|
+
throw new Error("Could not find queryClient or wagmiConfig, either pass them as arguments or set them using setDefaults()");
|
|
545
|
+
}
|
|
274
546
|
if (token === zeroAddress)
|
|
275
547
|
return EthTokenData.symbol;
|
|
276
548
|
const symbol = await queryClient.fetchQuery({
|
|
@@ -283,9 +555,15 @@ async function fetchSymbol(token, queryClient, wagmiConfig) {
|
|
|
283
555
|
});
|
|
284
556
|
return symbol;
|
|
285
557
|
}
|
|
286
|
-
async function
|
|
558
|
+
async function fetchNameX(token, queryClient, wagmiConfig) {
|
|
287
559
|
if (token === zeroAddress)
|
|
288
560
|
return EthTokenData.name;
|
|
561
|
+
if (!queryClient || !wagmiConfig) {
|
|
562
|
+
({ queryClient, wagmiConfig } = getDefaults());
|
|
563
|
+
}
|
|
564
|
+
if (!queryClient || !wagmiConfig) {
|
|
565
|
+
throw new Error("Could not find queryClient or wagmiConfig, either pass them as arguments or set them using setDefaults()");
|
|
566
|
+
}
|
|
289
567
|
const name = await queryClient.fetchQuery({
|
|
290
568
|
...readContractQueryOptions(wagmiConfig, {
|
|
291
569
|
address: token,
|
|
@@ -307,13 +585,13 @@ async function fetchName(token, queryClient, wagmiConfig) {
|
|
|
307
585
|
* @returns A `Token` object containing the symbol, decimals.
|
|
308
586
|
* @throws Will throw an error if symbol or decimals cannot be fetched.
|
|
309
587
|
*/
|
|
310
|
-
async function
|
|
588
|
+
async function fetchTokenX(token, queryClient, wagmiConfig) {
|
|
311
589
|
const [symbol, decimals, name] = await Promise.all([
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
590
|
+
fetchSymbolX(token, queryClient, wagmiConfig),
|
|
591
|
+
fetchDecimalsX(token, queryClient, wagmiConfig),
|
|
592
|
+
fetchNameX(token, queryClient, wagmiConfig),
|
|
315
593
|
]);
|
|
316
|
-
if (!symbol || !decimals) {
|
|
594
|
+
if (!symbol || !decimals || !name) {
|
|
317
595
|
throw new Error("Failed to fetch token data");
|
|
318
596
|
}
|
|
319
597
|
return {
|
|
@@ -323,138 +601,63 @@ async function fetchToken(token, queryClient, wagmiConfig) {
|
|
|
323
601
|
};
|
|
324
602
|
}
|
|
325
603
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
throw new Error("Failed to fetch token data or allowance");
|
|
338
|
-
}
|
|
339
|
-
return {
|
|
340
|
-
bigIntValue: allowance,
|
|
341
|
-
decimals: tokenData.decimals,
|
|
342
|
-
symbol: tokenData.symbol,
|
|
343
|
-
};
|
|
344
|
-
};
|
|
345
|
-
const HookFetchAssetAllowanceQK = (asset, spender, userAddress, config, queryClient) => [
|
|
346
|
-
"hookFetchAllowance",
|
|
604
|
+
/**
|
|
605
|
+
* Returns a query key for fetching token data.
|
|
606
|
+
*
|
|
607
|
+
* @param {Address | undefined} asset - The token address.
|
|
608
|
+
* @returns {Array} A unique query key for the token fetch.
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* const queryKey = HookFetchTokenQK("0x123...");
|
|
612
|
+
*/
|
|
613
|
+
const HookFetchTokenQK = (asset) => [
|
|
614
|
+
"HookTokenWagmiExtended",
|
|
347
615
|
asset,
|
|
348
|
-
spender,
|
|
349
|
-
userAddress,
|
|
350
|
-
config,
|
|
351
|
-
queryClient,
|
|
352
616
|
];
|
|
353
617
|
/**
|
|
354
|
-
* Custom hook for fetching
|
|
618
|
+
* Custom hook for fetching token metadata using extended Wagmi functionality.
|
|
355
619
|
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
620
|
+
* This hook leverages React Query for data fetching and caching.
|
|
621
|
+
* It retrieves token metadata (such as symbol, decimals, name, etc.) for a given token address.
|
|
622
|
+
*
|
|
623
|
+
* @param {Address} [asset] - The token address.
|
|
624
|
+
* @returns {Object} An object with the following properties:
|
|
625
|
+
* - `data`: The token data (or undefined if not loaded).
|
|
626
|
+
* - `isLoading`: Boolean indicating if the data is loading.
|
|
627
|
+
* - `error`: Any error encountered during the fetch.
|
|
628
|
+
* - `queryKey`: The unique key used for the query.
|
|
629
|
+
*
|
|
630
|
+
* @example
|
|
631
|
+
* // In your component:
|
|
632
|
+
* function MyTokenComponent() {
|
|
633
|
+
* const { data, isLoading, error, queryKey } = useTokenX("0x123456...");
|
|
634
|
+
*
|
|
635
|
+
* if (isLoading) return <div>Loading token data...</div>;
|
|
636
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
637
|
+
*
|
|
638
|
+
* return (
|
|
639
|
+
* <div>
|
|
640
|
+
* <p>Token Symbol: {data.symbol}</p>
|
|
641
|
+
* <p>Decimals: {data.decimals}</p>
|
|
642
|
+
* <p>Name: {data.name}</p>
|
|
643
|
+
* </div>
|
|
644
|
+
* );
|
|
645
|
+
* }
|
|
358
646
|
*/
|
|
359
|
-
const
|
|
360
|
-
const config = useConfig();
|
|
647
|
+
const useTokenX = (asset) => {
|
|
361
648
|
const queryClient = useQueryClient();
|
|
362
|
-
const
|
|
649
|
+
const config = useConfig();
|
|
363
650
|
const { data, ...rest } = useQuery({
|
|
364
|
-
queryKey:
|
|
365
|
-
queryFn: () =>
|
|
366
|
-
enabled:
|
|
367
|
-
...queryConfig.sensitiveDataQueryConfig,
|
|
651
|
+
queryKey: HookFetchTokenQK(asset),
|
|
652
|
+
queryFn: () => fetchTokenX(asset, queryClient, config),
|
|
653
|
+
enabled: Boolean(asset),
|
|
368
654
|
});
|
|
369
655
|
return {
|
|
370
656
|
...rest,
|
|
371
657
|
data,
|
|
372
|
-
queryKey:
|
|
373
|
-
};
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Helper function to determine human readable state of approve.
|
|
378
|
-
*
|
|
379
|
-
* @param {boolean} isApproved - Indicates if the approval is already done.
|
|
380
|
-
* @param {boolean} justApproved - Indicates if the user has just approved.
|
|
381
|
-
* @return {string} - The appropriate button text.
|
|
382
|
-
*/
|
|
383
|
-
function getApproveState(isApproved, justApproved) {
|
|
384
|
-
if (isApproved) {
|
|
385
|
-
return justApproved ? "Approve Confirmed" : "Approved";
|
|
386
|
-
}
|
|
387
|
-
return "Approve";
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Custom hook for approving ERC20 token transfers.
|
|
391
|
-
*
|
|
392
|
-
* This hook provides functionality for approving ERC20 token transfers, checking the current allowance, and handling the approval transaction using Wagmi.
|
|
393
|
-
*
|
|
394
|
-
* @param {Address} tokenAddress - The address of the ERC20 token contract.
|
|
395
|
-
* @param {Address} spenderAddress - The address of the spender to approve the transfer to.
|
|
396
|
-
* @param {bigint} [amount=BigInt(0)] - The amount of tokens to approve for transfer. Defaults to 0.
|
|
397
|
-
* @returns {Object} Object containing the following properties:
|
|
398
|
-
* - {boolean} isApproved - Indicates whether the spender is already approved to transfer the specified amount of tokens.
|
|
399
|
-
* - {boolean} isApproving - Indicates whether an approval transaction is currently pending.
|
|
400
|
-
* - {Function} approveAsync - Function to trigger the approval transaction.
|
|
401
|
-
*/
|
|
402
|
-
const useERC20Approve = (tokenAddress, spenderAddress, amount) => {
|
|
403
|
-
const [isApproved, setIsApproved] = useState(false);
|
|
404
|
-
const [justApproved, setJustApproved] = useState(false);
|
|
405
|
-
const { data: allowance, queryKey } = useFetchAssetAllowance({
|
|
406
|
-
asset: tokenAddress,
|
|
407
|
-
spender: spenderAddress,
|
|
408
|
-
});
|
|
409
|
-
const { writeContractAsync: approveTokenAsync, isPending } = useContractWriteExtended({
|
|
410
|
-
queriesToInvalidate: [queryKey],
|
|
411
|
-
});
|
|
412
|
-
useEffect(() => {
|
|
413
|
-
if (amount == null) {
|
|
414
|
-
setIsApproved(false);
|
|
415
|
-
}
|
|
416
|
-
else if (allowance && allowance.bigIntValue >= amount) {
|
|
417
|
-
setIsApproved(true);
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
setIsApproved(false);
|
|
421
|
-
}
|
|
422
|
-
}, [allowance, amount]);
|
|
423
|
-
const approveAsync = async () => {
|
|
424
|
-
const amountToApprove = amount;
|
|
425
|
-
if (!spenderAddress) {
|
|
426
|
-
throw new Error("spenderAddress is undefined!");
|
|
427
|
-
}
|
|
428
|
-
if (!tokenAddress) {
|
|
429
|
-
throw new Error("tokenAddress is undefined!");
|
|
430
|
-
}
|
|
431
|
-
if (amountToApprove == null) {
|
|
432
|
-
throw new Error("amountToApprove is undefined!");
|
|
433
|
-
}
|
|
434
|
-
try {
|
|
435
|
-
await approveTokenAsync({
|
|
436
|
-
address: tokenAddress,
|
|
437
|
-
abi: erc20Abi,
|
|
438
|
-
functionName: "approve",
|
|
439
|
-
args: [spenderAddress, amountToApprove],
|
|
440
|
-
}, {
|
|
441
|
-
onSuccess: () => {
|
|
442
|
-
setJustApproved(true);
|
|
443
|
-
},
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
catch (e) {
|
|
447
|
-
console.error("Error approving token:", e);
|
|
448
|
-
throw e;
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
return {
|
|
452
|
-
isApproved,
|
|
453
|
-
isApproving: isPending,
|
|
454
|
-
justApproved,
|
|
455
|
-
approveAsync,
|
|
658
|
+
queryKey: HookFetchTokenQK(asset),
|
|
456
659
|
};
|
|
457
660
|
};
|
|
458
661
|
|
|
459
|
-
export {
|
|
662
|
+
export { EthTokenData, HookFetchTokenQK, fetchDecimalsX, fetchNameX, fetchSymbolX, fetchTokenX, getDefaults, getErrorMapping, getParsedErrorX, resetErrorMapping, setDefaults, setErrorMapping, useContractWriteX, useERC20ApproveX, useFetchAssetAllowanceX, useSendTransactionX, useTokenX };
|
|
460
663
|
//# sourceMappingURL=index.esm.js.map
|