Skip to content

Commit

Permalink
fix interchain queries
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Oct 15, 2024
1 parent 6d7d7d2 commit 584a411
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 75 deletions.
62 changes: 13 additions & 49 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ overflow-checks = true

[workspace.dependencies]
neutron-sdk = { package = "neutron-sdk", git = "https://github.com/neutron-org/neutron-sdk", branch = "feat/clean-bindings" }
neutron-std = { git = "https://github.com/neutron-org/neutron-std", rev = "f8168e5727bf8d397b144e700b9c22af40c9710c" }
neutron-std = { git = "https://github.com/neutron-org/neutron-std", branch = "feat/clean-bindings" }

prost = "0.12.4"
prost-types = "0.12.4"
Expand Down
48 changes: 27 additions & 21 deletions contracts/neutron_interchain_queries/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use neutron_sdk::interchain_queries::get_registered_query;
use neutron_sdk::interchain_queries::types::{
QueryPayload, TransactionFilterItem, TransactionFilterOp, TransactionFilterValue,
};
use neutron_sdk::interchain_queries::helpers::register_interchain_query;
use neutron_sdk::interchain_queries::v045::types::HEIGHT_FIELD;
use neutron_sdk::interchain_queries::v047::queries::{
query_balance, query_bank_total, query_delegations, query_distribution_fee_pool,
Expand All @@ -50,7 +51,9 @@ use neutron_sdk::interchain_queries::v047::types::{COSMOS_SDK_TRANSFER_MSG_URL,
use neutron_sdk::sudo::msg::SudoMsg;
use neutron_sdk::{NeutronError, NeutronResult};
use neutron_sdk::bindings::types::Height;
use neutron_std::types::neutron::interchainqueries::KvKey;
use prost::Message as ProstMessage;
use neutron_sdk::interchain_queries::helpers::{update_interchain_query as helpers_update_interchain_query, remove_interchain_query as helpers_remove_interchain_query};

/// defines the incoming transfers limit to make a case of failed callback possible.
const MAX_ALLOWED_TRANSFER: u64 = 20000;
Expand Down Expand Up @@ -145,14 +148,14 @@ pub fn execute(
recipients,
update_period,
min_height,
} => register_transfers_query(connection_id, recipients, update_period, min_height),
} => register_transfers_query(env.contract.address, connection_id, recipients, update_period, min_height),
ExecuteMsg::UpdateInterchainQuery {
query_id,
new_keys,
new_update_period,
new_recipient,
} => update_interchain_query(query_id, new_keys, new_update_period, new_recipient),
ExecuteMsg::RemoveInterchainQuery { query_id } => remove_interchain_query(query_id),
} => update_interchain_query(env.contract.address, query_id, new_keys, new_update_period, new_recipient),
ExecuteMsg::RemoveInterchainQuery { query_id } => remove_interchain_query(env.contract.address, query_id),
ExecuteMsg::IntegrationTestsSetQueryMock {} => set_query_mock(deps),
ExecuteMsg::IntegrationTestsUnsetQueryMock {} => unset_query_mock(deps),
ExecuteMsg::IntegrationTestsRegisterQueryEmptyId { connection_id } => {
Expand Down Expand Up @@ -293,6 +296,7 @@ pub fn register_validators_signing_infos_query(
}

pub fn register_transfers_query(
contract: Addr,
connection_id: String,
recipients: Vec<String>,
update_period: u64,
Expand All @@ -316,6 +320,7 @@ pub fn register_transfers_query(
}

let msg = register_interchain_query(
contract,
QueryPayload::TX(query_data),
connection_id,
update_period,
Expand All @@ -326,46 +331,47 @@ pub fn register_transfers_query(

pub fn register_query_empty_id(
_: DepsMut,
_: Env,
env: Env,
connection_id: String,
) -> NeutronResult<Response> {
let kv_key = KVKey {
let kv_key = KvKey {
path: "test".to_string(),
key: Binary::new(vec![]),
key: vec![],
};
let msg =
register_interchain_query(QueryPayload::KV(vec![kv_key]), connection_id, 10)?;
register_interchain_query(env.contract.address, QueryPayload::KV(vec![kv_key]), connection_id, 10)?;

Ok(Response::new().add_message(msg))
}

pub fn register_query_empty_path(
_: DepsMut,
_: Env,
env: Env,
connection_id: String,
) -> NeutronResult<Response> {
let kv_key = KVKey {
let kv_key = KvKey {
path: "".to_string(),
key: Binary::new("test".as_bytes().to_vec()),
key: "test".as_bytes().to_vec(),
};
let msg =
register_interchain_query(QueryPayload::KV(vec![kv_key]), connection_id, 10)?;
register_interchain_query(env.contract.address, QueryPayload::KV(vec![kv_key]), connection_id, 10)?;
Ok(Response::new().add_message(msg))
}

pub fn register_query_empty_keys(
_: DepsMut,
_: Env,
env: Env,
connection_id: String,
) -> NeutronResult<Response> {
let msg = register_interchain_query(QueryPayload::KV(vec![]), connection_id, 10)?;
let msg = register_interchain_query(env.contract.address, QueryPayload::KV(vec![]), connection_id, 10)?;
Ok(Response::new().add_message(msg))
}

pub fn update_interchain_query(
contract: Addr,
query_id: u64,
new_keys: Option<Vec<KVKey>>,
new_update_period: Option<u64>,
new_keys: Vec<KvKey>,
new_update_period: u64,
new_recipient: Option<String>,
) -> NeutronResult<Response> {
let new_filter = new_recipient.map(|recipient| {
Expand All @@ -377,12 +383,12 @@ pub fn update_interchain_query(
});

let update_msg =
update_interchain_query(query_id, new_keys, new_update_period, new_filter)?;
helpers_update_interchain_query(contract, query_id, new_keys, new_update_period, new_filter)?;
Ok(Response::new().add_message(update_msg))
}

pub fn remove_interchain_query(query_id: u64) -> NeutronResult<Response> {
let remove_msg = remove_interchain_query(query_id);
pub fn remove_interchain_query(contract: Addr, query_id: u64) -> NeutronResult<Response> {
let remove_msg = helpers_remove_interchain_query(contract, query_id)?;
Ok(Response::new().add_message(remove_msg))
}

Expand Down Expand Up @@ -487,17 +493,17 @@ pub fn sudo_tx_query_result(
let body: TxBody = TxBody::decode(tx.body_bytes.as_slice())?;

// Get the registered query by ID and retrieve the raw query string
let registered_query: QueryRegisteredQueryResponse =
let registered_query =
get_registered_query(deps.as_ref(), query_id)?;
let transactions_filter = registered_query.registered_query.transactions_filter;
let transactions_filter = registered_query.transactions_filter;

#[allow(clippy::match_single_binding)]
// Depending of the query type, check the transaction data to see whether is satisfies
// the original query. If you don't write specific checks for a transaction query type,
// all submitted results will be treated as valid.
//
// TODO: come up with solution to determine transactions filter type
match registered_query.registered_query.query_type {
match registered_query.query_type {
_ => {
// For transfer queries, query data looks like `[{"field:"transfer.recipient", "op":"eq", "value":"some_address"}]`
let query_data: Vec<TransactionFilterItem> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::state::{IntegrationTestsQueryMock, INTEGRATION_TESTS_QUERY_MOCK};
use cosmwasm_std::{DepsMut, Response};
use neutron_sdk::bindings::msg::NeutronMsg;
use neutron_sdk::bindings::query::NeutronQuery;
use neutron_sdk::NeutronResult;

pub fn set_query_mock(deps: DepsMut) -> NeutronResult<Response> {
Expand Down
4 changes: 2 additions & 2 deletions contracts/neutron_interchain_queries/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub enum ExecuteMsg {
},
UpdateInterchainQuery {
query_id: u64,
new_keys: Option<Vec<KvKey>>,
new_update_period: Option<u64>,
new_keys: Vec<KvKey>,
new_update_period: u64,
new_recipient: Option<String>,
},
RemoveInterchainQuery {
Expand Down

0 comments on commit 584a411

Please sign in to comment.