Skip to content

Commit

Permalink
fix: correct tests, update pool to not pass assets
Browse files Browse the repository at this point in the history
  • Loading branch information
ApolloGie committed Aug 14, 2024
1 parent 39f89b9 commit 9d65db2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 79 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ osmosis-std = "0.22.0"
cw-it = { git = "https://github.com/apollodao/cw-it.git", rev = "331de15256f955584f5ef1cd1bac339f87657ef2", features = ["multi-test"]}
apollo-utils = "0.1.0"
astroport = "2.9.0"
astroport_v5 = { package = "astroport", version = "5.1.0" }
astroport_v5 = { package = "astroport", version = "5.2.0" }
test-case = "3.0.0"
proptest = "1.0.0"
cw-multi-test = "=1.2.0"
Expand Down
30 changes: 18 additions & 12 deletions cw-dex-astroport/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use cosmwasm_std::{
};
use cw20::Cw20ExecuteMsg;
use cw_utils::Expiration;
use osmosis_std::types::osmosis::tokenfactory::v1beta1::TokenfactoryQuerier;

use apollo_utils::assets::separate_natives_and_cw20s;
use astroport::asset::Asset as AstroAsset;
Expand Down Expand Up @@ -110,12 +109,7 @@ impl AstroportPool {
// To figure out if the native denom is a LP token, we need to check which address
// created the native denom and check if that address is an Astroport pair
// contract.
let denom_authority_metadata = TokenfactoryQuerier::new(&deps.querier)
.denom_authority_metadata(native_denom.to_string())?
.authority_metadata
.ok_or(CwDexError::NotLpToken {})?;

println!("denom_authority_metadata: {:?}", denom_authority_metadata);
let denom_authority_metadata = parse_address(native_denom)?;

// Try to create an `AstroportPool` object with the creator address. This will
// query the contract and assume that it is an Astroport pair
Expand All @@ -125,7 +119,7 @@ impl AstroportPool {
// factory, and that it is an "official" Astroport pool.
let pool = AstroportPool::new(
deps,
Addr::unchecked(denom_authority_metadata.admin),
Addr::unchecked(denom_authority_metadata),
astroport_liquidity_manager,
)?;

Expand Down Expand Up @@ -157,7 +151,7 @@ impl Pool for AstroportPool {
_deps: Deps,
env: &Env,
assets: AssetList,
_min_out: Uint128,
min_out: Uint128,
) -> Result<Response, CwDexError> {
let (funds, cw20s) = separate_natives_and_cw20s(&assets);

Expand Down Expand Up @@ -188,11 +182,12 @@ impl Pool for AstroportPool {
// Create the provide liquidity message
let provide_liquidity_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.pair_addr.to_string(),
msg: to_json_binary(&astroport::pair::ExecuteMsg::ProvideLiquidity {
assets: assets_vec.into_elementwise(),
msg: to_json_binary(&astroport_v5::pair::ExecuteMsg::ProvideLiquidity {
assets: assets_vec.iter().map(asset_to_astroport_v5_asset).collect(),
slippage_tolerance: Some(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?),
auto_stake: Some(false),
receiver: None,
min_lp_to_receive: Some(min_out),
})?,
funds,
});
Expand Down Expand Up @@ -268,7 +263,7 @@ impl Pool for AstroportPool {
let withdraw_liquidity = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.pair_addr.to_string(),
msg: to_json_binary(&astroport_v5::pair::ExecuteMsg::WithdrawLiquidity {
assets: vec![asset_to_astroport_v5_asset(&asset.clone())],
assets: vec![],
min_assets_to_receive: Some(
min_out
.to_vec()
Expand Down Expand Up @@ -438,3 +433,14 @@ pub fn astroport_v5_pairtype_to_astroport_v3_pairtype(
}
}

fn parse_address(input_string: &str) -> Result<String, CwDexError> {
let parts: Vec<&str> = input_string.split('/').collect();

if parts.len() < 3 {
return Err(CwDexError::AddressParsingErrors {
token_denom: input_string.to_string(),
});
}

Ok(parts[1].to_string())
}
85 changes: 30 additions & 55 deletions cw-dex-astroport/tests/astroport_tests.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
mod tests {
use apollo_cw_asset::{Asset, AssetInfo, AssetInfoBase, AssetList};
use apollo_cw_multi_test::{AppBuilder, BasicAppBuilder};
use apollo_cw_multi_test::{
BankKeeper, DistributionKeeper, FailingModule, StakeKeeper, WasmKeeper,
};
use apollo_cw_multi_test::BasicAppBuilder;
use apollo_cw_multi_test::WasmKeeper;
use apollo_utils::assets::separate_natives_and_cw20s;
use apollo_utils::coins::coin_from_str;
use apollo_utils::submessages::{find_event, parse_attribute_value};
use astroport::factory::PairType;
use astroport_v5::asset::{Asset as AstroportAsset, PairInfo};
use astroport_v5::pair::QueryMsg as PairQueryMsg;
use cosmwasm_std::testing::{mock_env, MockStorage};
use astroport_v5::pair::{PoolResponse, QueryMsg as PairQueryMsg};
use cosmwasm_std::{assert_approx_eq, coin, coins, Addr, Coin, Empty, SubMsgResponse, Uint128};
use cw_dex_astroport::AstroportPool;
use cw_dex_test_contract::msg::{AstroportExecuteMsg, ExecuteMsg, QueryMsg};
use cw_dex_test_helpers::astroport::setup_pool_and_test_contract;
use cw_dex_test_helpers::{cw20_transfer, query_asset_balance};
use cw_it::astroport::utils::AstroportContracts;
use cw_it::cosmrs::proto::cosmos::tx::signing::v1beta1::signature_descriptor::data::Multi;
use cw_it::cw_multi_test::{StargateKeeper, StargateMessageHandler};
use cw_it::helpers::{bank_all_balances_query, bank_balance_query, bank_send, Unwrap};
use cw_it::helpers::{bank_balance_query, bank_send, Unwrap};
use cw_it::multi_test::api::MockApiBech32;
use cw_it::multi_test::modules::TokenFactory;
use cw_it::multi_test::test_addresses::MockAddressGenerator;
use cw_it::multi_test::MultiTestRunner;
use cw_it::osmosis_std::types::cosmos::bank::v1beta1::QueryBalanceRequest;
use cw_it::test_tube::cosmrs::proto::cosmwasm::wasm::v1::MsgExecuteContractResponse;
use cw_it::test_tube::{
Account, ExecuteResponse, Module, Runner, RunnerResult, SigningAccount, Wasm,
};
use cw_it::traits::CwItRunner;
use cw_it::{OwnedTestRunner, TestRunner};
use osmosis_std::types::cosmos::bank::v1beta1::{
QueryAllBalancesRequest, QueryAllBalancesResponse,
};
use std::str::FromStr;
use test_case::test_case;
// use cw_multi_test::BasicAppBuilder;
Expand Down Expand Up @@ -111,12 +103,12 @@ mod tests {
setup_pool_and_testing_contract(&runner, pool_type.clone(), initial_liquidity).unwrap();
let admin = &accs[0];
let wasm = Wasm::new(&runner);
let pair_config_res: PairInfo = wasm.query(&pair_addr, &PairQueryMsg::Pair {}).unwrap();
println!("pair_config_res: {:?}", pair_config_res);
let _pair_config_res: PairInfo = wasm.query(&pair_addr, &PairQueryMsg::Pair {}).unwrap();

// Check contract's LP token balance before providing liquidity
let lp_token_before =
bank_balance_query(&runner, contract_addr.clone(), lp_token_denom.clone()).unwrap();
println!("lp_token_before {:?}", lp_token_before);

assert_eq!(lp_token_before, Uint128::zero());

// Simulate Provide Liquidity. Not supported for concentrated liquidity, so we
Expand All @@ -131,13 +123,8 @@ mod tests {
}
};

println!("expected_out: {:?}", expected_out);

let (funds, cw20s) = separate_natives_and_cw20s(&asset_list);

println!("funds: {:?}", funds);
println!("cw20s: {:?}", cw20s);

// Send cw20 tokens to the contract
for cw20 in cw20s {
cw20_transfer(
Expand All @@ -152,7 +139,7 @@ mod tests {

// Provide liquidity with min_out one more than expected_out. Should fail.
let unwrap = Unwrap::Err("Slippage is more than expected");
let min_out = expected_out + Uint128::new(10);
let min_out = expected_out + Uint128::new(1);
println!("min_out: {:?}", min_out);
let provide_msg = ExecuteMsg::ProvideLiquidity {
assets: asset_list.clone(),
Expand All @@ -162,7 +149,6 @@ mod tests {
&[provide_msg.into_cosmos_msg(contract_addr.clone(), funds.clone())],
admin,
));
println!("after unwrap");

// Provide liquidity with expected_out as min_out. Should succeed.
let provide_msg = ExecuteMsg::ProvideLiquidity {
Expand Down Expand Up @@ -199,17 +185,13 @@ mod tests {
fn test_withdraw_liquidity(pool_type: PairType, initial_liquidity: Vec<(&str, u64)>) {
let owned_runner = get_test_runner();
let runner = owned_runner.as_ref();
let (accs, lp_token_denom, _pair_addr, contract_addr, asset_list, _) =
let (accs, lp_token_denom, pair_addr, contract_addr, asset_list, _) =
setup_pool_and_testing_contract(&runner, pool_type, initial_liquidity).unwrap();
let admin = &accs[0];

println!("lp_token_denom: {:?}", lp_token_denom);
let admin_lp_token_balance =
bank_balance_query(&runner, admin.address(), lp_token_denom.clone()).unwrap();
println!("admin_lp_token_balance: {:?}", admin_lp_token_balance);
let admin_lp_token_balance =
bank_balance_query(&runner, contract_addr.clone(), lp_token_denom.clone()).unwrap();
println!("admin_lp_token_balance: {:?}", admin_lp_token_balance);

let amount_to_send = admin_lp_token_balance / Uint128::from(2u128);
let wasm = Wasm::new(&runner);
// Send LP tokens to contract
Expand Down Expand Up @@ -251,6 +233,8 @@ mod tests {
admin,
));

let _pool_res: PoolResponse = wasm.query(&pair_addr, &PairQueryMsg::Pool {}).unwrap();

// Withdraw liquidity with expected_out as min_out. Should succeed.
let withdraw_msg = ExecuteMsg::WithdrawLiquidity {
amount: contract_lp_token_balance,
Expand Down Expand Up @@ -286,7 +270,7 @@ mod tests {
) -> ExecuteResponse<MsgExecuteContractResponse> {
// Query LP token balance
let lp_token_balance =
bank_balance_query(runner, contract_addr.clone(), lp_token_denom.clone()).unwrap();
bank_balance_query(runner, signer.address().clone(), lp_token_denom.clone()).unwrap();

// Stake LP tokens
let stake_msg = ExecuteMsg::Stake {
Expand Down Expand Up @@ -326,25 +310,17 @@ mod tests {

// Query LP token balance
let lp_token_balance =
bank_balance_query(&runner, contract_addr.clone(), lp_token_denom.clone()).unwrap();

let all_balances_res =
bank_all_balances_query(&runner, admin.address().clone(), None).unwrap();

println!("all_balances_res : {:?}", all_balances_res);

let all_balances_res =
bank_all_balances_query(&runner, contract_addr.clone(), None).unwrap();
bank_balance_query(&runner, admin.address().clone(), lp_token_denom.clone()).unwrap();

println!("all_balances_res : {:?}", all_balances_res);
// Send LP tokens to the test contract
bank_send(
&runner,
admin,
&contract_addr.clone(),
coins(lp_token_balance.u128(), lp_token_denom.clone()),
)
.unwrap();
// println!("admin_lp_token_balance: {:?}", lp_token_balance);
// // Send LP tokens to the test contract
// bank_send(
// &runner,
// admin,
// &contract_addr.clone(),
// coins(lp_token_balance.u128(), lp_token_denom.clone()),
// )
// .unwrap();

// Stake LP tokens
let events = stake_all_lp_tokens(
Expand Down Expand Up @@ -617,13 +593,13 @@ mod tests {
bank_balance_query(&runner, admin.address().clone(), lp_token_denom.clone()).unwrap();

// Send LP tokens to the test contract
bank_send(
&runner,
admin,
&testing_contract_addr.clone(),
coins(lp_token_balance.u128(), lp_token_denom.clone()),
)
.unwrap();
// bank_send(
// &runner,
// admin,
// &testing_contract_addr.clone(),
// coins(lp_token_balance.u128(), lp_token_denom.clone()),
// )
// .unwrap();

// Stake LP tokens
let _events = stake_all_lp_tokens(
Expand Down Expand Up @@ -721,4 +697,3 @@ mod tests {
);
}
}

7 changes: 7 additions & 0 deletions cw-dex/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ pub enum CwDexError {
/// The actual amount of tokens received
received: Uint128,
},

/// For when the token_denom can't be parsed
#[error("Could not parse address: {token_denom}")]
AddressParsingErrors {
/// The token_denom that couldn't be parsed
token_denom: String,
},
}

impl From<CwDexError> for StdError {
Expand Down
1 change: 0 additions & 1 deletion test-helpers/src/astroport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,3 @@ pub fn instantiate_test_astroport_contract<'a, R: Runner<'a>>(
.data
.address)
}

0 comments on commit 9d65db2

Please sign in to comment.