如何将Pubkey添加到锚程序中包含Pubkeys矢量的帐户

发布于 2025-01-17 21:43:10 字数 1521 浏览 0 评论 0原文

use anchor_lang::prelude::*;
use rand::Rng;
use solana_program::{declare_id, pubkey::Pubkey};
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod raffle_impl {
use super::*;

pub fn create_raffle(ctx: Context<CreateRaffle>, authority: Pubkey) -> ProgramResult{
    let payer = &mut ctx.accounts.wallet;
    let escrow_account = &mut ctx.accounts.escrow_account;
    Ok(())
}

wallet_address_to_add 是一个将从前端在交易中传递的公钥。它应该被推送到下面定义的帐户宏中定义的向量。

pub fn add_participants(ctx: Context<AddParticipants>, wallet_address_to_add: Pubkey) -> 
ProgramResult{
    let payer = &mut ctx.accounts.wallet;
    let mut data = &mut ctx.accounts.escrow_account.data;
    data = data.push(wallet_address_to_add); // Error occurs here
    Ok(())
}
}
#[derive(Accounts)]
pub struct AddParticipants<'info>{
#[account(mut,signer)]
pub wallet: AccountInfo<'info>,
#[account(mut)]
pub owner: AccountInfo<'info>,
#[account(init_if_needed, payer = wallet, space=8+16)]
pub escrow_account: Account<'info, EscrowAccount>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,

#[account]
#[derive(Default)]    
pub struct EscrowAccount{
    pub data: Vec<Pubkey>,
    pub length: usize,
    pub payer: Pubkey,
    pub authority: Pubkey,
}

我不断收到的错误是“预期可变引用 &mut Vec 找到单元类型 ()”。

use anchor_lang::prelude::*;
use rand::Rng;
use solana_program::{declare_id, pubkey::Pubkey};
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod raffle_impl {
use super::*;

pub fn create_raffle(ctx: Context<CreateRaffle>, authority: Pubkey) -> ProgramResult{
    let payer = &mut ctx.accounts.wallet;
    let escrow_account = &mut ctx.accounts.escrow_account;
    Ok(())
}

This wallet_address_to_add is a Pubkey that would be passed in the transaction from the frontend. It should be pushed to a vector defined in the account macro defined below.

pub fn add_participants(ctx: Context<AddParticipants>, wallet_address_to_add: Pubkey) -> 
ProgramResult{
    let payer = &mut ctx.accounts.wallet;
    let mut data = &mut ctx.accounts.escrow_account.data;
    data = data.push(wallet_address_to_add); // Error occurs here
    Ok(())
}
}
#[derive(Accounts)]
pub struct AddParticipants<'info>{
#[account(mut,signer)]
pub wallet: AccountInfo<'info>,
#[account(mut)]
pub owner: AccountInfo<'info>,
#[account(init_if_needed, payer = wallet, space=8+16)]
pub escrow_account: Account<'info, EscrowAccount>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,

#[account]
#[derive(Default)]    
pub struct EscrowAccount{
    pub data: Vec<Pubkey>,
    pub length: usize,
    pub payer: Pubkey,
    pub authority: Pubkey,
}

The error I keep getting is "expected mutable reference &mut Vec<anchor_lang::prelude::Pubkey> found unit type ()."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-01-24 21:43:10

您应该只执行不带 data = 部分的 data.push(wallet_address_to_add);,而不是 data = data.push(wallet_address_to_add);

Instead of data = data.push(wallet_address_to_add);, you should just do data.push(wallet_address_to_add); without the data = part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文