Skip to content

Commit

Permalink
gauge-adapter-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hard-nett committed Jul 9, 2024
1 parent 24bb87c commit 2f94dba
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ members = [
"contracts/test/*",
"contracts/voting/*",
"packages/*",
# "test-suite/",
"test-suite/",
"ci/*",
]
resolver = "2"
Expand Down Expand Up @@ -80,6 +80,7 @@ test-context = "0.1"
thiserror = { version = "1.0" }
wynd-utils = "0.4"
cw-orch = "0.22.2"
cw-orch-core = "1.0.0-rc"

# One commit ahead of version 0.3.0. Allows initialization with an
# optional owner.
Expand Down
22 changes: 19 additions & 3 deletions contracts/gauges/gauge-adapter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,35 @@ pub mod execute {
}
}
Some(sent_assets) => match sent_assets {
AssetInfo::Cw20(_) => return Err(ContractError::Unauthorized {}),
AssetInfo::Cw20(coin) => {
if required_deposit.is_none() {
return Err(ContractError::InvalidDepositAmount {
provided: sent_assets.clone(),
expected: Uint128::zero(),
});
} else if !coin.amount.is_zero()
&& (AssetInfo::Cw20(coin.clone()) != required_deposit.clone().unwrap())
{
return Err(ContractError::InvalidDeposit {
provided: AssetInfo::Cw20(coin.clone()),
expected: required_deposit.unwrap(),
});
}
}
AssetInfo::Native(coin) => {
if required_deposit.is_none() {
return Err(ContractError::InvalidDepositAmount {
correct_amount: Uint128::zero(),
provided: sent_assets.clone(),
expected: Uint128::zero(),
});
} else if !coin.amount.is_zero()
&& (info.funds.len() != 1
|| info.funds[0].denom != coin.denom
|| info.funds[0].amount != coin.amount
|| AssetInfo::Native(coin.clone()) != required_deposit.clone().unwrap()) // can unwrap due to checks prior
|| AssetInfo::Native(coin.clone()) != required_deposit.clone().unwrap())
{
return Err(ContractError::InvalidDeposit {
provided: AssetInfo::Native(info.funds[0].clone()),
expected: required_deposit.unwrap(),
});
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/gauges/gauge-adapter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub enum ContractError {
#[error("Invalid submission - required deposit set in incorrect denom")]
InvalidDepositType {},

#[error("Invalid submission - invalid amount for required deposit. Either multiple denoms were sent or amount does not match {correct_amount}")]
InvalidDepositAmount { correct_amount: Uint128 },
#[error("Invalid submission - invalid required deposit. Provided: {provided}. Expected: {expected}")]
InvalidDepositAmount { provided: AssetInfo, expected: Uint128 },

#[error("Invalid submission - invalid asset for required deposit. Either multiple denoms were sent or amount does not match {expected}")]
InvalidDeposit { expected: AssetInfo },
#[error("Invalid submission - invalid asset for required deposit. Provided: {provided}, Expected {expected}")]
InvalidDeposit { provided: AssetInfo, expected: AssetInfo },

#[error("No deposit was required, therefore no deposit can be returned")]
NoDepositToRefund {},
Expand Down
26 changes: 26 additions & 0 deletions test-suite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "test-suite"
authors = [""]
description = "A package for test coverage of DAO DAO contracts."
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
version = { workspace = true }

[dependencies]
cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
cw2 = { workspace = true }
cw20 = { workspace = true }
cw721 = { workspace = true }
cw-utils = { workspace = true }
gauge-adapter = { workspace = true }
cw-orch = { workspace = true }
cw-orch-core = { workspace = true }
dao-cw-orch = { version = "2.4.2", path = "../packages/cw-orch" }
abstract-cw-plus-interface = "2.0.1"
abstract-cw20 = "2.0.0"
abstract-cw20-base = "2.0.0"

[dev-dependencies]
cosmwasm-schema = { workspace = true }
Empty file added test-suite/src/common_setup.rs
Empty file.
2 changes: 2 additions & 0 deletions test-suite/src/gauges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod adapter;
// mod orchestrator;
2 changes: 2 additions & 0 deletions test-suite/src/gauges/adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod test;
pub mod helpers;
158 changes: 158 additions & 0 deletions test-suite/src/gauges/adapter/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use std::ptr::eq;

use abstract_cw20::{Cw20Coin as AbsCw20Coin, MinterResponse};
use abstract_cw_plus_interface::cw20_base::Cw20Base;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{coin, Addr, Coin, Uint128};
use cw20::Cw20Coin;
use cw_orch::{
contract::interface_traits::{CwOrchExecute, CwOrchInstantiate, CwOrchUpload},
environment::IndexResponse,
mock::{cw_multi_test::AppResponse, MockBech32},
prelude::TxHandler,
};
use cw_orch_core::CwEnvError;
use dao_cw_orch::GaugeAdapter;
use gauge_adapter::{
msg::{AdapterQueryMsgFns, InstantiateMsg},
state::{AssetInfo, Config},
};

pub const DEFAULT_AMOUNT: u128 = 1_000u128;
pub const NATIVE: &str = "juno";
pub const CW20_ADDR: &str = "cw20-contract";
pub const COMMUNITY_POOL: &str = "mock1fawgu2qh6zu8j5e5t83fvwr0lxdp200mjkh8j5aguytl6x7qs2lsyx2wfn";

pub fn create_gauge_helpers(
mock: MockBech32,
gauge_adapter: GaugeAdapter<MockBech32>,
required_deposit: Option<AssetInfo>,
reward: AssetInfo,
) {
gauge_adapter.upload().unwrap();
let admin = mock.sender().to_string();
let community_pool = mock.addr_make("commuity-pool").to_string();
let instantiate = InstantiateMsg {
admin: admin.clone(),
required_deposit,
community_pool,
reward: reward.clone(),
};
gauge_adapter.instantiate(&instantiate, None, None).unwrap();
}
pub fn setup_default_accounts(mock: MockBech32) {
let default_amt = coin(100_000, "juno");
mock.addr_make_with_balance("owner", [default_amt.clone()].to_vec())
.unwrap();
mock.addr_make_with_balance("einstein", [default_amt.clone()].to_vec())
.unwrap();
mock.add_balance(&mock.sender, [default_amt.clone()].to_vec())
.unwrap();
}

// creates default testing environment for GaugeAdapter.
pub fn create_default(
mock: MockBech32,
required_deposit: Option<AssetInfo>,
) -> (GaugeAdapter<MockBech32>) {
// create new gauge adapter inside simulation
let gauge_adapter = GaugeAdapter::new("gauge_adapter", mock.clone());
// create default acccounts
setup_default_accounts(mock.clone());

// default simulation reward
let reward = AssetInfo::Native(Coin {
denom: "juno".to_string(),
amount: Uint128::new(1_000_000u128),
});

match required_deposit.clone() {
Some(required) => match required {
AssetInfo::Cw20(coin) => {
create_gauge_helpers(
mock.clone(),
gauge_adapter.clone(),
Some(AssetInfo::Cw20(Cw20Coin {
address: coin.address,
amount: coin.amount,
})),
reward.clone(),
);
}
AssetInfo::Native(token) => {
create_gauge_helpers(
mock.clone(),
gauge_adapter.clone(),
Some(AssetInfo::Native(token)),
reward.clone(),
);
}
},
None => {
create_gauge_helpers(mock.clone(), gauge_adapter.clone(), None, reward.clone());
}
};

let state = gauge_adapter.clone().config().unwrap();

(gauge_adapter)
}

//
pub fn create_native_submission_helper(
gauge: GaugeAdapter<MockBech32>,
sender: Addr,
native_tokens: Option<Coin>,
) -> Result<AppResponse, CwEnvError> {
if let Some(assets) = native_tokens.clone() {
let assets_info = AssetInfo::Native(assets.clone());
let res = gauge.execute(
&gauge_adapter::msg::ExecuteMsg::CreateSubmission {
name: "DAOers".to_string(),
url: "https://daodao.zone".to_string(),
address: sender.to_string(),
assets: Some(assets_info.clone()),
},
Some(&[assets]),
);
res
} else {
let res = gauge.execute(
&gauge_adapter::msg::ExecuteMsg::CreateSubmission {
name: "DAOers".to_string(),
url: "https://daodao.zone".to_string(),
address: sender.to_string(),
assets: None,
},
None,
);
res
}
}

pub fn cw20_helper(mock: MockBech32) -> (Cw20Base<MockBech32>, Addr) {
let cw20 = Cw20Base::new("cw20", mock.clone());
let cw20_code_id = cw20.upload().unwrap().uploaded_code_id().unwrap();
let res = init_cw20(cw20.clone(), cw20_code_id, mock.sender.to_string());
(cw20, Addr::unchecked(res))
}

pub fn init_cw20(mock: Cw20Base<MockBech32>, code_id: u64, minter: String) -> String {
let init_msg = abstract_cw20_base::msg::InstantiateMsg {
name: "test".to_string(),
symbol: "TEST".to_string(),
decimals: 6u8,
initial_balances: vec![AbsCw20Coin {
address: minter.clone(),
amount: Uint128::from(1_000_000u128),
}],
mint: Some(MinterResponse { minter, cap: None }),
marketing: None,
};
let res = mock.instantiate(&init_msg, None, None).unwrap();
println!("{:#?}", res);
let addr = res
.event_attr_value("instantiate", "_contract_address")
.unwrap();
addr
}
Loading

0 comments on commit 2f94dba

Please sign in to comment.