To allow users to make transactions on the Solana blockchain using React, you can follow these steps. This involves integrating Solana's Web3 libraries into your React application. Below is a simple guide to get you started:
1. Set Up Your React Project
If you don't already have a React project, create one using Create React App:
npx create-react-app solana-transaction-app
cd solana-transaction-app
2. Install Required Dependencies
Install the @solana/web3.js library, which allows you to interact with the Solana blockchain:
npm install @solana/web3.js
For wallet integration, you can use @solana/wallet-adapter-react and a UI library like @solana/wallet-adapter-react-ui:
npm install @solana/wallet-adapter-react @solana/wallet-adapter-react-ui @solana/wallet-adapter-wallets
3. Set Up Solana Wallet Integration
Create a SolanaProvider component to wrap your app and provide wallet functionality.
// src/SolanaProvider.js
import { FC, useMemo } from 'react';
import { ConnectionProvider, WalletProvider } from '@solana/wallet-adapter-react';
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
import { PhantomWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets';
import { WalletModalProvider, WalletMultiButton } from '@solana/wallet-adapter-react-ui';
import { clusterApiUrl } from '@solana/web3.js';
export const SolanaProvider = ({ children }) => {
const network = WalletAdapterNetwork.Devnet; // or 'mainnet-beta'
const endpoint = useMemo(() => clusterApiUrl(network), [network]);
const wallets = useMemo(
() => [
new PhantomWalletAdapter(),
new SolflareWalletAdapter(),
],
[network]
);
return (
<ConnectionProvider endpoint={endpoint}>
<WalletProvider wallets={wallets} autoConnect>
<WalletModalProvider>{children}</WalletModalProvider>
</WalletProvider>
</ConnectionProvider>
);
};
Wrap your app with the SolanaProvider in index.js:
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { SolanaProvider } from './SolanaProvider';
ReactDOM.render(
<React.StrictMode>
<SolanaProvider>
<App />
</SolanaProvider>
</React.StrictMode>,
document.getElementById('root')
);
4. Create a Transaction Component
Create a component to handle transactions. For example, a simple transfer of SOL:
// src/TransactionComponent.js
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Keypair, SystemProgram, Transaction, LAMPORTS_PER_SOL } from '@solana/web3.js';
import { useState } from 'react';
export const TransactionComponent = () => {
const { connection } = useConnection();
const { publicKey, sendTransaction } = useWallet();
const [toAddress, setToAddress] = useState('');
const [amount, setAmount] = useState(0);
const [message, setMessage] = useState('');
const handleTransaction = async () => {
if (!publicKey || !toAddress || amount <= 0) {
setMessage('Invalid input');
return;
}
const recipientPubKey = new Keypair().publicKey; // Replace with actual recipient address
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: recipientPubKey,
lamports: amount * LAMPORTS_PER_SOL,
})
);
try {
const signature = await sendTransaction(transaction, connection);
await connection.confirmTransaction(signature, 'processed');
setMessage(`Transaction successful! Signature: ${signature}`);
} catch (error) {
setMessage(`Transaction failed: ${error.message}`);
}
};
return (
<div>
<input
type="text"
placeholder="Recipient Address"
value={toAddress}
onChange={(e) => setToAddress(e.target.value)}
/>
<input
type="number"
placeholder="Amount (SOL)"
value={amount}
onChange={(e) => setAmount(e.target.value)}
/>
<button onClick={handleTransaction}>Send SOL</button>
{message && <p>{message}</p>}
</div>
);
};
5. Add Wallet Button to Your App
In your App.js, add the WalletMultiButton and the TransactionComponent:
// src/App.js
import { WalletMultiButton } from '@solana/wallet-adapter-react-ui';
import { TransactionComponent } from './TransactionComponent';
function App() {
return (
<div className="App">
<h1>Solana Transaction App</h1>
<WalletMultiButton />
<TransactionComponent />
</div>
);
}
export default App;
6. Run Your App
Start your React app:
npm start
Key Points
- Wallet Integration: The
@solana/wallet-adapter libraries handle wallet connections (e.g., Phantom, Solflare).
- Transactions: Use
@solana/web3.js to construct and send transactions.
- Devnet vs Mainnet: Test on Devnet before moving to Mainnet.
This setup provides a simple way to interact with the Solana blockchain from a React application. For production, ensure proper error handling and security measures.