简体   繁体   English

Solana 无法铸造到不同的代币账户

[英]Solana can't mint to a different token account

Folks, I'm starting with Solana and it's been rough to learn.伙计们,我从 Solana 开始,学习起来很艰难。 Despite the poor error messages and the learning curve of Rust, I'm making my way through it.尽管错误消息和 Rust 的学习曲线很糟糕,但我正在努力解决它。

I'm trying to develop a new token that will be a fungible asset (0 decimals, supply greater than 1 for the same token).我正在尝试开发一种新的代币,它将成为一种可替代的资产(0 位小数,同一代币的供应量大于 1)。

In another transaction, I already created a mint account and initialized it.在另一笔交易中,我已经创建了一个铸币账户并对其进行了初始化。 Now I'm trying to mint that to another wallet.现在我正试图将其铸造到另一个钱包。 Basically, this is mint accounts context (for simplicity reasons I've excluded the metadata accounts):基本上,这是 mint 帐户上下文(出于简单原因,我排除了元数据帐户):

pub struct MintAsset<'info> {
    #[account(mut)]
    pub mint: Account<'info, token::Mint>,
    #[account(mut)]
    pub mint_authority: Signer<'info>,
    /// CHECK: We're about to create this with Anchor
    #[account(mut)]
    pub minter_token: UncheckedAccount<'info>,
    #[account(mut)]
    pub payer: Signer<'info>,
    pub rent: Sysvar<'info, Rent>,
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, token::Token>,
    pub associated_token_program: Program<'info, associated_token::AssociatedToken>,
}

Then, I proceed to run this transaction for minting my tokens然后,我继续运行此交易来铸造我的代币

pub fn mint_asset(ctx: Context<MintAsset>, data: MintArgs) -> Result<()> {   
        associated_token::create(
            CpiContext::new(
                ctx.accounts.associated_token_program.to_account_info(),
                associated_token::Create {
                    payer: ctx.accounts.mint_authority.to_account_info(),
                    associated_token: ctx.accounts.minter_token.to_account_info(),
                    authority: ctx.accounts.mint_authority.to_account_info(),
                    mint: ctx.accounts.mint.to_account_info(),
                    system_program: ctx.accounts.system_program.to_account_info(),
                    token_program: ctx.accounts.token_program.to_account_info(),
                    rent: ctx.accounts.rent.to_account_info(),
                },
            ),
        )?;

        token::mint_to(
            CpiContext::new(
                ctx.accounts.token_program.to_account_info(),
                token::MintTo {
                    mint: ctx.accounts.mint.to_account_info(),
                    to: ctx.accounts.minter_token.to_account_info(),
                    authority: ctx.accounts.mint_authority.to_account_info(),
                },
            ),
            data.amount,
        )?;

        Ok(())
    }

By using the following test:通过使用以下测试:

async function mintToken(
    program: anchor.Program<Pdas>,
    wallet: anchor.Wallet,
    mintKeypair: anchor.web3.Keypair,
    minterWallet: anchor.Wallet,
    amount: number
  ) {
    try {
      
      const buyerTokenAddress = await anchor.utils.token.associatedAddress({
        mint: mintKeypair.publicKey,
        owner: wallet.publicKey,
      });
  
      const accounts = {
        mint: mintKeypair.publicKey,
        mintAuthority: wallet.publicKey,
        minterToken: buyerTokenAddress,
        payer: wallet.publicKey,
      };
  
      const signers = [];
  
      const args = {
        amount: new BN(amount),
      };
  
      const signature = await program.methods
        .mintAsset(args)
        .accounts(accounts)
        .signers(signers)
        .rpc();
    } catch (error) {
      console.log("MINT ERROR:", inspect({ error }, { depth: null }));
    }
  }

Now, that test throws an error if for the buyerTokenAddress I use the minterWallet as owner.现在,如果对于buyerTokenAddress我使用minterWallet作为所有者,该测试会抛出错误。 I understand that for owning a token, one must have an associated token account, which, as stated by the documentation, is an我明白要拥有一个代币,一个人必须有一个关联的代币账户,正如文档所述,这是一个

account for a given wallet address is simply a program-derived account consisting of the wallet address itself and the token mint给定钱包地址的账户只是一个程序派生账户,由钱包地址本身和代币铸币厂组成

https://spl.solana.com/associated-token-account https://spl.solana.com/associated-token-account

Why's that?为什么? I mean, doesn't anyone can mint a this token?我的意思是,没有人可以铸造这个令牌吗? I understand that only the mintAuthority can mint the tokens, but it makes sense to have it as a signer (as accounts struct expect), but (and here's another question) if I put an empty array of signers, the code still runs (again, why is that? I thought I had to provide an signer account at least for mint_authority ).我知道只有mintAuthority可以铸造令牌,但是将它作为签名者(如帐户结构所期望的那样)是有意义的,但是(这是另一个问题)如果我放置一个空的签名者数组,代码仍然运行(再次,这是为什么呢?我想我必须至少为mint_authority提供一个签名者帐户)。

Should I create a new mint account and initialize it?我应该创建一个新的薄荷帐户并初始化它吗? Wouldn't that be a new token, instead?那不是一个新的令牌吗?

What is obvious in Solana token development that I'm missing here?在 Solana 令牌开发中,我在这里缺少什么? Thanks in advance提前致谢

I may not be able to answer your first question properly, so I'm leaving that for someone more experienced.我可能无法正确回答您的第一个问题,所以我将把它留给更有经验的人。 For your second question you are right about signer, as you are using anchor it implicitly takes owner wallet from configuration (anchor.toml file) and signs it.对于你的第二个问题,你对签名者的看法是正确的,因为你使用的是锚点,它隐式地从配置(anchor.toml 文件)中获取所有者钱包并对其进行签名。 if you provide your wallet.key in signer array there will be no difference.如果您在签名者数组中提供 wallet.key,则不会有任何区别。 because anchor does that so generally that isn't provided in signers[]因为 anchor 通常会这样做,所以 signers[] 中没有提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM