Skip to content

Commit

Permalink
Implement ParseInitCosmosChannelOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchen1991 committed Jan 17, 2025
1 parent ec9fef0 commit 0f8f5af
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/cli-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hermes-relayer-components = { workspace = true }
hermes-encoding-components = { workspace = true }
hermes-logging-components = { workspace = true }
hermes-test-components = { workspace = true }
hermes-cosmos-chain-components = { workspace = true }

cgp = { workspace = true }
http = { workspace = true }
Expand Down
16 changes: 12 additions & 4 deletions crates/cli/cli-components/src/impls/commands/channel/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct CreateChannelArgs {
target_connection_id: String,

#[clap(long = "target-port-id", value_name = "TARGET_PORT_ID")]
target_port_id: Option<String>,
target_port_id: String,

#[clap(
long = "counterparty-chain-id",
Expand All @@ -70,7 +70,13 @@ pub struct CreateChannelArgs {
counterparty_client_id: String,

#[clap(long = "counterparty-port-id", value_name = "COUNTERPARTY_PORT_ID")]
counterparty_port_id: Option<String>,
counterparty_port_id: String,

#[clap(long = "ordering", value_name = "ORDERING")]
ordering: String,

#[clap(long = "version", value_name = "VERSION")]
version: String,
}

impl<App, Args, Builder, Chain, Counterparty, Relay> CommandRunner<App, Args>
Expand All @@ -86,10 +92,12 @@ where
+ CanParseArg<Args, symbol!("target_chain_id"), Parsed = Chain::ChainId>
+ CanParseArg<Args, symbol!("target_client_id"), Parsed = Chain::ClientId>
+ CanParseArg<Args, symbol!("target_connection_id"), Parsed = Chain::ConnectionId>
+ CanParseArg<Args, symbol!("target_port_id"), Parsed = Option<Chain::PortId>>
+ CanParseArg<Args, symbol!("target_port_id"), Parsed = Chain::PortId>
+ CanParseArg<Args, symbol!("counterparty_chain_id"), Parsed = Counterparty::ChainId>
+ CanParseArg<Args, symbol!("counterparty_client_id"), Parsed = Counterparty::ClientId>
+ CanParseArg<Args, symbol!("counterparty_port_id"), Parsed = Option<Counterparty::PortId>>,
+ CanParseArg<Args, symbol!("counterparty_port_id"), Parsed = Counterparty::PortId>
+ CanParseArg<Args, symbol!("ordering"), Parsed = Counterparty::PortId>,
+ CanParseArg<Args, symbol!("version"), Parsed = Counterparty::PortId>,
App::Logger: CanLog<LevelInfo>,
Args: Async,
Builder: CanBuildRelay<Index<0>, Index<1>, Relay = Relay>
Expand Down
74 changes: 61 additions & 13 deletions crates/cli/cli-components/src/impls/parse/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
use core::marker::PhantomData;

use cgp::prelude::*;
use hermes_cosmos_chain_components::types::channel::CosmosInitChannelOptions;
use ibc::core::channel::types::channel::Order;
use ibc::core::channel::types::error::ChannelError;
use ibc::core::channel::types::Version;
use ibc::core::host::types::error::IdentifierError;
use ibc::core::host::types::identifiers::ConnectionId;
use ibc::core::host::types::identifiers::PortId;

use crate::traits::parse::ArgParser;

const TRANSFER_PORT_ID: &str = "transfer";
const DEFAULT_VERSION: &str = "ics20-1";

pub struct ParsePortId<Parsed>(pub PhantomData<Parsed>);
pub struct ParsePortId;

impl<App, Args, Tag, Parsed> ArgParser<App, Args, Tag> for ParsePortId<Parsed>
impl<App, Args, Tag> ArgParser<App, Args, Tag> for ParsePortId
where
App: CanRaiseAsyncError<IdentifierError>,
Args: HasField<Tag, Value = String>,
Parsed: Async,
{
type Parsed = PortId;

fn parse_arg(_app: &App, args: &Args, _tag: PhantomData<Tag>) -> Result<Parsed, App::Error> {
let port_id: PortId = args
.get_field(PhantomData)
.parse()
.map_err(App::raise_error)?;

if port_id.validate().is_err() {
PortId::new(TRANSFER_PORT_ID.to_string())
} else {
fn parse_arg(
_app: &App,
args: &Args,
_tag: PhantomData<Tag>,
) -> Result<Self::Parsed, App::Error> {
if let Ok(port_id) = args.get_field(PhantomData).parse::<PortId>() {
Ok(port_id)
} else {
Ok(PortId::transfer())
}
}
}

pub struct ParseInitCosmosChannelOptions;

impl<App, Args, Tag> ArgParser<App, Args, Tag> for ParseInitCosmosChannelOptions
where
App: CanRaiseAsyncError<IdentifierError> + CanRaiseAsyncError<ChannelError>,
Args: HasField<symbol!("target_connection_id"), Value = String>
+ HasField<symbol!("version"), Value = String>
+ HasField<symbol!("ordering"), Value = String>,
{
type Parsed = CosmosInitChannelOptions;

fn parse_arg(
_app: &App,
args: &Args,
_tag: PhantomData<Tag>,
) -> Result<Self::Parsed, App::Error> {
let connection_hops = if let Ok(conn_id) = args
.get_field(PhantomData::<symbol!("target_connection_id")>)
.parse::<ConnectionId>()
{
vec![conn_id]
} else {
Default::default()
};

let ordering =
if let Ok(ordering) = args.get_field(PhantomData::<symbol!("ordering")>).parse() {
ordering
} else {
Order::Unordered
};

let channel_version = match args.get_field(PhantomData::<symbol!("version")>).parse() {
Ok(version) => version,
Err(_) => Version::new(DEFAULT_VERSION.to_string()),
};

Ok(CosmosInitChannelOptions {
connection_hops,
ordering,
channel_version,
})
}
}

0 comments on commit 0f8f5af

Please sign in to comment.