I am doing something similar to this issue..
here are the two ix:
let tx = new anchor.web3.Transaction();
tx.add(
anchor.web3.SystemProgram.createAccount({
fromPubkey: provider.wallet.publicKey,
newAccountPubkey: rafflePDA,
programId: programId,
lamports: await provider.connection.getMinimumBalanceForRentExemption(raffleAccountSpace),
space: raffleAccountSpace,
}),
);
tx.add(
await program.methods
.initializeRaffle(args.slug, args.maxEntries.toNumber())
.accounts({
creator: provider.wallet.publicKey,
raffle: rafflePDA,
})
.instruction()
);
let sig = await provider.sendAndConfirm(tx);
This. Below explaination I got from Anchor discord.. But I am not sure how to use it
if rafflePDA is PDA then the system account can't able to create this account. only your programID can able to create it. as PDA stand for Program Derived Address. so each PDA should have to create by the same program itself. remove the createAccount from tx.
So what should I add to signer in
let sig = await provider.sendAndConfirm(tx);
After removing the createAccount..?
UPDATE-----******------
This is my Code
const [wallet, connection] = getWalletConnection(program);
const lamports = await connection.getMinimumBalanceForRentExemption(
MINT_SIZE
);
const mintKey = anchor.web3.Keypair.generate();
const associatedTokenAddress = await getAssociatedTokenAddress(
mintKey.publicKey,
wallet.publicKey
);
console.log("NFT Account: ", associatedTokenAddress.toBase58());
const mint_tx = new anchor.web3.Transaction().add(
anchor.web3.SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: mintKey.publicKey,
space: MINT_SIZE,
programId: TOKEN_PROGRAM_ID,
lamports,
}),
createInitializeMintInstruction(
mintKey.publicKey,
0,
wallet.publicKey,
wallet.publicKey
),
createAssociatedTokenAccountInstruction(
wallet.publicKey,
associatedTokenAddress,
wallet.publicKey,
mintKey.publicKey
)
);
let blockhashObj = await connection.getLatestBlockhash();
console.log("blockhashObj", blockhashObj);
mint_tx.recentBlockhash = blockhashObj.blockhash;
try {
const signature = await wallet.sendTransaction(mint_tx, connection, {
signers: [mintKey],
});
await connection.confirmTransaction(signature, "confirmed");
} catch {
return null;
}
console.log("Mint key: ", mintKey.publicKey.toString());
console.log("User: ", wallet.publicKey.toString());
const metadataAddress = await getMetadata(mintKey.publicKey);
console.log("Metadata address: ", metadataAddress.toBase58());
try {
const tx = program.transaction.mintNft(
mintKey.publicKey,
name,
symbol,
metadataUrl,
{
accounts: {
mintAuthority: wallet.publicKey,
mint: mintKey.publicKey,
tokenAccount: associatedTokenAddress,
tokenProgram: TOKEN_PROGRAM_ID,
metadata: metadataAddress,
tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
payer: wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
}
);
const signature = await wallet.sendTransaction(tx, connection);
await connection.confirmTransaction(signature, "confirmed");
console.log("Mint Success!");