Blockchain fullstack structure - Part 4 - React.js and Next.js
Published:
Joseph
Alright, the series of articles goes to frontend part. I post an article related to Blockchain with React.js and Next.js. If you haven’t seen my previous posts Part 1 Introduction, Part 2 Hardhat, and Part 3 Golang Gin, please read them first.
In this article, I demonstrate how to use React.js (Next.js) to interact with smart contract by Golang Gin API and hardhat RPC URL, and implement a simple Sign-in with Ethereum (SIWE) authentication and setGreeting to Solidity.
After you get the wallet and the extension, you have to use Hardhat localhost network to Metamask by setting RPC URL to http://127.0.0.1:8545/ and Chain ID to 31337.
And then, you can get a rich wallet account by importing an account with the private key hardhat gave you.
I choose the create-m2-app to generate nextjs-ts template as my frontend project. It already includes Typescript, Material UI, ESLint, Jest, and React Testing Library.
1 2
cd frontend npx create-next-app --example https://github.com/MileTwo/nextjs-ts app
Okay, I have a project now. Let’s set docker for it.
Dockerfile
1 2 3 4 5 6 7 8 9
FROM node:18-alpine3.15 RUN apk add --no-cache git
Wagmi is a collection of React Hooks containing everything you need to start working with Ethereum. wagmi makes it easy to “Connect Wallet,” display ENS and balance information, sign messages, interact with contracts, and much more — all with caching, request deduplication, and persistence.
You can also call ethers.js or web3.js directly, but I don’t need to reinvent the wheel.
I set NEXT_PUBLIC_RPC_URL to hardhat RPC URL, and create a client with a InjectedConnector. I will import DappWagmiProvider in pages/_app.tsx later. Actually, WagmiConfig uses React Context, so that wagmi can use hooks anywhere.
In previous article Part 3 Golang Gin, I mentioned that I use the SIWE to verify sign-in messages. You can also follow this tutorial to integrate wagmi and siwe.
Don’t worry. There are only three mainly files: nextauth api endpoint, ConnectWallet component, and protected page. Okay! First, I create an api endpoint in pages/api/auth.
Line 27-38: prepares and sends message to contract from frontend by using wagmiusePrepareContractWrite and useContractWrite; in addition, You can use contract ABI.
Line 40-47: calls backend POST /contract/greeting api to set greeting.
Yeah, everything’s done. Just try to click button and send messages.
Do you notice that gas fee do not show when you send message to backend?
Eventually, the last article is finished. The whole blockchain infrastructure project might contains many insufficient and untested codes, but I think I can modify in the future. Moreover, through this infrastructure project, I start using Golang, learn how to use Gin, and make sense of interacting with smart contract from backend and frontend. If you have any improvement in codes, please feel free send PR to me. And, if you need a blockchain developer, I’m glad to help you too.