Skip to content

Commit

Permalink
Merge pull request #515 from CosmWasm/use-protobuf-de-helpers
Browse files Browse the repository at this point in the history
Use protobuf de helpers
  • Loading branch information
ethanfrey authored Oct 27, 2021
2 parents 33dbabe + 6577c8a commit f149596
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
5 changes: 4 additions & 1 deletion packages/cw0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ mod payment;
pub use pagination::{
calc_range_end, calc_range_start, calc_range_start_string, maybe_addr, maybe_canonical,
};
pub use parse_reply::{parse_reply_execute_data, parse_reply_instantiate_data};
pub use parse_reply::{
parse_execute_response_data, parse_instantiate_response_data, parse_reply_execute_data,
parse_reply_instantiate_data,
};
pub use payment::{may_pay, must_pay, nonpayable, one_coin, PaymentError};

pub use crate::balance::NativeBalance;
Expand Down
41 changes: 27 additions & 14 deletions packages/cw0/src/parse_reply.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use cosmwasm_std::{Binary, Reply};
Expand All @@ -7,13 +9,13 @@ const WIRE_TYPE_LENGTH_DELIMITED: u8 = 2;
// Up to 9 bytes of varints as a practical limit (https://github.com/multiformats/unsigned-varint#practical-maximum-of-9-bytes-for-security)
const VARINT_MAX_BYTES: usize = 9;

#[derive(Clone, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct MsgInstantiateContractResponse {
pub contract_address: String,
pub data: Option<Binary>,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct MsgExecuteContractResponse {
pub data: Option<Binary>,
}
Expand Down Expand Up @@ -112,8 +114,24 @@ pub fn parse_reply_instantiate_data(
.map_err(ParseReplyError::SubMsgFailure)?
.data
.ok_or_else(|| ParseReplyError::ParseFailure("Missing reply data".to_owned()))?;
parse_instantiate_response_data(&data.0)
}

pub fn parse_reply_execute_data(msg: Reply) -> Result<MsgExecuteContractResponse, ParseReplyError> {
let data = msg
.result
.into_result()
.map_err(ParseReplyError::SubMsgFailure)?
.data
.ok_or_else(|| ParseReplyError::ParseFailure("Missing reply data".to_owned()))?;
parse_execute_response_data(&data.0)
}

pub fn parse_instantiate_response_data(
data: &[u8],
) -> Result<MsgInstantiateContractResponse, ParseReplyError> {
// Manual protobuf decoding
let mut data = data.0;
let mut data = data.to_vec();
// Parse contract addr
let contract_addr = parse_protobuf_string(&mut data, 1)?;

Expand All @@ -126,19 +144,14 @@ pub fn parse_reply_instantiate_data(
})
}

pub fn parse_reply_execute_data(msg: Reply) -> Result<MsgExecuteContractResponse, ParseReplyError> {
let data = msg
.result
.into_result()
.map_err(ParseReplyError::SubMsgFailure)?
.data
.ok_or_else(|| ParseReplyError::ParseFailure("Missing reply data".to_owned()))?;
pub fn parse_execute_response_data(
data: &[u8],
) -> Result<MsgExecuteContractResponse, ParseReplyError> {
// Manual protobuf decoding
let mut data = data.0;
// Parse (optional) data
let data = parse_protobuf_bytes(&mut data, 1)?;
let mut data = data.to_vec();
let inner_data = parse_protobuf_bytes(&mut data, 1)?;

Ok(MsgExecuteContractResponse { data })
Ok(MsgExecuteContractResponse { data: inner_data })
}

#[derive(Error, Debug, PartialEq)]
Expand Down
7 changes: 4 additions & 3 deletions packages/multi-test/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::transactions::transactional;
use cosmwasm_std::testing::mock_wasmd_attr;

use anyhow::{anyhow, bail, Result as AnyResult};
use cw0::parse_instantiate_response_data;

// Contract state is kept in Storage, separate from the contracts themselves
const CONTRACTS: Map<&Addr, ContractData> = Map::new("contracts");
Expand Down Expand Up @@ -856,11 +857,11 @@ pub fn parse_contract_addr(data: &Option<Binary>) -> AnyResult<Addr> {
.ok_or_else(|| anyhow!("No data response"))?
.to_vec();
// parse the protobuf struct
let init_data = InstantiateData::decode(bin.as_slice())?;
if init_data.address.is_empty() {
let init_data = parse_instantiate_response_data(bin.as_slice())?;
if init_data.contract_address.is_empty() {
bail!("no contract address provided");
}
Ok(Addr::unchecked(init_data.address))
Ok(Addr::unchecked(init_data.contract_address))
}

#[cfg(test)]
Expand Down

0 comments on commit f149596

Please sign in to comment.