From f3e5b84443e1a7493e42c9875bbe66abe457d784 Mon Sep 17 00:00:00 2001 From: drseu55 Date: Fri, 11 Mar 2022 19:39:35 +0200 Subject: [PATCH] add anchor_spl crate in code and toml file (#32) --- src/chapter_3/the_accounts_struct.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/chapter_3/the_accounts_struct.md b/src/chapter_3/the_accounts_struct.md index 1f1a2e3..98d8d2e 100644 --- a/src/chapter_3/the_accounts_struct.md +++ b/src/chapter_3/the_accounts_struct.md @@ -51,6 +51,7 @@ There may be cases where you want your program to interact with a non-Anchor pro ```rust,ignore use anchor_lang::prelude::*; +use anchor_spl::token::TokenAccount; declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); @@ -85,6 +86,8 @@ pub struct SetData<'info> { } ``` +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. + In this example, we set the `data` field of an account if the caller has admin rights. We decide whether the caller is an admin by checking whether they own admin tokens for the account they want to change. We do most of this via constraints which we will look at in the next section. The important thing to take away is that we use the `TokenAccount` type (that wraps around the token program's `Account` struct and adds the required functions) to make anchor ensure that the incoming account is owned by the token program and to make anchor deserialize it. This means we can use the `TokenAccount` properties inside our constraints (e.g. `token_account.mint`) as well as in the instruction function.