|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import type { Idl } from "@coral-xyz/anchor"; |
| 4 | +import { Program, AnchorProvider } from "@coral-xyz/anchor"; |
| 5 | +import { WalletIcon } from "@heroicons/react/24/outline"; |
| 6 | +import { |
| 7 | + TransactionBuilder, |
| 8 | + sendTransactions, |
| 9 | +} from "@pythnetwork/solana-utils"; |
| 10 | +import type { AnchorWallet } from "@solana/wallet-adapter-react"; |
| 11 | +import { useConnection } from "@solana/wallet-adapter-react"; |
| 12 | +import { useWalletModal } from "@solana/wallet-adapter-react-ui"; |
| 13 | +import { PublicKey, Connection } from "@solana/web3.js"; |
| 14 | +import type { ComponentProps } from "react"; |
| 15 | +import { useCallback, useState } from "react"; |
| 16 | + |
| 17 | +import WalletTesterIDL from "./wallet-tester-idl.json"; |
| 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"; |
| 21 | +import { useToast } from "../../hooks/use-toast"; |
| 22 | +import { Button } from "../Button"; |
| 23 | + |
| 24 | +export const WalletTester = () => ( |
| 25 | + <div className="grid size-full place-content-center"> |
| 26 | + <div className="w-96 border border-neutral-600 p-10"> |
| 27 | + <h1 className="mb-4 text-2xl font-medium text-neutral-300"> |
| 28 | + Wallet Tester |
| 29 | + </h1> |
| 30 | + <WalletTesterContents /> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | +); |
| 34 | + |
| 35 | +const WalletTesterContents = () => { |
| 36 | + const api = useApi(); |
| 37 | + |
| 38 | + switch (api.type) { |
| 39 | + case ApiStateType.WalletConnecting: |
| 40 | + case ApiStateType.WalletDisconnecting: { |
| 41 | + return <ConnectWallet isLoading />; |
| 42 | + } |
| 43 | + |
| 44 | + case ApiStateType.NoWallet: { |
| 45 | + return <ConnectWallet />; |
| 46 | + } |
| 47 | + |
| 48 | + case ApiStateType.NotLoaded: |
| 49 | + case ApiStateType.ErrorLoadingStakeAccounts: |
| 50 | + case ApiStateType.Loaded: |
| 51 | + case ApiStateType.LoadedNoStakeAccount: |
| 52 | + case ApiStateType.LoadingStakeAccounts: { |
| 53 | + return <WalletConnected wallet={api.wallet} />; |
| 54 | + } |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +const ConnectWallet = ({ isLoading }: { isLoading?: boolean | undefined }) => { |
| 59 | + const modal = useWalletModal(); |
| 60 | + const showModal = useCallback(() => { |
| 61 | + modal.setVisible(true); |
| 62 | + }, [modal]); |
| 63 | + |
| 64 | + return ( |
| 65 | + <> |
| 66 | + <Description className="mb-10 text-neutral-400"> |
| 67 | + Please connect your wallet to get started. |
| 68 | + </Description> |
| 69 | + <div className="flex justify-center"> |
| 70 | + <Button |
| 71 | + className="px-10 py-4" |
| 72 | + size="nopad" |
| 73 | + isLoading={isLoading} |
| 74 | + {...(!isLoading && { onPress: showModal })} |
| 75 | + > |
| 76 | + {isLoading ? ( |
| 77 | + "Loading..." |
| 78 | + ) : ( |
| 79 | + <> |
| 80 | + <WalletIcon className="size-4" /> |
| 81 | + <div>Connect wallet</div> |
| 82 | + </> |
| 83 | + )} |
| 84 | + </Button> |
| 85 | + </div> |
| 86 | + </> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +const WalletConnected = ({ wallet }: { wallet: AnchorWallet }) => { |
| 91 | + const { connection } = useConnection(); |
| 92 | + |
| 93 | + const testedStatus = useData( |
| 94 | + ["wallet-tested", wallet.publicKey.toString()], |
| 95 | + () => getHasAlreadyTested(connection, wallet), |
| 96 | + { |
| 97 | + revalidateIfStale: false, |
| 98 | + revalidateOnFocus: false, |
| 99 | + revalidateOnReconnect: false, |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + switch (testedStatus.type) { |
| 104 | + case StateType.NotLoaded: |
| 105 | + case StateType.Loading: { |
| 106 | + return <Description>Loading...</Description>; |
| 107 | + } |
| 108 | + case StateType.Error: { |
| 109 | + return ( |
| 110 | + <Description> |
| 111 | + Uh oh, we ran into an issue while checking if your wallet has been |
| 112 | + tested. Please reload and try again. |
| 113 | + </Description> |
| 114 | + ); |
| 115 | + } |
| 116 | + case StateType.Loaded: { |
| 117 | + return testedStatus.data.hasTested ? ( |
| 118 | + <p className="text-green-600"> |
| 119 | + Your wallet has already been tested succesfully! |
| 120 | + </p> |
| 121 | + ) : ( |
| 122 | + <Tester wallet={wallet} /> |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +const Tester = ({ wallet }: { wallet: AnchorWallet }) => { |
| 129 | + const logger = useLogger(); |
| 130 | + const toast = useToast(); |
| 131 | + const [tested, setTested] = useState(false); |
| 132 | + const { connection } = useConnection(); |
| 133 | + const test = useCallback(() => { |
| 134 | + testWallet(connection, wallet) |
| 135 | + .then(() => { |
| 136 | + setTested(true); |
| 137 | + toast.success("Successfully tested wallet, thank you!"); |
| 138 | + }) |
| 139 | + .catch((error: unknown) => { |
| 140 | + logger.error(error); |
| 141 | + toast.error(error); |
| 142 | + }); |
| 143 | + }, [setTested, logger, toast, wallet, connection]); |
| 144 | + |
| 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 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +const getHasAlreadyTested = async ( |
| 163 | + connection: Connection, |
| 164 | + wallet: AnchorWallet, |
| 165 | +) => { |
| 166 | + const receiptAddress = PublicKey.findProgramAddressSync( |
| 167 | + [wallet.publicKey.toBytes()], |
| 168 | + new PublicKey(WalletTesterIDL.address), |
| 169 | + )[0]; |
| 170 | + const receipt = await connection.getAccountInfo(receiptAddress); |
| 171 | + return { hasTested: receipt !== null }; |
| 172 | +}; |
| 173 | + |
| 174 | +const testWallet = async (connection: Connection, wallet: AnchorWallet) => { |
| 175 | + const walletTester = new Program( |
| 176 | + WalletTesterIDL as Idl, |
| 177 | + new AnchorProvider(connection, wallet), |
| 178 | + ); |
| 179 | + const testMethod = walletTester.methods.test; |
| 180 | + if (testMethod) { |
| 181 | + return sendTransactions( |
| 182 | + await TransactionBuilder.batchIntoVersionedTransactions( |
| 183 | + wallet.publicKey, |
| 184 | + connection, |
| 185 | + [ |
| 186 | + { |
| 187 | + instruction: await testMethod().instruction(), |
| 188 | + signers: [], |
| 189 | + }, |
| 190 | + ], |
| 191 | + {}, |
| 192 | + ), |
| 193 | + connection, |
| 194 | + wallet, |
| 195 | + ); |
| 196 | + } else { |
| 197 | + throw new Error("No test method found in program"); |
| 198 | + } |
| 199 | +}; |
| 200 | + |
| 201 | +const Description = (props: ComponentProps<"p">) => ( |
| 202 | + <p className="mb-10 text-neutral-400" {...props} /> |
| 203 | +); |
0 commit comments