Skip to content

Commit

Permalink
add mock exec to Astro testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Nov 18, 2023
1 parent 92acdf7 commit da2d02a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ optimize = """docker run --rm -v "$(pwd)":/code \
[dependencies]
astroport = "3.6.1"
cosmwasm-schema = "1.5.0"
cosmwasm-std = { version = "1.5.0", features = [
"cosmwasm_1_3",
# Enable this if you only deploy to chains that have CosmWasm 1.4 or higher
# "cosmwasm_1_4",
cosmwasm-std = { version = "1.2.7", features = [
"abort",
"iterator",
"stargate",
] }

cw-storage-plus = "1.1.0"
cw-utils = "1.0.2"
cw2 = "1.1.1"
Expand All @@ -55,4 +56,4 @@ serde = { version = "1.0.189", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.49" }

[dev-dependencies]
cw-multi-test = "0.17.0"
cw-multi-test = "0.18.0"
60 changes: 51 additions & 9 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cosmwasm_std::{
coins, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cw2::set_contract_version;

use crate::error::ContractError;
Expand Down Expand Up @@ -43,12 +45,13 @@ pub fn execute(
match msg {
ExecuteMsg::Increment {} => execute::increment(deps),
ExecuteMsg::Reset { count } => execute::reset(deps, info, count),
ExecuteMsg::Astro {} => execute::astro_exec(deps, info),
}
}

pub mod execute {
use astroport::asset::Asset;
use cosmwasm_std::Coin;
use cosmwasm_std::{Coin, Decimal};

use super::*;

Expand All @@ -72,22 +75,21 @@ pub mod execute {
Ok(Response::new().add_attribute("action", "reset"))
}
pub fn astro_exec(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
let inj_amount = cw_utils::must_pay(&info, "uinj")?;
let pair = "inj1r2qrk95gmnr3zwasy8u9shtzsx39q3yah73unj";
let inj_amount = cw_utils::must_pay(&info, "inj")?.u128();
// Pair of hINJ-INJ on testnet
let pair = "inj1relf3d6yh2e6arv84gaajawskttdq3vptn8qrs";
let swap_astro_msg = pair::ExecuteMsg::Swap {
offer_asset: Asset::native("inj", inj_amount),
ask_asset_info: None,
belief_price: None,
max_spread: None,
max_spread: Some(Decimal::percent(50)),
to: None,
};
assert!(inj_amount > 0);
let exec_cw20_mint_msg = WasmMsg::Execute {
contract_addr: pair.into(),
msg: to_json_binary(&swap_astro_msg)?,
funds: vec![Coin {
denom: "uinj".to_string(),
amount: inj_amount,
}],
funds: coins(inj_amount, "inj"),
};
let res = Response::new()
.add_message(exec_cw20_mint_msg)
Expand Down Expand Up @@ -119,7 +121,9 @@ pub mod query {
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::Addr;
use cosmwasm_std::{coins, from_json};
use cw_multi_test::{App, ContractWrapper, Executor};

#[test]
fn proper_initialization() {
Expand Down Expand Up @@ -184,4 +188,42 @@ mod tests {
let value: GetCountResponse = from_json(&res).unwrap();
assert_eq!(5, value.count);
}
#[test]
fn astro_test() {
let mut app = App::new(|router, _, storage| {
router
.bank
.init_balance(storage, &Addr::unchecked("user"), coins(1000, "uinj"))
.unwrap()
});
let code = ContractWrapper::new(execute, instantiate, query);
let code_id = app.store_code(Box::new(code));
let addr = app
.instantiate_contract(
code_id,
Addr::unchecked("user"),
&InstantiateMsg { count: 17 },
&[],
"Contract",
None,
)
.unwrap();
let resp = app
.execute_contract(
Addr::unchecked("user"),
addr,
&ExecuteMsg::Astro {},
&coins(10, "uinj"),
)
.unwrap();
// let wasm = resp.events.iter().find(|ev| ev.ty == "wasm").unwrap();
// assert_eq!(
// wasm.attributes
// .iter()
// .find(|attr| attr.key == "action")
// .unwrap()
// .value,
// "swap"
// );
}
}
17 changes: 15 additions & 2 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#[cfg(test)]
mod tests {
use std::marker::PhantomData;

use crate::helpers::CwTemplateContract;
use crate::msg::InstantiateMsg;
use cosmwasm_std::{Addr, Coin, Empty, Uint128};
use cosmwasm_std::{
testing::{MockApi, MockQuerier, MockStorage},
Addr, Coin, Empty, OwnedDeps, Querier, Uint128,
};
use cw_multi_test::{App, AppBuilder, Contract, ContractWrapper, Executor};

pub fn contract_template() -> Box<dyn Contract<Empty>> {
Expand Down Expand Up @@ -33,7 +38,15 @@ mod tests {
.unwrap();
})
}

fn mock_deps_with_query() -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
let mock_querier = MockQuerier::<Empty>::new(&[]);
OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: mock_querier,
custom_query_type: PhantomData,
}
}
fn proper_instantiate() -> (App, CwTemplateContract) {
let mut app = mock_app();
let cw_template_id = app.store_code(contract_template());
Expand Down
1 change: 1 addition & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct InstantiateMsg {
pub enum ExecuteMsg {
Increment {},
Reset { count: i32 },
Astro {},
}

#[cw_serde]
Expand Down

0 comments on commit da2d02a

Please sign in to comment.