Skip to content

Commit

Permalink
Merge pull request #331 from CosmWasm/proper-proto-multitest-init
Browse files Browse the repository at this point in the history
Use prost to create and parse proper InstantiateData
  • Loading branch information
ethanfrey authored Jul 14, 2021
2 parents 3356773 + b4ab1d5 commit 9ea8f5a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/multi-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ cw-storage-plus = { path = "../../packages/storage-plus", version = "0.7.0" }
cosmwasm-std = { version = "0.15.0", features = ["staking"] }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
prost = "0.8.0"
46 changes: 35 additions & 11 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use crate::wasm::{Contract, StorageFactory, WasmCache, WasmCommittable, WasmOps,
use schemars::JsonSchema;
use std::fmt;

use prost::Message;
// use bytes::buf::BufMut;

#[derive(Default, Clone, Debug)]
pub struct AppResponse {
pub events: Vec<Event>,
Expand All @@ -29,11 +32,42 @@ impl AppResponse {
}
}

#[derive(Clone, PartialEq, Message)]
pub struct InstantiateData {
#[prost(string, tag = "1")]
pub address: ::prost::alloc::string::String,
/// Unique ID number for this person.
#[prost(bytes, tag = "2")]
pub data: ::prost::alloc::vec::Vec<u8>,
}

fn init_response<C>(res: &mut Response<C>, contact_address: &Addr)
where
C: Clone + fmt::Debug + PartialEq + JsonSchema,
{
res.data = Some(contact_address.as_bytes().into());
let data = res.data.clone().unwrap_or_default().to_vec();
let init_data = InstantiateData {
address: contact_address.into(),
data,
};
let mut new_data = Vec::<u8>::with_capacity(init_data.encoded_len());
// the data must encode successfully
init_data.encode(&mut new_data).unwrap();
res.data = Some(new_data.into());
}

// this parses the result from a wasm contract init
pub fn parse_contract_addr(data: &Option<Binary>) -> Result<Addr, String> {
let bin = data
.as_ref()
.ok_or_else(|| "No data response".to_string())?
.to_vec();
// parse the protobuf struct
let init_data = InstantiateData::decode(bin.as_slice()).map_err(|e| e.to_string())?;
if init_data.address.is_empty() {
return Err("no contract address provided".into());
}
Ok(Addr::unchecked(init_data.address))
}

impl<C> Querier for App<C>
Expand Down Expand Up @@ -487,16 +521,6 @@ where
}
}

// this parses the result from a wasm contract init
pub fn parse_contract_addr(data: &Option<Binary>) -> Result<Addr, String> {
let bin = data
.as_ref()
.ok_or_else(|| "No data response".to_string())?
.to_vec();
let str = String::from_utf8(bin).map_err(|e| e.to_string())?;
Ok(Addr::unchecked(str))
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 9ea8f5a

Please sign in to comment.