Solana Rust(锚):TypeError:无法读取未定义的属性(读取' _bn')
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题与函数调用无关,这是
const name =等待program.account.greetingaccount.fetch();
底部的代码。
相当自我解释,当调用
fetch()
时,我没有传递我要检索的单个帐户的公钥。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.