|
1 | 1 | "use client"; |
2 | 2 |
|
3 | 3 | import { |
4 | | - ENTRYPOINT_ADDRESS_V07, |
5 | | - createComethPaymasterClient, |
6 | | - createSafeSmartAccount, |
7 | | - createSmartAccountClient, |
8 | | - importSafeActions, |
| 4 | + createComethPaymasterClient, |
| 5 | + createSafeSmartAccount, |
| 6 | + createSmartAccountClient, |
| 7 | + importSafeActions, |
9 | 8 | } from "@cometh/connect-sdk-4337"; |
10 | 9 | import { http, type Hex, encodeFunctionData } from "viem"; |
11 | 10 | import { privateKeyToAccount } from "viem/accounts"; |
12 | 11 | import { gnosis } from "viem/chains"; |
13 | 12 | import countContractAbi from "../contract/counterABI.json"; |
14 | 13 |
|
15 | 14 | export const COUNTER_CONTRACT_ADDRESS = |
16 | | - "0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F"; |
| 15 | + "0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F"; |
17 | 16 |
|
18 | 17 | export default function App() { |
19 | | - const apiKey = process.env.NEXT_PUBLIC_COMETH_API_KEY!; |
20 | | - const bundlerUrl = process.env.NEXT_PUBLIC_4337_BUNDLER_URL!; |
21 | | - const paymasterUrl = process.env.NEXT_PUBLIC_4337_PAYMASTER_URL!; |
22 | | - const walletToImport = "0x"; |
23 | | - |
24 | | - const migrateSafe = async () => { |
25 | | - const legacyWalletAddress = walletToImport; |
26 | | - |
27 | | - const signer = privateKeyToAccount( |
28 | | - process.env.NEXT_PUBLIC_PRIVATE_KEY! as Hex |
29 | | - ); |
30 | | - |
31 | | - const safe4337SmartAccount = await createSafeSmartAccount({ |
32 | | - apiKey, |
33 | | - chain: gnosis, |
34 | | - smartAccountAddress: legacyWalletAddress, |
35 | | - entryPoint: ENTRYPOINT_ADDRESS_V07, |
36 | | - signer, |
37 | | - }); |
38 | | - |
39 | | - const paymasterClient = await createComethPaymasterClient({ |
40 | | - transport: http(paymasterUrl), |
41 | | - chain: gnosis, |
42 | | - entryPoint: ENTRYPOINT_ADDRESS_V07, |
43 | | - }); |
44 | | - const smartAccountClient = createSmartAccountClient({ |
45 | | - account: safe4337SmartAccount, |
46 | | - entryPoint: ENTRYPOINT_ADDRESS_V07, |
47 | | - chain: gnosis, |
48 | | - bundlerTransport: http(bundlerUrl, { |
49 | | - retryCount: 10, |
50 | | - retryDelay: 200, |
51 | | - }), |
52 | | - middleware: { |
53 | | - sponsorUserOperation: paymasterClient.sponsorUserOperation, |
54 | | - gasPrice: paymasterClient.gasPrice, |
55 | | - }, |
56 | | - }); |
57 | | - |
58 | | - const extendedClient = smartAccountClient.extend(importSafeActions()); |
59 | | - |
60 | | - const importMessage = await extendedClient.prepareImportSafe1_4Tx(); |
61 | | - |
62 | | - const signature = (await extendedClient.signTransactionByExternalOwner({ |
63 | | - signer, |
64 | | - tx: importMessage.tx, |
65 | | - })) as Hex; |
66 | | - |
67 | | - await extendedClient.importSafe({ |
68 | | - signature, |
69 | | - ...importMessage, |
70 | | - }); |
71 | | - |
72 | | - const safe4337SmartAccountImported = await createSafeSmartAccount({ |
73 | | - apiKey, |
74 | | - chain: gnosis, |
75 | | - smartAccountAddress: legacyWalletAddress, |
76 | | - entryPoint: ENTRYPOINT_ADDRESS_V07, |
77 | | - }); |
78 | | - |
79 | | - const smartAccountClientImported = createSmartAccountClient({ |
80 | | - account: safe4337SmartAccountImported, |
81 | | - entryPoint: ENTRYPOINT_ADDRESS_V07, |
82 | | - chain: gnosis, |
83 | | - bundlerTransport: http(bundlerUrl, { |
84 | | - retryCount: 10, |
85 | | - retryDelay: 200, |
86 | | - }), |
87 | | - middleware: { |
88 | | - sponsorUserOperation: paymasterClient.sponsorUserOperation, |
89 | | - gasPrice: paymasterClient.gasPrice, |
90 | | - }, |
91 | | - }); |
92 | | - |
93 | | - const calldata = encodeFunctionData({ |
94 | | - abi: countContractAbi, |
95 | | - functionName: "count", |
96 | | - }); |
97 | | - |
98 | | - const txHash = await smartAccountClientImported.sendTransaction({ |
99 | | - to: COUNTER_CONTRACT_ADDRESS, |
100 | | - data: calldata, |
101 | | - }); |
102 | | - |
103 | | - console.log("Transaction hash:", txHash); |
104 | | - }; |
105 | | - |
106 | | - return ( |
107 | | - <div |
108 | | - style={{ |
109 | | - height: "100vh", |
110 | | - display: "flex", |
111 | | - flexDirection: "column", |
112 | | - justifyContent: "center", |
113 | | - alignItems: "center", |
114 | | - }} |
115 | | - > |
116 | | - <div className="md:min-h-[70vh] gap-2 flex flex-col justify-center items-center"> |
117 | | - <div className="absolute left-1/2 z-10 mt-5 flex w-screen max-w-max -translate-x-1/2 px-4"> |
118 | | - <div className="w-screen max-w-md flex-auto overflow-hidden rounded-3xl bg-white text-sm leading-6 shadow-lg ring-1 ring-gray-900/5"> |
119 | | - <div className="grid divide-gray-900/5 bg-gray-50"> |
120 | | - <button onClick={migrateSafe}>Migrate Safe</button> |
121 | | - </div> |
122 | | - </div> |
123 | | - </div> |
| 18 | + const apiKey = process.env.NEXT_PUBLIC_COMETH_API_KEY!; |
| 19 | + const bundlerUrl = process.env.NEXT_PUBLIC_4337_BUNDLER_URL!; |
| 20 | + const paymasterUrl = process.env.NEXT_PUBLIC_4337_PAYMASTER_URL!; |
| 21 | + const walletToImport = "0x"; |
| 22 | + |
| 23 | + const migrateSafe = async () => { |
| 24 | + const legacyWalletAddress = walletToImport; |
| 25 | + |
| 26 | + const signer = privateKeyToAccount( |
| 27 | + process.env.NEXT_PUBLIC_PRIVATE_KEY! as Hex |
| 28 | + ); |
| 29 | + |
| 30 | + const safe4337SmartAccount = await createSafeSmartAccount({ |
| 31 | + apiKey, |
| 32 | + chain: gnosis, |
| 33 | + smartAccountAddress: legacyWalletAddress, |
| 34 | + signer, |
| 35 | + }); |
| 36 | + |
| 37 | + const paymasterClient = await createComethPaymasterClient({ |
| 38 | + transport: http(paymasterUrl), |
| 39 | + chain: gnosis, |
| 40 | + }); |
| 41 | + const smartAccountClient = createSmartAccountClient({ |
| 42 | + account: safe4337SmartAccount, |
| 43 | + chain: gnosis, |
| 44 | + bundlerTransport: http(bundlerUrl, { |
| 45 | + retryCount: 10, |
| 46 | + retryDelay: 200, |
| 47 | + }), |
| 48 | + paymaster: paymasterClient, |
| 49 | + userOperation: { |
| 50 | + estimateFeesPerGas: async () => { |
| 51 | + return await paymasterClient.getUserOperationGasPrice(); |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const extendedClient = smartAccountClient.extend(importSafeActions()); |
| 57 | + |
| 58 | + const importMessage = await extendedClient.prepareImportSafe1_4Tx(); |
| 59 | + |
| 60 | + const signature = (await extendedClient.signTransactionByExternalOwner({ |
| 61 | + signer, |
| 62 | + tx: importMessage.tx, |
| 63 | + })) as Hex; |
| 64 | + |
| 65 | + await extendedClient.importSafe({ |
| 66 | + signature, |
| 67 | + ...importMessage, |
| 68 | + }); |
| 69 | + |
| 70 | + const safe4337SmartAccountImported = await createSafeSmartAccount({ |
| 71 | + apiKey, |
| 72 | + chain: gnosis, |
| 73 | + smartAccountAddress: legacyWalletAddress, |
| 74 | + }); |
| 75 | + |
| 76 | + const smartAccountClientImported = createSmartAccountClient({ |
| 77 | + account: safe4337SmartAccountImported, |
| 78 | + chain: gnosis, |
| 79 | + bundlerTransport: http(bundlerUrl, { |
| 80 | + retryCount: 10, |
| 81 | + retryDelay: 200, |
| 82 | + }), |
| 83 | + paymaster: paymasterClient, |
| 84 | + userOperation: { |
| 85 | + estimateFeesPerGas: async () => { |
| 86 | + return await paymasterClient.getUserOperationGasPrice(); |
| 87 | + }, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + const calldata = encodeFunctionData({ |
| 92 | + abi: countContractAbi, |
| 93 | + functionName: "count", |
| 94 | + }); |
| 95 | + |
| 96 | + const txHash = await smartAccountClientImported.sendTransaction({ |
| 97 | + to: COUNTER_CONTRACT_ADDRESS, |
| 98 | + data: calldata, |
| 99 | + }); |
| 100 | + |
| 101 | + console.log("Transaction hash:", txHash); |
| 102 | + }; |
| 103 | + |
| 104 | + return ( |
| 105 | + <div |
| 106 | + style={{ |
| 107 | + height: "100vh", |
| 108 | + display: "flex", |
| 109 | + flexDirection: "column", |
| 110 | + justifyContent: "center", |
| 111 | + alignItems: "center", |
| 112 | + }} |
| 113 | + > |
| 114 | + <div className="md:min-h-[70vh] gap-2 flex flex-col justify-center items-center"> |
| 115 | + <div className="absolute left-1/2 z-10 mt-5 flex w-screen max-w-max -translate-x-1/2 px-4"> |
| 116 | + <div className="w-screen max-w-md flex-auto overflow-hidden rounded-3xl bg-white text-sm leading-6 shadow-lg ring-1 ring-gray-900/5"> |
| 117 | + <div className="grid divide-gray-900/5 bg-gray-50"> |
| 118 | + <button onClick={migrateSafe}>Migrate Safe</button> |
124 | 119 | </div> |
| 120 | + </div> |
125 | 121 | </div> |
126 | | - ); |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + ); |
127 | 125 | } |
0 commit comments