4

I am trying to create an account but I get an error while creating it.

const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider); 

const newAccountKeypair = anchor.web3.Keypair.generate();

const create_account_instruction = anchor.web3.SystemProgram.createAccount({
  fromPubkey: provider.wallet.publicKey,
  newAccountPubkey: newAccountKeypair.publicKey,
  space: 1000000,
  lamports: lamports,
  programId: anchor.web3.SystemProgram.programId, 
});

const createAccountTransaction = new anchor.web3.Transaction().add(create_account_instruction)
const latestBlockhash = await provider.connection.getLatestBlockhash();
createAccountTransaction.recentBlockhash = latestBlockhash.blockhash;
const createAccountResult = await provider.sendAndConfirm(createAccountTransaction);

Provider in this example is AnchorProvider but I don't think it's related with it. I've tried with Phantom and got same error.

sendAndConfirm method description:

“Sends the given transaction, paid for and signed by the provider's wallet.”

2 Answers 2

4

I think you need to also pass the keypair for newAccountPubkey as a signer. The sendAndConfirm method should have an array for signers you can use for this.

This will also mean creating a variable for the key pair and using the someKeypair.pubkey property on the Keypair object.

3
  • Yeah, I think he needs to specify the fee payer on createAccountTransaction: Transaction using the feePayer property. Something like createAccountTransaction.feePayer = provider.wallet.publicKey; Jul 14, 2022 at 0:53
  • Thank you. Problem is solved when I added newAccountKeypair as signer in sendAndConfirm.
    – curiosity
    Jul 14, 2022 at 1:05
  • Thanks to T.J. Littlejohn I added signers: [user], to { accounts: { user: user.publicKey, authority: provider.wallet.publicKey, systemProgram: SystemProgram.programId, // clock: anchor.web3.SYSVAR_CLOCK_PUBKEY, }, signers: [user], } Jul 28, 2022 at 9:58
0

Yeah when you're creating new accounts with "throwaway" keypairs like this that keypair needs to sign the initial instruction as well. I've seen this with stake accounts but I assume it'd be the same with regular ATAs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.