3. Add Wallet Support
In the third chapter of the tutorial on building an end-to-end dapp on Aptos, you will be adding wallet support to your React app. You now need a wallet to submit a transaction to the blockchain.
Aptos provides a wallet adapter that supports many ecosystem wallets to offering a common interface and UI package that can be used to add a wallet connect button and a wallet selector modal.
- Stop the local server if running.
- In the
client
folder, run:
npm i @aptos-labs/wallet-adapter-react
npm i @aptos-labs/wallet-adapter-ant-design
This installs two packages:
- the adapter React provider that holds the logic.
- a wallet connect UI package.
- We now need to add wallets to our app. There is a list of wallets the adapter supports; but to keep this tutorial simple, we will use only one wallet.
Still in the
client
folder, run
npm i petra-plugin-wallet-adapter
If you haven't installed the Petra wallet extension yet:
- Install Petra Wallet and open the Chrome extension.
- Follow the user instructions on petra.app for help.
- Switch to the Devnet network by clicking Settings > Network and selecting devnet.
- Click the Faucet button to ensure you can receive test tokens.
- Open
Index.tsx
file. At the top of the file, add the following:
import { PetraWallet } from "petra-plugin-wallet-adapter";
import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";
- Still in
Index.tsx
, add a constant that holds an array of wallets:
...
const wallets = [new PetraWallet()];
...
- Inside the
render
method, update the code with the following:
...
<AptosWalletAdapterProvider plugins={wallets} autoConnect={true}>
<App />
</AptosWalletAdapterProvider>
...
That wraps our app with the adapter provider and initializes it with our wallets. It also sets the provider to autoConnect a wallet.
- Open the
App.tsx
file and import the wallet connect UI package we installed in the previous step. At the top of the file add the following:
import { WalletSelector } from "@aptos-labs/wallet-adapter-ant-design";
- The UI package uses a style
.css
file; let's import that one also at the bottom of the import statements.
...
import "@aptos-labs/wallet-adapter-ant-design/dist/index.css";
- In the
return
statement, remove the<h1>Connect Wallet</h1>
text and add theWalletSelector
component:
...
<Col span={12} style={{ textAlign: "right", paddingRight: "200px" }}>
<WalletSelector />
</Col>
...
- Start the local server with
npm start
and open the app in the browser.
We now have a working Wallet connect button and a wallet selector modal. Feel free to play with it and connect a wallet with it.
Then learn how to fetch data from chain in chapter 4.