From dac675a6913ccb1ad2e4a763232733e1103207b8 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Thu, 22 Feb 2024 09:02:18 +0100 Subject: [PATCH 01/13] feat: improve balances query --- .../src/contract.rs | 4 +-- .../src/interchain_queries/v045/mod.rs | 2 +- .../v045/register_queries.rs | 30 ++++++++++--------- .../src/interchain_queries/v045/types.rs | 8 +++-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/contracts/neutron_interchain_queries/src/contract.rs b/contracts/neutron_interchain_queries/src/contract.rs index 9b49a41e..d4fb3b86 100644 --- a/contracts/neutron_interchain_queries/src/contract.rs +++ b/contracts/neutron_interchain_queries/src/contract.rs @@ -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, @@ -128,7 +128,7 @@ pub fn register_balance_query( denom: String, update_period: u64, ) -> NeutronResult> { - 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)) } diff --git a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs index c9ea40df..c39112de 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs @@ -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, diff --git a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs index 135d79e6..f32d9d5c 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs @@ -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, update_period: u64, ) -> NeutronResult { 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 = 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) + } + + 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 diff --git a/packages/neutron-sdk/src/interchain_queries/v045/types.rs b/packages/neutron-sdk/src/interchain_queries/v045/types.rs index 24555633..d82051e3 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/types.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/types.rs @@ -104,9 +104,11 @@ impl KVReconstruct for Balances { let mut coins: Vec = 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 }) From f8b167fe1eb42e0619f74ec6fc3b777b6ebf2776 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Thu, 22 Feb 2024 09:43:55 +0100 Subject: [PATCH 02/13] chore: return old method --- .../interchain_queries/v045/register_queries.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs index f32d9d5c..d7492f4c 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs @@ -47,6 +47,21 @@ pub fn new_register_balances_query_msg( NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, update_period) } +/// 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. +pub fn new_register_balance_query_msg( + connection_id: String, + addr: String, + denom: String, + update_period: u64, +) -> NeutronResult { + 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 /// /// * **connection_id** is an IBC connection identifier between Neutron and remote chain; From a5c2d894829f497ae32b44b2c37f7415310055b5 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Thu, 22 Feb 2024 12:01:53 +0100 Subject: [PATCH 03/13] chore: deprecate old method --- packages/neutron-sdk/src/interchain_queries/v045/mod.rs | 9 +++++---- .../src/interchain_queries/v045/register_queries.rs | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs index c39112de..31eb9e63 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs @@ -3,11 +3,12 @@ pub mod queries; pub mod register_queries; pub mod types; +#[allow(deprecated)] pub use register_queries::{ - 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, + 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_distribution_fee_pool_query_msg, new_register_gov_proposal_query_msg, + new_register_staking_validators_query_msg, new_register_transfers_query_msg, }; #[cfg(test)] diff --git a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs index d7492f4c..49e0ea47 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs @@ -53,6 +53,7 @@ pub fn new_register_balances_query_msg( /// * **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, From 7f8fcc806033981e1c61c5b249ac80df897a74ba Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Thu, 22 Feb 2024 12:29:58 +0100 Subject: [PATCH 04/13] feat: keys helper for balances query --- .../neutron-sdk/src/interchain_queries/v045/mod.rs | 2 +- .../src/interchain_queries/v045/register_queries.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs index 31eb9e63..0c068f04 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs @@ -5,7 +5,7 @@ pub mod types; #[allow(deprecated)] pub use register_queries::{ - new_register_balance_query_msg, new_register_balances_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_distribution_fee_pool_query_msg, new_register_gov_proposal_query_msg, new_register_staking_validators_query_msg, new_register_transfers_query_msg, diff --git a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs index 49e0ea47..1eecb990 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs @@ -29,8 +29,16 @@ pub fn new_register_balances_query_msg( denoms: Vec, update_period: u64, ) -> NeutronResult { - let converted_addr_bytes = decode_and_convert(addr.as_str())?; + 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) -> NeutronResult> { + let converted_addr_bytes = decode_and_convert(addr.as_str())?; let mut kv_keys: Vec = Vec::with_capacity(denoms.len()); for denom in denoms { @@ -43,8 +51,7 @@ pub fn new_register_balances_query_msg( kv_keys.push(kv_key) } - - NeutronMsg::register_interchain_query(QueryPayload::KV(kv_keys), connection_id, update_period) + Ok(kv_keys) } /// Creates a message to register an Interchain Query to get balance of account on remote chain for a particular denom From 0f396a9d606aaf67046137e8be47077e8688c77f Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Thu, 22 Feb 2024 15:32:00 +0100 Subject: [PATCH 05/13] feat: test for empty value for Balances --- .../src/interchain_queries/v045/testing.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/testing.rs b/packages/neutron-sdk/src/interchain_queries/v045/testing.rs index 10648784..76dfb881 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/testing.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/testing.rs @@ -920,6 +920,17 @@ fn test_balance_reconstruct_from_hex() { ); } +#[test] +fn test_balance_reconstruct_from_empty_value() { + let s = StorageValue { + storage_prefix: String::default(), // not used in reconstruct + key: Binary::default(), // not used in reconstruct + value: Binary::from(vec![]), + }; + let bank_balances = Balances::reconstruct(&[s]).unwrap(); + assert_eq!(bank_balances, Balances { coins: vec![] }); +} + #[test] fn test_bank_total_supply_reconstruct_from_hex() { let bytes = hex::decode(TOTAL_SUPPLY_HEX_RESPONSE).unwrap(); // decode hex string to bytes From cb6dd54bbeb2f8999ec6e3e468878bca566aa91b Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Fri, 31 May 2024 12:01:18 +0300 Subject: [PATCH 06/13] move create_balances_query_keys to helpers.rs --- .../src/interchain_queries/v045/helpers.rs | 27 ++++++++++++++++--- .../src/interchain_queries/v045/mod.rs | 2 +- .../v045/register_queries.rs | 25 ++--------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs b/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs index 07da09f0..25934a9a 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs @@ -3,9 +3,9 @@ use crate::errors::error::NeutronResult; use crate::interchain_queries::helpers::{decode_and_convert, length_prefix}; use crate::interchain_queries::types::AddressBytes; use crate::interchain_queries::v045::types::{ - BALANCES_PREFIX, DELEGATION_KEY, FEE_POOL_KEY, PARAMS_STORE_DELIMITER, PROPOSALS_KEY_PREFIX, - SUPPLY_PREFIX, UNBONDING_DELEGATION_KEY, VALIDATORS_KEY, VALIDATOR_SIGNING_INFO_KEY, - WASM_CONTRACT_STORE_PREFIX, + BALANCES_PREFIX, BANK_STORE_KEY, DELEGATION_KEY, FEE_POOL_KEY, PARAMS_STORE_DELIMITER, + PROPOSALS_KEY_PREFIX, SUPPLY_PREFIX, UNBONDING_DELEGATION_KEY, VALIDATORS_KEY, + VALIDATOR_SIGNING_INFO_KEY, WASM_CONTRACT_STORE_PREFIX, }; use crate::NeutronError; use cosmos_sdk_proto::cosmos::staking::v1beta1::Commission as ValidatorCommission; @@ -46,6 +46,27 @@ pub fn create_account_denom_balance_key, S: AsRef>( Ok(account_balance_key) } +/// Creates 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 create_balances_query_keys(addr: String, denoms: Vec) -> NeutronResult> { + let converted_addr_bytes = decode_and_convert(addr.as_str())?; + let mut kv_keys: Vec = Vec::with_capacity(denoms.len()); + + 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), + }; + + kv_keys.push(kv_key) + } + Ok(kv_keys) +} + /// Deconstructs a storage key for an **account** balance of a particular **denom**. /// Returns two values: **address** of an account and **denom** pub fn deconstruct_account_denom_balance_key>( diff --git a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs index f019eb9c..4dfdbe4c 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/mod.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/mod.rs @@ -5,7 +5,7 @@ pub mod types; #[allow(deprecated)] pub use register_queries::{ - get_balances_query_keys, new_register_balance_query_msg, new_register_balances_query_msg, + 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_proposals_query_msg, diff --git a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs index b7c9b63d..0169b8d2 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/register_queries.rs @@ -10,7 +10,7 @@ use crate::{ errors::error::NeutronResult, interchain_queries::helpers::decode_and_convert, interchain_queries::v045::helpers::{ - create_account_denom_balance_key, create_delegation_key, create_fee_pool_key, + create_balances_query_keys, create_delegation_key, create_fee_pool_key, create_gov_proposal_keys, create_gov_proposals_voters_votes_keys, create_params_store_key, create_total_denom_key, create_unbonding_delegation_key, create_validator_key, create_validator_signing_info_key, create_wasm_contract_store_key, @@ -30,31 +30,10 @@ pub fn new_register_balances_query_msg( denoms: Vec, update_period: u64, ) -> NeutronResult { - let kv_keys = get_balances_query_keys(addr, denoms)?; + let kv_keys = create_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) -> NeutronResult> { - let converted_addr_bytes = decode_and_convert(addr.as_str())?; - let mut kv_keys: Vec = Vec::with_capacity(denoms.len()); - - 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), - }; - - 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; From 3d93bb86ee294de3d47761877fdb868f327d989d Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Fri, 31 May 2024 12:02:10 +0300 Subject: [PATCH 07/13] adapt ICQ contract's code and tests to plural balances ICQ --- .../src/contract.rs | 12 +-- .../neutron_interchain_queries/src/msg.rs | 4 +- .../src/testing/tests.rs | 76 +++++++++++++++---- 3 files changed, 71 insertions(+), 21 deletions(-) diff --git a/contracts/neutron_interchain_queries/src/contract.rs b/contracts/neutron_interchain_queries/src/contract.rs index 6588df0e..53e94f0d 100644 --- a/contracts/neutron_interchain_queries/src/contract.rs +++ b/contracts/neutron_interchain_queries/src/contract.rs @@ -69,12 +69,12 @@ pub fn execute( msg: ExecuteMsg, ) -> NeutronResult> { match msg { - ExecuteMsg::RegisterBalanceQuery { + ExecuteMsg::RegisterBalancesQuery { connection_id, addr, - denom, + denoms, update_period, - } => register_balance_query(connection_id, addr, denom, update_period), + } => register_balances_query(connection_id, addr, denoms, update_period), ExecuteMsg::RegisterBankTotalSupplyQuery { connection_id, denoms, @@ -143,13 +143,13 @@ pub fn execute( } } -pub fn register_balance_query( +pub fn register_balances_query( connection_id: String, addr: String, - denom: String, + denoms: Vec, update_period: u64, ) -> NeutronResult> { - let msg = new_register_balances_query_msg(connection_id, addr, vec![denom], update_period)?; + let msg = new_register_balances_query_msg(connection_id, addr, denoms, update_period)?; Ok(Response::new().add_message(msg)) } diff --git a/contracts/neutron_interchain_queries/src/msg.rs b/contracts/neutron_interchain_queries/src/msg.rs index 79170e54..6f6b5cb6 100644 --- a/contracts/neutron_interchain_queries/src/msg.rs +++ b/contracts/neutron_interchain_queries/src/msg.rs @@ -10,11 +10,11 @@ pub struct InstantiateMsg {} #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum ExecuteMsg { - RegisterBalanceQuery { + RegisterBalancesQuery { connection_id: String, update_period: u64, addr: String, - denom: String, + denoms: Vec, }, RegisterBankTotalSupplyQuery { connection_id: String, diff --git a/contracts/neutron_interchain_queries/src/testing/tests.rs b/contracts/neutron_interchain_queries/src/testing/tests.rs index 3bc0bb59..55f55829 100644 --- a/contracts/neutron_interchain_queries/src/testing/tests.rs +++ b/contracts/neutron_interchain_queries/src/testing/tests.rs @@ -212,20 +212,27 @@ fn build_interchain_query_gov_proposal_value(proposal_id: u64) -> StorageValue { } } -fn build_interchain_query_balance_response(addr: Addr, denom: String, amount: String) -> Binary { +fn build_interchain_query_balances_response(addr: Addr, balances: Vec) -> Binary { let converted_addr_bytes = decode_and_convert(addr.as_str()).unwrap(); - let balance_key = create_account_denom_balance_key(converted_addr_bytes, denom).unwrap(); + let s: Vec = balances + .iter() + .map(|c| { + let balance_key = + create_account_denom_balance_key(converted_addr_bytes.clone(), c.denom.clone()) + .unwrap(); + StorageValue { + storage_prefix: "".to_string(), + key: Binary(balance_key), + value: Binary(c.amount.to_string().into_bytes()), + } + }) + .collect(); - let s = StorageValue { - storage_prefix: "".to_string(), - key: Binary(balance_key), - value: Binary(amount.into_bytes()), - }; Binary::from( to_string(&QueryRegisteredQueryResultResponse { result: InterchainQueryResult { - kv_results: vec![s], + kv_results: s, height: 123456, revision: 2, }, @@ -256,11 +263,11 @@ fn register_query( fn test_query_balance() { let mut deps = dependencies(&[]); - let msg = ExecuteMsg::RegisterBalanceQuery { + let msg = ExecuteMsg::RegisterBalancesQuery { connection_id: "connection".to_string(), update_period: 10, addr: "osmo1yz54ncxj9csp7un3xled03q6thrrhy9cztkfzs".to_string(), - denom: "uosmo".to_string(), + denoms: vec!["uosmo".to_string()], }; let keys = register_query(&mut deps, mock_env(), mock_info("", &[]), msg); @@ -271,10 +278,9 @@ fn test_query_balance() { deps.querier.add_registered_queries(1, registered_query); deps.querier.add_query_response( 1, - build_interchain_query_balance_response( + build_interchain_query_balances_response( Addr::unchecked("osmo1yz54ncxj9csp7un3xled03q6thrrhy9cztkfzs"), - "uosmo".to_string(), - "8278104".to_string(), + vec![Coin::new(8278104u128, "uosmo")], ), ); let query_balance = QueryMsg::Balance { query_id: 1 }; @@ -291,6 +297,50 @@ fn test_query_balance() { ) } +#[test] +fn test_query_balances() { + let mut deps = dependencies(&[]); + + let msg = ExecuteMsg::RegisterBalancesQuery { + connection_id: "connection".to_string(), + update_period: 10, + addr: "osmo1yz54ncxj9csp7un3xled03q6thrrhy9cztkfzs".to_string(), + denoms: vec!["uosmo".to_string(), "uatom".to_string()], + }; + + let keys = register_query(&mut deps, mock_env(), mock_info("", &[]), msg); + + let registered_query = + build_registered_query_response(1, QueryParam::Keys(keys.0), QueryType::KV, 987); + + deps.querier.add_registered_queries(1, registered_query); + deps.querier.add_query_response( + 1, + build_interchain_query_balances_response( + Addr::unchecked("osmo1yz54ncxj9csp7un3xled03q6thrrhy9cztkfzs"), + vec![ + Coin::new(8278104u128, "uosmo"), + Coin::new(1234567u128, "uatom"), + ], + ), + ); + let query_balance = QueryMsg::Balance { query_id: 1 }; + let resp: BalanceResponse = + from_json(query(deps.as_ref(), mock_env(), query_balance).unwrap()).unwrap(); + assert_eq!( + resp, + BalanceResponse { + last_submitted_local_height: 987, + balances: Balances { + coins: vec![ + Coin::new(8278104u128, "uosmo"), + Coin::new(1234567u128, "uatom") + ] + }, + } + ) +} + #[test] fn test_bank_total_supply_query() { let mut deps = dependencies(&[]); From 05bd36b4e4fed9e05b9f4c082ba4de1a3773c37a Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Fri, 31 May 2024 16:46:46 +0300 Subject: [PATCH 08/13] debug: add query_kv_result logging --- packages/neutron-sdk/src/interchain_queries/queries.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/neutron-sdk/src/interchain_queries/queries.rs b/packages/neutron-sdk/src/interchain_queries/queries.rs index 399cea7a..15cbe9fe 100644 --- a/packages/neutron-sdk/src/interchain_queries/queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/queries.rs @@ -36,6 +36,11 @@ pub fn query_kv_result( ) -> NeutronResult { let registered_query_result = get_raw_interchain_query_result(deps, query_id)?; + deps.api.debug(&format!( + "WASMDEBUG: got KV results to reconstruct: {:?}", + registered_query_result.result.kv_results + )); + KVReconstruct::reconstruct(®istered_query_result.result.kv_results) } From 8929ae5bcb076cc4c67a0f67c4ab6dc20e2b3fb8 Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Fri, 31 May 2024 16:56:59 +0300 Subject: [PATCH 09/13] Revert "debug: add query_kv_result logging" This reverts commit 05bd36b4e4fed9e05b9f4c082ba4de1a3773c37a. --- packages/neutron-sdk/src/interchain_queries/queries.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/neutron-sdk/src/interchain_queries/queries.rs b/packages/neutron-sdk/src/interchain_queries/queries.rs index 15cbe9fe..399cea7a 100644 --- a/packages/neutron-sdk/src/interchain_queries/queries.rs +++ b/packages/neutron-sdk/src/interchain_queries/queries.rs @@ -36,11 +36,6 @@ pub fn query_kv_result( ) -> NeutronResult { let registered_query_result = get_raw_interchain_query_result(deps, query_id)?; - deps.api.debug(&format!( - "WASMDEBUG: got KV results to reconstruct: {:?}", - registered_query_result.result.kv_results - )); - KVReconstruct::reconstruct(®istered_query_result.result.kv_results) } From 616225095a89947623b24c39fd6103678bc36357 Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Fri, 31 May 2024 17:34:21 +0300 Subject: [PATCH 10/13] fix v045 empty Balance reconstruct by adding 0 amount Coin to response instead of nothing --- .../src/interchain_queries/v045/testing.rs | 23 +++++++++++++++---- .../src/interchain_queries/v045/types.rs | 13 +++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/testing.rs b/packages/neutron-sdk/src/interchain_queries/v045/testing.rs index 75619d35..0fdba3a3 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/testing.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/testing.rs @@ -1005,10 +1005,15 @@ fn test_delegations_reconstruct() { fn test_balance_reconstruct_from_hex() { let bytes = hex::decode(BALANCES_HEX_RESPONSE).unwrap(); // decode hex string to bytes let base64_input = BASE64_STANDARD.encode(bytes); // encode bytes to base64 string + let balance_key = create_account_denom_balance_key( + decode_and_convert("osmo1yz54ncxj9csp7un3xled03q6thrrhy9cztkfzs").unwrap(), + "uosmo", + ) + .unwrap(); let s = StorageValue { storage_prefix: String::default(), // not used in reconstruct - key: Binary::default(), // not used in reconstruct + key: Binary::from(balance_key), value: Binary::from_base64(base64_input.as_str()).unwrap(), }; let bank_balances = Balances::reconstruct(&[s]).unwrap(); @@ -1016,7 +1021,7 @@ fn test_balance_reconstruct_from_hex() { bank_balances, Balances { coins: vec![StdCoin { - denom: String::from("stake"), + denom: String::from("uosmo"), amount: Uint128::from(99999000u64), }] } @@ -1025,13 +1030,23 @@ fn test_balance_reconstruct_from_hex() { #[test] fn test_balance_reconstruct_from_empty_value() { + let balance_key = create_account_denom_balance_key( + decode_and_convert("osmo1yz54ncxj9csp7un3xled03q6thrrhy9cztkfzs").unwrap(), + "uosmo", + ) + .unwrap(); let s = StorageValue { storage_prefix: String::default(), // not used in reconstruct - key: Binary::default(), // not used in reconstruct + key: Binary::from(balance_key), value: Binary::from(vec![]), }; let bank_balances = Balances::reconstruct(&[s]).unwrap(); - assert_eq!(bank_balances, Balances { coins: vec![] }); + assert_eq!( + bank_balances, + Balances { + coins: vec![StdCoin::new(0u128, "uosmo")] + } + ); } #[test] diff --git a/packages/neutron-sdk/src/interchain_queries/v045/types.rs b/packages/neutron-sdk/src/interchain_queries/v045/types.rs index d8300682..4b6c2655 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/types.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/types.rs @@ -1,5 +1,6 @@ use crate::interchain_queries::helpers::uint256_to_u128; use crate::interchain_queries::types::KVReconstruct; +use crate::interchain_queries::v045::helpers::deconstruct_account_denom_balance_key; use crate::{ bindings::types::StorageValue, errors::error::{NeutronError, NeutronResult}, @@ -112,11 +113,15 @@ impl KVReconstruct for Balances { let mut coins: Vec = Vec::with_capacity(storage_values.len()); for kv in storage_values { - if kv.value.len() > 0 { + let (_, denom) = deconstruct_account_denom_balance_key(kv.key.to_vec())?; + let amount = 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)); - } + Uint128::from_str(balance.amount.as_str())?.u128() + } else { + 0u128 + }; + + coins.push(Coin::new(amount, denom)) } Ok(Balances { coins }) From 5d668c3e68d15c6d49fc4e95cd828b205d080119 Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Fri, 31 May 2024 17:35:17 +0300 Subject: [PATCH 11/13] add empty balance test case for v047 Balance reconstruct --- .../src/interchain_queries/v047/testing.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/neutron-sdk/src/interchain_queries/v047/testing.rs b/packages/neutron-sdk/src/interchain_queries/v047/testing.rs index bcc0efa7..3d22e7b2 100644 --- a/packages/neutron-sdk/src/interchain_queries/v047/testing.rs +++ b/packages/neutron-sdk/src/interchain_queries/v047/testing.rs @@ -935,7 +935,7 @@ fn test_balance_reconstruct_from_hex() { let s = StorageValue { storage_prefix: String::default(), // not used in reconstruct - key: Binary(create_account_denom_balance_key("addr", "uatom").unwrap()), // not used in reconstruct + key: Binary(create_account_denom_balance_key("addr", "uatom").unwrap()), value: Binary::from_base64(base64_input.as_str()).unwrap(), }; let bank_balances = Balances::reconstruct(&[s]).unwrap(); @@ -947,6 +947,22 @@ fn test_balance_reconstruct_from_hex() { ); } +#[test] +fn test_balance_reconstruct_from_empty_value() { + let s = StorageValue { + storage_prefix: String::default(), // not used in reconstruct + key: Binary(create_account_denom_balance_key("addr", "uatom").unwrap()), + value: Binary::from(vec![]), + }; + let bank_balances = Balances::reconstruct(&[s]).unwrap(); + assert_eq!( + bank_balances, + Balances { + coins: vec![StdCoin::new(0u128, "uatom")] + } + ); +} + #[test] fn test_bank_total_supply_reconstruct_from_hex() { let bytes = hex::decode(TOTAL_SUPPLY_HEX_RESPONSE).unwrap(); // decode hex string to bytes From e2a6a3122c1f9b7f8e58323da20c53aee6052ee2 Mon Sep 17 00:00:00 2001 From: sotnikov-s Date: Mon, 3 Jun 2024 10:14:16 +0300 Subject: [PATCH 12/13] regenerate schemas --- .../schema/execute_msg.json | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/contracts/neutron_interchain_queries/schema/execute_msg.json b/contracts/neutron_interchain_queries/schema/execute_msg.json index b324b5cd..bc56043c 100644 --- a/contracts/neutron_interchain_queries/schema/execute_msg.json +++ b/contracts/neutron_interchain_queries/schema/execute_msg.json @@ -5,15 +5,15 @@ { "type": "object", "required": [ - "register_balance_query" + "register_balances_query" ], "properties": { - "register_balance_query": { + "register_balances_query": { "type": "object", "required": [ "addr", "connection_id", - "denom", + "denoms", "update_period" ], "properties": { @@ -23,8 +23,11 @@ "connection_id": { "type": "string" }, - "denom": { - "type": "string" + "denoms": { + "type": "array", + "items": { + "type": "string" + } }, "update_period": { "type": "integer", From f8472305235437ced9f927860e1e33c8434e4818 Mon Sep 17 00:00:00 2001 From: sotnikov-s <34917380+sotnikov-s@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:46:08 +0300 Subject: [PATCH 13/13] optimise addr passing for key creation in create_balances_query_keys Co-authored-by: Mike Mozhaev --- packages/neutron-sdk/src/interchain_queries/v045/helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs b/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs index 25934a9a..f8960862 100644 --- a/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs +++ b/packages/neutron-sdk/src/interchain_queries/v045/helpers.rs @@ -55,7 +55,7 @@ pub fn create_balances_query_keys(addr: String, denoms: Vec) -> NeutronR let mut kv_keys: Vec = Vec::with_capacity(denoms.len()); for denom in denoms { - let balance_key = create_account_denom_balance_key(converted_addr_bytes.clone(), denom)?; + let balance_key = create_account_denom_balance_key(&converted_addr_bytes, denom)?; let kv_key = KVKey { path: BANK_STORE_KEY.to_string(),