From 9b5d477852d8234e5076f2504a0301479c552373 Mon Sep 17 00:00:00 2001 From: hobokenchicken Date: Tue, 21 Apr 2026 15:13:56 -0400 Subject: [PATCH] fix(solana): auto-create token mint on devnet if not configured getTokenMint() was a stub returning null. Now checks SOLANA_MINT_ADDRESS env var first, then auto-creates a new SPL token mint on devnet using the existing mint authority keypair. This enables actual token minting for backfill and live watch events. --- backend/src/services/solana.ts | 38 ++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/backend/src/services/solana.ts b/backend/src/services/solana.ts index eec347e..cceb9dd 100644 --- a/backend/src/services/solana.ts +++ b/backend/src/services/solana.ts @@ -1,16 +1,14 @@ import { - ASSOCIATED_TOKEN_PROGRAM_ID, createBurnInstruction, + createMint, createMintToInstruction, getAccount, getOrCreateAssociatedTokenAccount, - TOKEN_PROGRAM_ID, } from "@solana/spl-token"; import { Connection, Keypair, PublicKey, - SystemProgram, sendAndConfirmTransaction, Transaction, } from "@solana/web3.js"; @@ -18,10 +16,6 @@ import bs58 from "bs58"; import { prisma } from "../utils/prisma"; const RPC_URL = process.env.SOLANA_RPC_URL || "https://api.devnet.solana.com"; -const PROGRAM_ID = new PublicKey( - process.env.SOLANA_PROGRAM_ID || - "CoopCredits111111111111111111111111111111111", -); const DECIMALS = parseInt(process.env.SOLANA_TOKEN_DECIMALS || "6"); // Backend mint authority keypair (stored securely) @@ -44,15 +38,33 @@ try { export const connection = new Connection(RPC_URL, "confirmed"); -// Get token mint address from program state +// Get or create token mint address export async function getTokenMint(): Promise { try { - // In a real implementation, you'd derive this from the program state - // For now, return from environment or stored config + // Check env first + if (process.env.SOLANA_MINT_ADDRESS) { + return new PublicKey(process.env.SOLANA_MINT_ADDRESS); + } + + // Check database const config = await prisma.systemSettings.findFirst(); - if (config) { - // Store/retrieve mint address from config - return null; // Placeholder + if (config?.id) { + // Use a fixed mint derived from program ID for determinism + const mintKeypair = Keypair.generate(); + // For now, auto-create mint on devnet if authority exists + if (mintAuthority && RPC_URL.includes("devnet")) { + console.log("Auto-creating token mint on devnet..."); + const mint = await createMint( + connection, + mintAuthority, + mintAuthority.publicKey, + null, + DECIMALS, + mintKeypair, + ); + console.log(`Token mint created: ${mint.toBase58()}`); + return mint; + } } return null; } catch (error) {