如何将自定义 SPL 代币从我自己的帐户转移(使用程序指令)到另一个用户的钱包?
这是我的情况:
我创建了一个钱包
solana-keygen 新
我创建了自己的自定义 SPL 令牌
spl-token 创建令牌
然后我为此 SPL 令牌创建了一个帐户
spl-令牌创建帐户
SPL 代币现在在我的钱包 A 中
在 Solana 计划中,当满足某些条件时(例如,当 Alice 正确回答测验时,她会以编程方式将自定义 SPL 代币从钱包 A 转移到 Alice(用户)钱包中)将获得一些自定义 SPL 代币)。
如何授权 Solana 程序从我创建的钱包 A 中扣除代币并将代币转移到 Alice 钱包?
请告诉我如何去做这件事。真的很感激这一点。
This is my situation:
I have created a wallet
solana-keygen new
I have created my own custom SPL Token
spl-token create-token
Then I created an Account for this SPL Token
spl-token create-account
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在程序内转移 SPL 代币,最好的选择是让钱包 A 由程序派生地址拥有,然后您的程序可以根据它想要的任何逻辑从钱包 A 转移代币。
因此,首先,将所有权转移到您的程序派生地址:
然后在您的程序中,您可以使用以下内容转移给 Alice:
这是根据在程序中转移 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:
Then in your program, you can transfer to Alice with something like:
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.