1- import { EventEmitter } from "stream" ;
21import { type CreateConnectorFn , createConnector } from "@wagmi/core" ;
32import type { Prettify } from "@wagmi/core/chains" ;
4- import { createThirdwebClient , defineChain , getAddress } from "thirdweb" ;
3+ import { type ThirdwebClient , defineChain , getAddress } from "thirdweb" ;
54import {
65 EIP1193 ,
76 type InAppWalletConnectionOptions ,
@@ -11,20 +10,22 @@ import {
1110import type { InAppWalletCreationOptions } from "thirdweb/wallets/in-app" ;
1211
1312export 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+ }
1917> ;
2018
2119type Provider = EIP1193 . EIP1193Provider | undefined ;
2220type 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 < {
2829 accounts : readonly `0x${string } `[ ] ;
2930 chainId : number ;
3031 } > ;
@@ -39,53 +40,81 @@ type StorageItem = { "tw.lastChainId": number };
3940 * ```ts
4041 * import { http, createConfig } from "wagmi";
4142 * import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
43+ * import { createThirdwebClient, defineChain as thirdwebChain } from "thirdweb";
44+ *
45+ * const client = createThirdwebClient({
46+ * clientId: "...",
47+ * });
4248 *
4349 * export const config = createConfig({
4450 * chains: [sepolia],
4551 * connectors: [
4652 * inAppWalletConnector({
47- * clientId: "...",
48- * strategy: "google",
53+ * client,
54+ * // optional: turn on smart accounts
55+ * smartAccounts: {
56+ * sponsorGas: true,
57+ * chain: thirdwebChain(sepolia)
58+ * }
4959 * }),
5060 * ],
5161 * transports: {
5262 * [sepolia.id]: http(),
5363 * },
5464 * });
5565 * ```
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+ * ```
5679 */
5780export function inAppWalletConnector (
5881 args : InAppWalletParameters ,
5982) : CreateConnectorFn < Provider , Properties , StorageItem > {
60- const client = createThirdwebClient ( { clientId : args . clientId } ) ;
6183 const wallet = args . ecosystemId
6284 ? ecosystemWallet ( args . ecosystemId , { partnerId : args . partnerId } )
6385 : thirdwebInAppWallet ( args ) ;
86+ const client = args . client ;
6487 return createConnector < Provider , Properties , StorageItem > ( ( config ) => ( {
6588 id : "in-app-wallet" ,
6689 name : "In-App wallet" ,
6790 type : "in-app" ,
6891 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 ) ;
7492 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 ,
79114 client,
80115 chain,
81- ...args ,
82- } as unknown as InAppWalletConnectionOptions ;
83- const account = wagmiConnectOptions ?. isReconnecting
84- ? await wallet . autoConnect ( {
85- client,
86- chain,
87- } )
88- : await wallet . connect ( options ) ;
116+ } as InAppWalletConnectionOptions ;
117+ const account = await wallet . connect ( decoratedOptions ) ;
89118 await config . storage ?. setItem ( "tw.lastChainId" , chain . id ) ;
90119 return { accounts : [ getAddress ( account . address ) ] , chainId : chain . id } ;
91120 } ,
@@ -103,13 +132,21 @@ export function inAppWalletConnector(
103132 return wallet . getChain ( ) ?. id || 1 ;
104133 } ,
105134 getProvider : async ( params ) => {
135+ const lastChainId = await config . storage ?. getItem ( "tw.lastChainId" ) ;
136+ const chain = defineChain ( params ?. chainId || lastChainId || 1 ) ;
137+ if ( ! wallet . getAccount ( ) ) {
138+ await wallet . autoConnect ( {
139+ client,
140+ chain,
141+ } ) ;
142+ }
106143 return EIP1193 . toProvider ( {
107144 wallet,
108145 client,
109- chain : wallet . getChain ( ) || defineChain ( params ?. chainId || 1 ) ,
146+ chain : wallet . getChain ( ) || chain ,
110147 } ) ;
111148 } ,
112- isAuthorized : async ( ) => true ,
149+ isAuthorized : async ( ) => true , // always try to reconnect
113150 switchChain : async ( params ) => {
114151 const chain = config . chains . find ( ( x ) => x . id === params . chainId ) ;
115152 if ( ! chain ) {
@@ -129,13 +166,3 @@ export function inAppWalletConnector(
129166 } ,
130167 } ) ) ;
131168}
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