Interact with the Front-end
Creating a user-friendly web interface for smart contracts on the Rootstock network enhances user interaction. Here, we'll focus on using Wagmi and RainbowKit, some popular libraries for connecting your smart contracts to a web front-end.
Project Setup
-
Create a new web project. In this case, we'll be using Next.js as our web framework.
npx create-next-app@latest -
Go to the root of your Next.js project and, using your preferred package manager, install these dependencies:
yarn add @rainbow-me/rainbowkit wagmi viem@2.x @tanstack/react-query -
Create an
.envfile at the root of your project and add the following content. You can get your Wallet Connet ID from WalletConnect Dashboard.touch .env.local
echo "NEXT_PUBLIC_WC_PROJECT_ID=<YOUR_PROJECT_ID>" >> .env.local -
Create a
providers.tsxfile inside theappdirectory and add the following content:"use client";
import {
getDefaultConfig,
RainbowKitProvider,
} from "@rainbow-me/rainbowkit";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { WagmiProvider } from "wagmi";
import { rootstock, rootstockTestnet } from "wagmi/chains";
const config = getDefaultConfig({
appName: "Rootstock Wagmi Starter",
projectId: process.env.NEXT_PUBLIC_WC_PROJECT_ID as string,
chains: [rootstockTestnet, rootstock],
ssr: true,
});
const queryClient = new QueryClient();
export default function Providers({
children,
}: {
children: React.ReactNode;
}) {
return (
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
<RainbowKitProvider>{children}</RainbowKitProvider>
</QueryClientProvider>
</WagmiProvider>
);
} -
And now import and use the
Providerscomponent to wrap your application in thelayout.tsxfile inside theappdirectory:import type { Metadata } from "next";
import "./globals.css";
import localFont from "next/font/local";
import Providers from "./providers";
import "@rainbow-me/rainbowkit/styles.css";
export const metadata: Metadata = {
title: "Rootstock Wagmi Starter",
description:
"Interact with contracts on Rootstock Network with Wagmi and RainbowKit",
};
const geistSans = localFont({
src: "./fonts/GeistVF.woff",
variable: "--font-geist-sans",
weight: "100 900",
});
const geistMono = localFont({
src: "./fonts/GeistMonoVF.woff",
variable: "--font-geist-mono",
weight: "100 900",
});
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<Providers>{children}</Providers>
</body>
</html>
);
} -
Finally, start the web server.
yarn dev
If everything went well, you should be able to access your web app by navigating to http://localhost:3000 in your browser.
Congrats!
You're all set up. Let's get to the smart contract interaction.