@@ -4,9 +4,11 @@ import {isMobile, openDeeplink} from "./utils"
4
4
import { FLOW_METHODS , REQUEST_TYPES } from "./constants"
5
5
import { SignClient } from "@walletconnect/sign-client/dist/types/client"
6
6
import { createSessionProposal , makeSessionData , request } from "./session"
7
+ import { ModalCtrlState } from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"
7
8
8
- type WalletConnectModalType =
9
- typeof import ( "@walletconnect/modal" ) . WalletConnectModal
9
+ type WalletConnectModalType = import ( "@walletconnect/modal" ) . WalletConnectModal
10
+
11
+ type Constructor < T > = new ( ...args : any [ ] ) => T
10
12
11
13
export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
12
14
export const WC_SERVICE_METHOD = "WC/RPC"
@@ -44,7 +46,7 @@ export const makeServicePlugin = (
44
46
const makeExec = (
45
47
clientPromise : Promise < SignClient | null > ,
46
48
{ wcRequestHook, pairingModalOverride} : any ,
47
- WalletConnectModal : Promise < WalletConnectModalType >
49
+ WalletConnectModal : Promise < Constructor < WalletConnectModalType > >
48
50
) => {
49
51
return async ( {
50
52
service,
@@ -136,7 +138,9 @@ const makeExec = (
136
138
}
137
139
138
140
// Connect to WalletConnect directly from the browser via deep link or WalletConnectModal
139
- function connectWc ( WalletConnectModal : Promise < WalletConnectModalType > ) {
141
+ function connectWc (
142
+ WalletConnectModal : Promise < Constructor < WalletConnectModalType > >
143
+ ) {
140
144
return async ( {
141
145
service,
142
146
onClose,
@@ -165,7 +169,7 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
165
169
)
166
170
167
171
let _uri : string | null = null ,
168
- walletConnectModal : any
172
+ walletConnectModal : WalletConnectModalType | null = null
169
173
170
174
try {
171
175
const { uri, approval} = await createSessionProposal ( {
@@ -194,7 +198,22 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
194
198
walletConnectModal = new ( await WalletConnectModal ) ( {
195
199
projectId,
196
200
} )
197
- walletConnectModal . openModal ( { uri, onClose} )
201
+
202
+ // Open WalletConnectModal
203
+ walletConnectModal . openModal ( {
204
+ uri,
205
+ onClose,
206
+ } )
207
+
208
+ // Subscribe to modal state changes
209
+ const unsubscribeModal = walletConnectModal . subscribeModal (
210
+ ( state : ModalCtrlState ) => {
211
+ if ( state . open === false ) {
212
+ onClose ?.( )
213
+ unsubscribeModal ( )
214
+ }
215
+ }
216
+ )
198
217
} else {
199
218
pairingModalOverride ( uri , onClose )
200
219
}
0 commit comments