Skip to content

Commit

Permalink
fix register_queries in v047
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Oct 10, 2024
1 parent 9cc816c commit f6a46d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
12 changes: 5 additions & 7 deletions contracts/neutron_interchain_queries/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use cosmos_sdk_proto::cosmos::bank::v1beta1::MsgSend;
use cosmos_sdk_proto::cosmos::tx::v1beta1::{TxBody, TxRaw};
use cosmos_sdk_proto::traits::Message;
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError,
StdResult, Uint128,
};
use cosmwasm_std::{entry_point, to_json_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, Uint128};
use cw2::set_contract_version;
use neutron_sdk::interchain_queries::v047::register_queries::new_register_validators_signing_infos_query_msg;

Expand All @@ -13,7 +10,6 @@ use crate::msg::{
};
use crate::state::{Transfer, RECIPIENT_TXS, TRANSFERS};
use neutron_sdk::bindings::msg::NeutronMsg;
use neutron_sdk::bindings::query::{NeutronQuery, QueryRegisteredQueryResponse};
use neutron_sdk::bindings::types::{Height, KVKey};
use neutron_sdk::interchain_queries::v047::queries::{
query_balance, query_bank_total, query_delegations, query_distribution_fee_pool,
Expand Down Expand Up @@ -64,7 +60,7 @@ pub fn instantiate(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut,
_env: Env,
env: Env,
_: MessageInfo,
msg: ExecuteMsg,
) -> NeutronResult<Response> {
Expand Down Expand Up @@ -104,7 +100,7 @@ pub fn execute(
delegator,
validators,
update_period,
} => register_delegations_query(connection_id, delegator, validators, update_period),
} => register_delegations_query(env.contract.address, connection_id, delegator, validators, update_period),
ExecuteMsg::RegisterDelegatorUnbondingDelegationsQuery {
connection_id,
delegator,
Expand Down Expand Up @@ -205,12 +201,14 @@ pub fn register_validators_signing_infos_query(
}

pub fn register_delegations_query(
contract: Addr,
connection_id: String,
delegator: String,
validators: Vec<String>,
update_period: u64,
) -> NeutronResult<Response> {
let msg = new_register_delegator_delegations_query_msg(
contract,
connection_id,
delegator,
validators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ pub use crate::interchain_queries::v045::register_queries::*;
use crate::bindings::msg::NeutronMsg;
use crate::bindings::types::KVKey;
use crate::interchain_queries::helpers::decode_and_convert;
use crate::interchain_queries::types::QueryPayload;
use crate::interchain_queries::types::{QueryPayload, QueryType};
use crate::interchain_queries::v045::helpers::{create_delegation_key, create_validator_key};
use crate::interchain_queries::v045::types::STAKING_STORE_KEY;
use crate::interchain_queries::v047::types::STAKING_PARAMS_KEY;
use crate::NeutronResult;
use cosmwasm_std::Binary;
use cosmwasm_std::{Addr, Binary, CosmosMsg};
use neutron_std::types::neutron::interchainqueries::{MsgRegisterInterchainQuery, KvKey};

/// Creates a message to register an Interchain Query to get delegations of particular delegator on remote chain.
///
Expand All @@ -19,40 +20,48 @@ use cosmwasm_std::Binary;
/// * **validators** is a list of validators addresses for which you want to get delegations from particular **delegator**;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_delegator_delegations_query_msg(
contract: Addr,
connection_id: String,
delegator: String,
validators: Vec<String>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
) -> NeutronResult<dyn Into<CosmosMsg>> {
let delegator_addr = decode_and_convert(&delegator)?;

// Allocate memory for such KV keys as:
// * staking module params to get staking denomination
// * validators structures to calculate amount of delegated tokens
// * delegations structures to get info about delegations itself
let mut keys: Vec<KVKey> = Vec::with_capacity(validators.len() * 2 + 1);
let mut keys: Vec<KvKey> = Vec::with_capacity(validators.len() * 2 + 1);

// create KV key to get Staking Params from staking module
keys.push(KVKey {
keys.push(KvKey {
path: STAKING_STORE_KEY.to_string(),
key: Binary::new(vec![STAKING_PARAMS_KEY]),
key: vec![STAKING_PARAMS_KEY],
});

for v in validators {
let val_addr = decode_and_convert(&v)?;

// create delegation key to get delegation structure
keys.push(KVKey {
keys.push(KvKey {
path: STAKING_STORE_KEY.to_string(),
key: Binary::new(create_delegation_key(&delegator_addr, &val_addr)?),
key: create_delegation_key(&delegator_addr, &val_addr)?,
});

// create validator key to get validator structure
keys.push(KVKey {
keys.push(KvKey {
path: STAKING_STORE_KEY.to_string(),
key: Binary::new(create_validator_key(&val_addr)?),
key: create_validator_key(&val_addr)?,
})
}

NeutronMsg::register_interchain_query(QueryPayload::KV(keys), connection_id, update_period)
Ok(MsgRegisterInterchainQuery{
query_type: QueryType::KV.to_string(),
keys,
transactions_filter: "".to_string(),
connection_id,
update_period,
sender: contract.to_string(),
})
}

0 comments on commit f6a46d8

Please sign in to comment.