What you will learn
- How to set up a React Native Android project and integrate the Mobile Wallet Adapter library.
- How to use Mobile Wallet Adapter to connect to an installed wallet app.
- How to connect to devnet, check your wallet balance, and request an airdrop of SOL.
- How to use the memo program to write your message to the network and see your message on the blockchain!
Prerequisites
Read the prerequisite setup guide before starting the tutorial. This tutorial will be using the fakewallet app to test your app’s integration with Mobile Wallet Adapter.Clone the tutorial repo
Clone the tutorial repo from github.SolanaReactNativeTutorialStarter: A boilerplate app with the MWA packages/dependencies ready and starter code that we’ll be building up throughout the tutorial.SolanaReactNativeTutorialComplete: The complete version of the app and the end product of the tutorial.
First run
Move into the starter project directory, install dependencies, and try running the app.MainScreen.tsx and you can see your app update immediately, due to React Native’s Fast Refresh feature.
Connect to a wallet
Wallet apps manage your wallet’s private key and can do actions like signing and sending transactions/messages. You will learn how use the Mobile Wallet Adapter JS library to connect your dApp to thefakewallet app.
Build a connect button
InConnectButton.tsx:
- Use the
transactfunction to start a session with a wallet app. - Then within the session, request wallet authorization for the dApp.
- Save the results of authorization in the parent component’s state.
AuthorizationResult object from authorize. It contains a list of accounts and an authToken.
Each account object contains useful information like the account’s address (or publicKey) and account label. The authToken will be used for reauthorization in future transact’s with the wallet.
In MainScreen.tsx:
- Render the
ConnectButtoncomponent below theScrollView. - Create
authorizationstate andsetAuthorizationwithinonConnect.
Build a disconnect button
We also want to give the users the option to disconnect their wallet from the app. We’ll use adeauthorize request to invalidate the provided authToken.
In DisconnectButton.tsx:
- Pass in the stored
authorizationand, within atransactsession, send adeauthorizerequest to the wallet for the storedauthToken.
MainScreen.tsx:
- Conditionally render the
ConnectButtonorDisconnectButton. - Pass in
onDisconnectas props, setting the storedauthorizationstate to null.
View wallet account balance
After connecting to a wallet, we’ll use theconnection class from useConnection to view your wallet account’s SOL balance on devnet.
In MainScreen.tsx:
- Create a
balancestate and callconnection.getBalance(authorization.publicKey)to fetch the wallet balance from devnet. - Create a
useEffecthook, that calls the fetches and updates the balance once connected to a wallet. - Conditionally render the
AccountInfocomponent if connected to a wallet.
AccountInfoSection.tsx:
- The starter code includes function
convertLamportToSOLbecause the number returned fromgetBalanceis in units of lamport.
NOTEThe starter code handles wrapping the app with a
ConnectionProvider in App.tsx, enabling the useConnection hook.Request a SOL airdrop
If you try connecting to the wallet at this point, you’ll notice that you have 0 SOL tokens in your balance. In order to send transactions to devnet, we’ll need to fund the account by requesting an airdrop. WithRequestAirdropButton, use connection.requestAirdrop(...) to request an airdrop to your wallet’s address (public key).
Then in MainScreen, render the new button and pass in fetchAndUpdateBalance(authorization) to the onAirdropComplete prop.
INFOUnfortunately, airdropping on devnet is prone to flakiness and a request can often fail. If you are seeing a transaction confirmation error
at this step, it’s most likely because due to this instability.
Record a message to the blockchain
Construct and send a transaction
After receiving an airdrop successfully, you should see your SOL balance update to0.1. You can now pay the fee to send a transaction to record the message on the network. To do so, we’ll be invoking an on-chain program called the MemoProgram.
In RecordMessageButton.tsx, create a function recordMessage that:
- Constructs the
MemoProgramTransaction. - Sends a
signAndSendTransactionrequest to the wallet. - The wallet then signs the transaction with the private key and sends it to devnet.
View your message on explorer
If this transaction is successful, you can use the Solana Explorer to see your message on the blockchain itself. On the success case, add anAlert to give the user the option to click a link and navigate to the explorerUrl.
RecordMessageButton.
Congratulations!
You’ve successfully recorded your message onto the Solana blockchain and created a functioning Solana Mobile dApp! 🎉
Next steps
Explore guides and SDK references to learn more and create more advanced applications. Here are some links to explore:Sample App Collection
- If you want to see more examples of dApps, then check out this curated list of Solana mobile sample apps. It also includes a more robust version of the app built in this tutorial.
