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

CosmWasm多签合约编译报错:QueryMsg未实现QueryResponses trait

CosmWasm多签合约编译错误排查

问题概述

执行编译命令cargo build --release --target wasm32-unknown-unknown时,出现两个编译错误:

error: can't compile schema generator for the wasm32 arch

error[E0277]: the trait bound QueryMsg: QueryResponses is not satisfied
--> src/bin/schema.rs:5:5
|

/ write_api! {

= note: this error originates in the macro write_api (in Nightly builds, run with -Z macro-backtrace for more info)

相关代码文件如下:

schema.rs

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}
}

msg.rs

use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Coin};

#[cw_serde]
pub struct InstantiateMsg {
    pub minimum_approvals: i32,
    pub signers: Vec<Addr>,
}

#[cw_serde]
pub enum ExecuteMsg {
    Approve {},
    Revoke {},
    Execute { to: Addr, funds: Coin },
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    // GetCount returns the current count as a json-encoded number
    #[returns(Valueresp)]
    GetCurrentSignatures {},
}


#[cw_serde]
pub struct Valueresp {
    pub count: i32,
}

错误原因及解决方案

1. 架构不兼容错误(can't compile schema generator for the wasm32 arch)

src/bin/schema.rs是用于生成JSON Schema的本地工具,不需要编译成Wasm字节码。默认cargo build会编译所有目标(包括lib和bin),导致尝试将schema生成器编译为Wasm时失败。

解决方法:编译合约时仅指定编译库目标:

cargo build --release --target wasm32-unknown-unknown --lib

2. QueryResponses trait未实现错误

该错误是第一个问题的衍生结果——在Wasm架构下编译schema生成器时,QueryResponses宏的正常实现逻辑无法生效。解决第一个问题后,此错误会自动消失。

额外检查项:

  • 确认QueryMsg上已标注#[derive(QueryResponses)]宏
  • 确保#[returns(Valueresp)]中的类型名称与定义的结构体Valueresp拼写、大小写完全一致
  • 生成Schema时,使用本地架构运行schema.rs:
cargo run --bin schema

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 03:17:36