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 1 commit
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 @@ -21,7 +21,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_distribution_fee_pool_query_msg,
new_register_gov_proposal_query_msg, new_register_staking_validators_query_msg,
new_register_transfers_query_msg,
Expand Down Expand Up @@ -128,7 +128,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
2 changes: 1 addition & 1 deletion packages/neutron-sdk/src/interchain_queries/v045/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod register_queries;
pub mod types;

pub use register_queries::{
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_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
Expand Up @@ -17,32 +17,34 @@ use crate::{
};
use cosmwasm_std::Binary;

/// 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 converted_addr_bytes = decode_and_convert(addr.as_str())?;

let balance_key = create_account_denom_balance_key(converted_addr_bytes, denom)?;
let mut kv_keys: Vec<KVKey> = Vec::with_capacity(denoms.len());

let kv_key = KVKey {
path: BANK_STORE_KEY.to_string(),
key: Binary(balance_key),
};
for denom in denoms {
let balance_key = create_account_denom_balance_key(converted_addr_bytes.clone(), denom)?;

NeutronMsg::register_interchain_query(
QueryPayload::KV(vec![kv_key]),
connection_id,
update_period,
)
let kv_key = KVKey {
path: BANK_STORE_KEY.to_string(),
key: Binary(balance_key),
};

kv_keys.push(kv_key)
}
ratik marked this conversation as resolved.
Show resolved Hide resolved

NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, 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 @@ -104,9 +104,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