1
- import { EventEmitter } from "stream" ;
2
1
import { type CreateConnectorFn , createConnector } from "@wagmi/core" ;
3
2
import type { Prettify } from "@wagmi/core/chains" ;
4
- import { createThirdwebClient , defineChain , getAddress } from "thirdweb" ;
3
+ import { type ThirdwebClient , defineChain , getAddress } from "thirdweb" ;
5
4
import {
6
5
EIP1193 ,
7
6
type InAppWalletConnectionOptions ,
@@ -11,20 +10,22 @@ import {
11
10
import type { InAppWalletCreationOptions } from "thirdweb/wallets/in-app" ;
12
11
13
12
export type InAppWalletParameters = Prettify <
14
- Omit < InAppWalletConnectionOptions , "client" > &
15
- InAppWalletCreationOptions & {
16
- clientId : string ;
17
- ecosystemId ?: `ecosystem.${string } `;
18
- }
13
+ InAppWalletCreationOptions & {
14
+ client : ThirdwebClient ;
15
+ ecosystemId ?: `ecosystem.${string } `;
16
+ }
19
17
> ;
20
18
21
19
type Provider = EIP1193 . EIP1193Provider | undefined ;
22
20
type Properties = {
23
- connect ( parameters ?: {
24
- chainId ?: number | undefined ;
25
- isReconnecting ?: boolean | undefined ;
26
- foo ?: string ;
27
- } ) : Promise < {
21
+ connect (
22
+ parameters ?: Prettify <
23
+ Omit < InAppWalletConnectionOptions , "client" > & {
24
+ chainId ?: number | undefined ;
25
+ isReconnecting ?: boolean | undefined ;
26
+ }
27
+ > ,
28
+ ) : Promise < {
28
29
accounts : readonly `0x${string } `[ ] ;
29
30
chainId : number ;
30
31
} > ;
@@ -39,53 +40,86 @@ type StorageItem = { "tw.lastChainId": number };
39
40
* ```ts
40
41
* import { http, createConfig } from "wagmi";
41
42
* import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
43
+ * import { createThirdwebClient, defineChain as thirdwebChain } from "thirdweb";
44
+ *
45
+ * const client = createThirdwebClient({
46
+ * clientId: "...",
47
+ * });
42
48
*
43
49
* export const config = createConfig({
44
50
* chains: [sepolia],
45
51
* connectors: [
46
52
* inAppWalletConnector({
47
- * clientId: "...",
48
- * strategy: "google",
53
+ * client,
54
+ * // optional: turn on smart accounts
55
+ * smartAccounts: {
56
+ * sponsorGas: true,
57
+ * chain: thirdwebChain(sepolia)
58
+ * }
49
59
* }),
50
60
* ],
51
61
* transports: {
52
62
* [sepolia.id]: http(),
53
63
* },
54
64
* });
55
65
* ```
66
+ *
67
+ * Then in your app, you can use the connector to connect with any supported strategy:
68
+ *
69
+ * ```ts
70
+ * const { connect, connectors } = useConnect();
71
+ *
72
+ * const onClick = () => {
73
+ * const inAppWallet = connectors.find((x) => x.id === "in-app-wallet");
74
+ * connect({
75
+ * connector: inAppWallet, strategy: "google"
76
+ * });
77
+ * };
78
+ * ```
56
79
*/
57
80
export function inAppWalletConnector (
58
81
args : InAppWalletParameters ,
59
82
) : CreateConnectorFn < Provider , Properties , StorageItem > {
60
- const client = createThirdwebClient ( { clientId : args . clientId } ) ;
61
83
const wallet = args . ecosystemId
62
84
? ecosystemWallet ( args . ecosystemId , { partnerId : args . partnerId } )
63
85
: thirdwebInAppWallet ( args ) ;
86
+ const client = args . client ;
64
87
return createConnector < Provider , Properties , StorageItem > ( ( config ) => ( {
65
88
id : "in-app-wallet" ,
66
89
name : "In-App wallet" ,
67
90
type : "in-app" ,
68
91
connect : async ( params ) => {
69
- const inAppOptions = params && "client" in params ? params : undefined ;
70
- const wagmiConnectOptions =
71
- params && "chainId" in params ? params : undefined ;
72
- console . log ( "inAppOPtions" , inAppOptions ) ;
73
- console . log ( "wagmiConnectOptions" , wagmiConnectOptions ) ;
74
92
const lastChainId = await config . storage ?. getItem ( "tw.lastChainId" ) ;
75
- const chain = defineChain (
76
- wagmiConnectOptions ?. chainId || lastChainId || 1 ,
77
- ) ;
78
- const options = {
93
+ if ( params ?. isReconnecting ) {
94
+ const account = await wallet . autoConnect ( {
95
+ client,
96
+ chain : defineChain ( lastChainId || 1 ) ,
97
+ } ) ;
98
+ return {
99
+ accounts : [ getAddress ( account . address ) ] ,
100
+ chainId : lastChainId || 1 ,
101
+ } ;
102
+ }
103
+ const inAppOptions = params && "strategy" in params ? params : undefined ;
104
+ if ( ! inAppOptions ) {
105
+ throw new Error (
106
+ "Missing strategy prop, pass it to connect() when connecting to this connector" ,
107
+ ) ;
108
+ }
109
+ const chain =
110
+ inAppOptions . chain ||
111
+ defineChain ( inAppOptions ?. chainId || lastChainId || 1 ) ;
112
+ const decoratedOptions = {
113
+ ...inAppOptions ,
79
114
client,
80
115
chain,
81
- ...args ,
82
- } as unknown as InAppWalletConnectionOptions ;
83
- const account = wagmiConnectOptions ?. isReconnecting
116
+ } as InAppWalletConnectionOptions ;
117
+ const account = inAppOptions ?. isReconnecting
84
118
? await wallet . autoConnect ( {
85
119
client,
86
120
chain,
87
121
} )
88
- : await wallet . connect ( options ) ;
122
+ : await wallet . connect ( decoratedOptions ) ;
89
123
await config . storage ?. setItem ( "tw.lastChainId" , chain . id ) ;
90
124
return { accounts : [ getAddress ( account . address ) ] , chainId : chain . id } ;
91
125
} ,
@@ -103,10 +137,18 @@ export function inAppWalletConnector(
103
137
return wallet . getChain ( ) ?. id || 1 ;
104
138
} ,
105
139
getProvider : async ( params ) => {
140
+ const lastChainId = await config . storage ?. getItem ( "tw.lastChainId" ) ;
141
+ const chain = defineChain ( params ?. chainId || lastChainId || 1 ) ;
142
+ if ( ! wallet . getAccount ( ) ) {
143
+ await wallet . autoConnect ( {
144
+ client,
145
+ chain,
146
+ } ) ;
147
+ }
106
148
return EIP1193 . toProvider ( {
107
149
wallet,
108
150
client,
109
- chain : wallet . getChain ( ) || defineChain ( params ?. chainId || 1 ) ,
151
+ chain : wallet . getChain ( ) || chain ,
110
152
} ) ;
111
153
} ,
112
154
isAuthorized : async ( ) => true ,
@@ -129,13 +171,3 @@ export function inAppWalletConnector(
129
171
} ,
130
172
} ) ) ;
131
173
}
132
-
133
- const c = inAppWalletConnector ( {
134
- clientId : "..." ,
135
- strategy : "google" ,
136
- } ) ( {
137
- chains : [ sepolia ] ,
138
- emitter : new EventEmitter ( ) as any ,
139
- } ) ;
140
-
141
- c . connect ( { } ) ;
0 commit comments