rustsolanaanchor-solana

TokenAccount doesn't implement Discriminator


I am currently diving into programming on the Solana blockchain and started a very basic program with anchor. In the documentation they have the following example - see "Using Account<'a, T> with non-anchor program accounts":

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>,
}

When trying to run anchor test it throws the following errors:

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
    |
    | #[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
    |
    | 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
    |
    | #[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)

Is the documentation out of date or is there something i am doing wrong? My environment is:


Solution

  • Found the answer, but it is not very well documented. While the documentation mentions that:

    To run this example, add anchor-spl = "" to the dependencies section in your Cargo.toml, located in the programs// directory. should be equal to the anchor-lang version you're using.

    It fails to mention that you also need to add "anchor-spl/idl-build" to the idl-build list in the Cargo.toml. I found this mentioned in a similar issue on their GitHub.