import { Connection, Keypair, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js';
import {
createInitializeMintInstruction,
createAssociatedTokenAccountInstruction,
createMintToInstruction,
getMinimumBalanceForRentExemptMint,
MINT_SIZE,
TOKEN_PROGRAM_ID,
getAssociatedTokenAddress
} from '@solana/spl-token';
import fs from 'fs';
import config from '../config/index.js';
async function deployNFT(metadataFilePath) {
// 1. Read and validate NFT metadata
const metadata = JSON.parse(fs.readFileSync(metadataFilePath, 'utf8'));
// 2. Connect to Solayer Devnet
const connection = new Connection(config.solana.rpcUrl, { commitment: config.solana.commitment });
const payer = Keypair.fromSecretKey(/* loaded from config */);
// 3. Create metadata URI (e.g., upload to IPFS)
const metadataUri = await createMetadataUri(metadata);
// 4. Generate mint keypair & associated token account
const mintKeypair = Keypair.generate();
const tokenAccount = await getAssociatedTokenAddress(mintKeypair.publicKey, payer.publicKey);
// 5. Build and send mint transaction
const rent = await getMinimumBalanceForRentExemptMint(connection);
const { blockhash } = await connection.getLatestBlockhash();
const tx = new Transaction({ recentBlockhash: blockhash, feePayer: payer.publicKey })
.add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: mintKeypair.publicKey,
space: MINT_SIZE,
lamports: rent,
programId: TOKEN_PROGRAM_ID
}),
createInitializeMintInstruction(mintKeypair.publicKey, 0, payer.publicKey, payer.publicKey),
createAssociatedTokenAccountInstruction(payer.publicKey, tokenAccount, payer.publicKey, mintKeypair.publicKey),
createMintToInstruction(mintKeypair.publicKey, tokenAccount, payer.publicKey, 1)
);
tx.sign(payer, mintKeypair);
// 6. Send transaction
const signature = await connection.sendRawTransaction(tx.serialize());
console.log('🎉 NFT minted! Signature:', signature);
}
export { deployNFT };