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) {