Solana Rust(锚):TypeError:无法读取未定义的属性(读取' _bn')

发布于 2025-02-12 18:34:26 字数 3108 浏览 1 评论 0原文

我正在尝试使用React构建一个简单的应用程序,该应用程序与我的Solana Smart合同相互作用。当我单击调用getMsg()函数的< button>时,显示以下错误。

TypeError: Cannot read properties of undefined (reading '_bn')
    at isPublicKeyData (index.browser.esm.js?58d5:1777:1)
    at new PublicKey (index.browser.esm.js?58d5:1795:1)
    at T (index.js?c657:1:1828)
    at An.getAccountInfo (index.js?c657:16:47072)
    at An.fetchNullable (index.js?c657:16:45149)
    at An.fetch (index.js?c657:16:45284)
    at _callee3$ (index.js?44d8:57:58)
    at tryCatch (regeneratorRuntime.js?7ec2:86:1)
    at Generator.eval [as _invoke] (regeneratorRuntime.js?7ec2:66:1)
    at Generator.eval [as next] (regeneratorRuntime.js?7ec2:117:1)
    at asyncGeneratorStep (asyncToGenerator.js?1da1:3:1)
    at _next (asyncToGenerator.js?1da1:25:1)

合同

use anchor_lang::prelude::*;

declare_id!( <=== omitted ===> );

#[program]
pub mod greeting {

    use super::*;

    pub fn execute(ctx: Context<Execute>, name: String) -> Result<()> {
        let gm_account = &mut ctx.accounts.gm_account;
        gm_account.name = name;
        msg!("hello {}", gm_account.name);
        Ok(())
    }

}

#[derive(Accounts)]
pub struct Execute<'info> {
    #[account(init, payer = user, space = 8 + 32)]
    pub gm_account: Account<'info, GreetingAccount>,

    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
pub struct GreetingAccount {
    pub name: String,
}

反应:

  const programId = new PublicKey(idl.metadata.address);

  const getMsg = async () => {
    try {
      const provider = getProvider();
      const program = new Program(idl, programId, provider);
      await program.rpc.execute("a test message", {
        accounts: {
          gmAccount: greetingAccount.publicKey,
          user: provider.wallet.publicKey,
          systemProgram: SystemProgram.programId
        },
        signers: [greetingAccount]
      });

      const name = await program.account.greetingAccount.fetch();
    } catch (e) {
      console.error(e);
    }
  };

IDL

{
  "version": "0.1.0",
  "name": "greeting",
  "instructions": [
    {
      "name": "execute",
      "accounts": [
        {
          "name": "gmAccount",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "user",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "systemProgram",
          "isMut": false,
          "isSigner": false
        }
      ],
      "args": [
        {
          "name": "name",
          "type": "string"
        }
      ],
      "returns": null
    }
  ],
  "accounts": [
    {
      "name": "GreetingAccount",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "name",
            "type": "string"
          }
        ]
      }
    }
  ],
  "metadata": {
    "address": " <== omitted ==> "
  }
}

I'm trying to build a simple application with React that interacts with my Solana smart contract. When I click the <button> which calls getMsg() function, the following error is displayed.

TypeError: Cannot read properties of undefined (reading '_bn')
    at isPublicKeyData (index.browser.esm.js?58d5:1777:1)
    at new PublicKey (index.browser.esm.js?58d5:1795:1)
    at T (index.js?c657:1:1828)
    at An.getAccountInfo (index.js?c657:16:47072)
    at An.fetchNullable (index.js?c657:16:45149)
    at An.fetch (index.js?c657:16:45284)
    at _callee3$ (index.js?44d8:57:58)
    at tryCatch (regeneratorRuntime.js?7ec2:86:1)
    at Generator.eval [as _invoke] (regeneratorRuntime.js?7ec2:66:1)
    at Generator.eval [as next] (regeneratorRuntime.js?7ec2:117:1)
    at asyncGeneratorStep (asyncToGenerator.js?1da1:3:1)
    at _next (asyncToGenerator.js?1da1:25:1)

Contract

use anchor_lang::prelude::*;

declare_id!( <=== omitted ===> );

#[program]
pub mod greeting {

    use super::*;

    pub fn execute(ctx: Context<Execute>, name: String) -> Result<()> {
        let gm_account = &mut ctx.accounts.gm_account;
        gm_account.name = name;
        msg!("hello {}", gm_account.name);
        Ok(())
    }

}

#[derive(Accounts)]
pub struct Execute<'info> {
    #[account(init, payer = user, space = 8 + 32)]
    pub gm_account: Account<'info, GreetingAccount>,

    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
pub struct GreetingAccount {
    pub name: String,
}

React:

  const programId = new PublicKey(idl.metadata.address);

  const getMsg = async () => {
    try {
      const provider = getProvider();
      const program = new Program(idl, programId, provider);
      await program.rpc.execute("a test message", {
        accounts: {
          gmAccount: greetingAccount.publicKey,
          user: provider.wallet.publicKey,
          systemProgram: SystemProgram.programId
        },
        signers: [greetingAccount]
      });

      const name = await program.account.greetingAccount.fetch();
    } catch (e) {
      console.error(e);
    }
  };

Idl

{
  "version": "0.1.0",
  "name": "greeting",
  "instructions": [
    {
      "name": "execute",
      "accounts": [
        {
          "name": "gmAccount",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "user",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "systemProgram",
          "isMut": false,
          "isSigner": false
        }
      ],
      "args": [
        {
          "name": "name",
          "type": "string"
        }
      ],
      "returns": null
    }
  ],
  "accounts": [
    {
      "name": "GreetingAccount",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "name",
            "type": "string"
          }
        ]
      }
    }
  ],
  "metadata": {
    "address": " <== omitted ==> "
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2025-02-19 18:34:26

问题与函数调用无关,这是
const name =等待program.account.greetingaccount.fetch();
底部的代码。

相当自我解释,当调用fetch()时,我没有传递我要检索的单个帐户的公钥。

.fetch(greetingAccount.publicKey); 

The problem was nothing to do with the function call, it was the
const name = await program.account.greetingAccount.fetch();
code towards the bottom.

Pretty self explanatory, when calling fetch() I didn't pass in the public key of the single account I was trying to retrieve.

.fetch(greetingAccount.publicKey); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文