Skip to content

Commit

Permalink
removing runtimes (#345)
Browse files Browse the repository at this point in the history
* removing runtimes

* removed handle from anywhere

* Clippied

* Restore handle interface

* Fix nit
  • Loading branch information
Kayanski authored Mar 19, 2024
1 parent f5ff6fb commit 424a611
Show file tree
Hide file tree
Showing 21 changed files with 45 additions and 108 deletions.
9 changes: 2 additions & 7 deletions contracts/counter/examples/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use counter_contract::{
msg::InstantiateMsg, CounterContract, CounterExecuteMsgFns, CounterQueryMsgFns,
};
use cw_orch::{anyhow, prelude::*, tokio};
use tokio::runtime::Runtime;
use cw_orch::{anyhow, prelude::*};

const LOCAL_MNEMONIC: &str = "clip hire initial neck maid actor venue client foam budget lock catalog sweet steak waste crater broccoli pipe steak sister coyote moment obvious choose";
pub fn main() -> anyhow::Result<()> {
Expand All @@ -12,12 +11,8 @@ pub fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok(); // Used to load the `.env` file if any
pretty_env_logger::init(); // Used to log contract and chain interactions

let rt = Runtime::new()?;
let network = networks::LOCAL_JUNO;
let chain = DaemonBuilder::default()
.handle(rt.handle())
.chain(network)
.build()?;
let chain = DaemonBuilder::default().chain(network).build()?;
// ANCHOR_END: chain_construction

// ANCHOR: contract_interaction
Expand Down
1 change: 1 addition & 0 deletions cw-orch-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async-recursion = "1.0.5"

# Gzip
flate2 = { version = "1.0.26" }
lazy_static = "1.4.0"

[dev-dependencies]
cw-orch-daemon = { path = "." }
Expand Down
8 changes: 2 additions & 6 deletions cw-orch-daemon/examples/daemon-capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ use cosmwasm_std::coins;
// ANCHOR: full_counter_example
use cw_orch_daemon::DaemonBuilder;
use cw_orch_networks::networks;
use tokio::runtime::Runtime;

const LOCAL_MNEMONIC: &str = "clip hire initial neck maid actor venue client foam budget lock catalog sweet steak waste crater broccoli pipe steak sister coyote moment obvious choose";
pub fn main() -> anyhow::Result<()> {
std::env::set_var("LOCAL_MNEMONIC", LOCAL_MNEMONIC);

let rt = Runtime::new()?;
let network = networks::LOCAL_JUNO;
let daemon = DaemonBuilder::default()
.handle(rt.handle())
.chain(network)
.build()?;
let daemon = DaemonBuilder::default().chain(network).build()?;

// We commit the tx (also resimulates the tx)
// ANCHOR: send_tx
let wallet = daemon.wallet();
let rt = daemon.rt_handle;
rt.block_on(wallet.bank_send("<address-of-my-sister>", coins(345, "ujunox")))?;
// ANCHOR_END: send_tx

Expand Down
4 changes: 4 additions & 0 deletions cw-orch-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ pub(crate) mod cosmos_modules {

/// Re-export trait and data required to fetch daemon data from chain-registry
pub use ibc_chain_registry::{chain::ChainData as ChainRegistryData, fetchable::Fetchable};

lazy_static::lazy_static! {
pub static ref RUNTIME: tokio::runtime::Runtime = tokio::runtime::Runtime::new().unwrap();
}
37 changes: 14 additions & 23 deletions cw-orch-daemon/src/live_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use crate::queriers::Bank;
use crate::queriers::CosmWasm;
use crate::queriers::Staking;
use crate::RUNTIME;
use cosmwasm_std::testing::{MockApi, MockStorage};
use cosmwasm_std::Addr;
use cosmwasm_std::AllBalanceResponse;
use cosmwasm_std::BalanceResponse;
Expand All @@ -12,20 +14,17 @@ use cosmwasm_std::Binary;
use cosmwasm_std::Delegation;
use cosmwasm_std::Empty;
use cosmwasm_std::StakingQuery;
use cosmwasm_std::{AllDelegationsResponse, BondedDenomResponse};
use cw_orch_core::environment::BankQuerier;
use ibc_chain_registry::chain::ChainData;
use tokio::runtime::Runtime;
use tonic::transport::Channel;

use cosmwasm_std::testing::{MockApi, MockStorage};
use cosmwasm_std::{
from_json, to_json_binary, Coin, ContractResult, OwnedDeps, Querier, QuerierResult,
QueryRequest, SystemError, SystemResult, Uint128, WasmQuery,
};
use cosmwasm_std::{AllDelegationsResponse, BondedDenomResponse};
use cw_orch_core::environment::BankQuerier;
use cw_orch_core::environment::WasmQuerier;
use ibc_chain_registry::chain::ChainData;
use std::marker::PhantomData;
use std::str::FromStr;
use tonic::transport::Channel;

use crate::channel::GrpcChannel;

Expand Down Expand Up @@ -57,7 +56,6 @@ pub fn mock_dependencies(
/// Querier struct that fetches queries on-chain directly
pub struct WasmMockQuerier {
channel: Channel,
runtime: Runtime,
}

impl Querier for WasmMockQuerier {
Expand All @@ -80,18 +78,18 @@ impl WasmMockQuerier {
/// Function used to handle a query and customize the query behavior
/// This implements some queries by querying an actual node for the responses
pub fn handle_query(&self, request: &QueryRequest<Empty>) -> QuerierResult {
let handle = RUNTIME.handle();
match &request {
QueryRequest::Wasm(x) => {
let querier = CosmWasm {
channel: self.channel.clone(),
rt_handle: Some(self.runtime.handle().clone()),
rt_handle: Some(handle.clone()),
};
match x {
WasmQuery::Smart { contract_addr, msg } => {
// We forward the request to the cosmwasm querier

let query_result: Result<Binary, _> = self
.runtime
let query_result: Result<Binary, _> = handle
.block_on(
querier._contract_state(contract_addr.to_string(), msg.to_vec()),
)
Expand All @@ -115,7 +113,7 @@ impl WasmMockQuerier {
QueryRequest::Bank(x) => {
let querier = Bank {
channel: self.channel.clone(),
rt_handle: Some(self.runtime.handle().clone()),
rt_handle: Some(handle.clone()),
};
match x {
BankQuery::Balance { address, denom } => {
Expand Down Expand Up @@ -146,8 +144,7 @@ impl WasmMockQuerier {
let querier = Staking::new_async(self.channel.clone());
match x {
StakingQuery::BondedDenom {} => {
let query_result = self
.runtime
let query_result = handle
.block_on(querier._params())
.map(|result| BondedDenomResponse {
denom: result.params.unwrap().bond_denom,
Expand All @@ -159,8 +156,7 @@ impl WasmMockQuerier {
// This query is not perfect. I guess that on_chain you should be able to get ALL delegations and not a paginated result
// TODO, do better here
StakingQuery::AllDelegations { delegator } => {
let query_result = self
.runtime
let query_result = handle
.block_on(querier._delegator_delegations(delegator, None))
.map(|result| AllDelegationsResponse {
delegations: result
Expand Down Expand Up @@ -193,19 +189,14 @@ impl WasmMockQuerier {
impl WasmMockQuerier {
/// Creates a querier from chain information
pub fn new(chain: ChainData) -> Self {
let rt = Runtime::new().unwrap();

let channel = rt
let channel = RUNTIME
.block_on(GrpcChannel::connect(
&chain.apis.grpc,
chain.chain_id.as_str(),
))
.unwrap();

WasmMockQuerier {
channel,
runtime: rt,
}
WasmMockQuerier { channel }
}
}

Expand Down
11 changes: 6 additions & 5 deletions cw-orch-daemon/src/sync/builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bitcoin::secp256k1::All;
use ibc_chain_registry::chain::ChainData;

use crate::RUNTIME;
use crate::{
sender::{Sender, SenderBuilder, SenderOptions},
DaemonAsyncBuilder,
};
use bitcoin::secp256k1::All;
use ibc_chain_registry::chain::ChainData;

use super::{super::error::DaemonError, core::Daemon};

Expand Down Expand Up @@ -48,7 +48,7 @@ impl DaemonBuilder {
self
}

/// Set the tokio runtime handle to use for the Daemon
/// Set a custom tokio runtime handle to use for the Daemon
///
/// ## Example
/// ```no_run
Expand Down Expand Up @@ -102,7 +102,8 @@ impl DaemonBuilder {
let rt_handle = self
.handle
.clone()
.ok_or(DaemonError::BuilderMissing("runtime handle".into()))?;
.unwrap_or_else(|| RUNTIME.handle().clone());

// build the underlying daemon
let daemon = rt_handle.block_on(DaemonAsyncBuilder::from(self.clone()).build())?;

Expand Down
2 changes: 0 additions & 2 deletions cw-orch-daemon/src/sync/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use tonic::transport::Channel;
let rt = Runtime::new().unwrap();
let daemon: Daemon = Daemon::builder()
.chain(networks::JUNO_1)
.handle(rt.handle())
.build()
.unwrap();
```
Expand Down Expand Up @@ -71,7 +70,6 @@ impl Daemon {
builder
.chain(self.state().chain_data.clone())
.sender((*self.daemon.sender).clone())
.handle(&self.rt_handle)
.deployment_id(&self.state().deployment_id);
builder
}
Expand Down
11 changes: 3 additions & 8 deletions cw-orch-daemon/tests/authz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ mod tests {
fn authz() -> anyhow::Result<()> {
use cw_orch_networks::networks;

let runtime = tokio::runtime::Runtime::new().unwrap();

let daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.build()
.unwrap();

let sender = daemon.sender().to_string();

let second_daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.authz_granter(sender.clone())
.mnemonic(SECOND_MNEMONIC)
.build()
.unwrap();

let runtime = daemon.rt_handle.clone();

let grantee = second_daemon.sender().to_string();

let current_timestamp = daemon.block_info()?.time;
Expand Down Expand Up @@ -89,7 +87,7 @@ mod tests {

// Grants
let authz_querier: Authz = daemon.querier();
let grants: QueryGrantsResponse = runtime.handle().block_on(async {
let grants: QueryGrantsResponse = runtime.block_on(async {
authz_querier
._grants(sender.clone(), grantee.clone(), MsgSend::type_url(), None)
.await
Expand All @@ -98,19 +96,16 @@ mod tests {

// Grantee grants
let grantee_grants: QueryGranteeGrantsResponse = runtime
.handle()
.block_on(async { authz_querier._grantee_grants(grantee.clone(), None).await })?;
assert_eq!(grantee_grants.grants, vec![grant_authorization.clone()]);

// Granter grants
let granter_grants: QueryGranterGrantsResponse = runtime
.handle()
.block_on(async { authz_querier._granter_grants(sender.clone(), None).await })?;
assert_eq!(granter_grants.grants, vec![grant_authorization]);

// No grant gives out an error
runtime
.handle()
.block_on(async {
authz_querier
._grants(grantee.clone(), sender.clone(), MsgSend::type_url(), None)
Expand Down
7 changes: 0 additions & 7 deletions cw-orch-daemon/tests/daemon_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ mod tests {
fn helper_traits() {
use cw_orch_networks::networks;

let runtime = tokio::runtime::Runtime::new().unwrap();

let daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.build()
.unwrap();

Expand Down Expand Up @@ -102,7 +99,6 @@ mod tests {

// let daemon = Daemon::builder()
// .chain(chain)
// .handle(runtime.handle())
// .mnemonic("tide genuine angle mass fall promote blind skull swim army maximum add peasant fringe uncle october female crisp voyage blind extend jeans give wrap")
// .build()
// .unwrap();
Expand All @@ -120,11 +116,8 @@ mod tests {
fn cw_orch_interface_traits() {
use cw_orch_networks::networks;

let runtime = tokio::runtime::Runtime::new().unwrap();

let daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.build()
.unwrap();

Expand Down
4 changes: 0 additions & 4 deletions cw-orch-daemon/tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ mod tests {
fn mnemonic_index() -> anyhow::Result<()> {
use cw_orch_networks::networks;

let runtime = tokio::runtime::Runtime::new().unwrap();

let daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.build()
.unwrap();

let indexed_daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.hd_index(56)
.build()
.unwrap();
Expand Down
3 changes: 0 additions & 3 deletions cw-orch-daemon/tests/instantiate2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ pub mod test {

#[test]
fn instantiate2() -> anyhow::Result<()> {
let runtime = tokio::runtime::Runtime::new().unwrap();

let app = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(runtime.handle())
.build()
.unwrap();

Expand Down
1 change: 0 additions & 1 deletion cw-orch-daemon/tests/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ mod queriers {
let cosm_wasm = CosmWasm::new_async(channel);
let daemon = Daemon::builder()
.chain(networks::LOCAL_JUNO)
.handle(rt.handle())
.build()
.unwrap();

Expand Down
8 changes: 2 additions & 6 deletions cw-orch/examples/complex_testnet_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use osmosis_std::types::{
};
use prost::Message;
use prost_types::Any;
use tokio::runtime::Runtime;

pub const SUBDENOM: &str = "complex-test";

Expand All @@ -30,15 +29,11 @@ pub fn main() {
// Remember to set the `RUST_LOG` env variable to be able to see the execution
env_logger::init();

// We start by creating a runtime, which is required for a sync daemon.
let runtime = Runtime::new().unwrap();

// We can now create a daemon. This daemon will be used to interact with the chain.
// In the background, the `build` function uses the `TEST_MNEMONIC` variable, don't forget to set it !
let daemon = Daemon::builder()
// set the network to use
.chain(cw_orch::daemon::networks::UNI_6)
.handle(runtime.handle())
.build()
.unwrap();

Expand Down Expand Up @@ -106,7 +101,8 @@ pub fn main() {
.unwrap();
// We send some funds to the counter contract
let contract_addr = counter.addr_str().unwrap();
runtime
daemon
.rt_handle
.block_on(
daemon
.daemon
Expand Down
Loading

0 comments on commit 424a611

Please sign in to comment.