Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC Implementation #194

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
9 changes: 6 additions & 3 deletions cw-orch-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ exclude = [".env"]
all-features = true

[features]
default = []
default = ["grpc"]
# enable node-backed tests (ensure Docker is running)
# run with `cargo test --jobs 1 --features node-tests`
node-tests = []
eth = ["dep:ethers-signers", "dep:ethers-core"]
rpc = ["cosmrs/rpc"]
grpc = ["cosmrs/grpc", "dep:tonic"]

[dependencies]
# Default deps
cw-orch-core = { workspace = true }
Expand All @@ -44,14 +47,14 @@ bitcoin = { version = "0.30.0" }
hex = { version = "0.4.3" }
ripemd = { version = "0.1.3" }
tokio = { workspace = true, features = ["full"] }
tonic = { workspace = true, features = ["tls", "tls-roots"] }
tonic = { workspace = true, features = ["tls", "tls-roots"], optional = true }
reqwest = { version = "0.11.9" }
base64 = { version = "0.22.1" }
hkd32 = { version = "0.7.0", features = ["bip39", "mnemonic", "bech32"] }
rand_core = { version = "0.6.4", default-features = false }
ed25519-dalek = { version = "2", features = ["serde"] }
eyre = { version = "0.6" }
cosmrs = { version = "0.15.0", features = ["dev", "cosmwasm", "grpc"] }
cosmrs = { version = "0.15.0", features = ["dev", "cosmwasm"] }
chrono = { version = "0.4" }
base16 = { version = "0.2.1" }
ring = { version = "0.17.3" }
Expand Down
144 changes: 0 additions & 144 deletions cw-orch-daemon/src/channel.rs

This file was deleted.

18 changes: 8 additions & 10 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
queriers::CosmWasm,
queriers::{CosmWasm, CosmWasmBase},

Check failure on line 2 in cw-orch-daemon/src/core.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved imports `crate::queriers::CosmWasm`, `crate::queriers::CosmWasmBase`

error[E0432]: unresolved imports `crate::queriers::CosmWasm`, `crate::queriers::CosmWasmBase` --> cw-orch-daemon/src/core.rs:2:16 | 2 | queriers::{CosmWasm, CosmWasmBase}, | ^^^^^^^^ ^^^^^^^^^^^^ no `CosmWasmBase` in `queriers` | | | no `CosmWasm` in `queriers` | = help: consider importing this type alias through its public re-export instead: crate::queriers::clients::CosmWasm = help: consider importing this struct through its public re-export instead: crate::queriers::clients::CosmWasmBase
senders::{builder::SenderBuilder, query::QuerySender},
DaemonAsyncBuilder, DaemonState,
};

use super::{
cosmos_modules, error::DaemonError, queriers::Node, senders::Wallet, tx_resp::CosmTxResponse,

Check failure on line 8 in cw-orch-daemon/src/core.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `super::queriers::Node`

error[E0432]: unresolved import `super::queriers::Node` --> cw-orch-daemon/src/core.rs:8:41 | 8 | cosmos_modules, error::DaemonError, queriers::Node, senders::Wallet, tx_resp::CosmTxResponse, | ^^^^^^^^^^^^^^ no `Node` in `queriers` | = help: consider importing this struct through its public re-export instead: crate::queriers::clients::Node
};

use cosmrs::{
Expand Down Expand Up @@ -144,20 +144,18 @@
}

/// Query a contract.
pub async fn query<Q: Serialize + Debug, T: Serialize + DeserializeOwned>(
pub async fn query<Q: Serialize + Debug, R: Serialize + DeserializeOwned>(
&self,
query_msg: &Q,
contract_address: &Addr,
) -> Result<T, DaemonError> {
let mut client = cosmos_modules::cosmwasm::query_client::QueryClient::new(self.channel());
let resp = client
.smart_contract_state(cosmos_modules::cosmwasm::QuerySmartContractStateRequest {
address: contract_address.to_string(),
query_data: serde_json::to_vec(&query_msg)?,
})
) -> Result<R, DaemonError> {
let querier = CosmWasmBase::<Sender>::new_async(self.channel());

let resp: Vec<u8> = querier
._contract_state(contract_address, serde_json::to_vec(&query_msg)?)
.await?;

Ok(from_str(from_utf8(&resp.into_inner().data).unwrap())?)
Ok(from_str(from_utf8(&resp).unwrap())?)
}

/// Wait for a given amount of blocks.
Expand Down
173 changes: 173 additions & 0 deletions cw-orch-daemon/src/daemon_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
mod common;
#[cfg(feature = "node-tests")]
mod tests {
/*
DaemonAsync contract general tests
*/

use cw_orch_core::{contract::interface_traits::*, environment::TxHandler};
use cw_orch_daemon::{ConditionalMigrate, ConditionalUpload, Daemon};
use mock_contract::{InstantiateMsg, MigrateMsg, QueryMsg};

use cosmwasm_std::Addr;

use speculoos::prelude::*;

use crate::common::Id;

#[test]
#[serial_test::serial]
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();

let sender = daemon.sender();

let contract = mock_contract::MockContract::new(
format!("test:mock_contract:{}", Id::new()),
daemon.clone(),
);

asserting!("address is not present")
.that(&contract.address())
.is_err();

asserting!("upload_if_needed is ok")
.that(&contract.upload_if_needed())
.is_ok();

asserting!("latest_is_uploaded is true")
.that(&contract.latest_is_uploaded().unwrap())
.is_true();

let init_msg = &InstantiateMsg {};

let _ = contract.instantiate(init_msg, Some(&Addr::unchecked(sender)), Some(&[]));

asserting!("address is present")
.that(&contract.address())
.is_ok();

asserting!("migrate_if_needed is none")
.that(
&contract
.migrate_if_needed(&MigrateMsg {
t: "success".to_string(),
})
.unwrap(),
)
.is_none();

asserting!("is_running_latest is true")
.that(&contract.is_running_latest().unwrap())
.is_true();

let _ = contract.upload();

asserting!("is_running_latest is false")
.that(&contract.is_running_latest().unwrap())
.is_false();

asserting!("migrate_if_needed is some")
.that(
&contract
.migrate_if_needed(&MigrateMsg {
t: "success".to_string(),
})
.unwrap(),
)
.is_some();

asserting!("code_id is ok")
.that(&contract.code_id())
.is_ok();
}

// #[test]
// #[serial_test::serial]
// fn wrong_min_fee() {
// use cw_orch::prelude::networks;

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

// let mut chain = networks::UNI_6;
// chain.gas_price = 0.00001;

// 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();

// let contract = mock_contract::MockContract::new(
// format!("test:mock_contract:{}", Id::new()),
// daemon.clone(),
// );

// contract.upload().unwrap();
// }

#[test]
#[serial_test::serial]
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();

let sender = daemon.sender();

let contract = mock_contract::MockContract::new(
format!("test:mock_contract:{}", Id::new()),
daemon.clone(),
);

// upload contract
let upload_res = contract.upload();
asserting!("upload is successful").that(&upload_res).is_ok();

let code_id = upload_res.unwrap().logs[0].events[1].attributes[1]
.value
.clone();

log::info!("Using code_id {}", code_id);

// instantiate contract on chain
let init_res = contract.instantiate(&InstantiateMsg {}, Some(&sender), None);
asserting!("instantiate is successful")
.that(&init_res)
.is_ok();

// do a query and validate its successful
let query_res = contract.query::<String>(&QueryMsg::FirstQuery {});
asserting!("query is successful").that(&query_res).is_ok();

// validate migrations are successful
let migrate_res = contract.migrate(
&MigrateMsg {
t: "success".to_string(),
},
code_id.parse::<u64>().unwrap(),
);
asserting!("migrate is successful")
.that(&migrate_res)
.is_ok();

asserting!("that upload_if_needed returns None")
.that(&contract.upload_if_needed().unwrap())
.is_none();
}
}
Loading
Loading