简体   繁体   English

如何使用 solana rust 合约发送 SOL

[英]How to send SOL with a solana rust contract

I'm a noob Rust/Solana Developer and feel like I'm encountering an issue that would be helpful to most other noobs.我是一名 Rust/Solana 开发人员,我觉得我遇到了一个对大多数其他新手都有帮助的问题。

I want to how to transfer SOL from an account to a program during an instruction, and then be able to send SOL back to the account who called the instruction.我想知道如何在指令期间将 SOL 从帐户转移到程序,然后能够将 SOL 发送回调用指令的帐户。

I read the https://docs.solana.com/ but I could not find anything about SOL transfers via programs.我阅读了https://docs.solana.com/但我找不到任何关于通过程序转移 SOL 的信息。

Example:例子:

Acc1 calls the gamble instruction in Program1, which transfers 1 SOL from the caller and then runs a verifiable random function. Acc1 调用 Program1 中的 gamble 指令,该指令从调用者那里传输 1 个 SOL,然后运行可验证的随机 function。 If Acct1 wins the gamble, Program1 then sends 2 SOL back to Acct1.如果 Acct1 赢得赌注,Program1 然后将 2 SOL 发送回 Acct1。

I would really appreciate some help, thanks!我真的很感激一些帮助,谢谢!

You can have a program similar to this one in the cookbook: https://solanacookbook.com/references/programs.html#how-to-do-cross-program-invocation您可以在食谱中找到与此类似的程序: https://solanacookbook.com/references/programs.html#how-to-do-cross-program-invocation

The differences in your case are to:您的情况的不同之处在于:

  • use solana_program::system_instruction::transfer instead of spl_token::instruction::transfer使用solana_program::system_instruction::transfer而不是spl_token::instruction::transfer
  • only have source and destination accounts, without the owner只有源帐户和目标帐户,没有所有者
  • sign with the source account使用源帐户签名
  • pass in the system program instead of the token program传入系统程序而不是令牌程序

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

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