Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WalletConnectModal close detection #1994

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading