1- import  type  {  Address ,  Hex ,   Transport  }  from  "viem" ; 
2- import  {  createConnector  }  from  "wagmi" ; 
1+ import  type  {  Address ,  Hex  }  from  "viem" ; 
2+ import  {  type   CreateConnectorFn ,   createConnector  }  from  "wagmi" ; 
33import  {  currentChain  }  from  "@/module/blockchain/provider" ; 
4- import  {  getSmartAccountProvider  }  from  "@/module/wallet/smartWallet/provider" ; 
4+ import  { 
5+     getSmartAccountProvider , 
6+     type  SmartAccountProviderType , 
7+ }  from  "@/module/wallet/smartWallet/provider" ; 
58
69smartAccountConnector . type  =  "frakSmartAccountConnector"  as  const ; 
710
8- export  type  FrakWalletConnector  =  ReturnType < typeof  smartAccountConnector >  &  { 
9-     setEcdsaSigner : ( 
10-         signer :
11-             |  ( ( args : {  hash : Hex ;  address : Address  } )  =>  Promise < Hex > ) 
12-             |  undefined 
13-     )  =>  void ; 
14- } ; 
11+ export  type  FrakWalletConnector  =  ReturnType < FrakWalletConnectorFn > ; 
12+ export  type  FrakWalletConnectorFn  =  CreateConnectorFn < 
13+     SmartAccountProviderType , 
14+     { 
15+         setEcdsaSigner : ( 
16+             signer : ( args : {  hash : Hex ;  address : Address  } )  =>  Promise < Hex > 
17+         )  =>  void ; 
18+     } 
19+ > ; 
1520
1621/** 
1722 * Create a connector for the smart account 
1823 */ 
19- export  function  smartAccountConnector < 
20-     transport  extends  Transport  =  Transport , 
21- > ( )  { 
22-     // A few types shortcut 
23-     type  Provider  =  ReturnType < typeof  getSmartAccountProvider < transport > > ; 
24- 
25-     // The current provider 
26-     let  provider : Provider  |  undefined ; 
24+ export  function  smartAccountConnector ( ) : FrakWalletConnectorFn  { 
25+     // The current provider (cached) 
26+     let  cachedProvider : SmartAccountProviderType  |  undefined ; 
2727
2828    // The current ecdsa signer 
2929    let  ecdsaSigner :
3030        |  ( ( args : {  hash : Hex ;  address : Address  } )  =>  Promise < Hex > ) 
3131        |  undefined ; 
3232
3333    // Create the wagmi connector itself 
34-     return  createConnector < 
35-         Provider , 
36-         { 
37-             setEcdsaSigner : ( signer : typeof  ecdsaSigner )  =>  void ; 
38-         } 
39-     > ( ( config )  =>  ( { 
34+     return  createConnector ( ( config )  =>  ( { 
4035        id : "frak-wallet-connector" , 
4136        name : "Frak Smart Account" , 
4237        type : smartAccountConnector . type , 
@@ -55,25 +50,27 @@ export function smartAccountConnector<
5550         */ 
5651        async  connect ( {  chainId }  =  { } )  { 
5752            // Fetch the provider 
58-             const  provider  =  await  this . getProvider ( ) ; 
53+             const  accountProvider  =  await  this . getProvider ( ) ; 
5954
6055            // If the chain id is not provided, use the current chain 
6156            if  ( chainId  &&  chainId  !==  currentChain . id )  { 
6257                throw  new  Error ( "Invalid chain id" ) ; 
6358            } 
6459
6560            // If we got it in cache return it 
66-             if  ( provider . currentSmartAccountClient )  { 
61+             if  ( accountProvider . currentSmartAccountClient )  { 
6762                return  { 
6863                    accounts : [ 
69-                         provider . currentSmartAccountClient . account . address , 
64+                         accountProvider . currentSmartAccountClient . account 
65+                             . address , 
7066                    ] , 
7167                    chainId : currentChain . id , 
7268                } ; 
7369            } 
7470
7571            // Ask the provider to build it 
76-             const  smartAccountClient  =  await  provider . getSmartAccountClient ( ) ; 
72+             const  smartAccountClient  = 
73+                 await  accountProvider . getSmartAccountClient ( ) ; 
7774            return  { 
7875                accounts : smartAccountClient 
7976                    ? [ smartAccountClient . account . address ] 
@@ -93,12 +90,15 @@ export function smartAccountConnector<
9390         * Fetch the current accounts 
9491         */ 
9592        async  getAccounts ( )  { 
96-             const  provider  =  await  this . getProvider ( ) ; 
97-             if  ( provider . currentSmartAccountClient )  { 
98-                 return  [ provider . currentSmartAccountClient . account . address ] ; 
93+             const  accountProvider  =  await  this . getProvider ( ) ; 
94+             if  ( accountProvider . currentSmartAccountClient )  { 
95+                 return  [ 
96+                     accountProvider . currentSmartAccountClient . account . address , 
97+                 ] ; 
9998            } 
10099            // Otherwise, get the account for the default chain (could be the case just after the login) 
101-             const  smartAccountClient  =  await  provider . getSmartAccountClient ( ) ; 
100+             const  smartAccountClient  = 
101+                 await  accountProvider . getSmartAccountClient ( ) ; 
102102            if  ( ! smartAccountClient )  { 
103103                return  [ ] ; 
104104            } 
@@ -116,7 +116,7 @@ export function smartAccountConnector<
116116        } , 
117117
118118        async  getClient ( parameters ?: {  chainId ?: number  } )  { 
119-             const  provider  =  await  this . getProvider ( ) ; 
119+             const  accountProvider  =  await  this . getProvider ( ) ; 
120120
121121            if  ( 
122122                parameters ?. chainId  && 
@@ -125,17 +125,17 @@ export function smartAccountConnector<
125125                throw  new  Error ( "Invalid chain id" ) ; 
126126            } 
127127
128-             const  client  =  await  provider . getSmartAccountClient ( ) ; 
128+             const  client  =  await  accountProvider . getSmartAccountClient ( ) ; 
129129            if  ( ! client )  { 
130130                throw  new  Error ( "No client found for the given chain" ) ; 
131131            } 
132132            return  client ; 
133133        } , 
134134
135-         async  getProvider ( ) :  Promise < Provider >  { 
136-             if  ( ! provider )  { 
135+         async  getProvider ( )  { 
136+             if  ( ! cachedProvider )  { 
137137                // Create the provider 
138-                 provider  =  getSmartAccountProvider ( { 
138+                 cachedProvider  =  getSmartAccountProvider ( { 
139139                    onAccountChanged : ( wallet )  =>  { 
140140                        console . log ( "Wagmi provider account changed" ,  { 
141141                            wallet, 
@@ -162,7 +162,7 @@ export function smartAccountConnector<
162162                    } , 
163163                } ) ; 
164164            } 
165-             return  provider ; 
165+             return  cachedProvider ; 
166166        } , 
167167        onAccountsChanged ( )  { 
168168            // Not relevant 
@@ -180,5 +180,5 @@ export function smartAccountConnector<
180180        setEcdsaSigner ( signer : typeof  ecdsaSigner )  { 
181181            ecdsaSigner  =  signer ; 
182182        } , 
183-     } ) ) ; 
183+     } ) )   satisfies   FrakWalletConnectorFn ; 
184184} 
0 commit comments