You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Anchor运行test.js测试报crowdFundingPlatform未提供错误

报错原因
  • Anchor JS客户端默认会将Rust侧snake_case(下划线命名)的账户名自动转换为camelCase(小驼峰命名)作为调用时的匹配键名。你在测试代码的accounts对象里用了下划线命名的crowd_funding_platform、system_program作为键,客户端识别不到对应字段,首先就会提示缺失小驼峰格式的crowdFundingPlatform参数。
  • 你在合约的Initialize账户校验逻辑中,给crowd_funding_platform配置了seeds和bump属性,说明该账户是程序派生地址(PDA),不是普通的随机密钥对账户:PDA不存在对应私钥,不需要手动生成Keypair,也不能作为外部签名者传入,必须通过链下推导方法得到地址,初始化时由程序完成签名校验。你当前手动生成Keypair、将其加入signers数组的写法本身不符合PDA的使用规则,就算解决了命名问题也会触发签名错误。
修复方法

按以下步骤修改即可:

  1. 删除测试代码中冗余的const platform = anchor.web3.Keypair.generate();(该变量定义后未被使用),同时删除it块内const crowd_funding_platform = anchor.web3.Keypair.generate();的逻辑,替换为PDA地址推导代码:
const [crowdFundingPlatform, bump] = anchor.web3.PublicKey.findProgramAddressSync(
  [Buffer.from("crowd_funding_platform")],
  program.programId
);
  1. 修改program.rpc.initialize的调用逻辑:将accounts内的键名全部改为小驼峰格式,移除signers数组中无效的PDA签名配置,旧版本Anchor需要额外传入推导得到的bump参数,修改后代码如下:
await program.rpc.initialize(
  "limk",
  "description",
  "title",
  new anchor.BN(1),
  bump, // 0.24以上版本Anchor可自动识别bump,若运行提示bump参数多余可删除该行
  {
    accounts: {
      crowdFundingPlatform: crowdFundingPlatform,
      authority: program.provider.wallet.publicKey,
      systemProgram: anchor.web3.SystemProgram.programId,
    },
    // 移除原signers配置,PDA不需要外部签名
  }
)
  1. 修改后续账户查询代码,匹配客户端小驼峰命名规则,使用推导得到的PDA地址查询链上数据:
const PlatformAccount = await program.account.crowdFundingPlatform.fetch(crowdFundingPlatform);
console.log(PlatformAccount);
  1. 所有代码改完后,在项目根目录执行anchor build重新生成IDL文件,再运行anchor test即可正常执行测试。

内容的提问来源于stack exchange,提问作者Unreal._.N1K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 21:33:25