Skip to content

Commit

Permalink
Update reflect package, all compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 20, 2022
1 parent ee28ca0 commit 8ca5669
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion contracts/reflect/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::write_api;
use osmo_reflect::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use token_reflect::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
Expand Down
56 changes: 25 additions & 31 deletions contracts/reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ use cosmwasm_std::{
entry_point, to_binary, to_vec, ContractResult, CosmosMsg, Deps, DepsMut, Env, MessageInfo,
QueryRequest, QueryResponse, Reply, Response, StdError, StdResult, SubMsg, SystemResult,
};
use osmo_bindings::{OsmosisMsg, OsmosisQuery};
use token_bindings::{TokenFactoryMsg, TokenFactoryQuery};

use crate::errors::ReflectError;
use crate::msg::{ChainResponse, ExecuteMsg, InstantiateMsg, OwnerResponse, QueryMsg};
use crate::state::{config, config_read, replies, replies_read, State};

#[entry_point]
pub fn instantiate(
deps: DepsMut<OsmosisQuery>,
deps: DepsMut<TokenFactoryQuery>,
_env: Env,
info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response<OsmosisMsg>> {
) -> StdResult<Response<TokenFactoryMsg>> {
let state = State { owner: info.sender };
config(deps.storage).save(&state)?;
Ok(Response::default())
}

#[entry_point]
pub fn execute(
deps: DepsMut<OsmosisQuery>,
deps: DepsMut<TokenFactoryQuery>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response<OsmosisMsg>, ReflectError> {
) -> Result<Response<TokenFactoryMsg>, ReflectError> {
match msg {
ExecuteMsg::ReflectMsg { msgs } => execute_reflect(deps, env, info, msgs),
ExecuteMsg::ReflectSubMsg { msgs } => execute_reflect_subcall(deps, env, info, msgs),
Expand All @@ -35,11 +35,11 @@ pub fn execute(
}

pub fn execute_reflect(
deps: DepsMut<OsmosisQuery>,
deps: DepsMut<TokenFactoryQuery>,
_env: Env,
info: MessageInfo,
msgs: Vec<CosmosMsg<OsmosisMsg>>,
) -> Result<Response<OsmosisMsg>, ReflectError> {
msgs: Vec<CosmosMsg<TokenFactoryMsg>>,
) -> Result<Response<TokenFactoryMsg>, ReflectError> {
let state = config(deps.storage).load()?;

if info.sender != state.owner {
Expand All @@ -59,11 +59,11 @@ pub fn execute_reflect(
}

pub fn execute_reflect_subcall(
deps: DepsMut<OsmosisQuery>,
deps: DepsMut<TokenFactoryQuery>,
_env: Env,
info: MessageInfo,
msgs: Vec<SubMsg<OsmosisMsg>>,
) -> Result<Response<OsmosisMsg>, ReflectError> {
msgs: Vec<SubMsg<TokenFactoryMsg>>,
) -> Result<Response<TokenFactoryMsg>, ReflectError> {
let state = config(deps.storage).load()?;
if info.sender != state.owner {
return Err(ReflectError::NotCurrentOwner {
Expand All @@ -82,11 +82,11 @@ pub fn execute_reflect_subcall(
}

pub fn execute_change_owner(
deps: DepsMut<OsmosisQuery>,
deps: DepsMut<TokenFactoryQuery>,
_env: Env,
info: MessageInfo,
new_owner: String,
) -> Result<Response<OsmosisMsg>, ReflectError> {
) -> Result<Response<TokenFactoryMsg>, ReflectError> {
let api = deps.api;
config(deps.storage).update(|mut state| {
if info.sender != state.owner {
Expand All @@ -105,37 +105,41 @@ pub fn execute_change_owner(

/// This just stores the result for future query
#[entry_point]
pub fn reply(deps: DepsMut<OsmosisQuery>, _env: Env, msg: Reply) -> Result<Response, ReflectError> {
pub fn reply(
deps: DepsMut<TokenFactoryQuery>,
_env: Env,
msg: Reply,
) -> Result<Response, ReflectError> {
let key = msg.id.to_be_bytes();
replies(deps.storage).save(&key, &msg)?;
Ok(Response::default())
}

#[entry_point]
pub fn query(deps: Deps<OsmosisQuery>, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
pub fn query(deps: Deps<TokenFactoryQuery>, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::Owner {} => to_binary(&query_owner(deps)?),
QueryMsg::Chain { request } => to_binary(&query_chain(deps, &request)?),
QueryMsg::SubMsgResult { id } => to_binary(&query_subcall(deps, id)?),
}
}

fn query_owner(deps: Deps<OsmosisQuery>) -> StdResult<OwnerResponse> {
fn query_owner(deps: Deps<TokenFactoryQuery>) -> StdResult<OwnerResponse> {
let state = config_read(deps.storage).load()?;
let resp = OwnerResponse {
owner: state.owner.into(),
};
Ok(resp)
}

fn query_subcall(deps: Deps<OsmosisQuery>, id: u64) -> StdResult<Reply> {
fn query_subcall(deps: Deps<TokenFactoryQuery>, id: u64) -> StdResult<Reply> {
let key = id.to_be_bytes();
replies_read(deps.storage).load(&key)
}

fn query_chain(
deps: Deps<OsmosisQuery>,
request: &QueryRequest<OsmosisQuery>,
deps: Deps<TokenFactoryQuery>,
request: &QueryRequest<TokenFactoryQuery>,
) -> StdResult<ChainResponse> {
let raw = to_vec(request).map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err))
Expand Down Expand Up @@ -168,8 +172,8 @@ mod tests {

pub fn mock_dependencies(
contract_balance: &[Coin],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<OsmosisQuery>, OsmosisQuery> {
let custom_querier: MockQuerier<OsmosisQuery> =
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<TokenFactoryQuery>, TokenFactoryQuery> {
let custom_querier: MockQuerier<TokenFactoryQuery> =
MockQuerier::new(&[(MOCK_CONTRACT_ADDR, contract_balance)]).with_custom_handler(|_| {
SystemResult::Err(SystemError::InvalidRequest {
error: "not implemented".to_string(),
Expand Down Expand Up @@ -373,16 +377,6 @@ mod tests {
let outer: ChainResponse = from_binary(&response).unwrap();
let inner: AllBalanceResponse = from_binary(&outer.data).unwrap();
assert_eq!(inner.amount, coins(123, "ucosm"));

// TODO? or better in multitest?
// // with custom query
// let msg = QueryMsg::Chain {
// request: OsmosisQuery::Ping {}.into(),
// };
// let response = query(deps.as_ref(), mock_env(), msg).unwrap();
// let outer: ChainResponse = from_binary(&response).unwrap();
// let inner: SpecialResponse = from_binary(&outer.data).unwrap();
// assert_eq!(inner.msg, "pong");
}

#[test]
Expand Down
18 changes: 13 additions & 5 deletions contracts/reflect/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Binary, CosmosMsg, QueryRequest, SubMsg};

use osmo_bindings::{OsmosisMsg, OsmosisQuery};
use token_bindings::{TokenFactoryMsg, TokenFactoryQuery};

#[cw_serde]
pub struct InstantiateMsg {}

#[cw_serde]
pub enum ExecuteMsg {
ReflectMsg { msgs: Vec<CosmosMsg<OsmosisMsg>> },
ReflectSubMsg { msgs: Vec<SubMsg<OsmosisMsg>> },
ChangeOwner { owner: String },
ReflectMsg {
msgs: Vec<CosmosMsg<TokenFactoryMsg>>,
},
ReflectSubMsg {
msgs: Vec<SubMsg<TokenFactoryMsg>>,
},
ChangeOwner {
owner: String,
},
}

#[cw_serde]
Expand All @@ -20,7 +26,9 @@ pub enum QueryMsg {
Owner {},
/// Queries the blockchain and returns the result untouched
#[returns(ChainResponse)]
Chain { request: QueryRequest<OsmosisQuery> },
Chain {
request: QueryRequest<TokenFactoryQuery>,
},
/// If there was a previous ReflectSubMsg with this ID, returns cosmwasm_std::Reply
#[returns(cosmwasm_std::Reply)]
SubMsgResult { id: u64 },
Expand Down

0 comments on commit 8ca5669

Please sign in to comment.