我目前正在深入研究 Solana 区块链上的编程,并使用 anchor 启动了一个非常基本的程序。在文档中,他们有以下示例 - 请参阅“使用帐户<'a, T>与非锚定程序帐户”:
use anchor_lang::prelude::*;
use anchor_spl::token::TokenAccount;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
mod hello_anchor {
use super::*;
pub fn set_data(ctx: Context<SetData>, data: u64) -> Result<()> {
if ctx.accounts.token_account.amount > 0 {
ctx.accounts.my_account.data = data;
}
Ok(())
}
}
#[account]
#[derive(Default)]
pub struct MyAccount {
data: u64,
mint: Pubkey,
}
#[derive(Accounts)]
pub struct SetData<'info> {
#[account(mut)]
pub my_account: Account<'info, MyAccount>,
#[account(
constraint = my_account.mint == token_account.mint,
has_one = owner
)]
pub token_account: Account<'info, TokenAccount>,
pub owner: Signer<'info>,
}
尝试运行
anchor test
时,它会抛出以下错误:
error[E0599]: no function or associated item named `create_type` found for struct `anchor_spl::token::TokenAccount` in the current scope
--> programs/sol-test/src/lib.rs:46:10 | 46 | #[derive(Accounts)] | ^^^^^^^^ function or associated item not found in `TokenAccount` | = note: this error originates in the derive macro `Accounts` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `anchor_spl::token::TokenAccount: Discriminator` is not satisfied --> programs/sol-test/src/lib.rs:54:39 | 54 | pub token_account: Account<'info, TokenAccount>, | ^^^^^^^^^^^^ the trait `Discriminator` is not implemented for `anchor_spl::token::TokenAccount` | = help: the following other types implement trait `Discriminator`:
MyAccount
__idl::IdlAccount
anchor_lang::ProgramData
anchor_lang::idl::IdlAccount
instruction::SetData
error[E0599]: no function or associated item named `insert_types` found for struct `anchor_spl::token::TokenAccount` in the current scope --> programs/sol-test/src/lib.rs:46:10 | 46 |
#[derive(Accounts)] | ^^^^^^^^ function or associated item not found in `TokenAccount` | = note: this error originates in the derive macro `Accounts` (in Nightly builds, run with -Z macro-ba
cktrace for more info)
文档是否已过时,或者我做错了什么?我的环境是:
找到了答案,但没有很好的记录。虽然文档提到:
要运行此示例,请将anchor-spl =“”添加到依赖项中 Cargo.toml 中的部分,位于 程序//目录。应该等于 您正在使用的锚语言版本。
它没有提到您还需要将
"anchor-spl/idl-build"
添加到 idl-build
中的 Cargo.toml
列表中。我在他们的 GitHub 上的一个类似的issue中发现了这一点。