index.tsx 6.6 KB

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