Skip to content

Commit

Permalink
remove duplicated submit_tx; move hex functions to helpers; add and c…
Browse files Browse the repository at this point in the history
…leanup comments
  • Loading branch information
NeverHappened committed Oct 28, 2024
1 parent 94567bb commit 7d44584
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 49 deletions.
5 changes: 3 additions & 2 deletions contracts/neutron_interchain_queries/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use cosmwasm_std::testing::{message_info, mock_env, MockApi, MockStorage};
use cosmwasm_std::{
from_json, Addr, Binary, Coin, Decimal, Env, MessageInfo, OwnedDeps, StdError, Uint128,
};
use neutron_sdk::interchain_queries::helpers::{decode_and_convert, kv_key_from_string};
use neutron_sdk::interchain_queries::helpers::{
decode_and_convert, decode_hex, kv_key_from_string,
};
use neutron_sdk::interchain_queries::types::{
QueryType, TransactionFilterItem, TransactionFilterOp, TransactionFilterValue,
};
Expand All @@ -39,7 +41,6 @@ use neutron_std::types::neutron::interchainqueries::{
RegisteredQuery, StorageValue,
};

use neutron_sdk::interchain_queries::hex::decode_hex;
use neutron_sdk::interchain_queries::v047::queries::{
BalanceResponse, DelegatorDelegationsResponse, FeePoolResponse, ProposalResponse,
TotalSupplyResponse, ValidatorResponse, ValidatorSigningInfoResponse,
Expand Down
29 changes: 3 additions & 26 deletions contracts/neutron_interchain_txs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::storage::{
};
use cosmos_sdk_proto::traits::Message;
use cosmwasm_std::{
to_json_binary, Addr, Binary, CosmosMsg, CustomQuery, Deps, DepsMut, Env, MessageInfo, Reply,
to_json_binary, Binary, CosmosMsg, CustomQuery, Deps, DepsMut, Env, MessageInfo, Reply,
Response, StdError, StdResult, SubMsg,
};
use cw2::set_contract_version;
use neutron_sdk::interchain_txs::helpers::register_interchain_account;
use neutron_sdk::interchain_txs::helpers::{register_interchain_account, submit_tx};
use neutron_sdk::{
interchain_txs::helpers::{decode_message_response, get_port_id},
interchain_txs::v047::helpers::decode_acknowledgement_response,
Expand All @@ -25,9 +25,7 @@ use neutron_std::types::cosmos::staking::v1beta1::{
};
use neutron_std::types::ibc::core::channel::v1::Order;
use neutron_std::types::neutron::feerefunder::{Fee, FeerefunderQuerier};
use neutron_std::types::neutron::interchaintxs::v1::{
InterchaintxsQuerier, MsgSubmitTx, MsgSubmitTxResponse,
};
use neutron_std::types::neutron::interchaintxs::v1::{InterchaintxsQuerier, MsgSubmitTxResponse};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -272,27 +270,6 @@ fn execute_delegate(
Ok(Response::default().add_submessages(vec![submsg]))
}

fn submit_tx(
contract: Addr,
connection_id: String,
interchain_account_id: String,
msgs: Vec<neutron_std::shim::Any>,
memo: String,
timeout: u64,
fee: Fee,
) -> CosmosMsg {
MsgSubmitTx {
from_address: contract.to_string(),
interchain_account_id,
connection_id,
msgs,
memo,
timeout,
fee: Some(fee),
}
.into()
}

fn execute_undelegate(
mut deps: DepsMut,
env: Env,
Expand Down
26 changes: 24 additions & 2 deletions packages/neutron-sdk/src/interchain_queries/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::errors::error::{NeutronError, NeutronResult};
use crate::interchain_queries::hex::decode_hex;
use crate::interchain_queries::types::{
AddressBytes, QueryPayload, QueryType, TransactionFilterItem, MAX_ADDR_LEN,
};
Expand All @@ -9,6 +8,7 @@ use neutron_std::types::neutron::interchainqueries::{
MsgUpdateInterchainQueryRequest,
};
use serde_json_wasm::to_string;
use std::fmt::Write as _;

/// Decodes a bech32 encoded string and converts to base64 encoded bytes
/// <https://github.com/cosmos/cosmos-sdk/blob/ad9e5620fb3445c716e9de45cfcdb56e8f1745bf/types/bech32/bech32.go#L20>
Expand Down Expand Up @@ -47,6 +47,8 @@ pub fn uint256_to_u128(value: Uint256) -> Result<u128, StdError> {
}

/// Basic helper to define a register interchain query message:
/// * **contract** is a contract address that registers the interchain query.
/// Must be equal to the contract that sends the message.
/// * **query** is a query type identifier ('tx' or 'kv' for now) with a payload:
/// - when the query enum is 'kv' then payload is the KV-storage keys for which we want to get
/// values from remote chain;
Expand Down Expand Up @@ -83,6 +85,8 @@ pub fn register_interchain_query(
}

/// Basic helper to define a update interchain query message:
/// * **contract** is a contract address that updates the interchain query.
/// Must be equal to the contract that sends the message.
/// * **query_id** is ID of the query we want to update;
/// * **new_keys** is encoded keys to query;
/// * **new_update_period** is used to say how often (in neutron blocks) the query must be updated.
Expand All @@ -102,14 +106,15 @@ pub fn update_interchain_query(
Some(filters) => {
to_string(&filters).map_err(|e| StdError::generic_err(e.to_string()))?
}
// TODO: check if passing empty string is correct
None => "".to_string(),
},
}
.into())
}

/// Basic helper to define a remove interchain query message:
/// * **contract** is a contract address that removes the interchain query.
/// Must be equal to the contract that sends the message.
/// * **query_id** is ID of the query we want to remove.
pub fn remove_interchain_query(contract: Addr, query_id: u64) -> NeutronResult<CosmosMsg> {
Ok(MsgRemoveInterchainQueryRequest {
Expand All @@ -132,3 +137,20 @@ pub fn kv_key_from_string<S: AsRef<str>>(s: S) -> Option<KvKey> {
key: decode_hex(split[1])?,
})
}

/// Encodes bytes slice into hex string
pub fn encode_hex(bytes: &[u8]) -> String {
let mut s = String::with_capacity(bytes.len() * 2);
for &b in bytes {
let _ = write!(s, "{:02x}", b);
}
s
}

/// Decodes hex string into bytes vec
pub fn decode_hex(s: &str) -> Option<Vec<u8>> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok())
.collect()
}
18 changes: 0 additions & 18 deletions packages/neutron-sdk/src/interchain_queries/hex.rs

This file was deleted.

1 change: 0 additions & 1 deletion packages/neutron-sdk/src/interchain_queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod helpers;
pub mod hex;
pub mod queries;
pub mod types;
pub mod v045;
Expand Down
5 changes: 5 additions & 0 deletions packages/neutron-sdk/src/interchain_txs/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ pub fn get_port_id<R: AsRef<str>>(contract_address: R, interchain_account_id: R)
+ interchain_account_id.as_ref()
}

/// Basic helper to define a register interchain account message:
/// * **contract** is a contract that registers ICA. Must be the contract address that sends this message.
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **interchain_account_id** is an identifier of your new interchain account. Can be any string.
/// * **ordering** is an ordering of ICA channel. Set to ORDERED if not specified
pub fn register_interchain_account(
contract: Addr,
connection_id: String,
Expand Down

0 comments on commit 7d44584

Please sign in to comment.