Skip to content

Commit

Permalink
Simplify ICS-020 channel info setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Lacy committed Jan 31, 2025
1 parent 0e5b65b commit cf63847
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 60 deletions.
2 changes: 1 addition & 1 deletion contracts/babylon/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn setup_instance() -> Instance<MockApi, MockStorage, MockQuerier> {
btc_finality_code_id: None,
btc_finality_msg: None,
admin: None,
ics20_info: None,
ics20_channel_id: None,
};
let info = mock_info(CREATOR, &[]);
let res: Response = instantiate(&mut deps, mock_env(), info, msg).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions contracts/babylon/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn instantiate(
CONFIG.save(deps.storage, &cfg)?;

// Save the IBC transfer info
if let Some(transfer_info) = msg.ics20_info {
if let Some(transfer_info) = msg.ics20_channel_id {
IBC_TRANSFER.save(deps.storage, &transfer_info)?;
}

Expand Down Expand Up @@ -302,10 +302,10 @@ pub fn execute(
// Route to babylon over IBC, if available
let transfer_info = IBC_TRANSFER.may_load(deps.storage)?;
match transfer_info {
Some(transfer_info) => {
Some(ics20_channel_id) => {
// Construct the transfer message
let ibc_msg = IbcMsg::Transfer {
channel_id: transfer_info.channel_id,
channel_id: ics20_channel_id,
to_address,
amount: info.funds[0].clone(),
timeout: packet_timeout(&env),
Expand Down Expand Up @@ -364,7 +364,7 @@ mod tests {
admin: None,
consumer_name: None,
consumer_description: None,
ics20_info: None,
ics20_channel_id: None,
};
let info = message_info(&deps.api.addr_make(CREATOR), &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand All @@ -387,7 +387,7 @@ mod tests {
admin: None,
consumer_name: None,
consumer_description: None,
ics20_info: None,
ics20_channel_id: None,
};
let info = message_info(&deps.api.addr_make(CREATOR), &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand Down Expand Up @@ -423,7 +423,7 @@ mod tests {
admin: None,
consumer_name: None,
consumer_description: None,
ics20_info: None,
ics20_channel_id: None,
};
let info = message_info(&deps.api.addr_make(CREATOR), &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand Down
9 changes: 2 additions & 7 deletions contracts/babylon/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use babylon_proto::babylon::zoneconcierge::v1::{
zoneconcierge_packet_data::Packet, BtcTimestamp, ZoneconciergePacketData,
};

use crate::msg::ibc::IbcIcs20Info;
use crate::state::config::CONFIG;
use cosmwasm_std::{
Binary, DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel,
Expand All @@ -21,7 +20,7 @@ pub const IBC_ORDERING: IbcOrder = IbcOrder::Ordered;
pub const IBC_CHANNEL: Item<IbcChannel> = Item::new("ibc_channel");

/// IBC transfer (ICS-020) channel settings
pub const IBC_TRANSFER: Item<IbcIcs20Info> = Item::new("ibc_transfer");
pub const IBC_TRANSFER: Item<String> = Item::new("ibc_ics20");

/// This is executed during the ChannelOpenInit and ChannelOpenTry
/// of the IBC 4-step channel protocol
Expand Down Expand Up @@ -321,7 +320,6 @@ mod tests {
use super::*;
use crate::contract::instantiate;
use crate::msg::contract::InstantiateMsg;
use crate::msg::ibc::IbcIcs20Info;
use cosmwasm_std::testing::message_info;
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_ibc_channel_open_try, MockApi, MockQuerier, MockStorage,
Expand All @@ -345,10 +343,7 @@ mod tests {
admin: None,
consumer_name: None,
consumer_description: None,
ics20_info: Some(IbcIcs20Info {
channel_id: "channel-1".to_string(),
to_address: "bbn1wdptld6nw2plxzf0w62gqc60tlw5kypzej89y3".to_string(),
}),
ics20_channel_id: Some("channel-1".to_string()),
};
let info = message_info(&deps.api.addr_make(CREATOR), &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand Down
11 changes: 6 additions & 5 deletions contracts/babylon/src/msg/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct InstantiateMsg {
pub consumer_description: Option<String>,
/// IBC information for ICS-020 rewards transfer.
/// If not set, distributed rewards will be native to the Consumer
pub ics20_info: Option<crate::msg::ibc::IbcIcs20Info>,
pub ics20_channel_id: Option<String>,
}

impl ContractMsg for InstantiateMsg {
Expand Down Expand Up @@ -86,8 +86,10 @@ impl ContractMsg for InstantiateMsg {
}
}

if let Some(transfer_info) = &self.ics20_info {
transfer_info.validate()?;
if let Some(channel_id) = &self.ics20_channel_id {
if channel_id.trim().is_empty() {
return Err(StdError::generic_err("ICS-020 channel_id cannot be empty"));
}
}

Ok(())
Expand Down Expand Up @@ -172,7 +174,6 @@ pub enum QueryMsg {
CzHeader { height: u64 },
/// TransferInfo returns the IBC transfer information stored in the contract
/// for ICS-020 rewards transfer.
/// If not set, distributed rewards are native to the Consumer
#[returns(crate::msg::ibc::TransferInfoResponse)]
#[returns(Option<String>)]
TransferInfo {},
}
22 changes: 2 additions & 20 deletions contracts/babylon/src/msg/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
use babylon_apis::to_canonical_addr;
use cosmos_sdk_proto::ibc::core::channel::v1::{acknowledgement::Response, Acknowledgement};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{StdError, StdResult};

#[cw_serde]
pub struct IbcIcs20Info {
pub channel_id: String,
pub to_address: String,
}

impl IbcIcs20Info {
pub fn validate(&self) -> StdResult<()> {
if self.channel_id.is_empty() {
return Err(StdError::generic_err("Empty IBC channel id"));
}
to_canonical_addr(&self.to_address, "bbn")
.map_err(|e| StdError::generic_err(format!("Invalid recipient address: {}", e)))?;
Ok(())
}
}
use cosmwasm_schema::cw_serde;

pub type TransferInfoResponse = Option<IbcIcs20Info>;
pub type TransferInfoResponse = Option<String>;

pub fn new_ack_res() -> Acknowledgement {
let resp = Response::Result(vec![]);
Expand Down
12 changes: 3 additions & 9 deletions contracts/babylon/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,11 @@ mod instantiation {

#[test]
fn instantiate_ibc_ics20_works() {
let suite = SuiteBuilder::new()
.with_ics20_info("channel-10", "bbn1wdptld6nw2plxzf0w62gqc60tlw5kypzej89y3")
.build();
let suite = SuiteBuilder::new().with_ics20_channel("channel-10").build();

// Confirm the transfer info has been set
let transfer_info = suite.get_transfer_info().unwrap();
assert_eq!(transfer_info.channel_id, "channel-10");
assert_eq!(
transfer_info.to_address,
"bbn1wdptld6nw2plxzf0w62gqc60tlw5kypzej89y3".to_string()
);
let channel_id = suite.get_transfer_info().unwrap();
assert_eq!(channel_id, "channel-10");
}
}

Expand Down
13 changes: 4 additions & 9 deletions contracts/babylon/src/multitest/suite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::msg::ibc::IbcIcs20Info;
use crate::msg::ibc::TransferInfoResponse;
use anyhow::Result as AnyResult;
use derivative::Derivative;
Expand Down Expand Up @@ -44,7 +43,7 @@ pub struct SuiteBuilder {
funds: Vec<(Addr, u128)>,
staking_msg: Option<String>,
finality_msg: Option<String>,
transfer_info: Option<IbcIcs20Info>,
ics20_channel_id: Option<String>,
}

impl SuiteBuilder {
Expand All @@ -68,12 +67,8 @@ impl SuiteBuilder {
}

/// Sets the IBC transfer info
pub fn with_ics20_info(mut self, channel_id: &str, recipient: &str) -> Self {
let transfer_info = IbcIcs20Info {
channel_id: channel_id.into(),
to_address: recipient.to_string(),
};
self.transfer_info = Some(transfer_info);
pub fn with_ics20_channel(mut self, channel_id: &str) -> Self {
self.ics20_channel_id = Some(channel_id.to_owned());
self
}

Expand Down Expand Up @@ -115,7 +110,7 @@ impl SuiteBuilder {
admin: Some(owner.to_string()),
consumer_name: Some("TestConsumer".to_string()),
consumer_description: Some("Test Consumer Description".to_string()),
ics20_info: self.transfer_info,
ics20_channel_id: self.ics20_channel_id,
},
&[],
"babylon",
Expand Down
4 changes: 2 additions & 2 deletions contracts/babylon/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn setup() -> Instance<MockApi, MockStorage, MockQuerier> {
btc_finality_code_id: None,
btc_finality_msg: None,
admin: None,
ics20_info: None,
ics20_channel_id: None,
};
let info = message_info(&Addr::unchecked(CREATOR), &[]);
let res: Response = instantiate(&mut deps, mock_env(), info, msg).unwrap();
Expand Down Expand Up @@ -108,7 +108,7 @@ fn instantiate_works() {
btc_finality_code_id: None,
btc_finality_msg: None,
admin: None,
ics20_info: None,
ics20_channel_id: None,
};
let info = message_info(&Addr::unchecked(CREATOR), &[]);
let res: ContractResult<Response> = instantiate(&mut deps, mock_env(), info, msg);
Expand Down
2 changes: 1 addition & 1 deletion contracts/btc-finality/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl SuiteBuilder {
admin: Some(owner.to_string()),
consumer_name: Some("TestConsumer".to_string()),
consumer_description: Some("Test Consumer Description".to_string()),
ics20_info: None,
ics20_channel_id: None,
},
&[],
"babylon",
Expand Down

0 comments on commit cf63847

Please sign in to comment.