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

feat: matra dex swap adapter #154

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
test: add tests to mantra dex swap adapter
mantricjavier committed Dec 11, 2024
commit feb1eec14cadd57dfa4faeff7bf94997e59b4c2f
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion contracts/adapters/swap/mantra-dex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@ cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
skip = { workspace = true }
thiserror = { workspace = true }
white-whale-std = { workspace = true }

[dev-dependencies]
test-case = { workspace = true }
15 changes: 13 additions & 2 deletions contracts/adapters/swap/mantra-dex/src/contract.rs
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ use crate::{
state::ENTRY_POINT_CONTRACT_ADDRESS,
};
use cosmwasm_std::{
entry_point, to_json_binary, wasm_execute, Binary, Decimal, Deps, DepsMut, Env, MessageInfo,
Response, Uint128,
ensure, entry_point, to_json_binary, wasm_execute, Binary, Decimal, Deps, DepsMut, Env,
MessageInfo, Response, Uint128,
};
use cw2::set_contract_version;
use cw_utils::one_coin;
@@ -113,6 +113,12 @@ fn execute_swap(
// Get coin in from the message info, error if there is not exactly one coin sent
let coin_in = one_coin(&info)?;

// sanity check
ensure!(
coin_in.amount != Uint128::zero(),
ContractError::NoOfferAssetAmount
);

// Create a response object to return
let response: Response = Response::new().add_attribute("action", "execute_swap");

@@ -126,6 +132,11 @@ fn execute_swap(
})
.collect();

ensure!(
!mantra_swap_operations.is_empty(),
ContractError::SwapOperationsEmpty
);

let msg = MantraPoolManagerExecuteMsg::ExecuteSwapOperations {
operations: mantra_swap_operations,
minimum_receive: None,
244 changes: 0 additions & 244 deletions contracts/adapters/swap/mantra-dex/tests/test_execute_receive.rs

This file was deleted.

111 changes: 42 additions & 69 deletions contracts/adapters/swap/mantra-dex/tests/test_execute_swap.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use cosmwasm_std::{
testing::{mock_dependencies, mock_env, mock_info},
to_json_binary, Addr, Coin,
to_json_binary, Addr, Coin, Decimal,
ReplyOn::Never,
SubMsg, WasmMsg,
};
use skip::swap::{ExecuteMsg, SwapOperation};
use skip_go_swap_adapter_white_whale::{
error::{ContractError, ContractResult},
state::ENTRY_POINT_CONTRACT_ADDRESS,
use skip_go_swap_adapter_mantra_dex::error::{ContractError, ContractResult};
use skip_go_swap_adapter_mantra_dex::state::{
ENTRY_POINT_CONTRACT_ADDRESS, MANTRA_DEX_POOL_MANAGER_ADDRESS,
};
use test_case::test_case;

use skip_go_swap_adapter_mantra_dex::pool_manager::{
ExecuteMsg as MantraPoolManagerExecuteMsg, SwapOperation as MantraSwapOperation,
};

/*
Test Cases:
@@ -52,34 +56,22 @@ struct Params {
SubMsg {
id: 0,
msg: WasmMsg::Execute {
contract_addr: "swap_contract_address".to_string(),
msg: to_json_binary(&ExecuteMsg::WhiteWhalePoolSwap {
operation: SwapOperation {
pool: "pool_1".to_string(),
denom_in: "os".to_string(),
denom_out: "ua".to_string(),
interface: None,
}
contract_addr: "mantra_pool_manager".to_string(),
msg: to_json_binary(&MantraPoolManagerExecuteMsg::ExecuteSwapOperations {
operations: vec![MantraSwapOperation::MantraSwap {
pool_identifier: "pool_1".to_string(),
token_in_denom: "os".to_string(),
token_out_denom: "ua".to_string(),
}],
minimum_receive: None,
receiver: Some("entry_point".to_string()),
max_spread: Some(Decimal::percent(50)),
})?,
funds: vec![],
funds: vec![Coin::new(100, "os")],
}.into(),
gas_limit: None,
reply_on: Never,
},
SubMsg {
id: 0,
msg: WasmMsg::Execute {
contract_addr: "swap_contract_address".to_string(),
msg: to_json_binary(&ExecuteMsg::TransferFundsBack {
return_denom: "ua".to_string(),
swapper: Addr::unchecked("entry_point"),
})?,
funds: vec![],
}
.into(),
gas_limit: None,
reply_on: Never,
},
}
],
expected_error: None,
};
@@ -106,51 +98,28 @@ struct Params {
SubMsg {
id: 0,
msg: WasmMsg::Execute {
contract_addr: "swap_contract_address".to_string(),
msg: to_json_binary(&ExecuteMsg::WhiteWhalePoolSwap {
operation: SwapOperation {
pool: "pool_1".to_string(),
denom_in: "os".to_string(),
denom_out: "ua".to_string(),
interface: None,
}
})?,
funds: vec![],
}.into(),
gas_limit: None,
reply_on: Never,
},
SubMsg {
id: 0,
msg: WasmMsg::Execute {
contract_addr: "swap_contract_address".to_string(),
msg: to_json_binary(&ExecuteMsg::WhiteWhalePoolSwap {
operation: SwapOperation {
pool: "pool_2".to_string(),
denom_in: "ua".to_string(),
denom_out: "un".to_string(),
interface: None,
contract_addr: "mantra_pool_manager".to_string(),
msg: to_json_binary(&MantraPoolManagerExecuteMsg::ExecuteSwapOperations {
operations: vec![MantraSwapOperation::MantraSwap {
pool_identifier: "pool_1".to_string(),
token_in_denom: "os".to_string(),
token_out_denom: "ua".to_string(),
},
MantraSwapOperation::MantraSwap {
pool_identifier: "pool_2".to_string(),
token_in_denom: "ua".to_string(),
token_out_denom: "un".to_string(),
}
],
minimum_receive: None,
receiver: Some("entry_point".to_string()),
max_spread: Some(Decimal::percent(50)),
})?,
funds: vec![],
funds: vec![Coin::new(100, "os")],
}.into(),
gas_limit: None,
reply_on: Never,
},
SubMsg {
id: 0,
msg: WasmMsg::Execute {
contract_addr: "swap_contract_address".to_string(),
msg: to_json_binary(&ExecuteMsg::TransferFundsBack {
return_denom: "un".to_string(),
swapper: Addr::unchecked("entry_point"),
})?,
funds: vec![],
}
.into(),
gas_limit: None,
reply_on: Never,
},
}
],
expected_error: None,
};
@@ -212,9 +181,13 @@ fn test_execute_swap(params: Params) -> ContractResult<()> {

// Store the entry point contract address
ENTRY_POINT_CONTRACT_ADDRESS.save(deps.as_mut().storage, &Addr::unchecked("entry_point"))?;
MANTRA_DEX_POOL_MANAGER_ADDRESS.save(
deps.as_mut().storage,
&Addr::unchecked("mantra_pool_manager"),
)?;

// Call execute_swap with the given test parameters
let res = skip_go_swap_adapter_white_whale::contract::execute(
let res = skip_go_swap_adapter_mantra_dex::contract::execute(
deps.as_mut(),
env,
info,

This file was deleted.

This file was deleted.

42 changes: 42 additions & 0 deletions contracts/adapters/swap/mantra-dex/tests/test_instantiate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use cosmwasm_std::{
testing::{mock_dependencies, mock_env, mock_info},
Addr,
};
use skip::swap::MantraDexInstantiateMsg;
use skip_go_swap_adapter_mantra_dex::state::{
ENTRY_POINT_CONTRACT_ADDRESS, MANTRA_DEX_POOL_MANAGER_ADDRESS,
};

#[test]
fn test_instantiate() {
let mut deps = mock_dependencies();
let env = mock_env();

let sender = Addr::unchecked("sender");

// Create mock info with entry point contract address
let info = mock_info(&sender.to_string(), &[]);

// Call execute_swap with the given test parameters
let res = skip_go_swap_adapter_mantra_dex::contract::instantiate(
deps.as_mut(),
env,
info,
MantraDexInstantiateMsg {
entry_point_contract_address: "entry_point".to_string(),
mantra_pool_manager_address: "pool_manager".to_string(),
},
);

assert!(res.is_ok());

let entry_point = ENTRY_POINT_CONTRACT_ADDRESS
.load(deps.as_ref().storage)
.unwrap();
let pool_manager = MANTRA_DEX_POOL_MANAGER_ADDRESS
.load(deps.as_ref().storage)
.unwrap();

assert_eq!(entry_point, Addr::unchecked("entry_point"));
assert_eq!(pool_manager, Addr::unchecked("pool_manager"));
}