-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ParseInitCosmosChannelOptions
- Loading branch information
1 parent
ec9fef0
commit 0f8f5af
Showing
4 changed files
with
75 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |