1

I have a simple solana greeting-counter program, I have it deployed locally. When I am calling from my typescript code I am able to do pretty much all the transactions and verify them. Except the last one, where I am seeing the block explorer is showing the transaction went through it's a success, however when I try to read the data I am seeing the state has not been updated. I am using anchor 0.30.0

Here is the Solana App Code:

use anchor_lang::prelude::*;

declare_id!("8VEC1cycS5ry9aM5CBRWtxfVQBfNtWANTXXrXXmk2TXw");


#[program]
pub mod hello_please {
    use super::*;
    pub fn create_greeting(ctx: Context<CreateGreeting>) -> Result<()> {
        let greeting_account = &mut ctx.accounts.greeting_account;
        greeting_account.counter = 0;
        Ok(())
    }

    pub fn increment_greeting(ctx: Context<IncrementGreeting>) -> Result<()> {
        let greeting_account = &mut ctx.accounts.greeting_account;
        greeting_account.counter += 1;
        Ok(())
    }
}

#[derive(Accounts)]
pub struct CreateGreeting<'info> {
    #[account(init, payer = user, space = 8 + 8)]
    pub greeting_account: Account<'info, GreetingAccount>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct IncrementGreeting<'info> {
    #[account(mut)]
    pub greeting_account: Account<'info, GreetingAccount>,
}

#[account]
pub struct GreetingAccount {
    pub counter: u64,
}

Here is the Typescript code :

interface GreetingAccount {
  counter: anchor.BN
}

async function main() {
// Everything has been initialized

  const txSignature = await program.methods
      .incrementGreeting()
      .accounts({
          greetingAccount: greetingAccount.publicKey,
      })
      .signers([providerAccount])
      .rpc();

  const latestBlockHash = await connection.getLatestBlockhash();

  await connection.confirmTransaction({
      blockhash: latestBlockHash.blockhash,
      lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
      signature: txSignature,
    }, 'finalized');

  const accountInfo = await connection.getAccountInfo(greetingAccount.publicKey);
  if (accountInfo === null) {
      throw new Error("Error: Account not found");
  }

  const GreetingAccountSchema = borsh.struct<GreetingAccount>([borsh.u64("counter"),]);

  const {counter} = GreetingAccountSchema.decode(accountInfo?.data, "processed");

  console.log("decoded counter: "+counter.toNumber()+"\n");
}();

My console.log is showing 0 but the explorer shows it as 1 when I use msg! macro

1 Answer 1

1

Instead of manually deserializing the account data, try:

    const account = await program.account.greetingAccount.fetch(
      greetingAccount.publicKey
    )

Anchor can automatically deserialize the account data for you using the program IDL.

3
  • Hey @john thanks for getting back. What I am realizing is I have to be in anchor workspace to automatically deserialize it. Is there a way for me not to work from an anchor workspace necessarily? Commented May 24 at 14:05
  • how did you set up your program variable? if program.methods works to send the transaction, then im not sure why program.account wouldn't
    – john
    Commented May 24 at 16:49
  • Hey @john, What I did was call the Solana App from a strictly nodejs environment(outside an anchor workspace). And I created the program using idl.son. I was not able to create using type because it is throwing an error looking for Anchor.toml, I have actually added more detailed info. here, stackoverflow.com/questions/78433642/… Commented May 24 at 21:35

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.