【问题标题】:Solana Anchor Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctlySolana Anchor 错误:发送交易失败:交易无效:交易未能正确清理账户偏移
【发布时间】:2022-11-13 04:54:08
【问题描述】:

我正在尝试在 Anchor Solana 中运行以下代码,程序在 rust 中如下所示:

        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,
        }

测试程序如下:

        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);
          });

          
        });

运行以下命令时出现错误

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)

我尝试了以下

  1. 更改程序所有权,因为我发现这可能会导致问题,但没有奏效。
  2. 我还在 Anchor.toml 中添加了条目,测试在 localhost 上运行
  3. 我发现空钱包也可能导致这个问题,但我有空投100 sols
  4. Rust 代码正确部署,但测试失败,“清理失败”列出如下“交易未能正确清理帐户偏移量意味着此 TX 未采取帐户锁定,不应解锁。”我找不到任何信息如何取出锁 来源:https://docs.rs/solana-sdk/1.9.2/solana_sdk/transaction/enum.TransactionError.html

    任何帮助表示赞赏!

【问题讨论】:

    标签: rust anchor solana


    【解决方案1】:

    我唯一能看到的明显可能是您从前端传递系统程序的方式。当您应该传递系统程序 id 时,您正在传递程序的 id。因此,请尝试:

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

    【讨论】:

      【解决方案2】:

      我之前遇到过这个错误并且已经解决了。 就我而言,我发现 Anchor.toml 中的 Program_Id 与 lib.rs 不同,它们应该是相同的。 Program_Id 是通过在终端中运行“anchor deploy”生成的。

      Anchor.toml
      [programs.localnet]
      solana_app = "<Program_ID>"
      
      lib.rs
      declare_id!("<Program_ID>");
      

      【讨论】:

        【解决方案3】:

        在终端中运行“anchor deploy”。 并在 Anchor.toml 和 lib.rs 中复制程序 ID

        【讨论】:

          猜你喜欢
          • 2017-07-15
          • 2015-09-18
          • 2014-04-09
          • 2021-05-11
          • 1970-01-01
          • 2015-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多