繁体   English   中英

Solana 锚点错误:发送交易失败:交易无效:交易未能正确清理账户偏移

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

我正在尝试在 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

任何帮助表示赞赏!

我唯一能看到的明显可能是您从前端传递系统程序的方式。 当您应该传递系统程序 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],
});

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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