11import { ethereum } from "../../../../chains/chain-definitions/ethereum.js" ;
22import type { Chain } from "../../../../chains/types.js" ;
3+ import { getCachedChain } from "../../../../chains/utils.js" ;
34import type { ThirdwebClient } from "../../../../client/client.js" ;
45import {
56 type SocialAuthOption ,
67 socialAuthOptions ,
78} from "../../../../wallets/types.js" ;
9+ import { getEcosystemOptions } from "../../../ecosystem/get-ecosystem-wallet-auth-options.js" ;
810import type { Account , Wallet } from "../../../interfaces/wallet.js" ;
911import type { EcosystemWalletId , WalletId } from "../../../wallet-types.js" ;
1012import type {
@@ -13,6 +15,7 @@ import type {
1315 WalletConnectionOption ,
1416} from "../../../wallet-types.js" ;
1517import type { InAppConnector } from "../interfaces/connector.js" ;
18+ import type { Ecosystem } from "./types.js" ;
1619
1720/**
1821 * Checks if the provided wallet is an in-app wallet.
@@ -37,6 +40,7 @@ export async function connectInAppWallet(
3740 | CreateWalletArgs < "inApp" > [ 1 ]
3841 | CreateWalletArgs < EcosystemWalletId > [ 1 ] ,
3942 connector : InAppConnector ,
43+ ecosystem : Ecosystem | undefined ,
4044) : Promise < [ Account , Chain ] > {
4145 if (
4246 // if auth mode is not specified, the default is popup
@@ -73,6 +77,33 @@ export async function connectInAppWallet(
7377 } ) ;
7478 }
7579
80+ if ( ecosystem ) {
81+ const ecosystemOptions = await getEcosystemOptions ( ecosystem . id ) ;
82+ const smartAccountOptions = ecosystemOptions ?. smartAccountOptions ;
83+ if ( smartAccountOptions ) {
84+ const allowedChains = smartAccountOptions . chainIds ;
85+ const firstAllowedChain = allowedChains [ 0 ] ;
86+ if ( ! firstAllowedChain ) {
87+ throw new Error (
88+ "At least one chain must be allowed for ecosystem smart account" ,
89+ ) ;
90+ }
91+ const preferredChain =
92+ options . chain && allowedChains . includes ( options . chain . id )
93+ ? options . chain
94+ : getCachedChain ( firstAllowedChain ) ;
95+ return convertToSmartAccount ( {
96+ client : options . client ,
97+ authAccount,
98+ smartAccountOptions : {
99+ chain : preferredChain ,
100+ sponsorGas : smartAccountOptions . sponsorGas ,
101+ factoryAddress : smartAccountOptions . accountFactoryAddress ,
102+ } ,
103+ } ) ;
104+ }
105+ }
106+
76107 return [ authAccount , options . chain || ethereum ] as const ;
77108}
78109
@@ -87,6 +118,7 @@ export async function autoConnectInAppWallet(
87118 | CreateWalletArgs < "inApp" > [ 1 ]
88119 | CreateWalletArgs < EcosystemWalletId > [ 1 ] ,
89120 connector : InAppConnector ,
121+ ecosystem : Ecosystem | undefined ,
90122) : Promise < [ Account , Chain ] > {
91123 if ( options . authResult && connector . loginWithAuthToken ) {
92124 await connector . loginWithAuthToken ( options . authResult ) ;
@@ -112,6 +144,33 @@ export async function autoConnectInAppWallet(
112144 } ) ;
113145 }
114146
147+ if ( ecosystem ) {
148+ const ecosystemOptions = await getEcosystemOptions ( ecosystem . id ) ;
149+ const smartAccountOptions = ecosystemOptions ?. smartAccountOptions ;
150+ if ( smartAccountOptions ) {
151+ const allowedChains = smartAccountOptions . chainIds ;
152+ const firstAllowedChain = allowedChains [ 0 ] ;
153+ if ( ! firstAllowedChain ) {
154+ throw new Error (
155+ "At least one chain must be allowed for ecosystem smart account" ,
156+ ) ;
157+ }
158+ const preferredChain =
159+ options . chain && allowedChains . includes ( options . chain . id )
160+ ? options . chain
161+ : getCachedChain ( firstAllowedChain ) ;
162+ return convertToSmartAccount ( {
163+ client : options . client ,
164+ authAccount,
165+ smartAccountOptions : {
166+ chain : preferredChain ,
167+ sponsorGas : smartAccountOptions . sponsorGas ,
168+ factoryAddress : smartAccountOptions . accountFactoryAddress ,
169+ } ,
170+ } ) ;
171+ }
172+ }
173+
115174 return [ authAccount , options . chain || ethereum ] as const ;
116175}
117176
0 commit comments