Step 0 — Wallet Setup (one-time)
Your agent needs a persistent Solana wallet. It generates one on first run and reuses it forever.
import { Keypair } from "@solana/web3.js";
import fs from "fs";
import path from "path";
const WALLET_DIR = path.join(process.cwd(), "wallets", "agent-1");
const WALLET_FILE = path.join(WALLET_DIR, "keypair.json");
function loadOrCreateWallet() {
if (fs.existsSync(WALLET_FILE)) {
const data = JSON.parse(fs.readFileSync(WALLET_FILE, "utf-8"));
return Keypair.fromSecretKey(Uint8Array.from(data.secretKey));
}
fs.mkdirSync(WALLET_DIR, { recursive: true });
const keypair = Keypair.generate();
fs.writeFileSync(
WALLET_FILE,
JSON.stringify({
publicKey: keypair.publicKey.toBase58(),
secretKey: Array.from(keypair.secretKey),
}, null, 2),
{ mode: 0o600 },
);
console.log("Fund:", keypair.publicKey.toBase58());
return keypair;
}
const wallet = loadOrCreateWallet();