锚点:尝试初始化程序派生帐户 (PDA) 时出错
Anchor: Error when trying to initialize a program derived account (PDA)
每当尝试初始化 PDA 帐户时,我都会收到以下错误消息:
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
#[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: [],
});
我尝试使用新生成的密钥对作为创建者,并将该密钥对添加到签名者,但我似乎无法让它工作。
原来这段代码是创建 PDA 的正确方法 :) 我有一个测试验证器 运行 所以它试图针对以前存在的合同执行!
如您所述,您应该在执行前关闭测试验证程序终端
anchor test
anchor 运行 test-validator 单独为您进行测试,测试后您可以在 solana anchor 项目根目录中看到 test-ledger 文件夹
另一点是你应该将 test-ledger 文件夹添加到你的 .gitignore 文件中。
也许有帮助。
每当尝试初始化 PDA 帐户时,我都会收到以下错误消息:
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
#[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: [],
});
我尝试使用新生成的密钥对作为创建者,并将该密钥对添加到签名者,但我似乎无法让它工作。
原来这段代码是创建 PDA 的正确方法 :) 我有一个测试验证器 运行 所以它试图针对以前存在的合同执行!
如您所述,您应该在执行前关闭测试验证程序终端
anchor test
anchor 运行 test-validator 单独为您进行测试,测试后您可以在 solana anchor 项目根目录中看到 test-ledger 文件夹 另一点是你应该将 test-ledger 文件夹添加到你的 .gitignore 文件中。 也许有帮助。