实例化CosmWasm合约更新元数据时遇类型不匹配错误求助
类型不匹配错误:Response vs Response
问题原因
你的instantiate函数声明返回Result<Response, ContractError>,这里的Response默认是cosmwasm_std::Response<cosmwasm_std::Empty>。但你调用add_message添加ArchwayMsg类型的消息后,返回的Response实例泛型参数变成了ArchwayMsg,和函数声明的返回类型不匹配,因此触发类型错误。
解决方案
1. 修改函数返回类型
明确指定Response的泛型参数为ArchwayMsg,让函数返回类型与实际返回的Response实例类型一致:
#[cfg_attr(not(feature = "library"), entry_point)] pub fn instantiate( deps: DepsMut, env: Env, _info: MessageInfo, msg: InstantiateMsg, ) -> Result<Response<ArchwayMsg>, ContractError> { // 此处添加泛型参数ArchwayMsg let config = Config { native_denom: msg.native_denom, }; CONFIG.save(deps.storage, &config)?; ADMIN_FEE.save(deps.storage, &msg.admin_fee)?; CREDIT_CONTRACT.save(deps.storage, &msg.credit_contract)?; set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; Ok( Response::new().add_message(ArchwayMsg::UpdateContractMetadata { owner_address: todo!(), rewards_address: todo!(), }), ) }
2. 检查依赖与导入
确保你已在Cargo.toml中正确引入archway_bindings依赖,并且在代码顶部导入所需类型:
use archway_bindings::ArchwayMsg; use cosmwasm_std::{Response, DepsMut, Env, MessageInfo, Result}; // 其他必要导入...
内容的提问来源于stack exchange,提问作者Pete
相关产品推荐
相关产品推荐

