Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: improve balances query #130

Merged
merged 15 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/neutron_interchain_queries/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use neutron_sdk::interchain_queries::v045::queries::{
use neutron_sdk::interchain_queries::{
check_query_type, get_registered_query, query_kv_result,
v045::{
new_register_balance_query_msg, new_register_bank_total_supply_query_msg,
new_register_balances_query_msg, new_register_bank_total_supply_query_msg,
new_register_delegator_delegations_query_msg,
new_register_delegator_unbonding_delegations_query_msg,
new_register_distribution_fee_pool_query_msg, new_register_gov_proposal_query_msg,
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn register_balance_query(
denom: String,
update_period: u64,
) -> NeutronResult<Response<NeutronMsg>> {
let msg = new_register_balance_query_msg(connection_id, addr, denom, update_period)?;
let msg = new_register_balances_query_msg(connection_id, addr, vec![denom], update_period)?;

Ok(Response::new().add_message(msg))
}
Expand Down
5 changes: 3 additions & 2 deletions packages/neutron-sdk/src/interchain_queries/v045/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub mod queries;
pub mod register_queries;
pub mod types;

#[allow(deprecated)]
pub use register_queries::{
new_register_balance_query_msg, new_register_bank_total_supply_query_msg,
new_register_delegator_delegations_query_msg,
get_balances_query_keys, new_register_balance_query_msg, new_register_balances_query_msg,
new_register_bank_total_supply_query_msg, new_register_delegator_delegations_query_msg,
new_register_delegator_unbonding_delegations_query_msg,
new_register_distribution_fee_pool_query_msg, new_register_gov_proposal_query_msg,
new_register_staking_validators_query_msg, new_register_transfers_query_msg,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::helpers::create_validator_signing_info_key;
use super::types::SLASHING_STORE_KEY;
use crate::interchain_queries::types::{
QueryPayload, TransactionFilterItem, TransactionFilterOp, TransactionFilterValue,
};
Expand All @@ -17,35 +19,57 @@ use crate::{
};
use cosmwasm_std::Binary;

use super::helpers::create_validator_signing_info_key;
use super::types::SLASHING_STORE_KEY;

/// Creates a message to register an Interchain Query to get balance of account on remote chain for particular denom
/// Creates a message to register an Interchain Query to get balance of account on remote chain for list of denoms
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **addr** address of an account on remote chain for which you want to get balances;
/// * **denom** denomination of the coin for which you want to get balance;
/// * **denoms** denominations of the coins for which you want to get balance;
/// * **update_period** is used to say how often the query must be updated.
pub fn new_register_balance_query_msg(
pub fn new_register_balances_query_msg(
connection_id: String,
addr: String,
denom: String,
denoms: Vec<String>,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
let kv_keys = get_balances_query_keys(addr, denoms)?;
NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, update_period)
}

/// Creates a keys for an Interchain Query to get balance of account on remote chain for list of denoms
///
/// * **addr** address of an account on remote chain for which you want to get balances;
/// * **denoms** denominations of the coins for which you want to get balance;
pub fn get_balances_query_keys(addr: String, denoms: Vec<String>) -> NeutronResult<Vec<KVKey>> {
let converted_addr_bytes = decode_and_convert(addr.as_str())?;
let mut kv_keys: Vec<KVKey> = Vec::with_capacity(denoms.len());

let balance_key = create_account_denom_balance_key(converted_addr_bytes, denom)?;
for denom in denoms {
let balance_key = create_account_denom_balance_key(converted_addr_bytes.clone(), denom)?;

let kv_key = KVKey {
path: BANK_STORE_KEY.to_string(),
key: Binary(balance_key),
};
let kv_key = KVKey {
path: BANK_STORE_KEY.to_string(),
key: Binary(balance_key),
};

NeutronMsg::register_interchain_query(
QueryPayload::KV(vec![kv_key]),
connection_id,
update_period,
)
kv_keys.push(kv_key)
}
Ok(kv_keys)
}

/// Creates a message to register an Interchain Query to get balance of account on remote chain for a particular denom
///
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **addr** address of an account on remote chain for which you want to get balances;
/// * **denom** denomination of the coin for which you want to get balance;
/// * **update_period** is used to say how often the query must be updated.
#[deprecated(note = "Please use new_register_balances_query_msg instead")]
pub fn new_register_balance_query_msg(
connection_id: String,
addr: String,
denom: String,
update_period: u64,
) -> NeutronResult<NeutronMsg> {
new_register_balances_query_msg(connection_id, addr, vec![denom], update_period)
}

/// Creates a message to register an Interchain Query to get total supply on remote chain for particular denom
Expand Down
8 changes: 5 additions & 3 deletions packages/neutron-sdk/src/interchain_queries/v045/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ impl KVReconstruct for Balances {
let mut coins: Vec<Coin> = Vec::with_capacity(storage_values.len());

for kv in storage_values {
let balance: CosmosCoin = CosmosCoin::decode(kv.value.as_slice())?;
let amount = Uint128::from_str(balance.amount.as_str())?;
coins.push(Coin::new(amount.u128(), balance.denom));
if kv.value.len() > 0 {
let balance: CosmosCoin = CosmosCoin::decode(kv.value.as_slice())?;
let amount = Uint128::from_str(balance.amount.as_str())?;
coins.push(Coin::new(amount.u128(), balance.denom));
}
}

Ok(Balances { coins })
Expand Down
Loading