簡體   English   中英

如何在錨程序中將公鑰添加到包含公鑰向量的帳戶

[英]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(())
}

這個 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<anchor_lang::prelude::Pubkey> found unit type () 。”

而不是data = data.push(wallet_address_to_add); ,你應該只做data.push(wallet_address_to_add); 沒有data =部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM