@@ -4,172 +4,168 @@ import { PlusIcon } from "@radix-ui/react-icons";
44import type React from "react" ;
55import { useEffect , useState } from "react" ;
66import {
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" ;
1414import countContractAbi from "../contract/counterABI.json" ;
1515import { Icons } from "../lib/ui/components" ;
1616import Alert from "../lib/ui/components/Alert" ;
1717
1818export const COUNTER_CONTRACT_ADDRESS =
19- "0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F" ;
19+ "0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F" ;
2020
2121const 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
3030const 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
3636interface 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
4242function 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
175171export default Transaction ;
0 commit comments