MoonKey wallets are fully compatible with @solana/web3.js, allowing you to use standard Solana development patterns and tools.
Overview
MoonKey’s embedded Solana wallets work seamlessly with the official Solana JavaScript library. You can:
- Use standard
Transaction and Connection classes
- Leverage existing Solana tools and libraries
- Follow familiar Solana development patterns
- Integrate with Solana ecosystem libraries
MoonKey wallets implement the Solana wallet standard, making them compatible with any library that supports standard Solana wallets.
Basic Usage
import { useMoonKey } from '@moon-key/react-auth';
import { useWallets } from '@moon-key/react-auth/solana';
import {
Connection,
Transaction,
SystemProgram,
PublicKey,
LAMPORTS_PER_SOL
} from '@solana/web3.js';
function SolanaWeb3Example() {
const { user } = useMoonKey();
const { wallets } = useWallets();
const handleTransaction = async () => {
if (wallets.length === 0) return;
const wallet = wallets[0];
// Create connection
const connection = new Connection('https://api.devnet.solana.com');
// Create transaction
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey(wallet.address),
toPubkey: new PublicKey('RecipientAddressHere...'),
lamports: 0.001 * LAMPORTS_PER_SOL
})
);
// Get recent blockhash
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(wallet.address);
// Serialize transaction
const serializedTx = transaction.serialize({
requireAllSignatures: false,
verifySignatures: false
});
// Sign and send using MoonKey hooks
// (Use useSendTransaction hook as shown in other examples)
};
return (
<button onClick={handleTransaction}>
Send Transaction
</button>
);
}
Common Use Cases
Get Account Balance
import { Connection, PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js';
async function getBalance(address: string) {
const connection = new Connection('https://api.devnet.solana.com');
const publicKey = new PublicKey(address);
const balance = await connection.getBalance(publicKey);
const solBalance = balance / LAMPORTS_PER_SOL;
console.log(`Balance: ${solBalance} SOL`);
return solBalance;
}
Get Account Info
import { Connection, PublicKey } from '@solana/web3.js';
async function getAccountInfo(address: string) {
const connection = new Connection('https://api.devnet.solana.com');
const publicKey = new PublicKey(address);
const accountInfo = await connection.getAccountInfo(publicKey);
if (accountInfo) {
console.log('Owner:', accountInfo.owner.toBase58());
console.log('Lamports:', accountInfo.lamports);
console.log('Executable:', accountInfo.executable);
}
return accountInfo;
}
SPL Token Operations
import { Connection, PublicKey } from '@solana/web3.js';
import { getAssociatedTokenAddress, getAccount } from '@solana/spl-token';
async function getTokenBalance(
walletAddress: string,
tokenMintAddress: string
) {
const connection = new Connection('https://api.devnet.solana.com');
const walletPublicKey = new PublicKey(walletAddress);
const mintPublicKey = new PublicKey(tokenMintAddress);
// Get associated token account
const tokenAccount = await getAssociatedTokenAddress(
mintPublicKey,
walletPublicKey
);
// Get token account info
const accountInfo = await getAccount(connection, tokenAccount);
console.log('Token balance:', accountInfo.amount.toString());
return accountInfo.amount;
}
Working with Programs
import {
Connection,
Transaction,
TransactionInstruction,
PublicKey
} from '@solana/web3.js';
async function callProgram(
walletAddress: string,
programId: string
) {
const connection = new Connection('https://api.devnet.solana.com');
// Create instruction
const instruction = new TransactionInstruction({
keys: [
{
pubkey: new PublicKey(walletAddress),
isSigner: true,
isWritable: true
}
],
programId: new PublicKey(programId),
data: Buffer.from([]) // Your instruction data
});
// Create transaction
const transaction = new Transaction().add(instruction);
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(walletAddress);
return transaction;
}
Complete Example
Here’s a complete example showing balance checking and transfers:
'use client';
import { useMoonKey } from '@moon-key/react-auth';
import { useSendTransaction, useWallets } from '@moon-key/react-auth/solana';
import {
Connection,
Transaction,
SystemProgram,
PublicKey,
LAMPORTS_PER_SOL
} from '@solana/web3.js';
import { useState, useEffect } from 'react';
export default function SolanaWeb3Integration() {
const { user } = useMoonKey();
const { wallets } = useWallets();
const { sendTransaction } = useSendTransaction();
const [balance, setBalance] = useState<number>(0);
useEffect(() => {
if (wallets.length > 0) {
fetchBalance();
}
}, [wallets]);
const fetchBalance = async () => {
if (wallets.length === 0) return;
const connection = new Connection('https://api.devnet.solana.com');
const publicKey = new PublicKey(wallets[0].address);
const lamports = await connection.getBalance(publicKey);
setBalance(lamports / LAMPORTS_PER_SOL);
};
const handleTransfer = async (recipient: string, amount: number) => {
if (wallets.length === 0) return;
const wallet = wallets[0];
const connection = new Connection('https://api.devnet.solana.com');
// Create transaction using @solana/web3.js
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey(wallet.address),
toPubkey: new PublicKey(recipient),
lamports: amount * LAMPORTS_PER_SOL
})
);
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(wallet.address);
try {
// Send using MoonKey
const signature = await sendTransaction({
transaction,
connection
});
console.log('Transfer successful:', signature);
// Refresh balance
await fetchBalance();
} catch (error) {
console.error('Transfer failed:', error);
}
};
if (!user || wallets.length === 0) {
return <p>Please connect your wallet</p>;
}
return (
<div className="solana-integration">
<h2>Solana Web3.js Integration</h2>
<div className="wallet-info">
<p>Address: {wallets[0].address}</p>
<p>Balance: {balance.toFixed(4)} SOL</p>
<button onClick={fetchBalance}>Refresh Balance</button>
</div>
<div className="transfer">
<button onClick={() => handleTransfer('RecipientAddress...', 0.001)}>
Send 0.001 SOL
</button>
</div>
</div>
);
}
Install Dependencies
npm install @solana/web3.js @solana/spl-token
Next Steps