Skip to content

Commit

Permalink
Even better API for InterchainEnv (#492)
Browse files Browse the repository at this point in the history
* Added accessorfor all chains

* Merge

* Addedclone to interchain env

* Merge

* Changged accessor to iterator

* Version bumps
  • Loading branch information
Kayanski authored Sep 26, 2024
1 parent 5fe212f commit be0eee3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ cw-orch-fns-derive = { path = "packages/macros/cw-orch-fns-derive", version = "0
# cw-orch-osmosis-test-tube = { version = "0.3.0", path = "packages/cw-orch-osmosis-test-tube" }

# Interchain
cw-orch-interchain = { path = "cw-orch-interchain", version = "0.4.0" }
cw-orch-interchain-core = { path = "packages/interchain/interchain-core", version = "0.5.0" }
cw-orch-interchain-daemon = { path = "packages/interchain/interchain-daemon", version = "0.5.0" }
cw-orch-interchain-mock = { path = "packages/interchain/interchain-mock", version = "0.5.0" }
cw-orch-interchain = { path = "cw-orch-interchain", version = "0.5.0" }
cw-orch-interchain-core = { path = "packages/interchain/interchain-core", version = "0.6.0" }
cw-orch-interchain-daemon = { path = "packages/interchain/interchain-daemon", version = "0.6.0" }
cw-orch-interchain-mock = { path = "packages/interchain/interchain-mock", version = "0.6.0" }
cw-orch-starship = { path = "packages/interchain/starship", version = "0.5.0" }
# cw-orch-proto = { path = "packages/interchain/proto", version = "0.5.0" } # prost, tonic, cosmrs bump locked by osmosis (we use it for tokenfactory)

Expand Down
2 changes: 1 addition & 1 deletion cw-orch-interchain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-orch-interchain"
version = "0.4.0"
version = "0.5.0"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand Down
6 changes: 2 additions & 4 deletions cw-orch-interchain/examples/doc_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use cw_orch_interchain::{
fn create_daemon_env() -> cw_orch::anyhow::Result<DaemonInterchain> {
// ANCHOR: DAEMON_INTERCHAIN_CREATION
// This will create `Daemon` structures associated with chains `LOCAL_JUNO` and `LOCAL_OSMO`
let mut interchain = DaemonInterchain::new(
vec![(LOCAL_JUNO, None), (LOCAL_OSMO, None)],
&ChannelCreationValidator,
)?;
let mut interchain =
DaemonInterchain::new(vec![LOCAL_JUNO, LOCAL_OSMO], &ChannelCreationValidator)?;

let local_juno: Daemon = interchain.get_chain("testing")?;
let _local_osmo: Daemon = interchain.get_chain("localosmosis")?;
Expand Down
2 changes: 1 addition & 1 deletion cw-orch-interchain/examples/follow_packets_txhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn follow_by_tx_hash() -> cw_orch::anyhow::Result<()> {
let src_chain = OSMOSIS_1;

let interchain = DaemonInterchain::new(
vec![(src_chain.clone(), None), (dst_chain, None)],
vec![src_chain.clone(), dst_chain],
&ChannelCreationValidator,
)?;

Expand Down
2 changes: 1 addition & 1 deletion packages/interchain/interchain-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-orch-interchain-core"
version = "0.5.0"
version = "0.6.1"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
17 changes: 16 additions & 1 deletion packages/interchain/interchain-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub type ChainId<'a> = &'a str;
/// // This makes sure that the packets arrive successfully and present a success ack
/// let result = interchain.await_and_check_packets("juno-1", tx_resp).unwrap();
/// ```
pub trait InterchainEnv<Chain: IbcQueryHandler> {
pub trait InterchainEnv<Chain: IbcQueryHandler>: Clone {
/// Type returned by the internal channel creation function
/// Examples
/// Daemon : This is empty, because hermes doesn't return anything after channel creation
Expand Down Expand Up @@ -138,6 +138,21 @@ pub trait InterchainEnv<Chain: IbcQueryHandler> {
/// ```
fn get_chain(&self, chain_id: impl ToString) -> Result<Chain, Self::Error>;

/// Returns every chain registered within the environment.
/// To get a single chain, use [`InterchainEnv::get_chain`]
/// ``` rust
/// use cw_orch::prelude::*;
/// use cw_orch_interchain::prelude::*;
/// use counter_contract::CounterContract;
/// let interchain = MockBech32InterchainEnv::new(vec![("osmosis-1","osmo"),("archway-1","arch")]);
///
/// let all_chains: Vec<&MockBeck32> = interchain.chains().collect();
///
/// ```
fn chains<'a>(&'a self) -> impl Iterator<Item = &'a Chain>
where
Chain: 'a;

/// This triggers channel creation between 2 chains
/// Returns a channel creation receipt as well as as the connection_id on the src_chain side
/// This code is only for internal use and for most cases shouldn't be used outside of the [InterchainEnv<Chain>::create_channel] function
Expand Down
2 changes: 1 addition & 1 deletion packages/interchain/interchain-daemon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-orch-interchain-daemon"
version = "0.5.0"
version = "0.6.1"
description = "An interchain intergration crate for interacting with actual chain nodes (via gRPC)"
authors.workspace = true
edition.workspace = true
Expand Down
16 changes: 10 additions & 6 deletions packages/interchain/interchain-daemon/src/interchain_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ pub struct DaemonInterchain<C: ChannelCreator = ChannelCreationValidator> {
rt_handle: Handle,
}

type Mnemonic = String;

impl<C: ChannelCreator> DaemonInterchain<C> {
/// Builds a new [`DaemonInterchain`] instance.
/// For use with starship, we advise to use [`cw_orch_starship::Starship::interchain_env`] instead
/// channel_creator allows you to specify an object that is able to create channels
/// Use [`crate::ChannelCreationValidator`] for manual channel creations.
pub fn new<T>(chains: Vec<(T, Option<Mnemonic>)>, channel_creator: &C) -> IcDaemonResult<Self>
pub fn new<T>(chains: Vec<T>, channel_creator: &C) -> IcDaemonResult<Self>
where
T: Into<ChainInfoOwned>,
{
Expand All @@ -61,7 +59,7 @@ impl<C: ChannelCreator> DaemonInterchain<C> {
/// Use [`crate::ChannelCreationValidator`] for manual channel creations.
/// runtime allows you to control the async runtime (for advanced devs)
pub fn new_with_runtime<T>(
chains: Vec<(T, Option<Mnemonic>)>,
chains: Vec<T>,
channel_creator: &C,
runtime: &Handle,
) -> IcDaemonResult<Self>
Expand All @@ -71,8 +69,8 @@ impl<C: ChannelCreator> DaemonInterchain<C> {
let mut env = Self::raw(runtime, channel_creator);

// We create daemons for each chains
for (chain_data, mnemonic) in chains {
env.build_daemon(runtime, chain_data.into(), mnemonic)?;
for chain_data in chains {
env.build_daemon(runtime, chain_data.into(), None::<String>)?;
}

Ok(env)
Expand Down Expand Up @@ -289,6 +287,12 @@ impl<C: ChannelCreator> InterchainEnv<Daemon> for DaemonInterchain<C> {

Ok(ibc_trail)
}
fn chains<'a>(&'a self) -> impl Iterator<Item = &'a Daemon>
where
Daemon: 'a,
{
self.daemons.values()
}
}

impl<C: ChannelCreator> DaemonInterchain<C> {
Expand Down
2 changes: 1 addition & 1 deletion packages/interchain/interchain-mock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-orch-interchain-mock"
version = "0.5.0"
version = "0.6.1"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions packages/interchain/interchain-mock/src/interchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ impl<A: Api> InterchainEnv<MockBase<A>> for MockInterchainEnvBase<A> {

Ok(analysis_result)
}

fn chains<'a>(&'a self) -> impl Iterator<Item = &'a MockBase<A>>
where
MockBase<A>: 'a,
{
self.mocks.values()
}
}

fn get_events(tx: &AppResponse, event: &str) -> Vec<Event> {
Expand Down
2 changes: 1 addition & 1 deletion packages/interchain/proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cw-orch-proto"
description = "A helper crate for interaction with protos from different chains. Mostly used for handling cw20 coins and ibc transfers"
version = "0.5.0"
version = "0.6.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down

0 comments on commit be0eee3

Please sign in to comment.