Skip to content

Commit

Permalink
Add additional message saving
Browse files Browse the repository at this point in the history
  • Loading branch information
adairrr committed May 12, 2024
1 parent cd54040 commit 20ba4f8
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cw-semver = { version = "1.0", features = ["serde" ]}
cw-orch = { package = "cw-orch-interchain", git = "ssh://git@github.com/AbstractSDK/cw-orch-interchain.git", tag = "v0.22.0" }
abstract-cw-orch-polytone = "2.0.0"
abstract-interchain-tests = { git = "https://github.com/AbstractSDK/abstract", version = "0.22.1", branch = "removemm" }
cw-paginate = "0.2.1"
# TODO: cannot use this because of money-market adapter trait issue
#abstract-interchain-tests = { git = "https://github.com/AbstractSDK/abstract", version = "0.22.1", tag = "v0.22.1" }

Expand Down
1 change: 1 addition & 0 deletions contracts/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cw-asset = { workspace = true }
abstract-app = { workspace = true }
ibcmail = { workspace = true }
abstract-interface = { workspace = true, optional = true }
cw-paginate = { workspace = true }

# Dependencies for interface
cw-orch = { workspace = true, optional = true }
Expand Down
3 changes: 3 additions & 0 deletions contracts/client/src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ fn receive_msg(deps: DepsMut, info: MessageInfo, msg: Message, app: App) -> Clie

match msg.recipient {
Recipient::Account { id: ref account_id, .. } => {

println!("account_id: {:?}", account_id);
let our_id = app.account_id(deps.as_ref())?;
println!("our_id: {:?}", our_id);
// check that the recipient is the current account
ensure_eq!(account_id, &our_id, ClientError::NotRecipient {} );
}
Expand Down
20 changes: 20 additions & 0 deletions contracts/client/src/handlers/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@ use crate::contract::{App, ClientResult};
use crate::msg::{ClientQueryMsg, ConfigResponse};
use crate::state::{CONFIG};
use cosmwasm_std::{to_json_binary, Binary, Deps, Env, StdResult};
use cw_storage_plus::Bound;
use ibcmail::client::error::ClientError;
use ibcmail::client::msg::{MessageFilter, MessagesResponse};
use ibcmail::client::state::RECEIVED;
use ibcmail::MessageId;

pub const DEFAULT_LIMIT: u32 = 50;

pub fn query_handler(deps: Deps, _env: Env, _app: &App, msg: ClientQueryMsg) -> ClientResult<Binary> {
match msg {
ClientQueryMsg::Config {} => to_json_binary(&query_config(deps)?),
ClientQueryMsg::Messages { filter, start_after, limit } => to_json_binary(&query_messages(deps, filter, start_after, limit)?),
}
.map_err(Into::into)
}

fn query_messages(deps: Deps, filter: Option<MessageFilter>, start: Option<MessageId>, limit: Option<u32>) -> ClientResult<MessagesResponse> {
let messages = cw_paginate::paginate_map(
&RECEIVED,
deps.storage,
start.as_ref().map(Bound::exclusive),
limit,
|_id, message| Ok::<_, ClientError>(message),
)?;

Ok(MessagesResponse { messages })
}

fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let _config = CONFIG.load(deps.storage)?;
Ok(ConfigResponse {})
Expand Down
18 changes: 16 additions & 2 deletions contracts/client/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,19 @@ mod receive_msg {
let server_account_id = app.account().id().unwrap();
let app_account_id = app.account().id().unwrap();

println!("server_account_id: {:?}, app_account_id: {:?}", server_account_id, app_account_id);

let msg = create_test_message(server_account_id.clone(), app_account_id.clone());
let server_adr = app.account().module_addresses(vec![IBCMAIL_SERVER_ID.into()])?.modules[0].1.clone();
let res = app.call_as(&server_adr).receive_message(msg);
let server_addr = app.account().module_addresses(vec![IBCMAIL_SERVER_ID.into()])?.modules[0].1.clone();

println!("app_account_id: {:?}", app.account().id());
let res = app.call_as(&server_addr).receive_message(msg);

assert_that!(res).is_ok();

let messages = app.messages(None, None, None)?;
assert_that!(messages.messages).has_length(1);

Ok(())
}

Expand Down Expand Up @@ -199,6 +207,12 @@ mod send_msg {

assert_that!(res).is_ok();

let myos_messages = myos_client.messages(None, None, None)?;
assert_that!(myos_messages.messages).is_empty();

let juno_messages = juno_client.messages(None, None, None)?;
assert_that!(juno_messages.messages).has_length(1);

Ok(())
}
}
26 changes: 21 additions & 5 deletions packages/ibcmail/src/client/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use cosmwasm_schema::QueryResponses;
use crate::client::ClientApp;
use crate::{Message, NewMessage};
use crate::{Message, MessageId, NewMessage, Recipient, Sender};


// This is used for type safety and re-exporting the contract endpoint structs.
Expand All @@ -25,19 +25,35 @@ pub enum ClientExecuteMsg {
UpdateConfig {},
}

#[cosmwasm_schema::cw_serde]
pub struct AppMigrateMsg {}


/// App query messages
#[cosmwasm_schema::cw_serde]
#[cfg_attr(feature = "interface", derive(cw_orch::QueryFns))]
#[cfg_attr(feature = "interface", impl_into(QueryMsg))]
#[derive(QueryResponses)]
pub enum ClientQueryMsg {
#[returns(MessagesResponse)]
Messages {
filter: Option<MessageFilter>,
limit: Option<u32>,
start_after: Option<MessageId>,
},
#[returns(ConfigResponse)]
Config {},
}

#[cosmwasm_schema::cw_serde]
pub struct MessageFilter {
pub from: Option<Sender>,
}

#[cosmwasm_schema::cw_serde]
pub struct AppMigrateMsg {}


#[cosmwasm_schema::cw_serde]
pub struct ConfigResponse {}

#[cosmwasm_schema::cw_serde]
pub struct MessagesResponse {
pub messages: Vec<Message>,
}
9 changes: 9 additions & 0 deletions packages/ibcmail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ impl Recipient {
chain: chain_name
}
}
}

#[non_exhaustive]
#[cosmwasm_schema::cw_serde]
pub enum Sender {
Account {
id: AccountId,
chain: Option<ChainName>
}
}

0 comments on commit 20ba4f8

Please sign in to comment.