如何将自定义 SPL 代币从我自己的帐户转移(使用程序指令)到另一个用户的钱包?

发布于 2025-01-13 09:28:45 字数 415 浏览 0 评论 0原文

这是我的情况:

  1. 我创建了一个钱包

    solana-keygen 新

  2. 我创建了自己的自定义 SPL 令牌

    spl-token 创建令牌

  3. 然后我为此 SPL 令牌创建了一个帐户

    spl-令牌创建帐户

  4. SPL 代币现在在我的钱包 A 中

在 Solana 计划中,当满足某些条件时(例如,当 Alice 正确回答测验时,她会以编程方式将自定义 SPL 代币从钱包 A 转移到 Alice(用户)钱包中)将获得一些自定义 SPL 代币)。

如何授权 Solana 程序从我创建的钱包 A 中扣除代币并将代币转移到 Alice 钱包?

请告诉我如何去做这件事。真的很感激这一点。

This is my situation:

  1. I have created a wallet

    solana-keygen new

  2. I have created my own custom SPL Token

    spl-token create-token

  3. Then I created an Account for this SPL Token

    spl-token create-account

  4. The SPL token is now in my wallet A

In the Solana Program, I would like to programmatically transfer the custom SPL token from Wallet A to Alice(user) wallet when certain conditions are met (for example, when Alice answered a quiz correctly, she will be awarded some custom SPL token).

How do I authorise the Solana Program to deduct from Wallet A (which I had created) and transfer the tokens to Alice wallet?

Please advise me how to go about doing this. Really appreciate this.

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

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

发布评论

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

评论(1

陈年往事 2025-01-20 09:28:45

要在程序内转移 SPL 代币,最好的选择是让钱包 A 由程序派生地址拥有,然后您的程序可以根据它想要的任何逻辑从钱包 A 转移代币。

因此,首先,将所有权转移到您的程序派生地址:

spl-token authorize <WALLET_2_ADDRESS> owner <PROGRAM_DERIVED_ADDRESS>

然后在您的程序中,您可以使用以下内容转移给 Alice:

let transfer_instruction = spl_token::instruction::transfer(
    &token_program.key,
    &wallet_a_token_account.key,
    &alice_token_account.key,
    &program_derived_account.key,
    &[],
    transfer_amount,
)?;

let required_accounts_for_transfer = [
    wallet_a_token_account.clone(),
    alice_token_account.clone(),
    program_derived_account.clone(),
];

invoke_signed(
    &transfer_instruction,
    &required_accounts_for_transfer,
    &[
        &[b"your", b"seeds", b"here",]
    ]
)?;

这是根据在程序中转移 SPL 代币的完整示例改编的: https://solanacookbook.com/references/programs.html#how-to-do-cross-program-inspiration

有关程序派生地址的更多信息,请访问 https://solanacookbook.com/references/programs.html#how-to-create-a-pda,包含如何创建帐户的示例。

To transfer an SPL token within a program, your best option is to have wallet A owned by a program-derived address, and then your program can transfer the tokens from wallet A based on any logic it wants.

So first, transfer the ownership to your program-derived address:

spl-token authorize <WALLET_2_ADDRESS> owner <PROGRAM_DERIVED_ADDRESS>

Then in your program, you can transfer to Alice with something like:

let transfer_instruction = spl_token::instruction::transfer(
    &token_program.key,
    &wallet_a_token_account.key,
    &alice_token_account.key,
    &program_derived_account.key,
    &[],
    transfer_amount,
)?;

let required_accounts_for_transfer = [
    wallet_a_token_account.clone(),
    alice_token_account.clone(),
    program_derived_account.clone(),
];

invoke_signed(
    &transfer_instruction,
    &required_accounts_for_transfer,
    &[
        &[b"your", b"seeds", b"here",]
    ]
)?;

This was adapted from a full example for transferring SPL tokens within a program: https://solanacookbook.com/references/programs.html#how-to-do-cross-program-invocation

More information about program-derived addresses at https://solanacookbook.com/references/programs.html#how-to-create-a-pda, with an example of how to create an account.

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