@gelatonetwork/smartwallet-react-privy 0.0.21 → 0.0.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gelatonetwork/smartwallet-react-privy",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "author": "Gelato",
5
5
  "type": "module",
6
6
  "description": "Context provider supporting Privy WaaS",
@@ -11,8 +11,8 @@
11
11
  "typescript": "^5.3",
12
12
  "viem": "~2.29",
13
13
  "wagmi": "~2.15",
14
- "@gelatonetwork/smartwallet": "0.0.21",
15
- "@gelatonetwork/smartwallet-react-types": "0.0.21"
14
+ "@gelatonetwork/smartwallet": "0.0.22",
15
+ "@gelatonetwork/smartwallet-react-types": "0.0.22"
16
16
  },
17
17
  "peerDependenciesMeta": {
18
18
  "typescript": {
@@ -23,8 +23,8 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "@privy-io/react-auth": "2.13.0",
27
- "@privy-io/wagmi": "1.0.3",
26
+ "@privy-io/react-auth": "2.21.2",
27
+ "@privy-io/wagmi": "1.0.6",
28
28
  "@tanstack/react-query": "~5.74",
29
29
  "caip": "1.1.1",
30
30
  "react": "^18.3",
@@ -32,8 +32,8 @@
32
32
  "viem": "~2.29",
33
33
  "wagmi": "~2.15",
34
34
  "zod": "3.24.4",
35
- "@gelatonetwork/smartwallet": "0.0.21",
36
- "@gelatonetwork/smartwallet-react-types": "0.0.21"
35
+ "@gelatonetwork/smartwallet": "0.0.22",
36
+ "@gelatonetwork/smartwallet-react-types": "0.0.22"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/react": "^18.3",
package/src/provider.tsx CHANGED
@@ -12,7 +12,15 @@ import { WagmiProvider, createConfig } from "@privy-io/wagmi";
12
12
  import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
13
13
  import { ChainId } from "caip";
14
14
  import type { FC, ReactNode } from "react";
15
- import { createContext, useContext, useEffect, useState } from "react";
15
+ import {
16
+ createContext,
17
+ useCallback,
18
+ useContext,
19
+ useEffect,
20
+ useMemo,
21
+ useRef,
22
+ useState
23
+ } from "react";
16
24
  import {
17
25
  type Account,
18
26
  type Chain,
@@ -21,7 +29,12 @@ import {
21
29
  createWalletClient,
22
30
  custom
23
31
  } from "viem";
24
- import { prepareAuthorization } from "viem/actions";
32
+ import type { SmartAccount } from "viem/account-abstraction";
33
+ import {
34
+ type PrepareAuthorizationParameters,
35
+ SignAuthorizationReturnType,
36
+ prepareAuthorization
37
+ } from "viem/actions";
25
38
  import * as chains from "viem/chains";
26
39
  import { extractChain } from "viem/utils";
27
40
  import type { Config as WagmiConfig } from "wagmi";
@@ -59,27 +72,37 @@ const GelatoSmartWalletPrivyInternal: FC<{
59
72
  Chain,
60
73
  GelatoSmartAccount
61
74
  > | null>(null);
75
+ const currentChainIdRef = useRef<string | null>(null);
62
76
 
63
- const logoutWrapper = async () => {
77
+ const logoutWrapper = useCallback(async () => {
64
78
  if (!smartWalletClient) {
65
79
  return;
66
80
  }
67
81
 
68
82
  setSmartWalletClient(null);
69
83
  await logout();
70
- };
84
+ }, [smartWalletClient, logout]);
71
85
 
72
- const switchNetwork = async (chainId: number) => {
73
- if (!smartWalletClient) {
74
- return;
75
- }
86
+ const switchNetwork = useCallback(
87
+ async (chainId: number) => {
88
+ if (!smartWalletClient || !wallets || wallets.length === 0) {
89
+ return;
90
+ }
76
91
 
77
- const primaryWallet = wallets[0];
92
+ const primaryWallet = wallets[0];
78
93
 
79
- await primaryWallet.switchChain(chainId);
80
- smartWalletClient.switchChain({ id: chainId });
81
- };
94
+ try {
95
+ await primaryWallet.switchChain(chainId);
96
+ // The useEffect will handle recreating the client with the new chain
97
+ // We don't need to manually call smartWalletClient.switchChain
98
+ } catch (error) {
99
+ console.error("Failed to switch network:", error);
100
+ }
101
+ },
102
+ [smartWalletClient, wallets]
103
+ );
82
104
 
105
+ // biome-ignore lint/correctness/useExhaustiveDependencies: off
83
106
  useEffect(() => {
84
107
  if (!ready || !walletsReady) {
85
108
  return;
@@ -87,12 +110,19 @@ const GelatoSmartWalletPrivyInternal: FC<{
87
110
 
88
111
  if (!authenticated || !wallets || wallets.length === 0) {
89
112
  setSmartWalletClient(null);
113
+ currentChainIdRef.current = null;
90
114
  return;
91
115
  }
92
116
 
93
- const fetchWalletClient = async () => {
94
- const primaryWallet = wallets[0];
117
+ const primaryWallet = wallets[0];
118
+ const walletChainId = primaryWallet.chainId;
95
119
 
120
+ // Only recreate the client if the chain ID changes
121
+ if (currentChainIdRef.current === walletChainId && smartWalletClient) {
122
+ return;
123
+ }
124
+
125
+ const fetchWalletClient = async () => {
96
126
  try {
97
127
  // Privy wallet provides chainId in CAIP2 format
98
128
  const { reference: chainId } = ChainId.parse(primaryWallet.chainId);
@@ -113,7 +143,9 @@ const GelatoSmartWalletPrivyInternal: FC<{
113
143
  transport: custom(provider)
114
144
  });
115
145
 
116
- client.signAuthorization = async (parameters) => {
146
+ (
147
+ client.account as SmartAccount & { signAuthorization: typeof client.signAuthorization }
148
+ ).signAuthorization = async (parameters: PrepareAuthorizationParameters<Account>) => {
117
149
  const preparedAuthorization = await prepareAuthorization(client, parameters);
118
150
 
119
151
  const signedAuthorization = await signAuthorization({
@@ -130,13 +162,14 @@ const GelatoSmartWalletPrivyInternal: FC<{
130
162
  { apiKey, scw }
131
163
  );
132
164
  setSmartWalletClient(walletClientGelato);
165
+ currentChainIdRef.current = walletChainId;
133
166
  } catch (error) {
134
167
  console.error("Failed to get wallet client:", error);
135
168
  }
136
169
  };
137
170
 
138
171
  fetchWalletClient();
139
- }, [ready, wallets, walletsReady, authenticated, signAuthorization, apiKey, scw]);
172
+ }, [ready, walletsReady, authenticated, apiKey, scw, signAuthorization]);
140
173
 
141
174
  return (
142
175
  <GelatoSmartWalletPrivyProviderContext.Provider
@@ -166,6 +199,7 @@ export const GelatoSmartWalletPrivyContextProvider: FC<GelatoSmartWalletPrivyCon
166
199
  return (
167
200
  <PrivyProvider
168
201
  appId={settings.waas.appId}
202
+ clientId={settings.waas.clientId}
169
203
  config={{
170
204
  defaultChain:
171
205
  settings.defaultChain ?? settings.wagmi?.config?.chains?.[0] ?? chains.sepolia,