Skip to content

Commit

Permalink
refactor: use response.data in integration tests for dr_id
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesondh committed Oct 12, 2023
1 parent 3a32f29 commit 7b13f03
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 35 deletions.
2 changes: 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cosmwasm-std = { version = "1.4", features = ["staking"] }
cosmwasm-storage = "1.1.3"
cw-storage-plus = "1.1"
cw2 = "1.1"
cw-utils = "1.0.1"
hex = "0.4.3"
proxy-contract = { path = "./packages/proxy" }
schemars = "0.8.10"
Expand Down
1 change: 1 addition & 0 deletions packages/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-utils = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
6 changes: 6 additions & 0 deletions packages/common/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::types::Hash;
use cosmwasm_std::StdError;
use cw_utils::ParseReplyError;
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
// staking contract errors
Expand Down Expand Up @@ -40,4 +42,8 @@ pub enum ContractError {
ContractAlreadySet,
#[error("Unknown reply ID: {0}")]
UnknownReplyId(String),
#[error("Unexpected error: {0}")]
UnexpectedError(String),
#[error(transparent)]
ParseReplyError(#[from] ParseReplyError),
}
5 changes: 1 addition & 4 deletions packages/data-requests/src/data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use cosmwasm_std::{to_binary, Deps, DepsMut, MessageInfo, Order, Response, StdRe

use crate::state::{DATA_REQUESTS, DATA_REQUESTS_COUNT};

use crate::msg::PostDataRequestResponse;
use common::msg::{GetDataRequestResponse, GetDataRequestsFromPoolResponse};
use common::state::DataRequest;
use common::types::Hash;
Expand Down Expand Up @@ -110,9 +109,7 @@ pub mod data_requests {

Ok(Response::new()
.add_attribute("action", "post_data_request")
.set_data(to_binary(&PostDataRequestResponse {
dr_id: posted_dr.dr_id.clone(),
})?)
.set_data(to_binary(&posted_dr.dr_id)?)
.add_event(Event::new("seda-data-request").add_attributes([
("version", CONTRACT_VERSION),
("dr_id", &posted_dr.dr_id),
Expand Down
1 change: 0 additions & 1 deletion packages/data-requests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod contract;
pub mod data_request;
pub mod data_request_result;
pub mod msg;
pub mod state;
pub mod types;
pub mod utils;
Expand Down
7 changes: 0 additions & 7 deletions packages/data-requests/src/msg.rs

This file was deleted.

1 change: 0 additions & 1 deletion packages/integration-tests/src/data_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ fn commit_reveal_result() {
.query_wasm_smart(proxy_contract.addr(), &msg)
.unwrap();
assert!(res.value.is_some());
println!("res: {:?}", res);

helper_reveal_result(
&mut app,
Expand Down
18 changes: 13 additions & 5 deletions packages/integration-tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pub fn proxy_contract_template() -> Box<dyn Contract<Empty>> {
proxy_contract::contract::execute,
proxy_contract::contract::instantiate,
proxy_contract::contract::query,
);
)
.with_reply(proxy_contract::contract::reply);
Box::new(contract)
}

Expand Down Expand Up @@ -186,10 +187,17 @@ pub fn proper_instantiate() -> (App, CwTemplateContract) {
}

pub fn get_dr_id(res: AppResponse) -> String {
// TODO: this is an ugly way to get the dr_id.
// although PostDataRequest on the DataRequest contract returns it in `data`, the Proxy contract does not yet.
// https://github.com/sedaprotocol/seda-chain-contracts/issues/68
res.events.last().unwrap().attributes[2].value.clone()
let binary = res.data.unwrap();

// I don't know why protobuf adds 2 bytes to the beginning of the data payload but it does
let binary = &binary.to_vec()[2..];

let dr_id = String::from_utf8(binary.to_vec()).unwrap();

// remove first and last char (they are quotes)
let dr_id = &dr_id[1..dr_id.len() - 1];

dr_id.to_string()
}

pub fn calculate_commitment(reveal: &str, salt: &str) -> String {
Expand Down
1 change: 1 addition & 0 deletions packages/proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
cw-utils = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
36 changes: 19 additions & 17 deletions packages/proxy/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use common::{
},
};
use cosmwasm_std::{
to_binary, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, QueryRequest, Response,
WasmMsg, WasmQuery, SubMsg, Reply
to_binary, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, QueryRequest, Reply,
Response, SubMsg, WasmMsg, WasmQuery,
};
use cw2::set_contract_version;
use cw_utils::parse_reply_execute_data;

use crate::{
msg::{InstantiateMsg, ProxyExecuteMsg, ProxyQueryMsg},
Expand All @@ -27,7 +28,7 @@ use crate::{
const CONTRACT_NAME: &str = "proxy-contract";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

const POST_DATA_REQUEST_REPLY_ID: u64 = 1u64;
const POST_DATA_REQUEST_REPLY_ID: u64 = 1;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -79,16 +80,16 @@ pub fn execute(
// 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"))
},
.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 @@ -278,12 +279,13 @@ 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})?))
},
let data = parse_reply_execute_data(msg)?
.data
.ok_or_else(|| ContractError::UnexpectedError("Data is None".to_string()))?;
Ok(Response::new().set_data(data))
}
id => Err(ContractError::UnknownReplyId(id.to_string())),
}
}
Expand Down

0 comments on commit 7b13f03

Please sign in to comment.