Skip to content

Commit

Permalink
Fix WalletConnectModal close detection (#1994)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Oct 28, 2024
1 parent 3f066f9 commit a7df42f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-bikes-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl-wc": patch
---

Fix WalletConnectModal close detection
31 changes: 25 additions & 6 deletions packages/fcl-wc/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {isMobile, openDeeplink} from "./utils"
import {FLOW_METHODS, REQUEST_TYPES} from "./constants"
import {SignClient} from "@walletconnect/sign-client/dist/types/client"
import {createSessionProposal, makeSessionData, request} from "./session"
import {ModalCtrlState} from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"

type WalletConnectModalType =
typeof import("@walletconnect/modal").WalletConnectModal
type WalletConnectModalType = import("@walletconnect/modal").WalletConnectModal

type Constructor<T> = new (...args: any[]) => T

export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
export const WC_SERVICE_METHOD = "WC/RPC"
Expand Down Expand Up @@ -44,7 +46,7 @@ export const makeServicePlugin = (
const makeExec = (
clientPromise: Promise<SignClient | null>,
{wcRequestHook, pairingModalOverride}: any,
WalletConnectModal: Promise<WalletConnectModalType>
WalletConnectModal: Promise<Constructor<WalletConnectModalType>>
) => {
return async ({
service,
Expand Down Expand Up @@ -136,7 +138,9 @@ const makeExec = (
}

// Connect to WalletConnect directly from the browser via deep link or WalletConnectModal
function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
function connectWc(
WalletConnectModal: Promise<Constructor<WalletConnectModalType>>
) {
return async ({
service,
onClose,
Expand Down Expand Up @@ -165,7 +169,7 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
)

let _uri: string | null = null,
walletConnectModal: any
walletConnectModal: WalletConnectModalType | null = null

try {
const {uri, approval} = await createSessionProposal({
Expand Down Expand Up @@ -194,7 +198,22 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
walletConnectModal = new (await WalletConnectModal)({
projectId,
})
walletConnectModal.openModal({uri, onClose})

// Open WalletConnectModal
walletConnectModal.openModal({
uri,
onClose,
})

// Subscribe to modal state changes
const unsubscribeModal = walletConnectModal.subscribeModal(
(state: ModalCtrlState) => {
if (state.open === false) {
onClose?.()
unsubscribeModal()
}
}
)
} else {
pairingModalOverride(uri, onClose)
}
Expand Down

0 comments on commit a7df42f

Please sign in to comment.