|
|
const { Connection, PublicKey, Keypair } = require('@solana/web3.js'); |
|
|
const { Program, AnchorProvider, web3, utils } = require('@coral-xyz/anchor'); |
|
|
const fs = require('fs'); |
|
|
|
|
|
// Load configuration |
|
|
const config = JSON.parse(fs.readFileSync('./devnet-config.json', 'utf8')); |
|
|
const idl = JSON.parse(fs.readFileSync('./target/idl/lottery_simple.json', 'utf8')); |
|
|
|
|
|
async function initializeProgram() { |
|
|
console.log('🚀 Initializing PoWH Program...'); |
|
|
console.log('==============================='); |
|
|
|
|
|
// Set up connection and provider |
|
|
const connection = new Connection(config.rpcUrl, 'confirmed'); |
|
|
|
|
|
// Load wallet |
|
|
const walletKeypair = Keypair.fromSecretKey( |
|
|
Uint8Array.from(JSON.parse(fs.readFileSync(process.env.HOME + '/.config/solana/id.json', 'utf8'))) |
|
|
); |
|
|
|
|
|
const wallet = { |
|
|
publicKey: walletKeypair.publicKey, |
|
|
signTransaction: async (tx) => { |
|
|
tx.sign(walletKeypair); |
|
|
return tx; |
|
|
}, |
|
|
signAllTransactions: async (txs) => { |
|
|
return txs.map(tx => { |
|
|
tx.sign(walletKeypair); |
|
|
return tx; |
|
|
}); |
|
|
} |
|
|
}; |
|
|
|
|
|
const provider = new AnchorProvider(connection, wallet, { commitment: 'confirmed' }); |
|
|
const program = new Program(idl, new PublicKey(config.programId), provider); |
|
|
|
|
|
console.log('📋 Program ID:', config.programId); |
|
|
console.log('💰 Authority:', walletKeypair.publicKey.toString()); |
|
|
console.log('🪙 Mint:', config.mintAddress); |
|
|
console.log('🌐 Cluster:', config.cluster); |
|
|
|
|
|
try { |
|
|
// Derive PDAs |
|
|
const [statePda] = PublicKey.findProgramAddressSync( |
|
|
[Buffer.from("state")], |
|
|
program.programId |
|
|
); |
|
|
|
|
|
const [vaultPda] = PublicKey.findProgramAddressSync( |
|
|
[Buffer.from("vault")], |
|
|
program.programId |
|
|
); |
|
|
|
|
|
console.log('🏛️ State PDA:', statePda.toString()); |
|
|
console.log('🏦 Vault PDA:', vaultPda.toString()); |
|
|
|
|
|
// Initialize the program |
|
|
console.log('⏳ Sending initialize transaction...'); |
|
|
|
|
|
const tx = await program.methods |
|
|
.initialize() |
|
|
.accounts({ |
|
|
state: statePda, |
|
|
vault: vaultPda, |
|
|
mint: new PublicKey(config.mintAddress), |
|
|
authority: walletKeypair.publicKey, |
|
|
systemProgram: web3.SystemProgram.programId, |
|
|
tokenProgram: utils.token.TOKEN_PROGRAM_ID, |
|
|
}) |
|
|
.rpc(); |
|
|
|
|
|
console.log('✅ Program initialized successfully!'); |
|
|
console.log('📝 Transaction:', tx); |
|
|
|
|
|
// Update config with PDAs |
|
|
const updatedConfig = { |
|
|
...config, |
|
|
statePda: statePda.toString(), |
|
|
vaultPda: vaultPda.toString(), |
|
|
initialized: true, |
|
|
initTx: tx |
|
|
}; |
|
|
|
|
|
fs.writeFileSync('./devnet-config.json', JSON.stringify(updatedConfig, null, 2)); |
|
|
console.log('💾 Configuration updated with PDA addresses'); |
|
|
|
|
|
} catch (error) { |
|
|
console.error('❌ Initialization failed:', error); |
|
|
process.exit(1); |
|
|
} |
|
|
} |
|
|
|
|
|
// Run the initialization |
|
|
initializeProgram().catch(console.error); |