2

I am using Solana Playground to write Solana code.I have written a simple Greeting struct with rust that has a counter field. When I try to interact with the contract using typescript I get this error

   failed to sanitize accounts offsets correctly] logs: undefined }

After doing console logs I realized it is due to the function sendAndConfirm in sayHello function. Everything else works perfectly. can anyone help me please

import * as borsh from 'borsh';
class GreetingAccount {
  counter = 0;
  constructor(fields: {counter: number} | undefined = undefined ) {
    if (fields) {
      this.counter = fields.counter;
    }
  }
}

const GreetingSchema = new Map([
  [GreetingAccount, {kind: 'struct', fields: [['counter', 'u32']]},]
])

const GREETING_SIZE = borsh.serialize (
  GreetingSchema,
  new GreetingAccount(),
).length;

let payer;
let programId;
let greetedPubkey;

async function establishPayer(): Promise<void> {
  payer = pg.wallet.keypair;
  let lamports = await pg.connection.getBalance(pg.wallet.publicKey);
  console.log(
    'Using account',
    payer.publicKey.toBase58(),
    'containing',
    lamports / web3.LAMPORTS_PER_SOL,
    'SOL to pay for fees'
  );
}

async function checkProgram(): Promise<void> {
programId = pg.wallet.publicKey;
  console.log(`Using program ${programId.toBase58()}`);
  const GREETING_SEED = 'hello';
  greetedPubkey = await web3.PublicKey.createWithSeed(
    payer.publicKey,
    GREETING_SEED,
    programId
  );
  const greetedAccount = await pg.connection.getAccountInfo(greetedPubkey);
  if (greetedAccount === null){
    console.log(
      'Creating account',
      greetedPubkey.toBase58(),
      'to say hello to,'
    );
  const lamports = await pg.connection.getMinimumBalanceForRentExemption(
    GREETING_SIZE,
  );
  const transaction = new web3.Transaction().add(
    web3.SystemProgram.createAccountWithSeed({
      fromPubkey: payer.publicKey,
      basePubkey: payer.publicKey,
      seed: GREETING_SEED,
      newAccountPubkey: greetedPubkey,
      lamports,
      space: GREETING_SIZE,
      programId
    }),
  );
  await web3.sendAndConfirmTransaction(
    pg.connection,
    transaction,
    [payer]
  );
  }
}

async function sayHello(): Promise<void> {
  try {
    console.log('Saying hello to', greetedPubkey.toBase58());
    const instruction = new web3.TransactionInstruction({
      keys: [{pubkey: greetedPubkey, isSigner: false, isWritable:true}],
      programId,
      data: Buffer.alloc(0),
    });
    const transaction = new web3.Transaction();
    transaction.add(instruction);
    console.log(transaction);
    await web3.sendAndConfirmTransaction(
      pg.connection,
      transaction,
      [payer],
    );
  } catch (error) {
    console.error('Error occurred:', error);
    if (error.logs) {
    console.log('Transaction logs:', error.logs);
  }
  }
}


async function main(){
  await establishPayer();
  await checkProgram();
  await sayHello();
}

main().then(
  () => {},
  err => {
    console.error(err);
  }
)
2
  • Are you using Anchor, or native Rust for the program?
    – Joey Meere
    Commented May 24 at 18:36
  • I am using solana playground (native rust)
    – xyzkal
    Commented May 26 at 11:36

1 Answer 1

1

The program_id was not correct!

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.