Skip to content

Commit

Permalink
extract interchain helpers out
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Oct 15, 2024
1 parent a097d93 commit 236d124
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 153 deletions.
2 changes: 1 addition & 1 deletion contracts/ibc_transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ serde-json-wasm = { workspace = true }
cw-storage-plus = { workspace = true, features = ["iterator"]}
cosmwasm-schema = { workspace = true }
neutron-sdk = { path = "../../packages/neutron-sdk", default-features = false }
neutron-std = { git = "https://github.com/neutron-org/neutron-std", rev = "cc49ece967353bcae7ad29ce04f75d2cc167b028" }
neutron-std = { git = "https://github.com/neutron-org/neutron-std", rev = "f8168e5727bf8d397b144e700b9c22af40c9710c" }

[dev-dependencies]
cosmwasm-schema = { workspace = true }
34 changes: 16 additions & 18 deletions contracts/ibc_transfer/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use crate::{
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg},
state::{
read_reply_payload, read_sudo_payload, save_reply_payload, save_sudo_payload,
IBC_SUDO_ID_RANGE_END, IBC_SUDO_ID_RANGE_START,
},
};
use cosmwasm_std::{
entry_point, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError,
StdResult, SubMsg,
};
use cw2::set_contract_version;
use neutron_sdk::interchain_txs::helpers::decode_message_response;
use neutron_sdk::sudo::msg::{RequestPacket, TransferSudoMsg};
use neutron_std::types::neutron::transfer::{MsgTransfer, MsgTransferResponse};
// TODO: rename
use neutron_std::types::cosmos::base::v1beta1::Coin as SuperCoin;
use neutron_std::types::cosmos::base::v1beta1::Coin as SDKCoin;
use neutron_std::types::neutron::feerefunder::{Fee, FeerefunderQuerier};
// TODO: rename
use crate::{
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg},
state::{
read_reply_payload, read_sudo_payload, save_reply_payload, save_sudo_payload,
IBC_SUDO_ID_RANGE_END, IBC_SUDO_ID_RANGE_START,
},
};
use neutron_std::types::neutron::transfer::{MsgTransfer, MsgTransferResponse};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -149,7 +147,7 @@ fn execute_send(
source_channel: channel.clone(),
sender: env.contract.address.to_string(),
receiver: to.clone(),
token: Some(SuperCoin {
token: Some(SDKCoin {
denom: denom.clone(),
amount: amount.to_string(),
}),
Expand All @@ -166,7 +164,7 @@ fn execute_send(
source_channel: channel,
sender: env.contract.address.to_string(),
receiver: to,
token: Some(SuperCoin {
token: Some(SDKCoin {
denom,
amount: (2 * amount).to_string(),
}),
Expand Down Expand Up @@ -286,20 +284,20 @@ fn query_min_fee(deps: Deps) -> StdResult<Fee> {
Ok(min_fee)
}

fn min_ntrn_ibc_fee(fee: Fee) -> neutron_std::types::neutron::feerefunder::Fee {
neutron_std::types::neutron::feerefunder::Fee {
fn min_ntrn_ibc_fee(fee: Fee) -> Fee {
Fee {
recv_fee: fee
.recv_fee
.iter()
.map(|r| SuperCoin {
.map(|r| SDKCoin {
denom: r.denom.to_string(),
amount: r.amount.clone(),
})
.collect(),
ack_fee: fee
.ack_fee
.iter()
.map(|r| SuperCoin {
.map(|r| SDKCoin {
denom: r.denom.to_string(),
amount: r.amount.clone(),
})
Expand All @@ -308,7 +306,7 @@ fn min_ntrn_ibc_fee(fee: Fee) -> neutron_std::types::neutron::feerefunder::Fee {
timeout_fee: fee
.timeout_fee
.iter()
.map(|r| SuperCoin {
.map(|r| SDKCoin {
denom: r.denom.to_string(),
amount: r.amount.clone(),
})
Expand Down
9 changes: 4 additions & 5 deletions contracts/neutron_interchain_queries/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ use cosmwasm_std::{
use cw2::set_contract_version;
use neutron_sdk::interchain_queries::v047::register_queries::new_register_validators_signing_infos_query_msg;
use neutron_std::types::neutron::interchainqueries::{KvKey, RegisteredQuery};
// TODO: fix name
use neutron_sdk::interchain_queries::v045::register_queries::{
remove_interchain_query as helpers_remove_interchain_query,
update_interchain_query as helpers_update_interchain_query,
};

use crate::msg::{
Cw20BalanceResponse, ExecuteMsg, GetRecipientTxsResponse, InstantiateMsg, MigrateMsg, QueryMsg,
};
use crate::state::{Transfer, RECIPIENT_TXS, TRANSFERS};
use neutron_sdk::bindings::types::Height;
use neutron_sdk::interchain_queries::helpers::{
remove_interchain_query as helpers_remove_interchain_query,
update_interchain_query as helpers_update_interchain_query,
};
use neutron_sdk::interchain_queries::v047::queries::{
query_balance, query_bank_total, query_delegations, query_distribution_fee_pool,
query_government_proposals, query_staking_validators, query_unbonding_delegations,
Expand Down
10 changes: 5 additions & 5 deletions contracts/neutron_interchain_txs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use crate::storage::{
ACKNOWLEDGEMENT_RESULTS, INTERCHAIN_ACCOUNTS, SUDO_PAYLOAD_REPLY_ID,
};
use neutron_sdk::bindings::msg::ChannelOrdering;
use neutron_sdk::interchain_queries::v045::register_queries::register_interchain_account;
use neutron_sdk::interchain_txs::helpers::register_interchain_account;
use neutron_sdk::{
interchain_txs::helpers::{decode_message_response, get_port_id},
interchain_txs::v047::helpers::decode_acknowledgement_response,
sudo::msg::{RequestPacket, SudoMsg},
NeutronError, NeutronResult,
};
use neutron_std::types::cosmos::base::v1beta1::Coin as SuperCoin;
use neutron_std::types::cosmos::base::v1beta1::Coin as SDKCoin;
use neutron_std::types::cosmos::base::v1beta1::Coin;
use neutron_std::types::cosmos::staking::v1beta1::{
MsgDelegate, MsgDelegateResponse, MsgUndelegate, MsgUndelegateResponse,
Expand Down Expand Up @@ -659,15 +659,15 @@ fn min_ntrn_ibc_fee(fee: Fee) -> neutron_std::types::neutron::feerefunder::Fee {
recv_fee: fee
.recv_fee
.iter()
.map(|r| SuperCoin {
.map(|r| SDKCoin {
denom: r.denom.to_string(),
amount: r.amount.clone(),
})
.collect(),
ack_fee: fee
.ack_fee
.iter()
.map(|r| SuperCoin {
.map(|r| SDKCoin {
denom: r.denom.to_string(),
amount: r.amount.clone(),
})
Expand All @@ -676,7 +676,7 @@ fn min_ntrn_ibc_fee(fee: Fee) -> neutron_std::types::neutron::feerefunder::Fee {
timeout_fee: fee
.timeout_fee
.iter()
.map(|r| SuperCoin {
.map(|r| SDKCoin {
denom: r.denom.to_string(),
amount: r.amount.clone(),
})
Expand Down
18 changes: 1 addition & 17 deletions packages/neutron-sdk/src/bindings/msg.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
use cosmwasm_std::{Binary, Coin, CosmosMsg, CustomMsg};
use cosmwasm_std::{Binary, CosmosMsg, CustomMsg};
use neutron_std::shim::Any;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
/// IbcFee defines struct for fees that refund the relayer for `SudoMsg` messages submission.
/// Unused fee kind will be returned back to message sender.
/// Please refer to these links for more information:
/// IBC transaction structure - <https://docs.neutron.org/neutron/interchain-txs/messages/#msgsubmittx>
/// General mechanics of fee payments - <https://docs.neutron.org/neutron/feerefunder/overview/#general-mechanics>
pub struct IbcFee {
/// **recv_fee** currently is used for compatibility with ICS-29 interface only and must be set to zero (i.e. 0untrn),
/// because Neutron's fee module can't refund relayer for submission of Recv IBC packets due to compatibility with target chains.
pub recv_fee: Vec<Coin>,
/// **ack_fee** is an amount of coins to refund relayer for submitting ack message for a particular IBC packet.
pub ack_fee: Vec<Coin>,
/// **timeout_fee** amount of coins to refund relayer for submitting timeout message for a particular IBC packet.
pub timeout_fee: Vec<Coin>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub enum ChannelOrdering {
OrderUnordered,
Expand Down
84 changes: 82 additions & 2 deletions packages/neutron-sdk/src/interchain_queries/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use crate::errors::error::{NeutronError, NeutronResult};
use crate::interchain_queries::types::{AddressBytes, MAX_ADDR_LEN};
use cosmwasm_std::{StdError, Uint128, Uint256};
use crate::interchain_queries::types::{
AddressBytes, QueryPayload, QueryType, TransactionFilterItem, MAX_ADDR_LEN,
};
use cosmwasm_std::{Addr, CosmosMsg, StdError, Uint128, Uint256};
use neutron_std::types::neutron::interchainqueries::{
KvKey, MsgRegisterInterchainQuery, MsgRemoveInterchainQueryRequest,
MsgUpdateInterchainQueryRequest,
};
use serde_json_wasm::to_string;

/// 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 @@ -37,3 +44,76 @@ pub fn uint256_to_u128(value: Uint256) -> Result<u128, StdError> {
.map_err(|_| StdError::generic_err("Uint256 value exceeds u128 limits"))?;
Ok(converted.u128())
}

/// Basic helper to define a register interchain query 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;
/// - when the query enum is 'tx' then payload is the filters for transaction search ICQ,
/// maximum allowed number of filters is 32.
/// * **connection_id** is an IBC connection identifier between Neutron and remote chain;
/// * **update_period** is used to say how often (in neutron blocks) the query must be updated.
pub fn register_interchain_query(
contract: Addr,
query: QueryPayload,
connection_id: String,
update_period: u64,
) -> NeutronResult<CosmosMsg> {
Ok(match query {
QueryPayload::KV(keys) => MsgRegisterInterchainQuery {
sender: contract.to_string(),
query_type: QueryType::KV.into(),
keys,
transactions_filter: String::new(),
connection_id,
update_period,
},
QueryPayload::TX(transactions_filters) => MsgRegisterInterchainQuery {
sender: contract.to_string(),
query_type: QueryType::TX.into(),
keys: vec![],
transactions_filter: to_string(&transactions_filters)
.map_err(|e| StdError::generic_err(e.to_string()))?,
connection_id,
update_period,
},
}
.into())
}

/// Basic helper to define a update interchain query 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.
pub fn update_interchain_query(
contract: Addr,
query_id: u64,
new_keys: Vec<KvKey>,
new_update_period: u64,
new_transactions_filter: Option<Vec<TransactionFilterItem>>,
) -> NeutronResult<CosmosMsg> {
Ok(MsgUpdateInterchainQueryRequest {
sender: contract.to_string(),
query_id,
new_keys,
new_update_period,
new_transactions_filter: match new_transactions_filter {
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:
/// * **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 {
sender: contract.to_string(),
query_id,
}
.into())
}
4 changes: 1 addition & 3 deletions packages/neutron-sdk/src/interchain_queries/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use neutron_std::types::neutron::interchainqueries::{
pub fn check_query_type(actual: String, expected: QueryType) -> NeutronResult<()> {
let expected_str: String = expected.into();
if actual != expected_str {
return Err(NeutronError::InvalidQueryType {
query_type: actual,
});
return Err(NeutronError::InvalidQueryType { query_type: actual });
}
Ok(())
}
Expand Down
Loading

0 comments on commit 236d124

Please sign in to comment.