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

Commit

Permalink
feat: add recipient and status (#50)
Browse files Browse the repository at this point in the history
feat: fix test + add status + add recipient
  • Loading branch information
politeWall authored Nov 28, 2023
1 parent 6b1d9cc commit bcaaabb
Show file tree
Hide file tree
Showing 29 changed files with 194 additions and 416 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ thiserror = "1"
schemars = "0.8.1"
cosmwasm-schema = "1.1.4"
cw-utils = "0.13"
elys-bindings = { version = "0.3.0", git = "https://github.com/elys-network/bindings", tag = "v0.3.0" }
elys-bindings = { version = "0.4.0", git = "https://github.com/elys-network/bindings", branch = "swap-by-denom" }

[dev-dependencies]

cw-multi-test = "0.13.4"
serde_json = "1.0.107"
elys-bindings = { version = "0.3.0", git = "https://github.com/elys-network/bindings", tag = "v0.3.0", features = [
elys-bindings = { version = "0.4.0", git = "https://github.com/elys-network/bindings", branch = "swap-by-denom", features = [
"testing",
] }
elys-bindings-test = { version = "0.3.0", git = "https://github.com/elys-network/bindings", tag = "v0.3.0" }
elys-bindings-test = { version = "0.4.0", git = "https://github.com/elys-network/bindings", branch = "swap-by-denom" }
36 changes: 18 additions & 18 deletions src/action/execute/cancel_spot_order.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::states::PROCESSED_SPOT_ORDER;

use super::*;

pub fn cancel_spot_order(
info: MessageInfo,
deps: DepsMut<ElysQuery>,
order_id: u64,
) -> Result<Response<ElysMsg>, ContractError> {
let orders_list: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;
let order: SpotOrder = match orders_list.iter().find(|order| order.order_id == order_id) {
Some(order) => order.to_owned(),
let mut orders_list: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;

let order: &mut SpotOrder = match orders_list
.iter_mut()
.find(|order| order.order_id == order_id)
{
Some(order) => order,
None => return Err(ContractError::OrderNotFound { order_id }),
};

Expand All @@ -19,26 +21,24 @@ pub fn cancel_spot_order(
});
}

let processed_spot_order = PROCESSED_SPOT_ORDER.load(deps.storage)?;
for (id, _) in processed_spot_order {
if id == order_id {
return Err(ContractError::ProcessSpotOrderProcessing { order_id });
}
if order.status != Status::NotProcessed {
return Err(ContractError::CancelStatusError {
order_id,
status: order.status.clone(),
});
}

order.status = Status::Canceled;
let refund_msg = BankMsg::Send {
to_address: order.owner_address.to_string(),
amount: vec![order.order_amount],
amount: vec![order.order_amount.clone()],
};

let resp = Response::new().add_message(CosmosMsg::Bank(refund_msg));

let new_orders_list: Vec<SpotOrder> = orders_list
.into_iter()
.filter(|order| order.order_id != order_id)
.collect();
let resp = Response::new()
.add_message(CosmosMsg::Bank(refund_msg))
.add_attribute("canceled_spot_order_id", order_id.to_string());

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

Ok(resp)
}
46 changes: 27 additions & 19 deletions src/action/execute/cancel_spot_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ pub fn cancel_spot_orders(
});
}

let orders: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;
let processed_spot_order: Vec<(u64, BankMsg)> = PROCESSED_SPOT_ORDER.load(deps.storage)?;
let mut orders: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;

let user_orders: Vec<SpotOrder> = orders
.iter()
Expand All @@ -32,26 +31,31 @@ pub fn cancel_spot_orders(

let filtered_order: Vec<SpotOrder> = filter_order_by_id(&user_orders, &order_ids)?;

for (id, _) in processed_spot_order {
if filtered_order.iter().any(|order| order.order_id == id) {
return Err(ContractError::ProcessSpotOrderProcessing { order_id: id });
}
}

let filtered_order = filter_order_by_type(filtered_order, order_type)?;

let new_orders_list = orders
.into_iter()
.filter(|order| !filtered_order.contains(order))
.collect();

SPOT_ORDER.save(deps.storage, &new_orders_list)?;
if let Some(order) = filtered_order
.iter()
.find(|order| order.status != Status::NotProcessed)
{
return Err(ContractError::CancelStatusError {
order_id: order.order_id,
status: order.status.clone(),
});
}

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

for order in orders.iter_mut() {
if order_ids.contains(&order.order_id) {
order.status = Status::Canceled;
}
}

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

let refund_msg = make_refund_msg(filtered_order, owner_address);

Ok(Response::new()
Expand All @@ -60,13 +64,17 @@ pub fn cancel_spot_orders(
}

fn filter_order_by_id(
orders: &[SpotOrder],
orders: &Vec<SpotOrder>,
order_ids: &Option<Vec<u64>>,
) -> Result<Vec<SpotOrder>, ContractError> {
let order_ids: Vec<u64> = order_ids.as_ref().map_or_else(
|| orders.iter().map(|order| order.order_id).collect(),
|ids| ids.clone(),
);
let order_ids = match order_ids {
Some(order_ids) => order_ids,
None => return Ok(orders.to_owned()),
};

if order_ids.is_empty() {
return Err(StdError::generic_err("order_ids is defined empty").into());
}

let filtered_order: Vec<SpotOrder> = orders
.iter()
Expand Down
2 changes: 2 additions & 0 deletions src/action/execute/create_spot_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn create_spot_order(
&info.funds[0],
&order_source_denom,
&order_target_denom,
&Decimal::zero(),
)?;

let mut order_vec = SPOT_ORDER.load(deps.storage)?;
Expand Down Expand Up @@ -125,6 +126,7 @@ fn create_resp(
&in_route,
Int128::zero(),
Decimal::zero(),
&new_order.owner_address,
);

let info_id = if let Some(max_info) = reply_infos.iter().max_by_key(|info| info.id) {
Expand Down
4 changes: 2 additions & 2 deletions src/action/query/swap_estimation_by_denom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use cosmwasm_std::Coin;
use cosmwasm_std::{Coin, Decimal};

pub fn swap_estimation_by_denom(
deps: Deps<ElysQuery>,
Expand All @@ -10,7 +10,7 @@ pub fn swap_estimation_by_denom(
let querier = ElysQuerier::new(&deps.querier);

let resp: AmmSwapEstimationByDenomResponse =
querier.amm_swap_estimation_by_denom(&amount, denom_in, denom_out)?;
querier.amm_swap_estimation_by_denom(&amount, denom_in, denom_out, &Decimal::zero())?;

Ok(resp)
}
45 changes: 17 additions & 28 deletions src/action/reply/spot_order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{coins, from_json, Binary, DepsMut, SubMsgResult};
use cosmwasm_std::{from_json, Binary, DepsMut, SubMsgResult};

use crate::helper::get_response_from_reply;

Expand All @@ -9,41 +9,30 @@ pub fn reply_to_spot_order(
data: Option<Binary>,
module_resp: SubMsgResult,
) -> Result<Response<ElysMsg>, ContractError> {
let amm_response: AmmSwapExactAmountInResp = match get_response_from_reply(module_resp) {
Ok(expr) => expr,
Err(err) => return Ok(err),
};
let order_id: u64 = from_json(&data.unwrap()).unwrap();

let orders: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;
let mut orders: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;

let order_id: u64 = match data {
Some(ref order_id) => from_json(&order_id)?,
None => return Ok(Response::new().add_attribute("error", "no meta_data".to_string())),
};
let order: &mut SpotOrder = orders
.iter_mut()
.find(|order| order.order_id == order_id)
.unwrap();

let order: SpotOrder = match orders.iter().find(|order| order.order_id == order_id) {
Some(order) => order.to_owned(),
None => {
return Ok(Response::new().add_attribute(
"error",
format!("{:?}", ContractError::OrderNotFound { order_id }),
))
let _: AmmSwapExactAmountInResp = match get_response_from_reply(module_resp) {
Ok(expr) => expr,
Err(err) => {
order.status = Status::NotProcessed;
SPOT_ORDER.save(deps.storage, &orders)?;
return Ok(err);
}
};

let bank_msg = BankMsg::Send {
to_address: order.owner_address.to_string(),
amount: coins(
amm_response.token_out_amount.i64() as u128,
order.order_target_denom.to_string(),
),
};
order.status = Status::Processed;

let mut processd_spot_orders = PROCESSED_SPOT_ORDER.load(deps.storage)?;
processd_spot_orders.push((order_id, bank_msg));
PROCESSED_SPOT_ORDER.save(deps.storage, &processd_spot_orders)?;
SPOT_ORDER.save(deps.storage, &orders)?;

let resp: Response<ElysMsg> = Response::new();
let resp: Response<ElysMsg> =
Response::new().add_attribute("processed_order_id", order_id.to_string());

Ok(resp)
}
36 changes: 10 additions & 26 deletions src/action/reply/spot_order_market.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{coins, from_json, Binary, DepsMut, StdError, SubMsgResult};
use cosmwasm_std::{from_json, Binary, DepsMut, StdError, SubMsgResult};

use super::*;

Expand All @@ -7,43 +7,27 @@ pub fn reply_to_spot_order_market(
data: Option<Binary>,
module_resp: SubMsgResult,
) -> Result<Response<ElysMsg>, ContractError> {
let response = match module_resp.into_result() {
Ok(response) => response,
Err(err) => return Err(StdError::generic_err(err).into()),
if let Err(err) = module_resp.into_result() {
return Err(StdError::generic_err(err).into());
};

let meta_data = match response.data {
Some(data) => data,
None => return Err(StdError::generic_err("No Data").into()),
};

let amm_response: AmmSwapExactAmountInResp = from_json(&meta_data)?;

let orders: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;
let mut orders: Vec<SpotOrder> = SPOT_ORDER.load(deps.storage)?;

let order_id: u64 = match data {
Some(order_id) => from_json(&order_id)?,
None => return Err(StdError::generic_err("no meta_data".to_string()).into()),
};

let order: SpotOrder = match orders.iter().find(|order| order.order_id == order_id) {
Some(order) => order.to_owned(),
let order: &mut SpotOrder = match orders.iter_mut().find(|order| order.order_id == order_id) {
Some(order) => order,
None => return Err(ContractError::OrderNotFound { order_id }),
};

let bank_msg = BankMsg::Send {
to_address: order.owner_address.to_string(),
amount: coins(
amm_response.token_out_amount.i64() as u128,
order.order_target_denom.to_string(),
),
};
order.status = Status::Processed;

let mut processd_spot_orders = PROCESSED_SPOT_ORDER.load(deps.storage)?;
processd_spot_orders.push((order_id, bank_msg));
PROCESSED_SPOT_ORDER.save(deps.storage, &processd_spot_orders)?;
SPOT_ORDER.save(deps.storage, &orders)?;

let resp: Response<ElysMsg> = Response::new();
let resp: Response<ElysMsg> =
Response::new().add_attribute("processed_order_id", order_id.to_string());

Ok(resp)
}
Loading

0 comments on commit bcaaabb

Please sign in to comment.