Hey everyone! I’m working on building an NFT marketplace app and trying to set up a candy machine on Solana testnet. Every time I run my code, I keep getting this frustrating error: “Error creating Candy Machine: Error: Expected a Public Key”.
I think the problem might be with how I’m passing the candy machine keypair parameter, but I’ve tried different approaches and nothing works. I tested passing just the public key part and also the full keypair object, but same result.
Here’s my code:
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { signerIdentity } from '@metaplex-foundation/umi';
import { mplCandyMachine, createCandyMachine } from '@metaplex-foundation/mpl-candy-machine';
import { Keypair, Connection } from '@solana/web3.js';
import fs from 'fs';
// Load my wallet
const walletData = JSON.parse(fs.readFileSync('my-wallet.json', 'utf8'));
const myWallet = Keypair.fromSecretKey(Uint8Array.from(walletData));
console.log('My wallet address:', myWallet.publicKey.toString());
// Setup connection
const rpcConnection = new Connection('https://api.testnet.solana.com');
// Initialize UMI
const umiInstance = createUmi(rpcConnection);
umiInstance.use(signerIdentity(myWallet));
umiInstance.use(mplCandyMachine());
async function setupCandyMachineForNFTs() {
try {
const machineKeypair = Keypair.generate();
const machineConfig = {
itemsAvailable: 5,
price: 0.05,
authority: umiInstance.identity.publicKey,
payer: umiInstance.identity.publicKey,
candyMachine: machineKeypair,
};
console.log("Machine configuration:", machineConfig);
const result = await createCandyMachine(umiInstance, machineConfig);
console.log('Success! Candy machine created:', result);
} catch (err) {
console.error('Failed to create candy machine:', err);
}
}
setupCandyMachineForNFTs();
Anyone know what I’m doing wrong here? Really need help figuring this out!