简体   繁体   中英

How to add a Pubkey to an account containing a vector of Pubkeys in an anchor program

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 () ."

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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