-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
middleware base #129
base: dev
Are you sure you want to change the base?
middleware base #129
Changes from all commits
5d43765
2661681
db3f1fd
c7ea87b
284c1e7
824299f
a6a090d
5ab9d5b
34d7bd9
ce31c1a
e73390f
c498862
345325f
1a9b6cb
6706b01
0daaa8d
f7f15ca
2c51981
7186127
47b5464
8339f91
c310e7f
765b1dd
470097d
9310661
e365d40
b7404af
479f6c7
4c23aeb
ffe17c8
7bead44
19816ff
bc5f28c
1bbdcae
2a03ef9
7827c34
b5283fe
24fc336
6ea6245
303df34
53b3419
c15ea13
7e4fc32
f66436e
8839dd7
6f47458
c7351f3
c4a5602
51aba70
aa25831
4b7dd15
61139ee
832adfe
53ef99f
5e786fb
0896dc1
41f2e6a
4afddb5
e2b7b93
8cd9047
8087f82
6f28d26
9070486
fee1217
cd01680
ddb4185
d5dc81e
7742ab8
8c28d9c
a0286b3
a43aacd
2bc238d
1d4a851
ef45e9b
31fd78d
9cdaa37
4b9c680
9756fec
181f533
e3a00e0
28caad3
f293e04
007d2dc
6af3591
d5a5ad3
22616e1
d6ecb31
3ce96a3
d46e8a3
8423e2f
594bb4d
31ff6a4
ee9411a
579f11b
81308f8
5eec62c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[alias] | ||
wasm = "build --release --lib --target wasm32-unknown-unknown" | ||
schema = "run --bin schema" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "valence-icq-querier" | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
version = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
# use library feature to disable all instantiate/execute/query exports | ||
library = [] | ||
|
||
[dependencies] | ||
cosmwasm-std = { workspace = true } | ||
cosmwasm-schema = { workspace = true } | ||
cw-ownable = { workspace = true } | ||
valence-macros = { workspace = true } | ||
valence-library-utils = { workspace = true } | ||
valence-library-base = { workspace = true } | ||
neutron-sdk = { workspace = true } | ||
serde-json-wasm = { version = "1.0.0", default-features = false } | ||
cw-storage-plus = { workspace = true } | ||
thiserror = { workspace = true } | ||
valence-middleware-broker = { workspace = true } | ||
valence-middleware-utils = { workspace = true } | ||
|
||
[dev-dependencies] | ||
cw20 = { workspace = true } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ICQ-querier library | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prob worth adding something here |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
use std::collections::BTreeMap; | ||
|
||
#[cfg(not(feature = "library"))] | ||
use cosmwasm_std::entry_point; | ||
use cosmwasm_std::{ | ||
to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, Reply, Response, StdError, | ||
StdResult, SubMsg, | ||
}; | ||
use neutron_sdk::{ | ||
bindings::{ | ||
msg::{MsgRegisterInterchainQueryResponse, NeutronMsg}, | ||
query::NeutronQuery, | ||
types::KVKey, | ||
}, | ||
interchain_queries::{queries::get_raw_interchain_query_result, types::QueryType}, | ||
sudo::msg::SudoMsg, | ||
}; | ||
|
||
use valence_library_utils::error::LibraryError; | ||
use valence_middleware_utils::type_registry::types::{NativeTypeWrapper, RegistryQueryMsg}; | ||
|
||
use crate::{ | ||
msg::{Config, FunctionMsgs, InstantiateMsg, LibraryConfig, QueryMsg}, | ||
state::{PendingQueryIdConfig, ASSOCIATED_QUERY_IDS, QUERY_RESULTS}, | ||
}; | ||
|
||
// version info for migration info | ||
const _CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); | ||
const _CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
pub type QueryDeps<'a> = Deps<'a, NeutronQuery>; | ||
pub type ExecuteDeps<'a> = DepsMut<'a, NeutronQuery>; | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn instantiate( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: InstantiateMsg, | ||
) -> Result<Response<NeutronMsg>, LibraryError> { | ||
Ok(Response::default()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set contract version here? |
||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn execute( | ||
deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
msg: FunctionMsgs, | ||
) -> Result<Response<NeutronMsg>, LibraryError> { | ||
match msg { | ||
FunctionMsgs::RegisterKvQuery { | ||
broker_addr, | ||
registry_version, | ||
type_id, | ||
connection_id, | ||
params, | ||
} => register_kv_query( | ||
deps, | ||
broker_addr, | ||
registry_version, | ||
type_id, | ||
connection_id, | ||
params, | ||
), | ||
} | ||
} | ||
|
||
fn register_kv_query( | ||
deps: DepsMut, | ||
broker_addr: String, | ||
registry_version: Option<String>, | ||
type_id: String, | ||
connection_id: String, | ||
params: BTreeMap<String, Binary>, | ||
) -> Result<Response<NeutronMsg>, LibraryError> { | ||
let query_kv_key: KVKey = deps.querier.query_wasm_smart( | ||
broker_addr.to_string(), | ||
&valence_middleware_broker::msg::QueryMsg { | ||
registry_version: registry_version.clone(), | ||
query: RegistryQueryMsg::KVKey { | ||
type_id: type_id.to_string(), | ||
params, | ||
}, | ||
}, | ||
)?; | ||
|
||
let kv_registration_msg = NeutronMsg::RegisterInterchainQuery { | ||
query_type: QueryType::KV.into(), | ||
keys: vec![query_kv_key], | ||
transactions_filter: String::new(), | ||
connection_id, | ||
update_period: 5, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should update_period be hardcoded? |
||
}; | ||
|
||
// here the key is set to the resp.reply_id just to get to the reply handler. | ||
// it will get overriden by the actual query id in the reply handler. | ||
ASSOCIATED_QUERY_IDS.save( | ||
deps.storage, | ||
1, | ||
&PendingQueryIdConfig { | ||
broker_addr, | ||
type_url: type_id, | ||
registry_version, | ||
}, | ||
)?; | ||
|
||
// fire registration in a submsg to get the registered query id back | ||
let submsg = SubMsg::reply_on_success(kv_registration_msg, 1); | ||
|
||
Ok(Response::default().add_submessage(submsg)) | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> { | ||
match msg { | ||
QueryMsg::Ownership {} => { | ||
to_json_binary(&valence_library_base::get_ownership(deps.storage)?) | ||
} | ||
QueryMsg::GetProcessor {} => { | ||
to_json_binary(&valence_library_base::get_processor(deps.storage)?) | ||
} | ||
QueryMsg::GetLibraryConfig {} => { | ||
let config: Config = valence_library_base::load_config(deps.storage)?; | ||
to_json_binary(&config) | ||
} | ||
QueryMsg::GetRawLibraryConfig {} => { | ||
let raw_config: LibraryConfig = | ||
valence_library_utils::raw_config::query_raw_library_config(deps.storage)?; | ||
to_json_binary(&raw_config) | ||
} | ||
QueryMsg::RegisteredQueries {} => { | ||
let mut resp = vec![]; | ||
for entry in ASSOCIATED_QUERY_IDS.range(deps.storage, None, None, Order::Ascending) { | ||
resp.push(entry?); | ||
} | ||
to_json_binary(&resp) | ||
Comment on lines
+133
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can just do .filter_map(Result::ok)
.map(|(_, entry)| entry)
.collect() for these queries |
||
} | ||
QueryMsg::QueryResults {} => { | ||
let mut resp = vec![]; | ||
for entry in QUERY_RESULTS.range(deps.storage, None, None, Order::Ascending) { | ||
resp.push(entry?); | ||
} | ||
to_json_binary(&resp) | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn sudo(deps: ExecuteDeps, _env: Env, msg: SudoMsg) -> StdResult<Response<NeutronMsg>> { | ||
match msg { | ||
SudoMsg::KVQueryResult { query_id } => handle_sudo_kv_query_result(deps, query_id), | ||
_ => Ok(Response::default()), | ||
} | ||
} | ||
|
||
fn handle_sudo_kv_query_result( | ||
deps: ExecuteDeps, | ||
query_id: u64, | ||
) -> StdResult<Response<NeutronMsg>> { | ||
let registered_query_result = get_raw_interchain_query_result(deps.as_ref(), query_id) | ||
.map_err(|_| StdError::generic_err("failed to get the raw icq result"))?; | ||
|
||
let pending_query_config = ASSOCIATED_QUERY_IDS.load(deps.storage, query_id)?; | ||
|
||
let reconstruction_response: NativeTypeWrapper = deps.querier.query_wasm_smart( | ||
pending_query_config.broker_addr, | ||
&valence_middleware_broker::msg::QueryMsg { | ||
registry_version: pending_query_config.registry_version, | ||
query: RegistryQueryMsg::ReconstructProto { | ||
type_id: pending_query_config.type_url, | ||
icq_result: registered_query_result.result, | ||
}, | ||
}, | ||
)?; | ||
|
||
QUERY_RESULTS.save(deps.storage, query_id, &reconstruction_response.binary)?; | ||
|
||
Ok(Response::new().add_attribute( | ||
"query_result", | ||
to_json_binary(&reconstruction_response)?.to_string(), | ||
)) | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn reply(deps: DepsMut, _: Env, msg: Reply) -> StdResult<Response> { | ||
try_associate_registered_query_id(deps, msg) | ||
} | ||
|
||
fn try_associate_registered_query_id(deps: DepsMut, reply: Reply) -> StdResult<Response> { | ||
let submsg_response = reply.result.into_result().map_err(StdError::generic_err)?; | ||
|
||
// response.data is deprecated | ||
// TODO: look into whether it's possible to use the cw2.0 method | ||
#[allow(deprecated)] | ||
let binary = submsg_response | ||
.data | ||
.ok_or_else(|| StdError::generic_err("no data in reply"))?; | ||
|
||
let resp: MsgRegisterInterchainQueryResponse = | ||
serde_json_wasm::from_slice(binary.as_slice()) | ||
.map_err(|e| StdError::generic_err(e.to_string()))?; | ||
|
||
let pending_query_config = ASSOCIATED_QUERY_IDS.load(deps.storage, reply.id)?; | ||
ASSOCIATED_QUERY_IDS.save(deps.storage, resp.id, &pending_query_config)?; | ||
ASSOCIATED_QUERY_IDS.remove(deps.storage, reply.id); | ||
|
||
Ok(Response::default()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe add the default-features flag in the contract?