Skip to main content

Multi-Provider Connection

If you want to support the Universal Profile Browser Extension alongside other wallets, you can use third-party libraries like Web3Modal from Wallet Connect or Web3-Onboard from Blocknative. Both libraries are open-source, framework-agnostic JavaScript tools to improve the user's onboarding experience.

ImageDescription
  • Customizable Connection Window
  • Remains connection on Page Refresh
  • Lists supported EIP-1193 Extensions
  • Automatically detects EIP-6963 Extensions
  • Requires Wallet Connect Account ID
  • Example Implementation

    You can check out the implementation of both libraries within our dApp Boilerplate. You can change between multiple provider methods on the fly using the provider switcher component.

    Installation​

    You can install the Web3 Modal using different configurations. The default package utilizes the Ethers.js library, which will be used in this example:

    npm i @web3modal/ethers ethers

    Setup​

    After the installation, you can proceed to configure the library to support the LUKSO network and your dApp.

    // Import the necessary components
    import { createWeb3Modal, defaultConfig } from '@web3modal/ethers/react';

    // Import the project ID from https://cloud.walletconnect.com
    const projectId = 'YOUR_PROJECT_ID';

    // Setup the Metadata
    const walletConnectMetadata = {
    name: 'Example dApp',
    description: 'dApp using Wallet Connect',
    url: 'https://my.dapp.domain',
    icons: ['https://my.dapp.domain/icon.svg'],
    };

    // Initialize the Configuration Element
    const walletConnectConfig = defaultConfig({
    metadata: walletConnectMetadata,
    });

    // Define the supported networks
    const supportedChains = [
    // https://docs.lukso.tech/networks/mainnet/parameters
    {
    chainId: 42,
    name: 'LUKSO Mainnet',
    currency: 'LYX',
    explorerUrl: 'https://explorer.execution.mainnet.lukso.network/',
    rpcUrl: 'https://42.rpc.thirdweb.com/',
    },
    // https://docs.lukso.tech/networks/testnet/parameters
    {
    chainId: 4021,
    name: 'LUKSO Testnet',
    currency: 'LYXt',
    explorerUrl: 'https://explorer.execution.testnet.lukso.network/',
    rpcUrl: 'https://4201.rpc.thirdweb.com/',
    },
    ];

    // Define chain images for the network screen
    const walletConnectChainImages = {
    42: 'https://my.dapp.domain/lyx_symbol.svg',
    4201: 'https://my.dapp.domain/lyx_symbol.svg',
    };

    // Create the Web3 Modal Instance
    const walletConnectInstance = createWeb3Modal({
    ethersConfig: walletConnectConfig,
    chains: supportedChains,
    projectId,
    chainImages: walletConnectChainImages,
    // OPTIONAL: Only show wallets that are installed by the user
    featuredWalletIds: ['NONE'],
    });

    Connect​

    To set and access the Wallet Connect provider within your dApp, you can call the integrated open() method provided by the createWeb3Modal instance. The library will show a connection window with all supported wallets. You can then fetch the active account and set it as the default provider within your dApp.

    // Trigger the connection process and screen
    await walletConnectInstance.open();
    // Subscribe to provider events, to track the connection
    walletConnectInstance.subscribeProvider(
    ({ provider, address, isConnected, error }) => {
    if (error) {
    console.log('Wallet Connect Error:', error);
    return;
    }
    // If access was granted
    if (isConnected && provider && address) {
    const provider = new ethers.BrowserProvider(provider);
    walletConnectInstance.close();
    }
    },
    );

    Disconnect​

    To disconnect your wallet, you have to call the disconnect() method provided by the createWeb3Modal instance.

    // Close all connections
    walletConnectInstance.disconnect();
    Maintaining compatibility

    The Universal Profile Browser Extension only handles one active account connection at a time. This behavior differs from regular wallets. Therefore, the Disconnect methods should be called on every provider's accountsChanged and chainChanged events.

    Sample Implementations​