4

I am currently working on "CreateAccount" test on solana, i am using provider and it automatically signs transactions so it is working but i want to test a new user so i want to create account by passing keypair to signers instead of provider.

My Anchor Version: 0.25.0

Rust Code:

#[derive(Accounts)]
pub struct CreateUserAccount<'info> {
    // User's account
    #[account(mut)]
    pub signer: Signer<'info>,
    // Creating a new account for every user with seed of their wallet address.
    // This constraint one-account per wallet address
    #[account(
        init, 
        payer = signer, 
        space = UserAccount::LEN, 
        seeds = ["user-account".as_bytes(), signer.key().as_ref()], 
        bump,
    )] 
    pub user_account: Account<'info, UserAccount>,
    pub system_program: Program<'info, System>,
}

Working frontend test:

describe('teamdao-tournament', () => {
  const provider = anchor.AnchorProvider.env()
  anchor.setProvider(provider)

  const program = anchor.workspace.TeamdaoTournament as Program<TeamdaoTournament>

  it('Creating a new account for user', async () => {
    const ix = await program.methods.createUserAccount().accounts().signers();
    const userAccountAddress = (await ix.pubkeys()).userAccount;
    console.log('User account address :: ', userAccountAddress.toString())

    const tx = await ix.rpc()
    console.log('Your transaction signature', tx)
  }
}

What i want to do (not working):

describe('teamdao-tournament', () => {

  const program = anchor.workspace.TeamdaoTournament as Program<TeamdaoTournament>

  const newUser = anchor.web3.Keypair.generate();
  it('Creating a new account for user', async () => {
    const ix = await program.methods.createUserAccount().accounts({
      signer: newUser.publicKey
    }).signers([newUser]);
    const userAccountAddress = (await ix.pubkeys()).userAccount;
    console.log('User account address :: ', userAccountAddress.toString())

    // Create user's facebook address
    const tx = await ix.rpc()
    console.log('Your transaction signature', tx)
 }
}

second one getting error: "Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1"

How to fix that?

3 Answers 3

4

You need to airdrop SOL to the newUser keypair before using it to pay for transactions

1
  • 2
    I was requesting airdrop but not confirming transaction after adding that it worked: await program.provider.connection.confirmTransaction
    – Mehmet Ayd
    Commented Oct 7, 2022 at 21:40
2

You might find it useful to create a program for your mock user so you can send a tx from their "provider":

export const createTestUserProgram = (program: Program, wallet: KeyPair) => {
  const userProgram = new Program(
    program.idl,
    program.programId,
    new AnchorProvider(program.provider.connection, new Wallet(wallet), {})
  );
  return userProgram;
};

You may also need to transfer some SOL to this fake wallet to pay for tx fees, you can send it from the provider wallet:

  const fundTx = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: wallet.publicKey,
      toPubkey: userWallet.publicKey,
      lamports: 1000 * LAMPORTS_PER_SOL,
    })
  );
  await provider.sendAndConfirm(fundTx, [wallet.payer]);
0
    import {  LAMPORTS_PER_SOL } from '@solana/web3.js';
    const user1KP = anchor.web3.Keypair.generate();

    let token_airdrop = await provider.connection.requestAirdrop(user1KP.publicKey, 
       10 * LAMPORTS_PER_SOL);

    await provider.connection.confirmTransaction(token_airdrop);

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.