@@ -12,15 +12,20 @@ import { useConnection } from "@solana/wallet-adapter-react";
12
12
import { useWalletModal } from "@solana/wallet-adapter-react-ui" ;
13
13
import { PublicKey , Connection } from "@solana/web3.js" ;
14
14
import type { ComponentProps } from "react" ;
15
- import { useCallback , useState } from "react" ;
15
+ import { useCallback } from "react" ;
16
16
17
17
import WalletTesterIDL from "./wallet-tester-idl.json" ;
18
18
import { StateType as ApiStateType , useApi } from "../../hooks/use-api" ;
19
- import { useData , StateType } from "../../hooks/use-data" ;
20
- import { useLogger } from "../../hooks/use-logger" ;
19
+ import {
20
+ useAsync ,
21
+ StateType as UseAsyncStateType ,
22
+ } from "../../hooks/use-async" ;
23
+ import { useData , StateType as UseDataStateType } from "../../hooks/use-data" ;
21
24
import { useToast } from "../../hooks/use-toast" ;
22
25
import { Button } from "../Button" ;
23
26
27
+ const MAX_TEST_RETRIES = 10 ;
28
+
24
29
export const WalletTester = ( ) => (
25
30
< div className = "grid size-full place-content-center" >
26
31
< div className = "w-96 border border-neutral-600 p-10" >
@@ -101,19 +106,19 @@ const WalletConnected = ({ wallet }: { wallet: AnchorWallet }) => {
101
106
) ;
102
107
103
108
switch ( testedStatus . type ) {
104
- case StateType . NotLoaded :
105
- case StateType . Loading : {
109
+ case UseDataStateType . NotLoaded :
110
+ case UseDataStateType . Loading : {
106
111
return < Description > Loading...</ Description > ;
107
112
}
108
- case StateType . Error : {
113
+ case UseDataStateType . Error : {
109
114
return (
110
115
< Description >
111
116
Uh oh, we ran into an issue while checking if your wallet has been
112
117
tested. Please reload and try again.
113
118
</ Description >
114
119
) ;
115
120
}
116
- case StateType . Loaded : {
121
+ case UseDataStateType . Loaded : {
117
122
return testedStatus . data . hasTested ? (
118
123
< p className = "text-green-600" >
119
124
Your wallet has already been tested succesfully!
@@ -126,37 +131,57 @@ const WalletConnected = ({ wallet }: { wallet: AnchorWallet }) => {
126
131
} ;
127
132
128
133
const Tester = ( { wallet } : { wallet : AnchorWallet } ) => {
129
- const logger = useLogger ( ) ;
130
134
const toast = useToast ( ) ;
131
- const [ tested , setTested ] = useState ( false ) ;
132
135
const { connection } = useConnection ( ) ;
133
- const test = useCallback ( ( ) => {
134
- testWallet ( connection , wallet )
136
+ const { state, execute } = useAsync ( ( ) => testWallet ( connection , wallet ) ) ;
137
+ const doTest = useCallback ( ( ) => {
138
+ execute ( )
135
139
. then ( ( ) => {
136
- setTested ( true ) ;
137
140
toast . success ( "Successfully tested wallet, thank you!" ) ;
138
141
} )
139
142
. catch ( ( error : unknown ) => {
140
- logger . error ( error ) ;
141
143
toast . error ( error ) ;
142
144
} ) ;
143
- } , [ setTested , logger , toast , wallet , connection ] ) ;
145
+ } , [ execute , toast ] ) ;
144
146
145
- return tested ? (
146
- < p className = "text-green-600" > Your wallet has been tested succesfully!</ p >
147
- ) : (
148
- < >
149
- < Description >
150
- Please click the button below and accept the transaction in your wallet
151
- to test the browser wallet compatibility. You will need 0.001 SOL.
152
- </ Description >
153
- < div className = "flex justify-center" >
154
- < Button className = "px-10 py-4" size = "nopad" onPress = { test } >
155
- Click to test
156
- </ Button >
157
- </ div >
158
- </ >
159
- ) ;
147
+ switch ( state . type ) {
148
+ case UseAsyncStateType . Base :
149
+ case UseAsyncStateType . Error :
150
+ case UseAsyncStateType . Running : {
151
+ return (
152
+ < >
153
+ < Description >
154
+ Please click the button below and accept the transaction in your
155
+ wallet to test the browser wallet compatibility. You will need 0.001
156
+ SOL.
157
+ </ Description >
158
+ < div className = "flex justify-center" >
159
+ < Button
160
+ className = "px-10 py-4"
161
+ size = "nopad"
162
+ { ...( state . type === UseAsyncStateType . Running
163
+ ? { isLoading : true }
164
+ : { onPress : doTest } ) }
165
+ >
166
+ Click to test
167
+ </ Button >
168
+ </ div >
169
+ { state . type === UseAsyncStateType . Error && (
170
+ < p className = "mt-4 text-red-600" >
171
+ Uh oh, an error occurred! Please try again
172
+ </ p >
173
+ ) }
174
+ </ >
175
+ ) ;
176
+ }
177
+ case UseAsyncStateType . Complete : {
178
+ return (
179
+ < p className = "text-green-600" >
180
+ Your wallet has been tested succesfully!
181
+ </ p >
182
+ ) ;
183
+ }
184
+ }
160
185
} ;
161
186
162
187
const getHasAlreadyTested = async (
@@ -178,7 +203,7 @@ const testWallet = async (connection: Connection, wallet: AnchorWallet) => {
178
203
) ;
179
204
const testMethod = walletTester . methods . test ;
180
205
if ( testMethod ) {
181
- return sendTransactions (
206
+ await sendTransactions (
182
207
await TransactionBuilder . batchIntoVersionedTransactions (
183
208
wallet . publicKey ,
184
209
connection ,
@@ -192,6 +217,7 @@ const testWallet = async (connection: Connection, wallet: AnchorWallet) => {
192
217
) ,
193
218
connection ,
194
219
wallet ,
220
+ MAX_TEST_RETRIES ,
195
221
) ;
196
222
} else {
197
223
throw new Error ( "No test method found in program" ) ;
0 commit comments