1+ // create_spl_mint.js
2+ // Usage: node create_spl_mint.js --rpc <RPC_URL> --keypair <KEYPAIR_PATH> --decimals 9 --supply 1000000
3+
4+ const { Connection, Keypair, PublicKey, LAMPORTS_PER_SOL } = require ( '@solana/web3.js' ) ;
5+ const splToken = require ( '@solana/spl-token' ) ;
6+ const fs = require ( 'fs' ) ;
7+ const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
8+
9+ async function main ( ) {
10+ const rpc = argv . rpc || process . env . SOLANA_RPC_URL || 'https://api.devnet.solana.com' ;
11+ const keypairPath = argv . keypair || process . env . SOLANA_KEYPAIR_PATH ;
12+ const decimals = parseInt ( argv . decimals || process . env . MINT_DECIMALS || '9' ) ;
13+ const supply = argv . supply || process . env . MINT_INITIAL_SUPPLY || '1000000' ;
14+
15+ if ( ! keypairPath ) {
16+ console . error ( 'Provide --keypair <path> or set SOLANA_KEYPAIR_PATH in .env' ) ;
17+ process . exit ( 1 ) ;
18+ }
19+
20+ const secret = JSON . parse ( fs . readFileSync ( keypairPath ) ) ;
21+ const payer = Keypair . fromSecretKey ( Buffer . from ( secret ) ) ;
22+ const connection = new Connection ( rpc , 'confirmed' ) ;
23+
24+ console . log ( 'Payer:' , payer . publicKey . toBase58 ( ) ) ;
25+
26+ // Airdrop on devnet (if using devnet)
27+ if ( rpc . includes ( 'devnet' ) ) {
28+ const airdropSig = await connection . requestAirdrop ( payer . publicKey , 2 * LAMPORTS_PER_SOL ) ;
29+ await connection . confirmTransaction ( airdropSig ) ;
30+ console . log ( 'Airdropped 2 SOL to payer (devnet)' ) ;
31+ }
32+
33+ // Create mint
34+ const mint = await splToken . createMint (
35+ connection ,
36+ payer , // payer
37+ payer . publicKey , // mint authority
38+ null , // freeze authority
39+ decimals
40+ ) ;
41+
42+ console . log ( 'Created mint:' , mint . toBase58 ( ) ) ;
43+
44+ // Create token account for payer
45+ const payerTokenAccount = await splToken . getOrCreateAssociatedTokenAccount (
46+ connection , payer , mint , payer . publicKey
47+ ) ;
48+
49+ // Mint initial supply (supply is in whole tokens; convert to smallest unit)
50+ const amount = BigInt ( supply ) * ( BigInt ( 10 ) ** BigInt ( decimals ) ) ;
51+ await splToken . mintTo (
52+ connection ,
53+ payer ,
54+ mint ,
55+ payerTokenAccount . address ,
56+ payer ,
57+ amount
58+ ) ;
59+
60+ console . log ( 'Minted' , supply , 'tokens to' , payerTokenAccount . address . toBase58 ( ) ) ;
61+ console . log ( 'Mint address:' , mint . toBase58 ( ) ) ;
62+ }
63+
64+ main ( ) . catch ( err => { console . error ( err ) ; process . exit ( 1 ) ; } ) ;
0 commit comments