← Module 02 Module 03 · about 60 min

Your first transaction

The wallet is connected and we have a public key. Now we do something with it. Today: request devnet SOL from a faucet, build a transfer instruction, sign it with MWA, send it, and confirm it landed on-chain.

1Devnet airdrop

On a Connection from @solana/web3.js, call requestAirdrop(publicKey, LAMPORTS_PER_SOL). Public airdrop is rate-limited. If it fails, open faucet.solana.com and paste the same address.

import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";

const connection = new Connection("https://api.devnet.solana.com", "confirmed");

async function airdrop(publicKey: PublicKey) {
  try {
    const sig = await connection.requestAirdrop(publicKey, LAMPORTS_PER_SOL);
    await connection.confirmTransaction(sig, "confirmed");
  } catch (error) {
    console.warn("Airdrop failed, use the faucet", error);
  }
}

2Build the transaction

Send 0.001 SOL to yourself. That is 1_000_000 lamports. SystemProgram.transfer is enough.

import { SystemProgram, Transaction } from "@solana/web3.js";

const lamports = 1_000_000; // 0.001 SOL
const tx = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: publicKey,
    lamports,
  }),
);
tx.feePayer = publicKey;
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;

3Sign and send with MWA

Use the template hook. The method name in useMobileWallet is signAndSendTransaction (check the file on disk). Pass the transaction; the wallet signs; you get a signature string back. Show it in Diagnostics.

4Confirm

await connection.confirmTransaction(signature, "confirmed"). Then open Explorer:

https://explorer.solana.com/tx/SIGNATURE?cluster=devnet

5Common errors

6Checkpoint

You're done when: you can see your transaction on Solana Explorer devnet with status Confirmed.