如何在具有多个帐户的 Solana 程序中引用特定帐户?
How to reference a specific account in Solana programs with multiple accounts?
这个程序允许用户创建一个账户来收钱。因此 'Alice' 可以创建帐户 Fundraiser。在 Fundraiser 中,有一个特定的变量 amount_raised 可以跟踪向她的帐户发送了多少 SOL。该程序允许多人创建新的筹款活动。那么,如何在函数 'donate' 中引用正确的帐户?我怀疑我需要使用 PDA 或至少遍历所有程序帐户并将其与创建者的公钥匹配。先感谢您。 (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
}
}
`
你几乎做对了——在你的 Donate
指令中,你还需要传入爱丽丝的 author
账户,这样你就可以从 user
到 author
在 Donate
期间。因此,它看起来像:
#[derive(Accounts)]
pub struct Donate<'info> {
#[account(mut)]
pub fundraiser: Account<'info, Fundraiser>,
#[account(mut)]
pub user: Signer<'info>,
#[account(mut)]
pub author: AccountInfo<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
并且您必须检查 author
提供的匹配项 fundraiser.author
。
这个程序允许用户创建一个账户来收钱。因此 'Alice' 可以创建帐户 Fundraiser。在 Fundraiser 中,有一个特定的变量 amount_raised 可以跟踪向她的帐户发送了多少 SOL。该程序允许多人创建新的筹款活动。那么,如何在函数 'donate' 中引用正确的帐户?我怀疑我需要使用 PDA 或至少遍历所有程序帐户并将其与创建者的公钥匹配。先感谢您。 (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
}
} `
你几乎做对了——在你的 Donate
指令中,你还需要传入爱丽丝的 author
账户,这样你就可以从 user
到 author
在 Donate
期间。因此,它看起来像:
#[derive(Accounts)]
pub struct Donate<'info> {
#[account(mut)]
pub fundraiser: Account<'info, Fundraiser>,
#[account(mut)]
pub user: Signer<'info>,
#[account(mut)]
pub author: AccountInfo<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
并且您必须检查 author
提供的匹配项 fundraiser.author
。