Building a Solana Mobile App with React Native

David OlufemiDavid Olufemi
6 min read

Introduction to Solana

Solana is a blockchain designed for mass adoption. It boasts high-performance capabilities and supports various use cases, including finance, NFTs, payments, and gaming. Solana operates as a single global state machine, emphasizing openness, interoperability, and decentralization.

The Solana Mobile Documentation Site offers valuable resources for developing within Solana’s mobile ecosystem.

Web3 and Mobile

Before diving into development, it's crucial to understand the current landscape of Web3 mobile development to anticipate potential challenges and opportunities.

Mobile Platforms

Android

Android enjoys robust support for Solana Mobile development. Solana Mobile SDKs, including Mobile Wallet Adapter, are natively implemented and readily available for Android. Moreover, the Google Play Store has less restrictive policies regarding Crypto/Web3 dApps compared to the Apple App Store. For Android development, Solana Mobile SDKs are available for Kotlin, React Native, and Flutter.

iOS

Developing Solana mobile apps on iOS presents several challenges. Firstly, the Mobile Wallet Adapter protocol is not supported on iOS due to technical limitations of the operating system. Additionally, the Apple App Store enforces stricter policies against web3/crypto apps. As a recommendation, for iOS, consider creating a mobile web app optimized for mobile browsers.

Mobile Web

Although native mobile apps typically offer the best user experience, there are cases, especially on iOS, where creating a web app optimized for mobile browsers is a more feasible option.

Browser Compatibility
  • Android Chrome

    • Supports Mobile Wallet Adapter (MWA) automatically if using the wallet-adapter-react library.
  • iOS Safari

    • Does not support MWA due to iOS limitations.

    • Wallet signing is only possible if the user has a Safari Extension wallet, like Glow Wallet.

  • Firefox, Opera, Brave, Other

    • Does not support MWA on either iOS or Android.
  • Wallet In-App-Browsers

    • Mobile wallets like Phantom, Solflare, and Ultimate provide an option to load a web dApp through an in-app browser.

    • In-App-Browsers have wallet signing capabilities but are limited within the wallet app.

Mobile dApp Architecture Overview

This section provides a conceptual overview of the architecture of a mobile dApp using the Solana Mobile Stack.

High-Level

The following diagram offers a bird's-eye view of a mobile dApp's interactions with mobile wallets and the Solana network.

Mobile Wallet Adapter Protocol

The Mobile Wallet Adapter (MWA) protocol defines the communication exchange between a dApp and a mobile wallet. In this protocol, the dApp sends requests (e.g., authorization or signing), while the wallet is responsible for displaying these requests to the user and responding back to the dApp if approved.

Official SDKs (Maintained by Solana Mobile Team)
  • React Native: Quickly start building your mobile dApp with React Native, with access to a selection of familiar web libraries.

  • Android: Develop with native Android to leverage the full capabilities of the Android OS and create a performant native experience.

Community SDKs (Actively Maintained)

These SDKs are actively maintained, supported, and used by members and developers of our community. Click to learn more about the capabilities supported by each, the state of development, and how to get started.

  • Flutter: An actively maintained SDK for building Solana mobile dApps with Flutter.

React Native SDK

Why React Native?

React Native is a popular development framework for creating mobile apps using React and JavaScript. Here, we'll explore the benefits of React Native and why you might choose it to build your mobile app.

Prerequisites

Before we get started, ensure you have the following prerequisites:

  1. Node.js and npm installed.

  2. React Native development environment set up.

Section 1: Setting Up Your Development Environment

  1. Install Node.js and npm: If you haven't already, download and install Node.js and npm from the official website.

  2. Set Up React Native Environment: Use the React Native CLI to set up your development environment. Follow the official React Native documentation for detailed instructions.

Section 2: Creating a New React Native Project

  1. Create a New React Native Project using CLI: install the React Native CLI by running the following command in your terminal:

     bash
     npm install -g react-native-cli
    

    Now, you can create a new React Native project using the following command:

     bash
     react-native init MySolanaApp
     //Replace MySolanaApp with the name you want to give your project.
    

    After the project is created, navigate to the project directory:

     cd MySolanaApp
    
  2. Now, you can run your React Native app on an Android or iOS simulator by executing the following command:

     react-native run-ios //for ios
     react-native run-android //for andriod
    
  3. Install dependencies

  1.  npm install \
       @solana/web3.js \
       @solana-mobile/mobile-wallet-adapter-protocol-web3js \
       @solana-mobile/mobile-wallet-adapter-protocol \
       react-native-get-random-values \
       bufferUpdate index.js with polyfills
    
  1. Update index.js with polyfills

  1.  import "react-native-get-random-values";
     import { Buffer } from "buffer";
     global.Buffer = Buffer;
    
     // Place the App component import below your polyfill imports!
     import App from "./App";At this point, your React Native project is set up and running with all the dependencies to build a Solana Mobile dApp!
    

SECTION 3:

Initialize Solana Wallet Connection

In your app, you'll need to connect to a Solana wallet. Use the Solana Wallet Adapter (MWA) API to establish this connection. Refer to the documentation for details on how to use methods like authorize and reauthorize.

import { useWallet } from "@solana/wallet-adapter-react";
import { useEffect } from "react";
const useSolanaWallet = () => {
  const { wallet, connect, connecting, publicKey } = useWallet();
  useEffect(() => {
    if (!wallet.connected) {
      connect();
    }
  }, [wallet, connect]);
  return { wallet, connecting, publicKey };
};
export default useSolanaWallet;

Interact with Solana

You can now use the @solana/web3.js library to interact with the Solana blockchain. This includes sending transactions, querying accounts, and more. Make sure you import the library and create a connection to the Solana network.

import {
  Connection,
  PublicKey,
  LAMPORTS_PER_SOL,
  Transaction,
  Account,
} from "@solana/web3.js";
// Create a connection to the Solana network
const connection = new Connection("https://api.devnet.solana.com");
// Generate a new keypair for the user
const keypair = new Account();
// Get the public key of the user's account
const publicKey = keypair.publicKey;
// Get the balance of the user's account
const balance = await connection.getBalance(publicKey);
// Create a new transaction to send SOL to the user's account
const transaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: publicKey,
    lamports: LAMPORTS_PER_SOL,
  })
);
// Sign the transaction with the user's private key
const signature = await connection.signTransaction(transaction, keypair);
// Send the transaction to the network
await connection.sendRawTransaction(signature);
// Get the transaction ID
const transactionId = signature.toString();
// Wait for the transaction to be confirmed
await connection.confirmTransaction(transactionId);
// Get the updated balance of the user's account
const newBalance = await connection.getBalance(publicKey);
// Print the new balance
console.log(`New balance: ${newBalance}`);

SECTION 4: Build Your Mobile App

  1. Design and build the user interface of your Solana mobile app using React Native components. You can create screens for wallet management, transaction history, and any other features you want to include.

  2. Test Your App

    Test your Solana mobile app on both Android and iOS simulators or real devices. Use the React Native CLI commands to start your app:

     react-native run-ios //for ios
     react-native run-android //for andriod
    
  3. Deploy Your App

    Once you've thoroughly tested your app, you can publish it to app stores like Google Play Store and Apple App Store. Follow the respective guidelines for app deployment.

  4. Maintain and Enhance

    Continue to maintain and enhance your Solana mobile app as needed. You can add more features, improve user experience, and stay up-to-date with Solana and React Native updates.

Conclusion

Congratulations! You've completed the comprehensive tutorial on building a Solana mobile app with React Native. You've learned how to create a mobile application that seamlessly connects to the Solana blockchain and offers a user-friendly experience.

Additional Resources

Final Thoughts

This detailed developer tutorial outline serves as a roadmap for creating your comprehensive Solana mobile app tutorial. Expand on each section with in-depth explanations, code examples, and visual aids to provide developers with a comprehensive learning experience.

17
Subscribe to my newsletter

Read articles from David Olufemi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

David Olufemi
David Olufemi