1

I'm trying to learn solana and currently doing NFTs. I'm wondering if it's possible to create an Associated Token Account with an owner that differs from the signer and then mint an NFT to that ATA? Kind of like an Airdrop. For some reason creating an ATA to use it for a transfer works the way it's supposed to, but when I use that exact same ATA for minting, I get an error. In my .ts file, when I create the ATA, I of course use the payer as the original account that is supposed to sign the transaction and the owner as the account that is supposed to get the tokens.

Also I have a pretty trivial question what authority stands for in Solana programs?

The code of the program where I have the issue:

pub fn mint_token(ctx: Context<MintToken>,) -> Result<()> {

    let cpi_accounts = MintTo {
        mint: ctx.accounts.mint.to_account_info(),
        to: ctx.accounts.token_account.to_account_info(),
        authority: ctx.accounts.authority.to_account_info(),
    };
    
    let cpi_program = ctx.accounts.token_program.to_account_info();
    let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);

    token::mint_to(cpi_ctx, 1)?;
    
    Ok(())
}

#[derive(Accounts)]
pub struct MintToken<'info> {
    /// CHECK: This is the token that we want to mint
    #[account(mut)]
    pub mint: UncheckedAccount<'info>,
    pub token_program: Program<'info, Token>,
    /// CHECK: This is the token account that we want to mint tokens to
    #[account(mut)]
    pub token_account: UncheckedAccount<'info>,
    /// CHECK: the authority of the mint account
    #[account(mut)]
    pub authority: AccountInfo<'info>,
}
3
  • 1
    authority is just another Account based on your code. However in most of my programs authority has to be type Signer<> so maybe your error is coming from that. you can name it to anything you like doesn't have to be authority e.g. pub user_signer: AccountInfo<> Jul 20, 2022 at 15:03
  • 1
    Check this out for more on Accounts: solanacookbook.com/core-concepts/accounts.html#deep-dive Jul 20, 2022 at 15:03
  • providing the error message in the question description would help others find solutions to their problem as well as improve search engine indexing
    – trent.sol
    Jul 20, 2022 at 20:07

2 Answers 2

3

Taken from the Solana Program Library docs (bottom of this page), an associated token account can be created for a given wallet by anyone.

Whether you use Anchor's init annotation or the instruction struct (anchor_spl::associated_token::Create) to create an ATA, the essential fields are payer and authority.

Here's a snippet using init:

/// CHECK: This is for airdrops
pub recipient: UncheckedAccount<'info>,
#[account(
    init,
    payer = alice,
    associated_token::mint = mint_account,
    associated_token::authority = recipient,
)]
pub token_account: Account<'info, TokenAccount>,
#[account(mut)]
pub alice: Signer<'info>,

As long as the payer is the signer, you can create the token account for any public key (ie. recipient).

However, it's worth noting that the authority field makes sure that no one can debit that token account except for the authority.

In other words, if Alice creates a token account for Bob, she can mint tokens to Bob's token account all she wants (that's how airdrops work), but she can never withdraw tokens from Bob's token account.

1

A ATA can be created from a different owner if this owner is able to sign the transaction. This can easily be done with Signer<'info> rather than AccountInfo<'info>

#[derive(Accounts)]
pub struct MintToken<'info> {
    /// CHECK: This is the token that we want to mint
    #[account(mut)]
    pub mint: UncheckedAccount<'info>,
    pub token_program: Program<'info, Token>,
    /// CHECK: This is the token account that we want to mint tokens to
    #[account(mut)]
    pub token_account: UncheckedAccount<'info>,
    /// CHECK: the authority of the mint account
    #[account(mut)]
    pub authority: Signer<'info>,
}
  

Also, authority stands for the authority that has permission to remove lamports/tokens from the account

1
  • 1
    to add a bit more context, "authority" is used as a term of art for the account that is authorized to perform actions for system and token accounts, and people will often use the same word for the same concept in their own programs. this word is used because "owner" refers to the program that owns and thus has write permissions on the account
    – hana
    Jul 21, 2022 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.