fix(solana): airdrop SOL to mint authority before creating mint
Mint authority had zero SOL balance on devnet, causing 'Attempt to debit an account but found no record of a prior credit' error. Now getTokenMint() checks balance, airdrops 2 SOL if needed, then creates the SPL token mint. Also added caching so mint is only created once.
This commit is contained in:
@@ -38,33 +38,55 @@ try {
|
|||||||
|
|
||||||
export const connection = new Connection(RPC_URL, "confirmed");
|
export const connection = new Connection(RPC_URL, "confirmed");
|
||||||
|
|
||||||
|
let cachedMint: PublicKey | null = null;
|
||||||
|
|
||||||
// Get or create token mint address
|
// Get or create token mint address
|
||||||
export async function getTokenMint(): Promise<PublicKey | null> {
|
export async function getTokenMint(): Promise<PublicKey | null> {
|
||||||
try {
|
try {
|
||||||
// Check env first
|
// Return cached mint
|
||||||
if (process.env.SOLANA_MINT_ADDRESS) {
|
if (cachedMint) {
|
||||||
return new PublicKey(process.env.SOLANA_MINT_ADDRESS);
|
return cachedMint;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check database
|
// Check env first
|
||||||
const config = await prisma.systemSettings.findFirst();
|
if (process.env.SOLANA_MINT_ADDRESS) {
|
||||||
if (config?.id) {
|
cachedMint = new PublicKey(process.env.SOLANA_MINT_ADDRESS);
|
||||||
// Use a fixed mint derived from program ID for determinism
|
return cachedMint;
|
||||||
const mintKeypair = Keypair.generate();
|
}
|
||||||
// For now, auto-create mint on devnet if authority exists
|
|
||||||
if (mintAuthority && RPC_URL.includes("devnet")) {
|
// Auto-create mint on devnet if authority exists
|
||||||
console.log("Auto-creating token mint on devnet...");
|
if (mintAuthority && RPC_URL.includes("devnet")) {
|
||||||
const mint = await createMint(
|
console.log("Auto-creating token mint on devnet...");
|
||||||
connection,
|
|
||||||
mintAuthority,
|
// Fund mint authority with SOL first
|
||||||
mintAuthority.publicKey,
|
const balance = await connection.getBalance(mintAuthority.publicKey);
|
||||||
null,
|
if (balance < 500000000) {
|
||||||
DECIMALS,
|
console.log(
|
||||||
mintKeypair,
|
`Airdropping SOL to mint authority ${mintAuthority.publicKey.toBase58()}...`,
|
||||||
);
|
);
|
||||||
console.log(`Token mint created: ${mint.toBase58()}`);
|
const signature = await connection.requestAirdrop(
|
||||||
return mint;
|
mintAuthority.publicKey,
|
||||||
|
2 * 1000000000,
|
||||||
|
);
|
||||||
|
await connection.confirmTransaction(signature);
|
||||||
|
console.log("Airdrop complete");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mintKeypair = Keypair.generate();
|
||||||
|
const mint = await createMint(
|
||||||
|
connection,
|
||||||
|
mintAuthority,
|
||||||
|
mintAuthority.publicKey,
|
||||||
|
null,
|
||||||
|
DECIMALS,
|
||||||
|
mintKeypair,
|
||||||
|
);
|
||||||
|
cachedMint = mint;
|
||||||
|
console.log(`Token mint created: ${mint.toBase58()}`);
|
||||||
|
console.log(
|
||||||
|
`Add SOLANA_MINT_ADDRESS=${mint.toBase58()} to your .env to persist it`,
|
||||||
|
);
|
||||||
|
return mint;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user