Implementing Frontend Setting
π½ Letβs Create the Frontendβ
In this section, we will build a frontend that interacts with our smart contract.
This time, weβll use TypeScript
+ React
+ Next.js
for frontend development.
TypeScript
: Programming languageReact.js
: LibraryNext.js
: Framework forReact.js
For an overview of each, please refer to this link. If this is your first time using them, you might find it helpful ποΌThe English version hasnβt been released yet.οΌ
π οΈ Letβs Set Up the Frontendβ
Navigate to the AVAX-AMM/packages
directory and run:
yarn create next-app client --ts
Here, weβre using the create-next-app
package to create a project named client
.
The --ts
flag specifies that we will use TypeScript
.
The client
directory will be created with the minimal setup required for a Next.js
project.
At this stage, your folder structure should look like this:
AVAX-AMM
βββ .gitignore
βββ package.json
βββ packages/
+β βββ client/
β βββ contract/
βββ tsconfig.json
Open the package.json
inside the client
directory and confirm that "private": true
is set (just like in the package.json
inside the contract
directory).
If itβs not set, add it.
Now, letβs install the packages weβll need for development. Move into the newly created client
directory and run:
yarn add ethers@5.7.1 @metamask/providers@9.1.0 react-icons
ethers
: Used to interact with the smart contract@metamask/providers
: Provides TypeScript types for interacting with MetaMaskreact-icons
: Allows you to use ready-made icons in React
Letβs check that our development environment works properly.
If node_modules/
or yarn.lock
already exist in the client
directory, delete them.
Then, from the root AVAX-AMM
directory, run:
yarn install
yarn client dev
Open your browser and go to:
http://localhost:3000
You will see the default website frontend.
β οΈ We are using the Chrome browser for this guide. If you encounter any issues, try switching browser.
Example: Local environment displaying the website
If your frontend looks like the above, youβre good to go!
From now on, whenever you want to view the frontend, run:
- From the
AVAX-AMM
directory:yarn client dev
- Or from inside the
client
directory:yarn dev
To stop the local server:
- Mac:
ctrl + c
- Windows:
ctrl + shift + w
The folder structure inside client
should look like this:
client
βββ README.md
βββ next-env.d.ts
βββ next.config.js
βββ package.json
βββ pages
β βββ _app.tsx
β βββ api
β β βββ hello.ts
β βββ index.tsx
βββ public
β βββ favicon.ico
β βββ vercel.svg
βββ styles
β βββ Home.module.css
β βββ globals.css
βββ tsconfig.json
π¦ Download MetaMaskβ
Weβll need a wallet to connect to our web application. In this project, weβll use MetaMask.
π About Core Wallet Ava Labs (the team behind the Avalanche ecosystem) supports a wallet called Core. Using Core can potentially enable faster transactions optimized for Avalanche. However, since itβs still in beta and may have bugs or frequent changes, we wonβt use it hereβbut itβs worth keeping an eye on.
- Download MetaMask from here and install it as a browser extension.
βοΈ: Why we need MetaMask When a user calls a smart contract, they need a wallet containing their address and private key. This serves as an authentication step.
Once MetaMask is installed, letβs add the Avalanche test network.
Open MetaMask, click the network dropdown at the top, and select Add Network
.
On the settings page, enter the following details and click save:
Network Name: Avalanche FUJI C-Chain
New RPC URL: https://api.avax-test.network/ext/bc/C/rpc
ChainID: 43113
Symbol: AVAX
Explorer: https://testnet.snowtrace.io/
If successful, you will now see Avalanche Fuji C-Chain
in your network list.
π° Get AVAX
Using the Faucetβ
Next, go to the Avalanche Faucet to get some AVAX
.
This is fake AVAX
that can only be used on the testnet.
Enter your wallet address in the form and request the tokens. π You can copy your wallet address by clicking your account name at the top of MetaMask.
πββοΈ Ask Questionsβ
If you have any questions about the steps so far, please ask in Discordβs #avalanche
channel.
To make the help process smoother, include the following in your error report β¨
1. The section number and lesson number related to your question
2. What you were trying to do
3. Copy & paste the error message
4. A screenshot of the error
Once your frontend environment is ready, move on to the next lesson π