rust Solana锚错误:无法发送事务:无效事务:事务处理无法正确清理帐户偏移

vi4fp9gy  于 2022-11-24  发布在  其他
关注(0)|答案(3)|浏览(134)

我试图在锚Solana中运行以下代码,程序如下所示:

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.更改程序所有权,因为我发现这可能会导致问题,但没有工作。
1.我还在锚.toml中添加了条目,测试在localhost上运行
1.我发现空钱包也可能导致这个问题,但我有空投100溶胶在它

  1. Rust代码部署正确,但测试失败,“sanitizationfailure”如下所示:“交易未能正确清理帐户偏移,这意味着该TX未采取帐户锁定,不应解锁。”我找不到如何取出锁定源的任何信息:https://docs.rs/solana-sdk/1.9.2/solana_sdk/transaction/enum.TransactionError.html
    任何帮助都是感激不尽的!
plicqrtu

plicqrtu1#

我能看到的唯一明显的一点是你从前端传入系统程序的方式。你传入了你的程序的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],
});
cgh8pdjw

cgh8pdjw2#

我以前遇到过这个错误,现在已经解决了。在我的案例中,我发现锚.toml中的Program_Id与www.example.com中的Program_Id不同lib.rs,它们应该是相同的。Program_Id是通过在终端中运行“anchor deploy”生成的。

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

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

eimct9ow3#

在终端中运行“锚部署”,并将程序id复制到Anchor.toml和lib.rs

相关问题