@solana/react-hooks 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -138,6 +138,42 @@ function TokenPanel({
138
138
  }
139
139
  ```
140
140
 
141
+ ### Fetch address lookup tables
142
+
143
+ ```tsx
144
+ import { useLookupTable } from "@solana/react-hooks";
145
+
146
+ function LookupTableInfo({ address }: { address: string }) {
147
+ const { data, isLoading, error } = useLookupTable(address);
148
+ if (isLoading) return <p>Loading…</p>;
149
+ if (error) return <p role="alert">Error loading LUT</p>;
150
+ return (
151
+ <div>
152
+ <p>Addresses in LUT: {data?.addresses.length ?? 0}</p>
153
+ <p>Authority: {data?.authority ?? "None"}</p>
154
+ </div>
155
+ );
156
+ }
157
+ ```
158
+
159
+ ### Fetch nonce accounts
160
+
161
+ ```tsx
162
+ import { useNonceAccount } from "@solana/react-hooks";
163
+
164
+ function NonceInfo({ address }: { address: string }) {
165
+ const { data, isLoading, error } = useNonceAccount(address);
166
+ if (isLoading) return <p>Loading…</p>;
167
+ if (error) return <p role="alert">Error loading nonce</p>;
168
+ return (
169
+ <div>
170
+ <p>Nonce: {data?.blockhash}</p>
171
+ <p>Authority: {data?.authority}</p>
172
+ </div>
173
+ );
174
+ }
175
+ ```
176
+
141
177
  ### Build and send arbitrary transactions
142
178
 
143
179
  ```tsx
@@ -395,6 +395,87 @@ function useSolTransfer() {
395
395
  };
396
396
  }
397
397
  __name(useSolTransfer, "useSolTransfer");
398
+ function useStake(validatorId) {
399
+ const client$1 = useSolanaClient();
400
+ const session = useWalletSession();
401
+ const helper = client$1.stake;
402
+ const sessionRef = react.useRef(session);
403
+ const normalizedValidatorId = react.useMemo(() => String(validatorId), [validatorId]);
404
+ react.useEffect(() => {
405
+ sessionRef.current = session;
406
+ }, [session]);
407
+ const controller = react.useMemo(
408
+ () => client.createStakeController({
409
+ authorityProvider: /* @__PURE__ */ __name(() => sessionRef.current, "authorityProvider"),
410
+ helper
411
+ }),
412
+ [helper]
413
+ );
414
+ const state = react.useSyncExternalStore(
415
+ controller.subscribe,
416
+ controller.getState,
417
+ controller.getState
418
+ );
419
+ const unstakeState = react.useSyncExternalStore(
420
+ controller.subscribeUnstake,
421
+ controller.getUnstakeState,
422
+ controller.getUnstakeState
423
+ );
424
+ const withdrawState = react.useSyncExternalStore(
425
+ controller.subscribeWithdraw,
426
+ controller.getWithdrawState,
427
+ controller.getWithdrawState
428
+ );
429
+ const stake = react.useCallback(
430
+ (config, options) => controller.stake({ ...config, validatorId: normalizedValidatorId }, options),
431
+ [controller, normalizedValidatorId]
432
+ );
433
+ const unstake = react.useCallback(
434
+ (config, options) => controller.unstake({ ...config }, options),
435
+ [controller]
436
+ );
437
+ const withdraw = react.useCallback(
438
+ (config, options) => controller.withdraw({ ...config }, options),
439
+ [controller]
440
+ );
441
+ const getStakeAccounts = react.useCallback(
442
+ async (wallet, validatorIdFilter) => {
443
+ if (!helper.getStakeAccounts) {
444
+ throw new Error(
445
+ "getStakeAccounts is not available. Make sure you have the latest version of @solana/client package."
446
+ );
447
+ }
448
+ const walletAddr = typeof wallet === "string" ? wallet : String(wallet);
449
+ const filterAddr = validatorIdFilter ? typeof validatorIdFilter === "string" ? validatorIdFilter : String(validatorIdFilter) : void 0;
450
+ return helper.getStakeAccounts(walletAddr, filterAddr);
451
+ },
452
+ [helper]
453
+ );
454
+ return {
455
+ error: state.error ?? null,
456
+ getStakeAccounts,
457
+ helper,
458
+ isStaking: state.status === "loading",
459
+ isUnstaking: unstakeState.status === "loading",
460
+ isWithdrawing: withdrawState.status === "loading",
461
+ reset: controller.reset,
462
+ resetUnstake: controller.resetUnstake,
463
+ resetWithdraw: controller.resetWithdraw,
464
+ stake,
465
+ unstake,
466
+ withdraw,
467
+ signature: state.data ?? null,
468
+ unstakeSignature: unstakeState.data ?? null,
469
+ withdrawSignature: withdrawState.data ?? null,
470
+ status: state.status,
471
+ unstakeStatus: unstakeState.status,
472
+ withdrawStatus: withdrawState.status,
473
+ unstakeError: unstakeState.error ?? null,
474
+ withdrawError: withdrawState.error ?? null,
475
+ validatorId: normalizedValidatorId
476
+ };
477
+ }
478
+ __name(useStake, "useStake");
398
479
  function useSplToken(mint, options = {}) {
399
480
  const client$1 = useSolanaClient();
400
481
  const session = useWalletSession();
@@ -838,6 +919,38 @@ function useWaitForSignature(signatureInput, options = {}) {
838
919
  };
839
920
  }
840
921
  __name(useWaitForSignature, "useWaitForSignature");
922
+ function useLookupTable(addressLike, options = {}) {
923
+ const addr = react.useMemo(() => addressLike ? client.toAddress(addressLike) : void 0, [addressLike]);
924
+ const key = addr?.toString() ?? null;
925
+ const fetcher = react.useCallback(
926
+ async (c) => {
927
+ if (!addr) throw new Error("Address required");
928
+ return c.actions.fetchLookupTable(addr, options.commitment);
929
+ },
930
+ [addr, options.commitment]
931
+ );
932
+ return useSolanaRpcQuery("lookupTable", [key, options.commitment], fetcher, {
933
+ disabled: !addr,
934
+ swr: options.swr
935
+ });
936
+ }
937
+ __name(useLookupTable, "useLookupTable");
938
+ function useNonceAccount(addressLike, options = {}) {
939
+ const addr = react.useMemo(() => addressLike ? client.toAddress(addressLike) : void 0, [addressLike]);
940
+ const key = addr?.toString() ?? null;
941
+ const fetcher = react.useCallback(
942
+ async (c) => {
943
+ if (!addr) throw new Error("Address required");
944
+ return c.actions.fetchNonceAccount(addr, options.commitment);
945
+ },
946
+ [addr, options.commitment]
947
+ );
948
+ return useSolanaRpcQuery("nonceAccount", [key, options.commitment], fetcher, {
949
+ disabled: !addr,
950
+ swr: options.swr
951
+ });
952
+ }
953
+ __name(useNonceAccount, "useNonceAccount");
841
954
  var createCache = /* @__PURE__ */ __name(() => /* @__PURE__ */ new Map(), "createCache");
842
955
  var DEFAULT_QUERY_CONFIG = Object.freeze({
843
956
  dedupingInterval: 2e3,
@@ -994,7 +1107,7 @@ function WalletPersistence({
994
1107
  if (!connector) return;
995
1108
  void (async () => {
996
1109
  try {
997
- await connectWallet(connectorId, { autoConnect: true });
1110
+ await connectWallet(connectorId, { autoConnect: true, allowInteractiveFallback: false });
998
1111
  } catch {
999
1112
  } finally {
1000
1113
  setHasAttemptedAutoConnect(true);
@@ -1119,6 +1232,8 @@ exports.useClusterStatus = useClusterStatus;
1119
1232
  exports.useConnectWallet = useConnectWallet;
1120
1233
  exports.useDisconnectWallet = useDisconnectWallet;
1121
1234
  exports.useLatestBlockhash = useLatestBlockhash;
1235
+ exports.useLookupTable = useLookupTable;
1236
+ exports.useNonceAccount = useNonceAccount;
1122
1237
  exports.useProgramAccounts = useProgramAccounts;
1123
1238
  exports.useSendTransaction = useSendTransaction;
1124
1239
  exports.useSignatureStatus = useSignatureStatus;
@@ -1126,6 +1241,7 @@ exports.useSimulateTransaction = useSimulateTransaction;
1126
1241
  exports.useSolTransfer = useSolTransfer;
1127
1242
  exports.useSolanaClient = useSolanaClient;
1128
1243
  exports.useSplToken = useSplToken;
1244
+ exports.useStake = useStake;
1129
1245
  exports.useTransactionPool = useTransactionPool;
1130
1246
  exports.useWaitForSignature = useWaitForSignature;
1131
1247
  exports.useWallet = useWallet;