Skip to content

Commit

Permalink
refactor: forward data payload to proxy using submessage
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesondh committed Oct 12, 2023
1 parent a036883 commit 3a32f29
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 64 deletions.
110 changes: 55 additions & 55 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ serde = { version = "1.0.145", default-features = false, features = ["derive"] }
serde_json = "1.0.107"
sha3 = "0.10.8"
thiserror = { version = "1.0.31" }
wasm-bin-storage = { path = "./packages/wasm-bin-storage" }
2 changes: 2 additions & 0 deletions packages/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ pub enum ContractError {
// proxy errors
#[error("Contract already set")]
ContractAlreadySet,
#[error("Unknown reply ID: {0}")]
UnknownReplyId(String),
}
38 changes: 30 additions & 8 deletions packages/proxy/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use common::{
};
use cosmwasm_std::{
to_binary, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, QueryRequest, Response,
WasmMsg, WasmQuery,
WasmMsg, WasmQuery, SubMsg, Reply
};
use cw2::set_contract_version;

Expand All @@ -27,6 +27,8 @@ use crate::{
const CONTRACT_NAME: &str = "proxy-contract";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

const POST_DATA_REQUEST_REPLY_ID: u64 = 1u64;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
Expand Down Expand Up @@ -72,13 +74,21 @@ pub fn execute(
// Delegated calls to contracts

// DataRequests
ProxyExecuteMsg::PostDataRequest { posted_dr } => Ok(Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: DATA_REQUESTS.load(deps.storage)?.to_string(),
msg: to_binary(&DataRequestsExecuteMsg::PostDataRequest { posted_dr })?,
funds: vec![],
}))
.add_attribute("action", "post_data_request")),
ProxyExecuteMsg::PostDataRequest { posted_dr } => {
// we create a submessage here rather than a fire-and-forget
// message to the DataRequest contract in order to return the dr_id
// in the data field of this call on the Proxy contract.
Ok(Response::new()
.add_submessage(SubMsg::reply_on_success(
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: DATA_REQUESTS.load(deps.storage)?.to_string(),
msg: to_binary(&DataRequestsExecuteMsg::PostDataRequest { posted_dr })?,
funds: vec![],
}),
POST_DATA_REQUEST_REPLY_ID
))
.add_attribute("action", "post_data_request"))
},
ProxyExecuteMsg::CommitDataResult { dr_id, commitment } => Ok(Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: DATA_REQUESTS.load(deps.storage)?.to_string(),
Expand Down Expand Up @@ -266,6 +276,18 @@ pub fn query(deps: Deps, _env: Env, msg: ProxyQueryMsg) -> cosmwasm_std::StdResu
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
println!("Reply: {:?}", msg);
match msg.id {
POST_DATA_REQUEST_REPLY_ID => {
println!("Reply: {:?}", msg.result);
Ok(Response::new().set_data(to_binary(&IsDataRequestExecutorEligibleResponse{value:true})?))
},
id => Err(ContractError::UnknownReplyId(id.to_string())),
}
}

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

0 comments on commit 3a32f29

Please sign in to comment.