【发布时间】:2022-01-11 05:40:40
【问题描述】:
此程序允许用户创建一个帐户来收款。所以“Alice”可以创建帐户 Fundraiser。在 Fundraiser 中,有一个特定的变量 amount_raised 用于跟踪向她的账户发送了多少 SOL。该程序允许多人创建新的筹款活动..SO,我如何在“捐赠”功能中引用正确的帐户?我怀疑我需要使用 PDA 或至少遍历所有程序帐户并将其与创建者的 pubkey 匹配。先感谢您。 (Sol 是在客户端发送的,我只想通过添加金额来跟踪 amount_raised)。
use super::*;
pub fn donate(ctx: Context<Donate>, amount: u32) -> ProgramResult {
let fundraiser: &mut Account<Fundraiser> = &mut ctx.accounts.fundraiser;
let user: &Signer = &ctx.accounts.user;
fundraiser.amount_raised += amount;
Ok(())
}
pub fn start_fund(ctx: Context<StartFund>, amount: u32, reason: String) -> ProgramResult {
let fundraiser: &mut Account<Fundraiser> = &mut ctx.accounts.fundraiser;
let author: &Signer = &ctx.accounts.author;
let clock: Clock = Clock::get().unwrap();
if reason.chars().count() > 350 {
return Err(ErrorCode::ContentTooLong.into())
}
fundraiser.author = *author.key;
fundraiser.amount_to_raise = amount;
fundraiser.timestamp = clock.unix_timestamp;
fundraiser.reason = reason;
Ok(())
}
pub struct StartFund<'info> {
#[account(init, payer = author, space = 64 + 64)]
pub fundraiser: Account<'info, Fundraiser>,
#[account(mut)]
pub author: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct Donate<'info> {
#[account(mut)]
pub fundraiser: Account<'info, Fundraiser>,
#[account(mut)]
pub user: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
#[account]
pub struct Fundraiser {
pub author: Pubkey,
pub amount_to_raise: u32,
pub amount_raised: u32,
pub timestamp: i64,
pub reason: String, //reason
}
} `
【问题讨论】: