Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
test: add test case for cancel spot orders
Browse files Browse the repository at this point in the history
  • Loading branch information
politeWall committed Nov 10, 2023
1 parent 0166deb commit 82320b4
Show file tree
Hide file tree
Showing 9 changed files with 541 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/action/execute/cancel_spot_orders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, Coin, StdError};
use cosmwasm_std::{to_binary, Addr, Coin, StdError};

use super::*;

Expand All @@ -11,7 +11,7 @@ pub fn cancel_spot_orders(
) -> Result<Response<ElysMsg>, ContractError> {
if info.sender.as_str() != owner_address {
return Err(ContractError::Unauthorized {
sender: Addr::unchecked(owner_address),
sender: info.sender,
});
}

Expand Down Expand Up @@ -47,9 +47,16 @@ pub fn cancel_spot_orders(

SPOT_ORDER.save(deps.storage, &new_orders_list)?;

let order_ids: Vec<u64> = match order_ids {
Some(order_ids) => order_ids,
None => filtered_order.iter().map(|order| order.order_id).collect(),
};

let refund_msg = make_refund_msg(filtered_order, owner_address);

Ok(Response::new().add_message(refund_msg))
Ok(Response::new()
.add_message(refund_msg)
.set_data(to_binary(&order_ids)?))
}

fn filter_order_by_id(
Expand Down
159 changes: 159 additions & 0 deletions src/tests/cancel_spot_orders/successfully_cancel_orders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use cosmwasm_std::{to_binary, Coin};

use super::*;

#[test]
fn successfully_cancel_orders() {
let wallet: Vec<(&str, Vec<Coin>)> = vec![(
"owner",
vec![coin(16, "btc"), coin(5, "eth"), coin(20, "usdt")],
)];

let orders = vec![
SpotOrder {
order_type: SpotOrderType::LimitBuy,
order_id: 0,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(10, "btc"),
owner_address: Addr::unchecked("user"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
SpotOrder {
order_type: SpotOrderType::LimitSell,
order_id: 1,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(5, "eth"),
owner_address: Addr::unchecked("user"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
SpotOrder {
order_type: SpotOrderType::StopLoss,
order_id: 2,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(20, "usdt"),
owner_address: Addr::unchecked("user1"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
SpotOrder {
order_type: SpotOrderType::StopLoss,
order_id: 3,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(6, "btc"),
owner_address: Addr::unchecked("user"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
];

let mut app = ElysApp::new_with_wallets(wallet);

// Create a mock message to instantiate the contract with an empty list of orders.
let instantiate_msg = InstantiateMockMsg {
process_order_executor: "owner".to_string(),
orders,
};

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("owner"),
&instantiate_msg,
&vec![coin(16, "btc"), coin(5, "eth"), coin(20, "usdt")],
"Contract",
None,
)
.unwrap();

assert_eq!(
app.wrap().query_balance(&addr, "btc").unwrap(),
coin(16, "btc")
);

assert_eq!(
app.wrap().query_balance(&addr, "eth").unwrap(),
coin(5, "eth")
);
assert_eq!(
app.wrap().query_balance(&addr, "usdt").unwrap(),
coin(20, "usdt")
);

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
coin(0, "btc")
);

assert_eq!(
app.wrap().query_balance("user", "eth").unwrap(),
coin(0, "eth")
);
assert_eq!(
app.wrap().query_balance("user", "usdt").unwrap(),
coin(0, "usdt")
);

let resp = app
.execute_contract(
Addr::unchecked("user"),
addr.clone(),
&&ExecuteMsg::CancelSpotOrders {
order_ids: None,
owner_address: "user".to_string(),
order_type: None,
},
&[],
)
.unwrap();

assert_eq!(resp.data.unwrap(), to_binary(&vec![0, 1, 3]).unwrap());

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
coin(16, "btc")
);

assert_eq!(
app.wrap().query_balance("user", "eth").unwrap(),
coin(5, "eth")
);
assert_eq!(
app.wrap().query_balance("user", "usdt").unwrap(),
coin(0, "usdt")
);

assert_eq!(
app.wrap().query_balance(&addr, "btc").unwrap(),
coin(0, "btc")
);

assert_eq!(
app.wrap().query_balance(&addr, "eth").unwrap(),
coin(0, "eth")
);
assert_eq!(
app.wrap().query_balance(&addr, "usdt").unwrap(),
coin(20, "usdt")
);
}
159 changes: 159 additions & 0 deletions src/tests/cancel_spot_orders/successfully_cancel_orders_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use cosmwasm_std::{to_binary, Coin};

use super::*;

#[test]
fn successfully_cancel_orders_ids() {
let wallet: Vec<(&str, Vec<Coin>)> = vec![(
"owner",
vec![coin(16, "btc"), coin(5, "eth"), coin(20, "usdt")],
)];

let orders = vec![
SpotOrder {
order_type: SpotOrderType::LimitBuy,
order_id: 0,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(10, "btc"),
owner_address: Addr::unchecked("user"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
SpotOrder {
order_type: SpotOrderType::LimitSell,
order_id: 1,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(5, "eth"),
owner_address: Addr::unchecked("user"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
SpotOrder {
order_type: SpotOrderType::StopLoss,
order_id: 2,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(20, "usdt"),
owner_address: Addr::unchecked("user1"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
SpotOrder {
order_type: SpotOrderType::StopLoss,
order_id: 3,
order_price: SpotOrderPrice {
base_denom: "".to_string(),
quote_denom: "".to_string(),
rate: Decimal::zero(),
},
order_amount: coin(6, "btc"),
owner_address: Addr::unchecked("user"),
order_target_denom: "".to_string(),
order_amm_routes: vec![],
},
];

let mut app = ElysApp::new_with_wallets(wallet);

// Create a mock message to instantiate the contract with an empty list of orders.
let instantiate_msg = InstantiateMockMsg {
process_order_executor: "owner".to_string(),
orders,
};

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("owner"),
&instantiate_msg,
&vec![coin(16, "btc"), coin(5, "eth"), coin(20, "usdt")],
"Contract",
None,
)
.unwrap();

assert_eq!(
app.wrap().query_balance(&addr, "btc").unwrap(),
coin(16, "btc")
);

assert_eq!(
app.wrap().query_balance(&addr, "eth").unwrap(),
coin(5, "eth")
);
assert_eq!(
app.wrap().query_balance(&addr, "usdt").unwrap(),
coin(20, "usdt")
);

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
coin(0, "btc")
);

assert_eq!(
app.wrap().query_balance("user", "eth").unwrap(),
coin(0, "eth")
);
assert_eq!(
app.wrap().query_balance("user", "usdt").unwrap(),
coin(0, "usdt")
);

let resp = app
.execute_contract(
Addr::unchecked("user"),
addr.clone(),
&&ExecuteMsg::CancelSpotOrders {
order_ids: Some(vec![1, 3]),
owner_address: "user".to_string(),
order_type: None,
},
&[],
)
.unwrap();

assert_eq!(resp.data.unwrap(), to_binary(&vec![1, 3]).unwrap());

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
coin(6, "btc")
);

assert_eq!(
app.wrap().query_balance("user", "eth").unwrap(),
coin(5, "eth")
);
assert_eq!(
app.wrap().query_balance("user", "usdt").unwrap(),
coin(0, "usdt")
);

assert_eq!(
app.wrap().query_balance(&addr, "btc").unwrap(),
coin(10, "btc")
);

assert_eq!(
app.wrap().query_balance(&addr, "eth").unwrap(),
coin(0, "eth")
);
assert_eq!(
app.wrap().query_balance(&addr, "usdt").unwrap(),
coin(20, "usdt")
);
}
Loading

0 comments on commit 82320b4

Please sign in to comment.