Skip to main content
Interact with Solana programs using the embedded wallet and the Solana web3.js library.

Implementation

import { useSendTransaction, useWallet } from '@moon-key/react-auth/solana';
import { 
  Connection, 
  Transaction, 
  PublicKey,
  SystemProgram,
  LAMPORTS_PER_SOL 
} from '@solana/web3.js';

export default function SolanaProgram() {
  const { sendTransaction } = useSendTransaction();
  const { wallet } = useWallet();
  const connection = new Connection('https://api.mainnet-beta.solana.com');

  const handleProgramInteraction = async () => {
    if (!wallet) return;

    try {
      // Example: Create an account
      const newAccount = PublicKey.unique();
      
      const transaction = new Transaction().add(
        SystemProgram.createAccount({
          fromPubkey: wallet.publicKey,
          newAccountPubkey: newAccount,
          lamports: 0.001 * LAMPORTS_PER_SOL,
          space: 0,
          programId: SystemProgram.programId
        })
      );

      const signature = await sendTransaction({
        transaction,
        connection
      });

      console.log('Program interaction successful:', signature);
    } catch (error) {
      console.error('Program interaction failed:', error);
    }
  };

  return (
    <div>
      <h2>Solana Program Interaction</h2>
      <button onClick={handleProgramInteraction}>
        Interact with Program
      </button>
    </div>
  );
}

Key concepts

  • Connection - Connects to Solana RPC endpoint
  • Transaction - Builds transaction with instructions
  • Program interaction - Calls Solana program instructions
  • Wallet integration - Uses embedded wallet’s public key

Common patterns

SPL Token Transfer

import { getAssociatedTokenAddress, createTransferInstruction } from '@solana/spl-token';

const handleTokenTransfer = async () => {
  const tokenMint = new PublicKey('YOUR_TOKEN_MINT');
  const recipient = new PublicKey('RECIPIENT_ADDRESS');
  
  const senderAta = await getAssociatedTokenAddress(tokenMint, wallet.publicKey);
  const recipientAta = await getAssociatedTokenAddress(tokenMint, recipient);
  
  const transaction = new Transaction().add(
    createTransferInstruction(
      senderAta,
      recipientAta,
      wallet.publicKey,
      100 * Math.pow(10, 6) // Amount with decimals
    )
  );
  
  await sendTransaction({ transaction, connection });
};

Custom Program Instruction

import { TransactionInstruction } from '@solana/web3.js';

const handleCustomInstruction = async () => {
  const programId = new PublicKey('YOUR_PROGRAM_ID');
  
  const instruction = new TransactionInstruction({
    keys: [
      { pubkey: wallet.publicKey, isSigner: true, isWritable: true },
      // Add other account keys as needed
    ],
    programId,
    data: Buffer.from([/* instruction data */])
  });
  
  const transaction = new Transaction().add(instruction);
  await sendTransaction({ transaction, connection });
};

Important notes

Make sure you’re using the correct RPC endpoint for your environment (mainnet, devnet, or testnet).
Always verify that the wallet has sufficient SOL to pay for transaction fees and any required lamports for the operation.

Next steps