Skip to content

Commit

Permalink
Wasm compilation works
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayanski committed Oct 28, 2024
1 parent bfedf70 commit a25c254
Show file tree
Hide file tree
Showing 65 changed files with 324 additions and 566 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ repository = "https://github.com/AbstractSDK/cw-orchestrator"

[workspace.dependencies]
cw-utils = { version = "2.0.0" }
cosmwasm-std = { version = "2.1", features = [
"cosmwasm_2_0",
"staking",
"stargate",
] }
cosmwasm-std = { version = "2.1" }
cw-storage-plus = { version = "2.0.0" }
cosmos-sdk-proto = { version = "0.24.0", default-features = false }

Expand All @@ -49,6 +45,7 @@ neutron-test-tube = { version = "4.2.3" }
anyhow = "1.0"
serde = { version = "1.0.208", default-features = false, features = ["derive"] }
tokio = { version = "1.39", features = ["full"] }
async-std = "1.13.0"

cw-orch = { path = "./cw-orch", version = "0.27.0" }
cw-orch-daemon = { path = "./cw-orch-daemon", version = "0.28.0" }
Expand Down
7 changes: 4 additions & 3 deletions artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cc02664dc02c571fd0e4bb02061b737571a510a6a4ac01f8cc783545bec6fff4 counter_contract.wasm
9f8d3bc6ea5c58c31dbed31588df591447ffd2410b3c4d1587e2adb8a29c0931 counter_contract.wasm
4bb1891434224d710cb1f9eea1da1a2a0aa3c4a133d699d58e26e72daf51b63b counter_cousin_contract.wasm
a266c5ab0ffdf78b3aee593d07d3c822fa3c2745bc7bc8210e49860439d65a73 cw_orch_compatibility_test.wasm
0dc79fb31e732310554a07881cfe8a193c068d2f93539356817e5aaf551650dd mock_contract.wasm
08d65b781d7357a6777c836229d971b6c87a5fdfc7cba9806ed508f917859f08 mock_contract_u64.wasm
322d2535c672abb0338f23689d703a468d6bdebe9ce975a842e9a8d5e95bf770 mock_contract.wasm
cdc883c20f370bd68494912e28053f0bef666e8f96c889ba8412b50f4a2cd1ed mock_contract_u64.wasm
Binary file modified artifacts/counter_contract.wasm
Binary file not shown.
Binary file added artifacts/counter_cousin_contract.wasm
Binary file not shown.
Binary file modified artifacts/mock_contract.wasm
Binary file not shown.
Binary file modified artifacts/mock_contract_u64.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions contracts-ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cosmwasm-std = "2.0.0"
cw20 = { version = "2.0.0" }
cw20-base = { version = "2.0.0" }
cw-storage-plus = { version = "2.0.0" }
async-std = "1.13.0"

serde = { version = "1.0.103", default-features = false, features = ["derive"] }
anyhow = "1.0"
Expand Down
3 changes: 2 additions & 1 deletion contracts-ws/contracts/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde = { workspace = true }
serde_json = "1.0.125"
cw-orch = { workspace = true, features = ["daemon"] }
# Unused, only there to check for wasm compatibility
cw-orch-interchain = { workspace = true, features = ["daemon"] }
# cw-orch-interchain = { workspace = true, features = ["daemon"] }

[[example]]
name = "deploy"
Expand All @@ -35,3 +35,4 @@ dotenv = { version = "0.15.0" }
pretty_env_logger = { version = "0.5.0" }
cw-orch = { workspace = true, features = ["daemon"] }
anyhow = { workspace = true }
async-std = { workspace = true }
4 changes: 2 additions & 2 deletions contracts-ws/contracts/counter/examples/async.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// ANCHOR: full_async_example
use counter_contract::AsyncCounterQueryMsgFns;
use counter_contract::CounterContract;
use cw_orch::{anyhow, prelude::*, tokio};
use cw_orch::{anyhow, prelude::*};

// From https://github.com/CosmosContracts/juno/blob/32568dba828ff7783aea8cb5bb4b8b5832888255/docker/test-user.env#L2
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]
#[async_std::main]
pub async fn main() -> anyhow::Result<()> {
std::env::set_var("LOCAL_MNEMONIC", LOCAL_MNEMONIC);
dotenv::dotenv().ok(); // Used to load the `.env` file if any
Expand Down
3 changes: 2 additions & 1 deletion contracts-ws/contracts/counter_cousin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde = { workspace = true }
serde_json = "1.0.125"
cw-orch = { workspace = true, features = ["daemon"] }
# Unused, only there to check for wasm compatibility
cw-orch-interchain = { workspace = true, features = ["daemon"] }
# cw-orch-interchain = { workspace = true, features = ["daemon"] }

cw-orch-on-chain = { path = "../../../packages/cw-orch-on-chain" }

Expand All @@ -34,3 +34,4 @@ dotenv = { version = "0.15.0" }
pretty_env_logger = { version = "0.5.0" }
cw-orch = { workspace = true, features = ["daemon"] }
anyhow = { workspace = true }
cw-orch-osmosis-test-tube = { path = "../../../packages/cw-orch-osmosis-test-tube" }
3 changes: 1 addition & 2 deletions contracts-ws/contracts/counter_cousin/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub fn instantiate(
let state = State {
count: msg.count,
owner: info.sender.clone(),
cousin: None,
};
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
STATE.save(deps.storage, &state)?;
Expand Down Expand Up @@ -52,7 +51,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetCount {} => to_json_binary(&query::count(deps)?),
QueryMsg::GetCousinCount {} => to_json_binary(&query::cousin_count(deps, env)?),
QueryMsg::GetRawCousinCount {} => to_json_binary(&query::raw_cousin_count(deps)?),
QueryMsg::GetRawCousinCount {} => to_json_binary(&query::raw_cousin_count(deps, &env)?),
}
}

Expand Down
12 changes: 6 additions & 6 deletions contracts-ws/contracts/counter_cousin/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use cw_orch::prelude::*;

use crate::CounterContract;
use crate::{error::*, state::*};

pub fn increment(deps: DepsMut) -> Result<Response, ContractError> {
Expand All @@ -23,18 +25,16 @@ pub fn reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, C
}

pub fn set_cousin(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
info: MessageInfo,
cousin: String,
) -> Result<Response, ContractError> {
assert_owner(deps.as_ref(), &info)?;

let cousin_addr = deps.api.addr_validate(&cousin)?;
let state = STATE.load(deps.storage)?;
if info.sender != state.owner {
return Err(ContractError::Unauthorized {});
}

crate::CounterContract::save(deps.branch(), &env, "cousin", cousin_addr.clone());
CounterContract::load(deps, &env, "cousin").set_address(&cousin_addr);

Ok(Response::new().add_attribute("action", "set_cousin"))
}
40 changes: 1 addition & 39 deletions contracts-ws/contracts/counter_cousin/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use cw_orch_on_chain::core::OnChain;

use cosmwasm_std::{Deps, Env};
use std::collections::HashMap;

#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg)]
pub struct CounterContract;
Expand All @@ -14,7 +13,7 @@ impl<Chain: CwEnv> Uploadable for CounterContract<Chain> {
/// Return the path to the wasm file corresponding to the contract
fn wasm(_chain: &ChainInfoOwned) -> WasmPath {
artifacts_dir_from_workspace!()
.find_wasm_path("counter_contract")
.find_wasm_path("counter_cousin_contract")
.unwrap()
}
/// Returns a CosmWasm contract wrapper
Expand All @@ -27,11 +26,7 @@ impl<Chain: CwEnv> Uploadable for CounterContract<Chain> {
}
}
// ANCHOR_END: custom_interface

use crate::contract::CONTRACT_NAME;
use cosmwasm_std::DepsMut;
use cw_orch::anyhow::Result;
use cw_orch::prelude::queriers::Node;
use cw_orch_on_chain::core::OnChainDeps;

impl<'a> CounterContract<OnChain<'a>> {
Expand Down Expand Up @@ -68,36 +63,3 @@ impl<'a> CounterContract<OnChain<'a>> {
contract
}
}

// ANCHOR: daemon
impl CounterContract<Daemon> {
/// Deploys the counter contract at a specific block height
pub fn await_launch(&self) -> Result<()> {
let daemon = self.environment();
let rt = daemon.rt_handle.clone();

rt.block_on(async {
// Get the node query client, there are a lot of other clients available.
let node: Node = daemon.querier();
let mut latest_block = node.latest_block().unwrap();

while latest_block.height < 100 {
// wait for the next block
daemon.next_block().unwrap();
latest_block = node.latest_block().unwrap();
}
});

let contract = CounterContract::new(CONTRACT_NAME, daemon.clone());

// Upload the contract
contract.upload().unwrap();

// Instantiate the contract
let msg = InstantiateMsg { count: 1i32 };
contract.instantiate(&msg, None, &[]).unwrap();

Ok(())
}
}
// ANCHOR_END: daemon
2 changes: 2 additions & 0 deletions contracts-ws/contracts/counter_cousin/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub enum ExecuteMsg {
/// Count value after reset
count: i32,
},
/// Sets the cousin contract, that this contract can query
SetCousin {
/// Address of the cousin contract of this counter
cousin: String,
},
}
Expand Down
13 changes: 5 additions & 8 deletions contracts-ws/contracts/counter_cousin/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use cosmwasm_std::{to_json_binary, Deps, Env, QueryRequest, StdResult, WasmQuery};
use cosmwasm_std::{Deps, Env, StdResult};

use crate::interface::CounterContract;
use crate::msg::QueryMsgFns;
use crate::{
msg::{GetCountResponse, QueryMsg},
state::STATE,
};
use crate::{msg::GetCountResponse, state::STATE};
use cw_orch::prelude::*;

pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
let state = STATE.load(deps.storage)?;
Expand All @@ -18,9 +16,8 @@ pub fn cousin_count(deps: Deps, env: Env) -> StdResult<GetCountResponse> {
.map_err(Into::into)
}

pub fn raw_cousin_count(deps: Deps) -> StdResult<GetCountResponse> {
let state = STATE.load(deps.storage)?;
let cousin_state = STATE.query(&deps.querier, state.cousin.unwrap())?;
pub fn raw_cousin_count(deps: Deps, env: &Env) -> StdResult<GetCountResponse> {
let cousin_state = CounterContract::load(deps, env, "cousin").item_query(STATE)?;
Ok(GetCountResponse {
count: cousin_state.count,
})
Expand Down
12 changes: 10 additions & 2 deletions contracts-ws/contracts/counter_cousin/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Addr;
use crate::ContractError;
use cosmwasm_std::{Addr, Deps, MessageInfo};
use cw_storage_plus::Item;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub count: i32,
pub owner: Addr,
pub cousin: Option<Addr>,
}

pub const STATE: Item<State> = Item::new("state");

pub fn assert_owner(deps: Deps, info: &MessageInfo) -> Result<(), ContractError> {
let state = STATE.load(deps.storage)?;
if info.sender != state.owner {
return Err(ContractError::Unauthorized {});
}
Ok(())
}
55 changes: 42 additions & 13 deletions contracts-ws/contracts/counter_cousin/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// ANCHOR: all
use counter_cousin_contract::{
msg::{GetCountResponse, InstantiateMsg, QueryMsg},
ContractError, CounterContract, CounterExecuteMsgFns, CounterQueryMsgFns,
msg::InstantiateMsg, CounterContract, CounterExecuteMsgFns, CounterQueryMsgFns,
};
// Use prelude to get all the necessary imports
use cw_orch::prelude::*;
use cw_orch_osmosis_test_tube::OsmosisTestTube;

use cosmwasm_std::Addr;
use cosmwasm_std::{coins, Addr};

// consts for testing
const USER: &str = "user";
const ADMIN: &str = "admin";
// ANCHOR: integration_test

// ANCHOR: count_test
#[test]
fn count() -> anyhow::Result<()> {
fn mock() -> anyhow::Result<()> {
// Create the mock. This will be our chain object throughout
let mock = Mock::new(ADMIN);
let user = Addr::unchecked(USER);
Expand Down Expand Up @@ -45,9 +42,45 @@ fn count() -> anyhow::Result<()> {

Ok(())
}
// ANCHOR_END: count_test

// ANCHOR: setup
#[test]
fn test_tube() -> anyhow::Result<()> {
// Create the mock. This will be our chain object throughout
let mut test_tube = OsmosisTestTube::new(coins(100_000_000_000_000, "uosmo"));
let user = test_tube.init_account(coins(100_000_000_000_000, "uosmo"))?;

// Set up the contract (Definition below) ↓↓
let (contract, cousin) = setup(test_tube.clone())?;

// Increment the count of the contract
contract.call_as(&user).increment()?;
contract.call_as(&user).increment()?;

cousin.call_as(&user).increment()?;
cousin.call_as(&user).increment()?;
cousin.call_as(&user).increment()?;

let count = contract.get_count()?;
assert_eq!(count.count, 3);

let count = contract.get_cousin_count()?;
assert_eq!(count.count, 4);

let count = cousin.get_count()?;
assert_eq!(count.count, 4);

let count = cousin.get_cousin_count()?;
assert_eq!(count.count, 3);

let count = contract.get_raw_cousin_count()?;
assert_eq!(count.count, 4);

let count = cousin.get_raw_cousin_count()?;
assert_eq!(count.count, 3);

Ok(())
}

/// Instantiate the contract in any CosmWasm environment
fn setup<Chain: CwEnv>(
chain: Chain,
Expand All @@ -73,7 +106,3 @@ fn setup<Chain: CwEnv>(
// Return the interface
Ok((contract, cousin))
}
// ANCHOR_END: setup

// ANCHOR_END: integration_test
// ANCHOR_END: all
6 changes: 3 additions & 3 deletions contracts-ws/contracts/mock_contract/src/msg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ pub fn execute_ordered(
pub fn query(_deps: Deps, _env: Env, _msg: Empty) -> StdResult<Binary> {
Ok(vec![].into())
}
#[cfg(not(target_arch = "wasm32"))]

mod interface {
use super::*;
use cw_orch::prelude::*;

impl<Chain> Uploadable for TestContract<Chain> {
fn wrapper() -> <Mock as TxHandler>::ContractSource {
fn wrapper() -> Box<dyn MockContract<Empty, Empty>> {
Box::new(ContractWrapper::new_with_empty(execute, instantiate, query))
}
}

impl<Chain> Uploadable for OrderedTestContract<Chain> {
fn wrapper() -> <Mock as TxHandler>::ContractSource {
fn wrapper() -> Box<dyn MockContract<Empty, Empty>> {
Box::new(ContractWrapper::new_with_empty(
execute_ordered,
instantiate,
Expand Down
2 changes: 1 addition & 1 deletion cw-orch-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ bitcoin = { version = "0.32.2" }
bech32 = { version = "0.11.0", default-features = false, features = ["alloc"] }
hex = { version = "0.4.3" }
ripemd = { version = "0.1.3" }
tokio = { workspace = true, features = ["full"] }
tonic = { workspace = true, features = ["tls"] }
reqwest = { version = "0.12.5" }
base64 = { version = "0.22.1" }
Expand Down Expand Up @@ -78,6 +77,7 @@ uid = "0.1.7"
toml = "0.8"
http = "1.1.0"
libc-print = "0.1.23"
async-std = { workspace = true, features = ["attributes"] }

[dev-dependencies]
cw-orch-daemon = { path = "." }
Expand Down
Loading

0 comments on commit a25c254

Please sign in to comment.