简体   繁体   English

让 Solana 程序为交易付费

[英]Make a Solana program pay for the transaction

I'm not sure that is possible, but what I would like to do is to make the program pay for the transaction cost and make the user use the program for free.我不确定这是否可能,但我想做的是让程序支付交易费用并让用户免费使用程序。

In my mind the process would be:在我看来,这个过程是:

  1. I send some Solana to the account of the program to handle future transactions.我将一些 Solana 发送到该程序的帐户以处理未来的交易。
  2. A user interact with the function SaveData用户与函数 SaveData 交互
  3. (Inside this function in the future will be some sort of mechanism to check if the user can interact with the function without paying) (未来在这个功能里面会有某种机制来检查用户是否可以在不付费的情况下与该功能进行交互)
  4. The program would pay for the transaction, so the user don't have to pay even a single Lamport.该程序将为交易付费,因此用户甚至不必支付一个 Lamport。

My code is:我的代码是:

#[derive(Accounts)]
pub struct SaveData<'info> {
    #[account(init, payer = system_program, space = 8 + 50 + 32 )]
    pub data_account: Account<'info, DataState>,

    #[account(mut)]
    pub authority: Signer<'info>,

    pub system_program: Program<'info, System>,
}
#[account]
pub struct DataState {
    pub authority: Pubkey,
    content: String
}

I tried setting system_program as the payer, but if I try to build the program with anchor it gives me this error:我尝试将system_program设置为付款人,但如果我尝试使用锚构建程序,它会给我这个错误:

error[E0599]: no method named `exit` found for struct `SaveData` in the current scope
  --> programs/test-smart-contract/src/lib.rs:5:1
   |
5  | #[program]
   | ^^^^^^^^^^ method not found in `SaveData<'_>`
...
61 | pub struct SaveData<'info> {
   | -------------------------- method `exit` not found for this

How can I achieve what I want to do?我怎样才能实现我想做的事情?


Update更新

In order to manage this problem, I started developing this service: cowsigner.com为了解决这个问题,我开始开发这个服务: cowsigner.com

Unfortunately every transaction has a fee payer which is specified and needs to sign on the transaction and be a signer account.不幸的是,每笔交易都有一个指定的费用支付者,需要在交易上签名并成为签名者帐户。

System program is a native program on Solana and you're misusing it in your case.系统程序是 Solana 上的本机程序,在您的情况下您误用了它。 This is the official definition for it:这是它的官方定义:

Create new accounts, allocate account data, assign accounts to owning programs, transfer lamports from System Program owned accounts and pay transaction fees.创建新帐户,分配帐户数据,将帐户分配给拥有的程序,从系统程序拥有的帐户转移灯并支付交易费用。

If you wish to pay for the fees then you would need a dedicated account, that is specified as the feePayer on each transaction and signs on each transaction as well.如果您想支付费用,那么您需要一个专用帐户,该帐户在每笔交易中指定为 feePayer 并在每笔交易上签名。

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

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