0

i am trying to post a title to my program by using the tweetAccount which is the payer and signer of the transaction , but when i run the program i get this error

bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)
Czj45PGYy6uo4QNQvM4iq9no1Lk1HisKFnyZHSbwA4xS
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: insufficient account keys for instruction
Stack trace: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: insufficient account keys for instruction
    at Connection.sendEncodedTransaction (/Users/adnanebadysoussi/anchor/sol-tutorial/solana-intro-client/node_modules/@solana/web3.js/src/connection.ts:5791:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Connection.sendRawTransaction (/Users/adnanebadysoussi/anchor/sol-tutorial/solana-intro-client/node_modules/@solana/web3.js/src/connection.ts:5750:20)
    at async Connection.sendTransaction (/Users/adnanebadysoussi/anchor/sol-tutorial/solana-intro-client/node_modules/@solana/web3.js/src/connection.ts:5738:12)
    at async Object.sendAndConfirmTransaction (/Users/adnanebadysoussi/anchor/sol-tutorial/solana-intro-client/node_modules/@solana/web3.js/src/utils/send-and-confirm-transaction.ts:35:21)

Which i dont understand i included the two accounts i specified in my anchor code

use anchor_lang::prelude::*;
use anchor_lang::solana_program::system_program;
use solana_program::entrypoint::ProgramResult;

declare_id!("FS1yZFS9Vjbv77rqXBk46eGUdTgUXP7esZwKxtQFW8Ku");

#[program]
pub mod solana_twitter {
    use super::*;
    pub fn send_tweet(ctx: Context<SendTweet>, title: String) -> ProgramResult {
        let tweet: &mut Account<Tweet> = &mut ctx.accounts.tweet;
        tweet.title = title.to_string();
        Ok(())
    }
}

#[derive(Accounts)]
pub struct SendTweet<'info> {
    #[account(init, payer = tweet, space = 100)]
    pub tweet: Account<'info, Tweet>,
    #[account(address = system_program::ID)]
    pub system_program: AccountInfo<'info>,
}

#[account]
pub struct Tweet {
    pub title: String,
}

I added two accounts aswell in my client code

import * as Web3 from '@solana/web3.js';
import * as fs from 'fs';
import dotenv from 'dotenv';
dotenv.config();

const PROGRAM_ID = new Web3.PublicKey("ChT1B39WKLS8qUrkLvFDXMhEJ4F1XZzwUNHUt4AU9aVa")
const PROGRAM_DATA_PUBLIC_KEY = new Web3.PublicKey("Ah9K7dQ8EHaZqcAsgBW8w37yN2eAy3koFmUn4x3CJtod")
const tweetAccount = new Web3.PublicKey("Czj45PGYy6uo4QNQvM4iq9no1Lk1HisKFnyZHSbwA4xS")

const TWEET_ACCOUNT_FILE = 'tweet_account.json';

async function pingProgram(
  connection: Web3.Connection,
  payer: Web3.Keypair,
  tweetAccount: Web3.Keypair
) {
  const transaction = new Web3.Transaction();
  console.log(tweetAccount.publicKey.toString());
  const tweetTitle = "My first tweet!";
  const instruction = new Web3.TransactionInstruction({
    keys: [
      { pubkey: tweetAccount.publicKey, isSigner: true, isWritable: true },
      { pubkey: PROGRAM_DATA_PUBLIC_KEY, isSigner: false, isWritable: false },
    ],
    
    programId: PROGRAM_ID,
  });
  
  transaction.add(instruction);
  
  const transactionSignature = await Web3.sendAndConfirmTransaction(
    connection,
    transaction,
    [tweetAccount], // add tweetAccount to signers
    { commitment: 'singleGossip', preflightCommitment: 'singleGossip' },
  );

  console.log(
    `Transaction https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
  );
}



async function main() {
  const connection = new Web3.Connection(Web3.clusterApiUrl('devnet'));
  const signer = await Web3.Keypair.generate();

  let tweetAccount: Web3.Keypair;

  if (fs.existsSync(TWEET_ACCOUNT_FILE)) {
    const tweetAccountJson = JSON.parse(fs.readFileSync(TWEET_ACCOUNT_FILE, { encoding: 'utf-8' }));
    tweetAccount = Web3.Keypair.fromSecretKey(new Uint8Array(tweetAccountJson));
  } else {
    tweetAccount = new Web3.Keypair(); // create a new keypair
    fs.writeFileSync(TWEET_ACCOUNT_FILE, JSON.stringify(tweetAccount.secretKey), { encoding: 'utf-8' }); // save the keypair to a file
  }


  // console.log("New balance is", newBalance / Web3.LAMPORTS_PER_SOL)
  await pingProgram(connection, signer, tweetAccount);
}


main()
  .then(() => {
    console.log('Finished successfully');
    process.exit(0);
  })
  .catch((error) => {
    console.log('Error:', error.message);
    console.log('Stack trace:', error.stack);
    process.exit(1);
  });

1 Answer 1

0
#[account(init, payer = tweet, space = 100)]

pub tweet: Account<'info, Tweet>

This seems to be the issue. The simple answer is that the payer you specify must be different from the account you're trying to initialize. Your account cannot pay to initialize itself!

There should be another account like :

#[account(mut)]
pub payer = Signer<'info>

The payer in the tweet account attribute should point to this account instead, and you would be passing three accounts from the frontend, not two.

1
  • Thanks that solves it for the rust code
    – Solanaly
    May 9, 2023 at 14:28

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.