在任何 solana 交易中,都有一个预定义的费用支付者,他将支付交易费用。
在我看来,这个费用支付者也会为创建帐户付费,相反,如果没有指定,在同一笔交易中,您可以拥有一个支付交易费用的钱包和一个支付创建帐户的钱包。帐户。
我有以下代码:
#[program]
pub mod solana_twitter {
use super::*;
pub fn send_tweet(ctx: Context<SendTweet>, topic: String, content: String) -> ProgramResult {
let tweet: &mut Account<Tweet> = &mut ctx.accounts.tweet;
let author: &Signer = &ctx.accounts.author;
let clock: Clock = Clock::get().unwrap();
if topic.chars().count() > 50 {
return Err(ErrorCode::TopicTooLong.into())
}
if content.chars().count() > 280 {
return Err(ErrorCode::ContentTooLong.into())
}
tweet.author = *author.key;
tweet.timestamp = clock.unix_timestamp;
tweet.topic = topic;
tweet.content = content;
Ok(())
}
...
}
#[derive(Accounts)]
pub struct SendTweet<'info> {
#[account(init, payer = author, space = Tweet::LEN)]
pub tweet: Account<'info, Tweet>,
#[account(mut)]
pub author: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
#[account]
pub struct Tweet {
pub author: Pubkey,
pub timestamp: i64,
pub topic: String,
pub content: String,
}
当我调用函数
SendTweet
时,会创建一个 Tweet
帐户,并且推文的 author
支付交易费用和用于创建免租帐户的 lamport 转账(即 tweet
本身) .
现在,在我的前端代码中,为了生成交易,我添加了一个新钱包并将其设置为 FeePayer。 在这种情况下,一切正常,但 FeePayer 仅支付交易费用,作者支付帐户创建费用。 我怎样才能将feePayer也设置为创建帐户的付款人,而将第一个钱包保留为推文的作者?
我尝试了以下方法
#[derive(Accounts)]
pub struct SendTweet<'info> {
#[account(init, payer = fee_payer, space = Tweet::LEN)]
pub tweet: Account<'info, Tweet>,
#[account()]
pub author: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
但它只是告诉我在范围内没有找到fee_payer。
你们非常接近!在第一部分中,您将作者指定为付款人:
#[derive(Accounts)]
pub struct SendTweet<'info> {
#[account(init, payer = author, space = Tweet::LEN)]
pub tweet: Account<'info, Tweet>,
#[account(mut)]
pub author: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
在第二步中,您尝试指定一个单独的付款人:
#[derive(Accounts)]
pub struct SendTweet<'info> {
#[account(init, payer = fee_payer, space = Tweet::LEN)]
pub tweet: Account<'info, Tweet>,
#[account()]
pub author: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
但是,您指定了
fee_payer
而不是 payer
,这是错误的变量名称,因此您只需将其更改为:
#[derive(Accounts)]
pub struct SendTweet<'info> {
#[account(init, payer = fee_payer, space = Tweet::LEN)]
pub tweet: Account<'info, Tweet>,
#[account()]
pub author: Signer<'info>,
#[account(mut)]
pub fee_payer: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
我需要更改付费者,因为我被黑客攻击,黑客把他的钱包拿给付费者,我对开发一无所知,但如果你能帮助我吗?