简体   繁体   中英

Solana Anchor Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctly

I am trying to run the following code in Anchor Solana, with program in rust as follows:

        use anchor_lang::prelude::*;

        declare_id!("RnbXAWg5mCvmSafjd1CnYaz32qLgZHdeHK6xzHDi1yU");

        #[program]
        pub mod sol_proj_1 {
            use super::*;
            pub fn initialize(ctx: Context<Initialize>, data: u64) -> ProgramResult {
                let my_account = &mut ctx.accounts.my_account;
                my_account.data = data;
                println!("hello there!");

                Ok(())
            }
            pub fn update(ctx: Context<Update>, data: u64) -> ProgramResult {
                let my_account = &mut ctx.accounts.my_account;
                my_account.data = data;
                Ok(())
            }
        }
        // #[derive(Accounts)]
        // pub struct Initialize {}
        #[derive(Accounts)]
        pub struct Initialize<'info> {
            #[account(init, payer = user, space = 8 + 8)]
            pub my_account: Account<'info, MyAccount>,
            #[account(mut)]
            pub user: Signer<'info>,
            pub system_program: Program<'info, System>,
        }

        #[derive(Accounts)]
        pub struct Update<'info> {
            #[account(mut)]
            pub my_account: Account<'info, MyAccount>,
        }

        #[account]
        pub struct MyAccount {
            pub data: u64,
        }

The test program is as follow:

        import * as anchor from '@project-serum/anchor';
        import { Program } from '@project-serum/anchor';
        import { SolProj1 } from '../target/types/sol_proj_1';
        const assert = require("assert");

        describe('sol_proj_1', () => {

          // Configure the client to use the local cluster.
          const provider = anchor.Provider.local();

           anchor.setProvider(provider);

           
          // The Account to create.
          const myAccount = anchor.web3.Keypair.generate();

          const program = anchor.workspace.SolProj1 as Program<SolProj1>;

          it('Is initialized!', async () => {

            
            // Add your test here.
            const tx = await program.rpc.initialize(new anchor.BN(1234), {
              accounts: {
                myAccount: myAccount.publicKey,
                user: provider.wallet.publicKey,
                systemProgram: program.programId,
              },
              signers: [myAccount],
            });
           
            /
            console.log("Your transaction signature", tx);
          });

          
        });

With error when I run the following command

Anchor test

  1) sol_proj_1
       Is initialized!:
     Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctly
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:3740:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:3700:20)
      at sendAndConfirmRawTransaction (node_modules/@solana/web3.js/src/util/send-and-confirm-raw-transaction.ts:27:21)
      at Provider.send (node_modules/@project-serum/anchor/src/provider.ts:118:18)
      at Object.rpc [as initialize] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:25:23)

I tried the following

  1. change the program ownership as I found that this can cause the issue but that did not work.
  2. I also added entries in Anchor.toml, the tests are running on localhost
  3. I found that empty wallet may also cause this issue but I have airdrop 100 sols in it
  4. The Rust code deploys correctly but the test is failing the 'sanitizationfailure' is listed as follow "Transaction failed to sanitize accounts offsets correctly implies that account locks are not taken for this TX, and should not be unlocked." I couldn't find any info how to take out the locks source: https://docs.rs/solana-sdk/1.9.2/solana_sdk/transaction/enum.TransactionError.html

Any help is appreciated!

The only thing I can see obvious is maybe the way you're passing in the system program from the frontend. You're passing in your program's id, when you should be passing the in system program id. So instead, try:

const tx = await program.rpc.initialize(new anchor.BN(1234), {
     accounts: {
         myAccount: myAccount.publicKey,
         user: provider.wallet.publicKey,
         systemProgram: SystemProgram.programId,
     },
     signers: [myAccount],
});

I had this error before and have solved. In my case, I find that the Program_Id in Anchor.toml is different from lib.rs, they should be the same. Program_Id is generated by running "anchor deploy" in terminal.

Anchor.toml
[programs.localnet]
solana_app = "<Program_ID>"

lib.rs
declare_id!("<Program_ID>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM