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

遵循Substrate add-a-pallet教程编译失败 u128类型不满足Event约束

Substrate add-a-pallet编译错误:u128未实现Currency trait 解决方案

报错详情

Compiling node-template-runtime v3.0.0-monthly-2021-10 (/home/bruce/substrate-node-template/runtime)
error: failed to run custom build command for `node-template-runtime v3.0.0-monthly-2021-10 (/home/bruce/substrate-node-template/runtime)`

Caused by:
  process didn't exit successfully: `/home/bruce/substrate-node-template/target/release/build/node-template-runtime-41f26d4f5bdef0b7/build-script-build` (exit status: 1)
  --- stdout
  Information that should be included in a bug report.
  Executing build command: "rustup" "run" "nightly" "cargo" "rustc" "--target=wasm32-unknown-unknown" "--manifest-path=/home/bruce/substrate-node-template/target/release/wbuild/node-template-runtime/Cargo.toml" "--color=always" "--release"
  Using rustc version: rustc 1.57.0-nightly (54bb4fec6 2021-10-08)


  --- stderr
     Compiling node-template-runtime v3.0.0-monthly-2021-10 (/home/bruce/substrate-node-template/runtime)
  error[E0277]: the trait bound `u128: Currency<AccountId32>` is not satisfied in `Event`
     --> /home/bruce/substrate-node-template/runtime/src/lib.rs:303:1
      |
  303 | / construct_runtime!(
  304 | |     pub enum Runtime where
  305 | |         Block = Block,
  306 | |         NodeBlock = opaque::Block,
  ...   |
  320 | |     }
  321 | | );
      | |__^ within `Event`, the trait `Currency<AccountId32>` is not implemented for `u128`
      |
  note: required because it appears within the type `Event`

  error: could not compile `node-template-runtime` due to 10 previous errors

错误根因

你在配置要添加的pallet的Config trait时,将需要实现Currency trait的关联类型错误赋值为了原生数值类型u128。u128只是余额的存储数值类型,并没有实现链上余额操作需要的Currency相关trait,所以编译器抛出绑定错误。

修复方法

  • 打开/home/bruce/substrate-node-template/runtime/src/lib.rs文件,定位到你新增的pallet的Config实现段
  • 找到type Currency = u128;这行配置,替换为type Currency = Balances;
    • 节点模板默认集成的pallet_balances对应的实例名为Balances,已经完整实现了Currency<AccountId32> trait,完全满足pallet的依赖要求
  • 如果你的pallet需要单独指定余额数值类型,单独配置type Balance = u128;即可,不会触发该错误

验证方法

修改完成后执行cargo check -p node-template-runtime,即可验证编译是否恢复正常。

内容的提问来源于stack exchange,提问作者Bruce

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 19:06:04