Integrating WalletConnect with Your React App on Conflux Chain
This tutorial will guide you how to seamlessly integrate WalletConnect into your React application to enable user interactions with Conflux eSpace, thereby providing your users with a secure and intuitive way to connect their wallets to your dApp on Conflux eSpace.
前提条件
- Node.js installed on your machine
- Basic understanding of React, JavaScript and Typescript
- A Conflux eSpace network-compatible wallet (e.g., MetaMask)
Step 1: Setting Up Your React Project
If you haven't already created a React project, start by setting up one. You can skip this step if you have an existing project.
npx create-react-app web3modal-conflux-react
cd web3modal-conflux-react
Step 2: Installing Necessary Libraries
Web3Modal SDK has support for Wagmi, which will help you interact with wallets and smart contracts.
npm install @web3modal/wagmi wagmi viem @tanstack/react-query
Step 3: Setting Up WalletConnect
Create a new file in your project, Web3ModalConfig.tsx
, and import the necessary modules to set up WalletConnect.
import { createWeb3Modal } from "@web3modal/wagmi/react";
import { http, createConfig, WagmiProvider } from "wagmi";
// Import Conflux eSpace network config for wagmi
import { confluxESpace} from "wagmi/chains";
import { walletConnect, injected, coinbaseWallet } from "wagmi/connectors";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactNode } from "react";
// 0. Setup queryClient
const queryClient = new QueryClient();
// 1. Get projectId at https://cloud.walletconnect.com
const projectId = "YOUR_PROJECT_ID";
// 2. Create wagmiConfig
const metadata = {
name: "Web3Modal",
description: "Web3Modal Example",
url: "https://web3modal.com", // origin must match your domain & subdomain
icons: ["https://avatars.githubusercontent.com/u/37784886"],
};
const config = (createConfig as any)({
chains: [confluxESpace],
transports: {
[confluxESpace.id]: http(),
},
connectors: [
walletConnect({ projectId, metadata, showQrModal: false }),
injected({ shimDisconnect: true }),
coinbaseWallet({
appName: metadata.name,
appLogoUrl: metadata.icons[0],
}),
],
});
// 3. Create modal
createWeb3Modal({
wagmiConfig: config,
projectId,
enableAnalytics: false, // Optional - defaults to your Cloud configuration
enableOnramp: false, // Optional - false as default
});
export function Web3ModalProvider({ children }: { children: ReactNode }) {
return (
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</WagmiProvider>
);
}
For details on how to obtain ProjectId
, please refer to this article.
Step 4: Use the Web3ModalProvider in Your Application
At your application's entry point, typically in the index.tsx, use the Web3ModalProvider
component to wrap your application.
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Web3ModalProvider } from "./Web3ModalConfig";
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<Web3ModalProvider>
<App />
</Web3ModalProvider>
</React.StrictMode>
);