Skip to content

Commit 20dc1a6

Browse files
authored
Merge pull request #92 from cometh-hq/develop
Develop
2 parents aabaed8 + 2020cfe commit 20dc1a6

File tree

12 files changed

+237
-202
lines changed

12 files changed

+237
-202
lines changed

bun.lockb

-832 Bytes
Binary file not shown.

examples/viem-demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@cometh/connect-sdk": "^1.2.31",
13-
"@cometh/connect-core-sdk": "workspace:*",
13+
"@cometh/connect-sdk-4337": "workspace:*",
1414
"@radix-ui/react-icons": "^1.3.0",
1515
"@radix-ui/react-slot": "^1.0.2",
1616
"@rhinestone/module-sdk": "^0.2.3",

examples/viem-demo/src/app/components/Transaction.tsx

Lines changed: 141 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -4,172 +4,168 @@ import { PlusIcon } from "@radix-ui/react-icons";
44
import type React from "react";
55
import { useEffect, useState } from "react";
66
import {
7-
http,
8-
createPublicClient,
9-
encodeFunctionData,
10-
getContract,
11-
parseEther,
7+
http,
8+
createPublicClient,
9+
encodeFunctionData,
10+
getContract,
11+
parseEther,
1212
} from "viem";
13-
import { arbitrumSepolia } from "viem/chains";
13+
import { gnosis } from "viem/chains";
1414
import countContractAbi from "../contract/counterABI.json";
1515
import { Icons } from "../lib/ui/components";
1616
import Alert from "../lib/ui/components/Alert";
1717

1818
export const COUNTER_CONTRACT_ADDRESS =
19-
"0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F";
19+
"0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F";
2020

2121
const publicClient = createPublicClient({
22-
chain: arbitrumSepolia,
23-
transport: http(),
24-
cacheTime: 60_000,
25-
batch: {
26-
multicall: { wait: 50 },
27-
},
22+
chain: gnosis,
23+
transport: http(),
24+
cacheTime: 60_000,
25+
batch: {
26+
multicall: { wait: 50 },
27+
},
2828
});
2929

3030
const counterContract = getContract({
31-
address: COUNTER_CONTRACT_ADDRESS,
32-
abi: countContractAbi,
33-
client: publicClient,
31+
address: COUNTER_CONTRACT_ADDRESS,
32+
abi: countContractAbi,
33+
client: publicClient,
3434
});
3535

3636
interface TransactionProps {
37-
smartAccount: any;
38-
transactionSuccess: boolean;
39-
setTransactionSuccess: React.Dispatch<React.SetStateAction<boolean>>;
37+
smartAccount: any;
38+
transactionSuccess: boolean;
39+
setTransactionSuccess: React.Dispatch<React.SetStateAction<boolean>>;
4040
}
4141

4242
function Transaction({
43-
smartAccount,
44-
transactionSuccess,
45-
setTransactionSuccess,
43+
smartAccount,
44+
transactionSuccess,
45+
setTransactionSuccess,
4646
}: TransactionProps) {
47-
const [isTransactionLoading, setIsTransactionLoading] =
48-
useState<boolean>(false);
49-
const [transactionSended, setTransactionSended] = useState<any | null>(
50-
null
47+
const [isTransactionLoading, setIsTransactionLoading] =
48+
useState<boolean>(false);
49+
const [transactionSended, setTransactionSended] = useState<any | null>(null);
50+
const [transactionFailure, setTransactionFailure] = useState(false);
51+
const [nftBalance, setNftBalance] = useState<number>(0);
52+
53+
function TransactionButton({
54+
sendTestTransaction,
55+
isTransactionLoading,
56+
label,
57+
}: {
58+
sendTestTransaction: () => Promise<void>;
59+
isTransactionLoading: boolean;
60+
label: string;
61+
}) {
62+
return (
63+
<button
64+
className="mt-1 flex h-11 py-2 px-4 gap-2 flex-none items-center justify-center rounded-lg bg-gray-100 hover:bg-gray-200"
65+
onClick={sendTestTransaction}
66+
>
67+
{isTransactionLoading ? (
68+
<Icons.spinner className="h-4 w-4 animate-spin" />
69+
) : (
70+
<>
71+
<PlusIcon width={16} height={16} />
72+
</>
73+
)}{" "}
74+
{label}
75+
</button>
5176
);
52-
const [transactionFailure, setTransactionFailure] = useState(false);
53-
const [nftBalance, setNftBalance] = useState<number>(0);
54-
55-
function TransactionButton({
56-
sendTestTransaction,
57-
isTransactionLoading,
58-
label,
59-
}: {
60-
sendTestTransaction: () => Promise<void>;
61-
isTransactionLoading: boolean;
62-
label: string;
63-
}) {
64-
return (
65-
<button
66-
className="mt-1 flex h-11 py-2 px-4 gap-2 flex-none items-center justify-center rounded-lg bg-gray-100 hover:bg-gray-200"
67-
onClick={sendTestTransaction}
68-
>
69-
{isTransactionLoading ? (
70-
<Icons.spinner className="h-4 w-4 animate-spin" />
71-
) : (
72-
<>
73-
<PlusIcon width={16} height={16} />
74-
</>
75-
)}{" "}
76-
{label}
77-
</button>
78-
);
77+
}
78+
79+
useEffect(() => {
80+
if (smartAccount) {
81+
(async () => {
82+
const balance = await counterContract.read.counters([
83+
smartAccount.account.address,
84+
]);
85+
setNftBalance(Number(balance));
86+
})();
87+
}
88+
}, []);
89+
90+
const sendTestTransaction = async (action: () => Promise<void>) => {
91+
setTransactionSended(null);
92+
setTransactionFailure(false);
93+
setTransactionSuccess(false);
94+
95+
setIsTransactionLoading(true);
96+
try {
97+
if (!smartAccount) throw new Error("No wallet instance");
98+
99+
const calldata = encodeFunctionData({
100+
abi: countContractAbi,
101+
functionName: "count",
102+
});
103+
104+
const txHash = await smartAccount.sendTransaction({
105+
to: COUNTER_CONTRACT_ADDRESS,
106+
data: calldata,
107+
});
108+
109+
const balance = await counterContract.read.counters([
110+
smartAccount.account.address,
111+
]);
112+
setNftBalance(Number(balance));
113+
setTransactionSended(txHash);
114+
115+
setTransactionSuccess(true);
116+
} catch (e) {
117+
console.log("Error:", e);
118+
setTransactionFailure(true);
79119
}
80120

81-
useEffect(() => {
82-
if (smartAccount) {
83-
(async () => {
84-
const balance = await counterContract.read.counters([
85-
smartAccount.account.address,
86-
]);
87-
setNftBalance(Number(balance));
88-
})();
89-
}
90-
}, []);
91-
92-
const sendTestTransaction = async (action: () => Promise<void>) => {
93-
setTransactionSended(null);
94-
setTransactionFailure(false);
95-
setTransactionSuccess(false);
96-
97-
setIsTransactionLoading(true);
98-
try {
99-
if (!smartAccount) throw new Error("No wallet instance");
100-
101-
const calldata = encodeFunctionData({
102-
abi: countContractAbi,
103-
functionName: "count",
104-
});
105-
106-
const txHash = await smartAccount.sendTransaction({
107-
to: COUNTER_CONTRACT_ADDRESS,
108-
data: calldata,
109-
});
110-
111-
const balance = await counterContract.read.counters([
112-
smartAccount.account.address,
113-
]);
114-
setNftBalance(Number(balance));
115-
setTransactionSended(txHash);
116-
117-
setTransactionSuccess(true);
118-
} catch (e) {
119-
console.log("Error:", e);
120-
setTransactionFailure(true);
121-
}
122-
123-
setIsTransactionLoading(false);
124-
};
125-
126-
return (
127-
<main>
128-
<div className="p-4">
129-
<div className="relative flex flex-col items-center gap-y-6 rounded-lg p-4">
130-
<TransactionButton
131-
sendTestTransaction={() =>
132-
sendTestTransaction(async () => {
133-
if (!smartAccount)
134-
throw new Error("No wallet instance");
135-
136-
const calldata = encodeFunctionData({
137-
abi: countContractAbi,
138-
functionName: "count",
139-
});
140-
141-
const txHash =
142-
await smartAccount.sendTransaction({
143-
to: COUNTER_CONTRACT_ADDRESS,
144-
data: calldata,
145-
});
146-
147-
setTransactionSended(txHash);
148-
})
149-
}
150-
isTransactionLoading={isTransactionLoading}
151-
label="Send tx"
152-
/>
153-
154-
<p className=" text-gray-600">{nftBalance}</p>
155-
</div>
156-
</div>
157-
158-
{transactionSuccess && (
159-
<Alert
160-
state="success"
161-
content="Transaction confirmed !"
162-
link={{
163-
content: "Go see your transaction",
164-
url: `https://jiffyscan.xyz/bundle/${transactionSended}?network=arbitrum-sepolia&pageNo=0&pageSize=10`,
165-
}}
166-
/>
167-
)}
168-
{transactionFailure && (
169-
<Alert state="error" content="Transaction Failed !" />
170-
)}
171-
</main>
172-
);
121+
setIsTransactionLoading(false);
122+
};
123+
124+
return (
125+
<main>
126+
<div className="p-4">
127+
<div className="relative flex flex-col items-center gap-y-6 rounded-lg p-4">
128+
<TransactionButton
129+
sendTestTransaction={() =>
130+
sendTestTransaction(async () => {
131+
if (!smartAccount) throw new Error("No wallet instance");
132+
133+
const calldata = encodeFunctionData({
134+
abi: countContractAbi,
135+
functionName: "count",
136+
});
137+
138+
const txHash = await smartAccount.sendTransaction({
139+
to: COUNTER_CONTRACT_ADDRESS,
140+
data: calldata,
141+
});
142+
143+
setTransactionSended(txHash);
144+
})
145+
}
146+
isTransactionLoading={isTransactionLoading}
147+
label="Send tx"
148+
/>
149+
150+
<p className=" text-gray-600">{nftBalance}</p>
151+
</div>
152+
</div>
153+
154+
{transactionSuccess && (
155+
<Alert
156+
state="success"
157+
content="Transaction confirmed !"
158+
link={{
159+
content: "Go see your transaction",
160+
url: `https://jiffyscan.xyz/bundle/${transactionSended}?network=arbitrum-sepolia&pageNo=0&pageSize=10`,
161+
}}
162+
/>
163+
)}
164+
{transactionFailure && (
165+
<Alert state="error" content="Transaction Failed !" />
166+
)}
167+
</main>
168+
);
173169
}
174170

175171
export default Transaction;

examples/viem-demo/src/app/modules/hooks/useSmartAccount.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { useState } from "react";
99
import { http, type Hex, type PublicClient, createPublicClient } from "viem";
1010
import { privateKeyToAccount } from "viem/accounts";
11-
import { arbitrumSepolia } from "viem/chains";
11+
import { gnosis } from "viem/chains";
1212

1313
export function useSmartAccount() {
1414
const [isConnecting, setIsConnecting] = useState(false);
@@ -39,7 +39,7 @@ export function useSmartAccount() {
3939
) as Hex;
4040

4141
const publicClient = createPublicClient({
42-
chain: arbitrumSepolia,
42+
chain: gnosis,
4343
transport: http(),
4444
cacheTime: 60_000,
4545
batch: {
@@ -51,21 +51,26 @@ export function useSmartAccount() {
5151
const owner = privateKeyToAccount(ownerPK as Hex); */
5252

5353
let smartAccount;
54+
const comethSignerConfig = {
55+
rpId: "metri.xyz",
56+
}
5457

5558
if (localStorageAddress) {
5659
smartAccount = await createSafeSmartAccount({
5760
apiKey,
58-
chain: arbitrumSepolia,
61+
chain: gnosis,
5962
publicClient,
6063
//signer: owner,
6164
smartAccountAddress: localStorageAddress,
65+
comethSignerConfig
6266
});
6367
} else {
6468
smartAccount = await createSafeSmartAccount({
6569
apiKey,
66-
chain: arbitrumSepolia,
70+
chain: gnosis,
6771
//signer: owner,
6872
publicClient,
73+
comethSignerConfig
6974
});
7075
window.localStorage.setItem(
7176
"walletAddress",
@@ -75,13 +80,13 @@ export function useSmartAccount() {
7580

7681
const paymasterClient = await createComethPaymasterClient({
7782
transport: http(paymasterUrl),
78-
chain: arbitrumSepolia,
83+
chain: gnosis,
7984
publicClient,
8085
});
8186

8287
const smartAccountClient = createSmartAccountClient({
8388
account: smartAccount,
84-
chain: arbitrumSepolia,
89+
chain: gnosis,
8590
bundlerTransport: http(bundlerUrl, {
8691
retryCount: 5,
8792
retryDelay: 1000,

0 commit comments

Comments
 (0)