|
1 |
| -import { encodeFunctionData, type SimulateContractParameters } from "viem"; |
2 |
| -import { useAccount, useWriteContract } from "wagmi"; |
3 |
| -import { createUseSimulateContract, createUseWriteContract } from "wagmi/codegen"; |
| 1 | +import { useCallback } from "react"; |
4 | 2 |
|
5 |
| -import { DEFAULT_CHAIN } from "consts/chains"; |
| 3 | +import { encodeFunctionData, type SimulateContractParameters } from "viem"; |
6 | 4 |
|
7 | 5 | import { isUndefined } from "src/utils";
|
8 | 6 |
|
9 |
| -const batcherAbi = [ |
10 |
| - { |
11 |
| - inputs: [ |
12 |
| - { |
13 |
| - internalType: "address[]", |
14 |
| - name: "targets", |
15 |
| - type: "address[]", |
16 |
| - }, |
17 |
| - { |
18 |
| - internalType: "uint256[]", |
19 |
| - name: "values", |
20 |
| - type: "uint256[]", |
21 |
| - }, |
22 |
| - { |
23 |
| - internalType: "bytes[]", |
24 |
| - name: "datas", |
25 |
| - type: "bytes[]", |
26 |
| - }, |
27 |
| - ], |
28 |
| - name: "batchSend", |
29 |
| - outputs: [], |
30 |
| - stateMutability: "payable", |
31 |
| - type: "function", |
32 |
| - }, |
33 |
| -] as const; |
34 |
| - |
35 |
| -const useBatchSimulate = createUseSimulateContract({ |
36 |
| - abi: batcherAbi, |
37 |
| - functionName: "batchSend", |
38 |
| -}); |
39 |
| - |
40 |
| -export const useBatchWrite = createUseWriteContract({ |
41 |
| - abi: batcherAbi, |
42 |
| - functionName: "batchSend", |
43 |
| -}); |
44 |
| - |
45 |
| -const batcherAddress = { |
46 |
| - 421614: "0xe8061d185D865ce2B2FbCfDa628b5F147d8eB8Ab", |
47 |
| - 42161: "0xE8f028aAc4d4B6A07E62c2C2f7B8818876a0CF2F", |
48 |
| - 100: "0x5ACD2B61ad3d25fa3422f29B0636C69c70f6588f", |
49 |
| -}; |
| 7 | +import { useSimulateTransactionBatcherBatchSend, useWriteTransactionBatcherBatchSend } from "./contracts/generated"; |
50 | 8 |
|
51 | 9 | export type TransactionBatcherConfig = SimulateContractParameters[];
|
52 | 10 |
|
| 11 | +/** |
| 12 | + * @param configs SimulateContractParameters[] - an array of useWriteContract Parameters |
| 13 | + * @description This takes in multiple write calls and batches them into a single transaction |
| 14 | + * @example useTransactionBatcher([ |
| 15 | + * { address : "contract one address", |
| 16 | + * abi : "contract one abi", |
| 17 | + * functionName : "...", |
| 18 | + * args: [...] |
| 19 | + * value: 0 |
| 20 | + * }, |
| 21 | + * { address : "contract 2 address", |
| 22 | + * abi : "contract 2 abi", |
| 23 | + * functionName : "...", |
| 24 | + * args: [...] |
| 25 | + * value: 0 |
| 26 | + * }, |
| 27 | + * ]) |
| 28 | + */ |
53 | 29 | const useTransactionBatcher = (configs?: TransactionBatcherConfig) => {
|
54 |
| - const { chainId } = useAccount(); |
55 |
| - |
56 |
| - const validConfigs = configs ?? []; |
| 30 | + const validatedConfigs = configs ?? []; |
57 | 31 | const {
|
58 | 32 | data: batchConfig,
|
59 | 33 | isLoading,
|
60 | 34 | isError,
|
61 |
| - } = useBatchSimulate({ |
| 35 | + } = useSimulateTransactionBatcherBatchSend({ |
62 | 36 | query: {
|
63 | 37 | enabled: !isUndefined(configs),
|
64 | 38 | },
|
65 |
| - address: batcherAddress[chainId ?? DEFAULT_CHAIN], |
66 | 39 | args: [
|
67 |
| - validConfigs.map((config) => config?.address), |
68 |
| - validConfigs.map((config) => config?.value ?? BigInt(0)), |
69 |
| - validConfigs.map((config) => encodeFunctionData(config)), |
| 40 | + validatedConfigs.map((config) => config?.address), |
| 41 | + validatedConfigs.map((config) => config?.value ?? BigInt(0)), |
| 42 | + validatedConfigs.map((config) => encodeFunctionData(config)), |
70 | 43 | ],
|
71 | 44 | });
|
72 |
| - const { writeContractAsync } = useWriteContract(); |
| 45 | + const { writeContractAsync } = useWriteTransactionBatcherBatchSend(); |
73 | 46 |
|
74 |
| - const executeBatch = () => batchConfig && writeContractAsync(batchConfig.request); |
| 47 | + const executeBatch = useCallback(() => writeContractAsync(batchConfig.request), [batchConfig, writeContractAsync]); |
75 | 48 | return { executeBatch, batchConfig, isError, isLoading };
|
76 | 49 | };
|
77 | 50 |
|
|
0 commit comments