'program' 宏未正确加载
'program' macro not correctly loading
我在编译我的 Rust 程序时遇到问题,当我 运行 anchor 时,我收到指向 program 宏的错误测试。是什么导致了这个问题?我尝试了 运行ning cargo check 但得到了相同的结果。
lib.rs
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod mycalculatorapp {
use super::*;
pub fn create(ctx: Context<Create>, init_message: String ) -> Result<()> {
let calculator = &mut ctx.accounts.calculator;
calculator.greeting = init_message;
Ok(())
}
#[derive(Accounts)]
pub struct Create<'info> {
// #[account(init, payer=user, space=264)]
// pub calculator: Account<'info, Calculator>,
#[account(mut)]
pub user: Signer<'info>,
pub systems_program: Program<'info, System>
}
#[account]
pub struct Calculator {
pub greeting: String,
pub result: i64,
pub remainder: i64
}
错误:
error[E0432]: unresolved import `crate`
--> programs/mycalculatorapp/src/lib.rs:5:1
|
| #[program]
| ^^^^^^^^^^ could not find `__client_accounts_create` in the crate root
|
error[E0599]: no function or associated item named `try_accounts` found for struct `Create` in the current scope
--> programs/mycalculatorapp/src/lib.rs:5:1
|
| #[program]
| ^^^^^^^^^^ function or associated item not found in `Create<'_>`
...
| pub struct Create<'info> {
| ------------------------ function or associated item `try_accounts` not found for this
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `try_accounts`, perhaps you need to implement it:
candidate #1: `anchor_lang::Accounts`
= note: this error originates in the attribute macro `program` (in Nightly builds, run with -Z macro-backtrace for more info)
您的代码应更改为
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod mycalculatorapp {
use super::*;
pub fn create(ctx: Context<Create>, init_message: String ) -> Result<()> {
let calculator = &mut ctx.accounts.calculator;
calculator.greeting = init_message;
Ok(())
}
#[derive(Accounts)]
pub struct Create<'info> {
#[account(init, payer=user, space=264)]
pub calculator: Account<'info, Calculator>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>
}
#[account]
pub struct Calculator {
pub greeting: String,
pub result: i64,
pub remainder: i64
}
由于类型而不是 systems_program
你需要 system_program
也提高了 github 的 PR。 https://github.com/devd-99/calculatordapp/pulls/1
随时合并结帐。
我在编译我的 Rust 程序时遇到问题,当我 运行 anchor 时,我收到指向 program 宏的错误测试。是什么导致了这个问题?我尝试了 运行ning cargo check 但得到了相同的结果。
lib.rs
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod mycalculatorapp {
use super::*;
pub fn create(ctx: Context<Create>, init_message: String ) -> Result<()> {
let calculator = &mut ctx.accounts.calculator;
calculator.greeting = init_message;
Ok(())
}
#[derive(Accounts)]
pub struct Create<'info> {
// #[account(init, payer=user, space=264)]
// pub calculator: Account<'info, Calculator>,
#[account(mut)]
pub user: Signer<'info>,
pub systems_program: Program<'info, System>
}
#[account]
pub struct Calculator {
pub greeting: String,
pub result: i64,
pub remainder: i64
}
错误:
error[E0432]: unresolved import `crate`
--> programs/mycalculatorapp/src/lib.rs:5:1
|
| #[program]
| ^^^^^^^^^^ could not find `__client_accounts_create` in the crate root
|
error[E0599]: no function or associated item named `try_accounts` found for struct `Create` in the current scope
--> programs/mycalculatorapp/src/lib.rs:5:1
|
| #[program]
| ^^^^^^^^^^ function or associated item not found in `Create<'_>`
...
| pub struct Create<'info> {
| ------------------------ function or associated item `try_accounts` not found for this
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `try_accounts`, perhaps you need to implement it:
candidate #1: `anchor_lang::Accounts`
= note: this error originates in the attribute macro `program` (in Nightly builds, run with -Z macro-backtrace for more info)
您的代码应更改为
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod mycalculatorapp {
use super::*;
pub fn create(ctx: Context<Create>, init_message: String ) -> Result<()> {
let calculator = &mut ctx.accounts.calculator;
calculator.greeting = init_message;
Ok(())
}
#[derive(Accounts)]
pub struct Create<'info> {
#[account(init, payer=user, space=264)]
pub calculator: Account<'info, Calculator>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>
}
#[account]
pub struct Calculator {
pub greeting: String,
pub result: i64,
pub remainder: i64
}
由于类型而不是 systems_program
你需要 system_program
也提高了 github 的 PR。 https://github.com/devd-99/calculatordapp/pulls/1
随时合并结帐。