gpt4 book ai didi

solana - anchor : Error when trying to initialize a program derived account (PDA)

转载 作者:行者123 更新时间:2023-12-05 04:38:32 25 4
gpt4 key购买 nike

每当尝试初始化 PDA 帐户时,我都会收到以下错误:

错误:发送交易失败:交易模拟失败:错误处理指令0:使用未经授权的签名者或可写帐户进行跨程序调用


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

pub fn initialize(ctx: Context<Initialize>, bump:u8) -> ProgramResult {
let base_account: &mut Account<BaseAccount> = &mut ctx.accounts.base_account;
base_account.bump = bump;
base_account.counter = Some(0);
return Ok(());
}
}



#[derive(Accounts)]
#[instruction(bump:u8)]
pub struct Initialize<'info> {
#[account(
seeds = [b"seed".as_ref()],
bump, init, payer = creator, space = 20000)]
pub base_account: Account<'info, BaseAccount>,
#[account(mut)]
pub creator: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}

#[account]
#[derive(Default)]
pub struct BaseAccount {
pub counter: Option<u64>,
pub bump: u8,
}

我的测试代码是这样的:

const [baseAccountPDA, baseAccountPDABump] = await anchor.web3.PublicKey.findProgramAddress(
[Buffer.from("seed")],
program.programId
);

await program.rpc.initialize(baseAccountPDABump, {
accounts: {
baseAccount: baseAccountPDA,
creator: program.provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: [],
});

我尝试使用新生成的 key 对作为创建者,并将该 key 对添加到签名者,但我似乎无法让它工作。

最佳答案

原来这段代码是创建 PDA 的正确方法 :) 我有一个测试验证器正在运行,所以它试图针对以前存在的合约执行!

关于solana - anchor : Error when trying to initialize a program derived account (PDA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70587929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com