簡體   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