How to transfer (via Program) custom SPL token from my own account to another user's wallet?

  1. I have created a wallet > solana-keygen new
  2. I have created my own custom SPL > Tokenspl-token create-token
  3. Then I created an Account for this SPL > Tokenspl-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.

Hi @tschew and welcome to the forum! :wave:

It depends on how you decide when to give out the reward.

For instance, you could have a traditional non-blockchain application that you use to run the quiz, and then when somebody get’s all the answers right, it simply uses the Solana web3.js API to transfer the tokens. In this scenario, you would give your web application server the wallet key for wallet A, so that it could sign the transaction to spend the tokens from wallet A.

I wouldn’t recommend putting quiz logic inside of a Solana program, because anybody could actually run a simulation of any transaction and test the answers to the quiz before submitting them to the actual Solana cluster.

If you aren’t actually doing a quiz or else you do want to have the Solana Program choose whether or not to distribute your token, then I think it would be easiest to store the reward tokens in an account owned by the Solana Program.

I haven’t done that with SPL tokens before, so I’d have to investigate more to figure out exactly how to do it. But, if you did that, then Solana Program would automatically have authorization to spend any amount of tokens from the address.

1 Like

Thanks @zicklag
Thanks for validating the approach!
Yes, I think it would be great if there is some working examples that can demonstrate how we can make our Solana Program as the owner of the Token Account. I believe it requires Cross-Program Invocations Calling Between Programs | Solana Docs. But I am not sure how to do this properly. If only I can find a sample code end-to-end to demonstrate this use case…

Please let me know if you have figured out.

Sincerely appreciate.