如何正确使用`close_account`函数以关闭关联的令牌帐户?

发布于 2025-02-10 04:15:27 字数 1298 浏览 2 评论 0原文

我正在尝试从程序内部关闭关联的令牌帐户(ATA)。 ATA属于该程序。我找到了一个称为colled_account的函数,但是我还没有弄清楚如何正确使用它。我正在使用锚。

我程序的所需流量是:

  1. 将令牌从程序ATA发送给用户ATA(我已经成功完成了此操作)
  2. 关闭用于令牌的程序ATA,

这是colled_account 看起来像:

pub fn close_account<'a, 'b, 'c, 'info>(
    ctx: CpiContext<'a, 'b, 'c, 'info, CloseAccount<'info>>,
) -> Result<()> {
    let ix = spl_token::instruction::close_account(
        &spl_token::ID,
        ctx.accounts.account.key,
        ctx.accounts.destination.key,
        ctx.accounts.authority.key,
        &[], // TODO: support multisig
    )?;
    solana_program::program::invoke_signed(
        &ix,
        &[
            ctx.accounts.account.clone(),
            ctx.accounts.destination.clone(),
            ctx.accounts.authority.clone(),
        ],
        ctx.signer_seeds,
    )
    .map_err(Into::into)
}

colleeaccount结构如下:(

#[derive(Accounts)]
pub struct CloseAccount<'info> {
    pub account: AccountInfo<'info>,
    pub destination: AccountInfo<'info>,
    pub authority: AccountInfo<'info>,
}

我假设)帐户是ATA,权威是我的程序 - 但是在这种情况下, destination 是什么?为什么关闭帐户需要一个目的地,我应该将哪个帐户用作目标帐户?

I am trying to close an Associated Token Account (ATA) from inside a program. The ATA belongs to the program. I found a function called close_account, but I haven't figured out how to use it properly. I'm using Anchor.

The desired flow of my program is:

  1. Send a token from the program ATA to the user ATA (I've done this successfully)
  2. Close the program ATA that was used for the token

This is what the implementation of close_account looks like:

pub fn close_account<'a, 'b, 'c, 'info>(
    ctx: CpiContext<'a, 'b, 'c, 'info, CloseAccount<'info>>,
) -> Result<()> {
    let ix = spl_token::instruction::close_account(
        &spl_token::ID,
        ctx.accounts.account.key,
        ctx.accounts.destination.key,
        ctx.accounts.authority.key,
        &[], // TODO: support multisig
    )?;
    solana_program::program::invoke_signed(
        &ix,
        &[
            ctx.accounts.account.clone(),
            ctx.accounts.destination.clone(),
            ctx.accounts.authority.clone(),
        ],
        ctx.signer_seeds,
    )
    .map_err(Into::into)
}

The CloseAccount struct looks as follows:

#[derive(Accounts)]
pub struct CloseAccount<'info> {
    pub account: AccountInfo<'info>,
    pub destination: AccountInfo<'info>,
    pub authority: AccountInfo<'info>,
}

(I assume) the account is the ATA and the authority is my program - but what is the destination in this context? Why would closing an account need a destination and which account should I use as the destination account?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-02-17 04:15:27

在这种情况下的目的地是什么?为什么关闭帐户需要目的地,我应该用哪个帐户作为目标帐户?

由于令牌帐户与Solana中的任何其他帐户一样,它们必须具有足够的溶胶来满足最低余额要求。关闭帐户时,您必须将该溶胶发送到其他地方,因此“目的地”。最好的选择是使用仅用于SOL而不使用的钱包 / SOL帐户。

更多信息 https://spl.solana.com/token#closing-closing-closing-closing-closing-accounts

what is the destination in this context? Why would closing an account need a destination and which account should I use as the destination account?

Since token accounts are like any other account in Solana, they must have enough SOL to cover the minimum balance requirement. When closing an account, you must send that SOL somewhere else, hence the "destination". The best option is to use a wallet / SOL account that is only used for SOL and nothing else.

More info at https://spl.solana.com/token#closing-accounts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文