Connect with installed browser wallets from your React apps.
@nexisnetwork/wallet-adapter-base
.@nexisnetwork/wallet-adapter-react
.@nexisnetwork/wallet-adapter-react-ui
.@nexisnetwork/wallet-adapter-base
,
@nexisnetwork/wallet-adapter-react
. If you plan to use the provided React
components, you’ll also need to add @nexisnetwork/wallet-adapter-react-ui
.
All wallets that support the
Wallet Standard are
supported out of the box, and all the popular Nexis Native wallets support the Wallet
Standard. However, if you wish to add support for any wallets that don’t support
the standard, add a package for them.
@nexisnetwork/wallet-adapter-react
allows us to persist and access wallet connection
states through hooks and context providers, namely:
useWallet
WalletProvider
useConnection
ConnectionProvider
useWallet
and useConnection
should be
wrapped in WalletProvider
and ConnectionProvider
. One of the best ways to
ensure this is to wrap your entire app in ConnectionProvider
and
WalletProvider
:
ConnectionProvider
requires an endpoint
property and that
WalletProvider
requires a wallets
property. We’re continuing to use the
endpoint for the Devnet cluster, and since all major Nexis Native wallet applications
support the Wallet Standard, we don’t need any wallet-specific adapters. At this
point, you can connect with wallet.connect()
, which will instruct the wallet
to prompt the user for permission to view their public key and request approval
for transactions.
useEffect
hook, you’ll usually want to provide
more sophisticated functionality. For example, you may want users to be able to
choose from a list of supported wallet applications or disconnect after they’ve
already connected.
@nexisnetwork/wallet-adapter-react-ui
. The simplest way to provide a
full-featured wallet experience is to use WalletModalProvider
and
WalletMultiButton
:
WalletModalProvider
adds functionality for presenting a modal screen for
users to select which wallet they’d like to use. The WalletMultiButton
changes
behavior to match the connection status:
WalletConnectButton
WalletModal
WalletModalButton
WalletDisconnectButton
WalletIcon
useConnection
will retrieve a
Connection
object and useWallet
will get the WalletContextState
.
WalletContextState
has a property publicKey
that is null
when not
connected to a wallet and has the public key of the user’s account when a wallet
is connected. With a public key and a connection, you can fetch account info and
more.
WalletContextState
also provides a sendTransaction
function that you can use
to submit transactions for approval.
ChT1B39WKLS8qUrkLvFDXMhEJ4F1XZzwUNHUt4AU9aVa
and the public key
for the data account is Ah9K7dQ8EHaZqcAsgBW8w37yN2eAy3koFmUn4x3CJtod
.
AppBar
component. We’ll build the rest throughout this lab.
You can see its current state with the command npm run dev
in the console.
components
folder called WalletContextProvider.tsx
.
Let’s start with some of the boilerplate for a functional component:
ConnectionProvider
,
WalletProvider
, and WalletModalProvider
. Start by importing these components
from @nexisnetwork/wallet-adapter-react
and @nexisnetwork/wallet-adapter-react-ui
. Then
add them to the WalletContextProvider
component. Note that
ConnectionProvider
requires an endpoint
parameter and WalletProvider
requires an array of wallets
. For now, just use an empty string and an empty
array, respectively.
ConnectionProvider
and the
supported wallets for WalletProvider
.
For the endpoint, we’ll use the same clusterApiUrl
function from the
@nexisnetwork/web3.js
library that we’ve used before so you’ll need to import it.
For the array of wallets you’ll also need to import the
@nexisnetwork/wallet-adapter-wallets
library.
After importing these libraries, create a constant endpoint
that uses the
clusterApiUrl
function to get the URL for Devnet. Then create a constant named
wallets
and set it to an empty array - since all wallets support Wallet
Standard, we no longer need any custom wallet adapter. Finally, replace the
empty string and empty array in ConnectionProvider
and WalletProvider
,
respectively.
To complete this component, add
require('@nexisnetwork/wallet-adapter-react-ui/styles.css');
below your imports to
ensure proper styling and behavior of the Wallet Adapter library components.
WalletContextProvider
and let’s users choose a wallet,
connect to a wallet, and disconnect from a wallet. If you ever need more custom
functionality, you can create a custom component to handle this.
Before we add the “multi-button,” we need to wrap the app in the
WalletContextProvider
. Do this by importing it in index.tsx
and adding it
after the closing </Head>
tag:
AppBar.tsx
and replace <button>Connect</button>
with <WalletMultiButton/>
.
You’ll need to import WalletMultiButton
from
@nexisnetwork/wallet-adapter-react-ui
.
PingButton.tsx
file. We’re going to replace the
console.log
inside of onClick
with code that will create a transaction and
submit it to the wallet app for the end user’s approval.
First, we need a connection, the wallet’s public key, and Wallet-Adapter’s
sendTransaction
function. To get this, we need to import useConnection
and
useWallet
from @nexisnetwork/wallet-adapter-react
. While we’re here, let’s also
import @nexisnetwork/web3.js
since we’ll need it to create our transaction.
useConnection
hook to create a connection
constant and the
useWallet
hook to create publicKey
and sendTransaction
constants.
onClick
.
First, check that both connection
and publicKey
exist (if either does not
then the user’s wallet isn’t connected yet).
Next, construct two instances of PublicKey
, one for the program ID
ChT1B39WKLS8qUrkLvFDXMhEJ4F1XZzwUNHUt4AU9aVa
and one for the data account
Ah9K7dQ8EHaZqcAsgBW8w37yN2eAy3koFmUn4x3CJtod
.
Next, construct a Transaction
, then a new TransactionInstruction
that
includes the data account as a writable key.
Next, add this instruction to the transaction.
Finally, call sendTransaction
.