diff --git a/README.md b/README.md index 5f32368..423b2b2 100644 --- a/README.md +++ b/README.md @@ -227,4 +227,4 @@ Note that most `tx_*` helper functions expose a `.with_key(key: &str)` builder f ### Complete Example -Examples of using almost every helper function provided by this repository are available in the [examples](https://github.com/timewave-computer/tree/main/examples) directory. +Examples of using almost every helper function provided by this repository are available in the [examples](https://github.com/timewave-computer/localic-utils/tree/main/examples) directory. diff --git a/examples/neutron_osmosis.rs b/examples/neutron_osmosis.rs index 5170c69..3c633a6 100644 --- a/examples/neutron_osmosis.rs +++ b/examples/neutron_osmosis.rs @@ -1,5 +1,5 @@ use localic_utils::{ConfigChainBuilder, TestContextBuilder}; -use std::error::Error; +use std::{error::Error, thread, time::Duration}; const ACC_0_ADDR: &str = "osmo1hj5fveer5cjtn4wd6wstzugjfdxzl0xpwhpz63"; const NEUTRON_ACC_0_ADDR: &str = "neutron1hj5fveer5cjtn4wd6wstzugjfdxzl0xpznmsky"; @@ -18,6 +18,16 @@ fn main() -> Result<(), Box> { .with_transfer_channels("osmosis", "neutron") .build()?; + // Kill and restart the relayer + ctx.stop_relayer(); + ctx.start_relayer(); + + // Wait for the relayer to start up + thread::sleep(Duration::from_secs(10)); + + // Wait for some blocks + ctx.get_chain("neutron").wait_for_blocks(20); + ctx.build_tx_create_tokenfactory_token() .with_chain_name("neutron") .with_subdenom("bruhtoken") diff --git a/src/utils/setup/ibc.rs b/src/utils/setup/ibc.rs index 590778e..a8e9905 100644 --- a/src/utils/setup/ibc.rs +++ b/src/utils/setup/ibc.rs @@ -1,5 +1,7 @@ use super::super::{ - super::{error::Error, DEFAULT_KEY, DEFAULT_TRANSFER_PORT, NEUTRON_CHAIN_NAME}, + super::{ + error::Error, DEFAULT_KEY, DEFAULT_TRANSFER_PORT, NEUTRON_CHAIN_ID, NEUTRON_CHAIN_NAME, + }, test_context::{LocalChain, TestContext}, }; @@ -134,4 +136,32 @@ impl TestContext { Ok(()) } + + pub fn start_relayer(&mut self) { + // Any random chain will allow us to kill the relayer, + // so select any chain we can get the API URL from + let chain = self.chains.values().next().unwrap(); + + // See above. chain_id does not matter, since there is + // one relayer running + reqwest::blocking::Client::default() + .post(&chain.rb.api) + .json(&serde_json::json!({ "chain_id": NEUTRON_CHAIN_ID, "action": "start-relayer"})) + .send() + .unwrap(); + } + + pub fn stop_relayer(&mut self) { + // Any random chain will allow us to kill the relayer, + // so select any chain we can get the API URL from + let chain = self.chains.values().next().unwrap(); + + // See above. chain_id does not matter, since there is + // one relayer running + reqwest::blocking::Client::default() + .post(&chain.rb.api) + .json(&serde_json::json!({ "chain_id": NEUTRON_CHAIN_ID, "action": "stop-relayer"})) + .send() + .unwrap(); + } } diff --git a/src/utils/test_context.rs b/src/utils/test_context.rs index c0feddb..915aa8a 100644 --- a/src/utils/test_context.rs +++ b/src/utils/test_context.rs @@ -5,7 +5,9 @@ use super::super::{ }; use localic_std::{ - modules::cosmwasm::CosmWasm, relayer::Channel, relayer::Relayer, + modules::cosmwasm::CosmWasm, + node::Chain, + relayer::{Channel, Relayer}, transactions::ChainRequestBuilder, }; use serde_json::Value; @@ -401,6 +403,15 @@ impl LocalChain { let id = abs_path.file_stem().unwrap().to_str().unwrap(); self.contract_codes.insert(id.to_string(), code); } + + pub fn wait_for_blocks(&self, blocks: u64) { + let chain = Chain::new(&self.rb); + let current_height = chain.get_height(); + + while chain.get_height() < current_height + blocks { + std::thread::sleep(std::time::Duration::from_millis(500)); + } + } } impl TestContext {