Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mint:: account constraint does not work #1342

Closed
EP-u-NW opened this issue Jan 19, 2022 · 3 comments
Closed

mint:: account constraint does not work #1342

EP-u-NW opened this issue Jan 19, 2022 · 3 comments
Labels
question Further information is requested

Comments

@EP-u-NW
Copy link

EP-u-NW commented Jan 19, 2022

I'm trying to init a mint account and then mint to an associated token account, but my code won't compile.

I'm using anchor 0.20.1, below is my code:

use crate::program::Example;
use anchor_lang::prelude::*;
use anchor_spl::{
    associated_token::AssociatedToken,
    token::{Mint, Token, TokenAccount},
};

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod example {
    use super::*;
    pub fn mint_request(ctx: Context<MintRequest>) -> ProgramResult {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct MintRequest<'info> {
    #[account(
        init,
        payer = minter,
        // I'd like my program as authority
        // and since mint::authority = <target_account>,
        // and per documentation target_account needs to be
        // part of this struct, I introduced this_program.
        mint::authority = this_program,
        mint::decimals = 0,
    )]
    pub mint_account: Account<'info, Mint>,
    #[account(
        init,
        payer = minter,
        associated_token::mint=mint_account,
        associated_token::authority = minter,
    )]
    pub token: Account<'info, TokenAccount>,
    #[account(mut)]
    pub minter: Signer<'info>,
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub this_program: Program<'info, Example>,
}

The problem seems to be #[derive(Accounts)]. anchor build results in

developer@7c961325b3bd:~/solana/example/programs/example/src$ anchor build
BPF SDK: /home/developer/.local/share/solana/install/releases/1.9.1/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
   Compiling example v0.1.0 (/home/developer/solana/example/programs/example)
error[E0425]: cannot find value `rent` in this scope
  --> programs/example/src/lib.rs:18:10
   |
18 | #[derive(Accounts)]
   |          ^^^^^^^^ not found in this scope
   |
   = note: this error originates in the derive macro `Accounts` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0425`.
error: could not compile `example` due to previous error

I'f I remove the account with the mint:: constraint, this example will still not work. In order to get it compiled I also need to remove the init constraint on the token account (but not the associated_token::). There, I can remove the init constraint, but the mint:: constraint requires the init constraint, so removing it there is not possible.

@fanatid
Copy link
Contributor

fanatid commented Jan 19, 2022

You need add rent: Sysvar<'info, Rent>, to MintRequest. Related with #1330.

@fanatid fanatid added the question Further information is requested label Jan 19, 2022
@EP-u-NW
Copy link
Author

EP-u-NW commented Jan 19, 2022

Thanks for the very fast answer, that fixed it! Unfortunately, the docs didn't mention that 😅

@fanatid
Copy link
Contributor

fanatid commented Jan 19, 2022

Yeah, unfortunately. But you can help by submitting pull request for #1330 😉

@fanatid fanatid closed this as completed Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant