Skip to content

useConnectWallet

Connect / disconnect actions and WalletConnect pairing state. Requires WalletsProvider.

This hook opens a wallet. For the list of already-connected instances, use useWalletConnections. To bind these methods to one provider (connect() without passing provider each time), use useWallet.

Options and errors: Connect wallet (Core).

Bring your own QR component — qrcode.react in the examples is only an example.

SituationUI
Extension installedConnect button
Desktop, no extension, WalletConnect supportedQR
Mobile in-app browserInjected
Mobile Safari / ChromeOpen in wallet / deeplink
Deeplink wallet on desktopQR or “open on phone”

Example

tsx
import { QRCodeSVG } from 'qrcode.react'
import { IntegrationProvider } from '@swapped/connect-sdk'
import { useConnectWallet } from '@swapped/connect-sdk/react'

function ConnectMetaMask() {
  const {
    connect,
    isConnecting,
    pairingUri,
    pairingDeeplinkUrl,
    errors,
  } = useConnectWallet()

  return (
    <div>
      <button
        type="button"
        disabled={isConnecting(IntegrationProvider.MetaMask)}
        onClick={() =>
          void connect({
            provider: IntegrationProvider.MetaMask,
            transport: 'injected',
            popupIfUnavailable: true,
          })
        }
      >
        Connect extension
      </button>
      <button
        type="button"
        onClick={() =>
          void connect({
            provider: IntegrationProvider.MetaMask,
            transport: 'walletconnect',
            force: true,
          })
        }
      >
        WalletConnect
      </button>
      {pairingUri ? <QRCodeSVG value={pairingUri} size={180} /> : null}
      {pairingDeeplinkUrl ? (
        <button
          type="button"
          onClick={() =>
            window.open(pairingDeeplinkUrl, '_blank', 'noopener,noreferrer')
          }
        >
          Open in wallet
        </button>
      ) : null}
      {errors[IntegrationProvider.MetaMask]}
    </div>
  )
}

Returns

FieldMeaning
connect(request)Promise<WalletConnection | null>. Same ConnectWalletRequest as Core
requiresDeepLink(provider)Open-in-wallet-app path on this device
supportsWalletConnect(provider)WalletConnect is offered
getDeepLinkUrl(provider)Browse URL, or null
openDeepLink(provider)Opens that URL; returns it or null
cancelPairing()Abort in-flight WalletConnect pairing
disconnect(walletId)One instance
disconnectProvider(provider)Every instance of that provider
disconnectAll()Every connection
isConnecting(provider?)In-flight connect. Omit provider for any connect
pairingUriCurrent WalletConnect URI, or null
pairingDeeplinkUrlCurrent WalletConnect mobile deep link, or null
errorsLast error message keyed by provider or walletId

Transports

When transport is omitted, the SDK uses injected if the extension is installed, otherwise walletconnect if supported.

Injectedconnect({ transport: 'injected', forcePopup?, popupIfUnavailable? }). Some wallets have isPopupConnection on AvailableWallet.

WalletConnectconnect({ transport: 'walletconnect', force? }). pairingUri / pairingDeeplinkUrl update while pairing. The demo renders pairingUri as a QR and uses pairingDeeplinkUrl for “Open in wallet”. cancelPairing() rejects the pending connect with WALLET_PAIRING_CANCELLED.

Deeplink — when requiresDeepLink(provider) is true, getDeepLinkUrl / openDeepLink open Swapped inside the wallet app. connect({ transport: 'injected' }) on that path returns null after handing off. Treat null as “opened the wallet app”, not a failed connect. isMobileBrowser() is exported from @swapped/connect-sdk.

Errors

connect / disconnect reject with ConnectSdkError. See Errors and Connect wallet (Core).