@@ -7,17 +7,24 @@ interface QNetWallet {
77 isQNet ?: boolean ;
88 connect : ( ) => Promise < string [ ] > ;
99 disconnect : ( ) => Promise < void > ;
10- getBalance : ( ) => Promise < number > ;
11- getNetwork : ( ) => Promise < string > ;
12- switchNetwork : ( network : string ) => Promise < void > ;
13- signTransaction : ( tx : any ) => Promise < string > ;
14- on : ( event : string , callback : Function ) => void ;
15- removeListener : ( event : string , callback : Function ) => void ;
10+ getBalance ?: ( ) => Promise < number > ;
11+ getNetwork ?: ( ) => Promise < string > ;
12+ switchNetwork ?: ( network : string ) => Promise < void > ;
13+ signTransaction ?: ( tx : any ) => Promise < string > ;
14+ on ?: ( event : string , callback : Function ) => void ;
15+ removeListener ?: ( event : string , callback : Function ) => void ;
16+ request ?: ( args : { method : string ; params ?: any [ ] } ) => Promise < any > ;
17+ isConnected ?: ( ) => boolean ;
18+ connected ?: boolean ;
19+ selectedAddress ?: string | null ;
20+ networkVersion ?: string ;
21+ version ?: string ;
1622}
1723
24+ // Don't redeclare window.qnet since it's already declared in AppContext.tsx
25+ // Just extend the Window interface for ethereum
1826declare global {
1927 interface Window {
20- qnet ?: QNetWallet ;
2128 ethereum ?: any ;
2229 }
2330}
@@ -74,8 +81,8 @@ export default function WalletConnection() {
7481 setWalletType ( 'qnet' ) ;
7582
7683 // Get additional info
77- const balance = await window . qnet . getBalance ( ) ;
78- const network = await window . qnet . getNetwork ( ) ;
84+ const balance = window . qnet . getBalance ? await window . qnet . getBalance ( ) : 0 ;
85+ const network = window . qnet . getNetwork ? await window . qnet . getNetwork ( ) : 'qnet' ;
7986
8087 setBalance ( balance ) ;
8188 setNetwork ( network ) ;
@@ -111,15 +118,15 @@ export default function WalletConnection() {
111118 */
112119 const setupWalletListeners = ( ) => {
113120 // QNet Wallet listeners
114- if ( window . qnet ) {
121+ if ( window . qnet && window . qnet . on ) {
115122 window . qnet . on ( 'accountsChanged' , handleAccountsChanged ) ;
116123 window . qnet . on ( 'networkChanged' , handleNetworkChanged ) ;
117124 window . qnet . on ( 'disconnect' , handleDisconnect ) ;
118125 }
119126
120127 // Phantom listeners
121128 const phantom = ( window as any ) . solana ;
122- if ( phantom ) {
129+ if ( phantom && phantom . on ) {
123130 phantom . on ( 'accountChanged' , handleAccountsChanged ) ;
124131 phantom . on ( 'disconnect' , handleDisconnect ) ;
125132 }
@@ -129,14 +136,14 @@ export default function WalletConnection() {
129136 * Cleanup wallet event listeners
130137 */
131138 const cleanupWalletListeners = ( ) => {
132- if ( window . qnet ) {
139+ if ( window . qnet && window . qnet . removeListener ) {
133140 window . qnet . removeListener ( 'accountsChanged' , handleAccountsChanged ) ;
134141 window . qnet . removeListener ( 'networkChanged' , handleNetworkChanged ) ;
135142 window . qnet . removeListener ( 'disconnect' , handleDisconnect ) ;
136143 }
137144
138145 const phantom = ( window as any ) . solana ;
139- if ( phantom ) {
146+ if ( phantom && phantom . removeListener ) {
140147 phantom . removeListener ( 'accountChanged' , handleAccountsChanged ) ;
141148 phantom . removeListener ( 'disconnect' , handleDisconnect ) ;
142149 }
@@ -180,8 +187,8 @@ export default function WalletConnection() {
180187 const updateWalletInfo = async ( ) => {
181188 try {
182189 if ( walletType === 'qnet' && window . qnet ) {
183- const balance = await window . qnet . getBalance ( ) ;
184- const network = await window . qnet . getNetwork ( ) ;
190+ const balance = window . qnet . getBalance ? await window . qnet . getBalance ( ) : 0 ;
191+ const network = window . qnet . getNetwork ? await window . qnet . getNetwork ( ) : 'qnet' ;
185192 setBalance ( balance ) ;
186193 setNetwork ( network ) ;
187194 }
@@ -215,8 +222,8 @@ export default function WalletConnection() {
215222 setWalletType ( 'qnet' ) ;
216223
217224 // Get wallet info
218- const balance = await window . qnet . getBalance ( ) ;
219- const network = await window . qnet . getNetwork ( ) ;
225+ const balance = window . qnet . getBalance ? await window . qnet . getBalance ( ) : 0 ;
226+ const network = window . qnet . getNetwork ? await window . qnet . getNetwork ( ) : 'qnet' ;
220227
221228 setBalance ( balance ) ;
222229 setNetwork ( network ) ;
@@ -295,7 +302,17 @@ export default function WalletConnection() {
295302 if ( walletType !== 'qnet' || ! window . qnet ) return ;
296303
297304 try {
298- await window . qnet . switchNetwork ( targetNetwork ) ;
305+ if ( window . qnet . switchNetwork ) {
306+ await window . qnet . switchNetwork ( targetNetwork ) ;
307+ } else if ( window . qnet . request ) {
308+ // Fallback to request method
309+ await window . qnet . request ( {
310+ method : 'wallet_switchEthereumChain' ,
311+ params : [ { chainId : targetNetwork } ]
312+ } ) ;
313+ } else {
314+ throw new Error ( 'Network switching not supported' ) ;
315+ }
299316 setNetwork ( targetNetwork ) ;
300317 await updateWalletInfo ( ) ;
301318 console . log ( `Switched to ${ targetNetwork } network` ) ;
0 commit comments