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

Aether Features #350

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contracts/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ exclude = [".env"]
[lib]
crate-type = ["cdylib", "rlib"]

[[example]]
name = "async"
required-features = ["interface"]

[features]
default = ["export"]
export = []
Expand Down
32 changes: 32 additions & 0 deletions contracts/counter/examples/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ANCHOR: full_counter_example
use counter_contract::{
msg::InstantiateMsg, AsyncCounterQueryMsgFns, CounterContract, CounterExecuteMsgFns,
};
use cw_orch::{anyhow, prelude::*, tokio};
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";

#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
std::env::set_var("TEST_MNEMONIC", LOCAL_MNEMONIC);
// ANCHOR: chain_construction
dotenv::dotenv().ok(); // Used to load the `.env` file if any
pretty_env_logger::init(); // Used to log contract and chain interactions

let network = networks::UNI_6;
let chain = DaemonAsyncBuilder::default().chain(network).build().await?;
// ANCHOR_END: chain_construction

// ANCHOR: contract_interaction

let counter = CounterContract::new(chain);

let count = counter.get_count().await?;
assert_eq!(count.count, 1);
// ANCHOR_END: clean_example
// ANCHOR_END: contract_interaction

Ok(())
}
// ANCHOR_END: full_counter_example
5 changes: 4 additions & 1 deletion contracts/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub mod state;

pub use crate::error::ContractError;
// ANCHOR: fn_re_export
pub use crate::msg::{ExecuteMsgFns as CounterExecuteMsgFns, QueryMsgFns as CounterQueryMsgFns};
pub use crate::msg::{
AsyncQueryMsgFns as AsyncCounterQueryMsgFns, ExecuteMsgFns as CounterExecuteMsgFns,
QueryMsgFns as CounterQueryMsgFns,
};
// ANCHOR_END: fn_re_export

// ANCHOR: custom_interface
Expand Down
59 changes: 41 additions & 18 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use cosmrs::{
use cosmwasm_std::{Addr, Binary, Coin};
use cw_orch_core::{
contract::interface_traits::Uploadable,
environment::{ChainState, IndexResponse},
environment::{AsyncWasmQuerier, ChainState, IndexResponse, Querier},
log::transaction_target,
};
use flate2::{write, Compression};
Expand Down Expand Up @@ -127,6 +127,23 @@ impl DaemonAsync {
Ok(result)
}

/// Query a contract.
pub async fn query<Q: Serialize + Debug, T: Serialize + DeserializeOwned>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is copy-pasting the smart_query from the AsyncWasmQuerier trait. Implement just once please

&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)?,
})
.await?;

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

/// Instantiate a contract.
pub async fn instantiate<I: Serialize + Debug>(
&self,
Expand Down Expand Up @@ -192,23 +209,6 @@ impl DaemonAsync {
Ok(result)
}

/// Query a contract.
pub async fn query<Q: Serialize + Debug, T: 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)?,
})
.await?;

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

/// Migration a contract.
pub async fn migrate<M: Serialize + Debug>(
&self,
Expand Down Expand Up @@ -315,6 +315,29 @@ impl DaemonAsync {
}
}

impl Querier for DaemonAsync {
type Error = DaemonError;
}

impl AsyncWasmQuerier for DaemonAsync {
/// Query a contract.
async fn smart_query<Q: Serialize, T: DeserializeOwned>(
&self,
address: impl Into<String>,
query_msg: &Q,
) -> 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: address.into(),
query_data: serde_json::to_vec(&query_msg)?,
})
.await?;

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

pub(crate) fn parse_cw_coins(
coins: &[cosmwasm_std::Coin],
) -> Result<Vec<cosmrs::Coin>, DaemonError> {
Expand Down
4 changes: 2 additions & 2 deletions cw-orch/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

// Contract traits
pub use crate::contract::interface_traits::{
CallAs, ConditionalMigrate, ConditionalUpload, ContractInstance, CwOrchExecute,
CwOrchInstantiate, CwOrchMigrate, CwOrchQuery, CwOrchUpload, ExecutableContract,
AsyncCwOrchQuery, CallAs, ConditionalMigrate, ConditionalUpload, ContractInstance,
CwOrchExecute, CwOrchInstantiate, CwOrchMigrate, CwOrchQuery, CwOrchUpload, ExecutableContract,
InstantiableContract, MigratableContract, QueryableContract, Uploadable,
};

Expand Down
27 changes: 12 additions & 15 deletions packages/cw-orch-contract-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The interface can be linked to its source code by implementing the `Uploadable`
```ignore
use cw_orch::prelude::*;

impl <Chain: CwEnv> Uploadable for Cw20<Chain> {
impl <Chain> Uploadable for Cw20<Chain> {
fn wrapper(&self) -> <Mock as cw_orch::TxHandler>::ContractSource {
Box::new(
ContractWrapper::new_with_empty(
Expand Down Expand Up @@ -204,7 +204,7 @@ pub fn interface(attrs: TokenStream, input: TokenStream) -> TokenStream {
let name = cw_orch_struct.ident.clone();
let default_num = if let Some(id_expr) = default_id {
quote!(
impl <Chain: ::cw_orch::prelude::CwEnv, #all_generics> #name<Chain, #all_generics> {
impl <Chain, #all_generics> #name<Chain, #all_generics> {
pub fn new(chain: Chain) -> Self {
Self(
::cw_orch::contract::Contract::new(#id_expr, chain)
Expand All @@ -214,7 +214,7 @@ pub fn interface(attrs: TokenStream, input: TokenStream) -> TokenStream {
)
} else {
quote!(
impl <Chain: ::cw_orch::prelude::CwEnv, #all_generics> #name<Chain, #all_generics> {
impl <Chain, #all_generics> #name<Chain, #all_generics> {
pub fn new(contract_id: impl ToString, chain: Chain) -> Self {
Self(
::cw_orch::contract::Contract::new(contract_id, chain)
Expand All @@ -228,7 +228,7 @@ pub fn interface(attrs: TokenStream, input: TokenStream) -> TokenStream {
#[derive(
::std::clone::Clone,
)]
pub struct #name<Chain: ::cw_orch::prelude::CwEnv, #all_generics>(::cw_orch::contract::Contract<Chain>, #(#all_phantom_markers,)*);
pub struct #name<Chain, #all_generics>(::cw_orch::contract::Contract<Chain>, #(#all_phantom_markers,)*);

#[cfg(target_arch = "wasm32")]
#[derive(
Expand All @@ -240,7 +240,7 @@ pub fn interface(attrs: TokenStream, input: TokenStream) -> TokenStream {
#default_num

#[cfg(not(target_arch = "wasm32"))]
impl<Chain: ::cw_orch::prelude::CwEnv, #all_generics> ::cw_orch::prelude::ContractInstance<Chain> for #name<Chain, #all_generics> {
impl<Chain: ::cw_orch::environment::ChainState, #all_generics> ::cw_orch::prelude::ContractInstance<Chain> for #name<Chain, #all_generics> {
fn as_instance(&self) -> &::cw_orch::contract::Contract<Chain> {
&self.0
}
Expand All @@ -250,22 +250,19 @@ pub fn interface(attrs: TokenStream, input: TokenStream) -> TokenStream {
}

#[cfg(not(target_arch = "wasm32"))]
impl<Chain: ::cw_orch::prelude::CwEnv, #all_generics> ::cw_orch::prelude::InstantiableContract for #name<Chain, #all_generics> #all_debug_serialize {
type InstantiateMsg = #init;
impl<Chain, #all_generics> ::cw_orch::prelude::QueryableContract for #name<Chain, #all_generics> #all_debug_serialize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the order of functions here ?

type QueryMsg = #query;
}

#[cfg(not(target_arch = "wasm32"))]
impl<Chain: ::cw_orch::prelude::CwEnv, #all_generics> ::cw_orch::prelude::ExecutableContract for #name<Chain, #all_generics> #all_debug_serialize {
type ExecuteMsg = #exec;
impl<Chain, #all_generics> ::cw_orch::prelude::InstantiableContract for #name<Chain, #all_generics> #all_debug_serialize {
type InstantiateMsg = #init;
}

#[cfg(not(target_arch = "wasm32"))]
impl<Chain: ::cw_orch::prelude::CwEnv, #all_generics> ::cw_orch::prelude::QueryableContract for #name<Chain, #all_generics> #all_debug_serialize {
type QueryMsg = #query;
impl<Chain, #all_generics> ::cw_orch::prelude::ExecutableContract for #name<Chain, #all_generics> #all_debug_serialize {
type ExecuteMsg = #exec;
}

#[cfg(not(target_arch = "wasm32"))]
impl<Chain: ::cw_orch::prelude::CwEnv, #all_generics> ::cw_orch::prelude::MigratableContract for #name<Chain, #all_generics> #all_debug_serialize {
impl<Chain, #all_generics> ::cw_orch::prelude::MigratableContract for #name<Chain, #all_generics> #all_debug_serialize {
type MigrateMsg = #migrate;
}
);
Expand Down
Loading
Loading