This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add query to multiple orders * save change * save change * test: create test for get spot orders * feat: bindings repository integration * feat: get orders logic with binding pagination * test: fix get orders tests * ci: trigger GitHub Actions * integrate the binding module to the smart contract (#29) * save change * save change * feat: bindings repository integration * feat: script to test the process order endpoint * fix: fix the code to be usable with the changes from the bindings repository * feat: add query to multiple orders * test: create test for get spot orders * save change * feat: bindings repository integration * feat: get orders logic with binding pagination * test: fix get orders tests * fix: remove unused import * test: create test for get spot orders * ci: trigger GitHub Actions
- Loading branch information
1 parent
a596ee9
commit 58eaabd
Showing
12 changed files
with
294 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use super::*; | ||
|
||
pub fn get_spot_orders( | ||
deps: Deps<ElysQuery>, | ||
pagination: PageRequest, | ||
order_owner: Option<String>, | ||
order_type: Option<SpotOrderType>, | ||
) -> Result<GetSpotOrdersResp, ContractError> { | ||
let orders = SPOT_ORDER.load(deps.storage)?; | ||
|
||
let (orders, page_response) = pagination.filter(orders)?; | ||
|
||
if orders.is_empty() { | ||
return Ok(GetSpotOrdersResp { | ||
page_response, | ||
orders, | ||
}); | ||
}; | ||
|
||
let orders = match (order_owner, order_type) { | ||
(None, Some(order_type)) => orders | ||
.iter() | ||
.filter(|order| order.order_type == order_type) | ||
.cloned() | ||
.collect(), | ||
(Some(owner), None) => orders | ||
.iter() | ||
.filter(|order| order.owner_address == owner) | ||
.cloned() | ||
.collect(), | ||
(Some(owner), Some(order_type)) => orders | ||
.iter() | ||
.filter(|order| order.owner_address == owner && order.order_type == order_type) | ||
.cloned() | ||
.collect(), | ||
(None, None) => orders, | ||
}; | ||
|
||
let page_response = match page_response.total { | ||
Some(_) => PageResponse { | ||
next_key: page_response.next_key, | ||
total: Some(orders.len() as u64), | ||
}, | ||
None => page_response, | ||
}; | ||
|
||
Ok(GetSpotOrdersResp { | ||
page_response, | ||
orders, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::Coin; | ||
use elys_bindings::types::Price; | ||
|
||
#[cw_serde] | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::types::{PageResponse, SpotOrder}; | ||
use cosmwasm_schema::cw_serde; | ||
|
||
#[cw_serde] | ||
pub struct GetSpotOrdersResp { | ||
pub page_response: PageResponse, | ||
pub orders: Vec<SpotOrder>, | ||
} | ||
|
||
impl GetSpotOrdersResp { | ||
pub fn empty(have_total: bool) -> Self { | ||
Self { | ||
page_response: PageResponse::empty(have_total), | ||
orders: vec![], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use crate::msg::query_resp::GetSpotOrdersResp; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn get_spot_orders() { | ||
let orders: Vec<SpotOrder> = create_orders(); | ||
let mut app = ElysApp::new(); | ||
|
||
let instantiate_msg = InstantiateMockMsg { | ||
process_order_executor: "owner".to_string(), | ||
orders: orders.clone(), | ||
}; | ||
|
||
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, | ||
&[], | ||
"Contract", | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
let mut page_req = PageRequest::new(2); | ||
|
||
let resp: GetSpotOrdersResp = app | ||
.wrap() | ||
.query_wasm_smart( | ||
&addr, | ||
&QueryMsg::GetSpotOrders { | ||
pagination: page_req.clone(), | ||
order_owner: None, | ||
order_type: None, | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
let (first_third, the_rest) = orders.split_at(2); | ||
|
||
assert_eq!(&resp.orders, first_third); | ||
|
||
page_req.update(resp.page_response.next_key); | ||
|
||
let resp: GetSpotOrdersResp = app | ||
.wrap() | ||
.query_wasm_smart( | ||
&addr, | ||
&QueryMsg::GetSpotOrders { | ||
pagination: page_req.clone(), | ||
order_owner: None, | ||
order_type: None, | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
let (second_third, last_order) = the_rest.split_at(2); | ||
|
||
assert_eq!(&resp.orders, second_third); | ||
|
||
page_req.update(resp.page_response.next_key); | ||
|
||
let resp: GetSpotOrdersResp = app | ||
.wrap() | ||
.query_wasm_smart( | ||
&addr, | ||
&QueryMsg::GetSpotOrders { | ||
pagination: page_req.clone(), | ||
order_owner: None, | ||
order_type: None, | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(&resp.orders, last_order); | ||
} | ||
|
||
fn create_orders() -> Vec<SpotOrder> { | ||
vec![ | ||
SpotOrder { | ||
order_type: SpotOrderType::LimitBuy, | ||
order_id: 0, | ||
order_price: SpotOrderPrice { | ||
base_denom: "btc".to_owned(), | ||
quote_denom: "usdc".to_owned(), | ||
rate: Decimal::from_atomics(Uint128::new(25), 1).unwrap(), | ||
}, | ||
order_amount: coin(255, "btc"), | ||
owner_address: Addr::unchecked("userA"), | ||
order_target_denom: "btc".to_owned(), | ||
order_amm_routes: vec![], | ||
}, | ||
SpotOrder { | ||
order_type: SpotOrderType::LimitSell, | ||
order_id: 1, | ||
order_price: SpotOrderPrice { | ||
base_denom: "eth".to_owned(), | ||
quote_denom: "usdt".to_owned(), | ||
rate: Decimal::from_atomics(Uint128::new(10), 1).unwrap(), | ||
}, | ||
order_amount: coin(100, "eth"), | ||
owner_address: Addr::unchecked("userB"), | ||
order_target_denom: "eth".to_owned(), | ||
order_amm_routes: vec![], | ||
}, | ||
SpotOrder { | ||
order_type: SpotOrderType::StopLoss, | ||
order_id: 2, | ||
order_price: SpotOrderPrice { | ||
base_denom: "xrp".to_owned(), | ||
quote_denom: "usdt".to_owned(), | ||
rate: Decimal::from_atomics(Uint128::new(5), 1).unwrap(), | ||
}, | ||
order_amount: coin(500, "xrp"), | ||
owner_address: Addr::unchecked("userC"), | ||
order_target_denom: "xrp".to_owned(), | ||
order_amm_routes: vec![], | ||
}, | ||
SpotOrder { | ||
order_type: SpotOrderType::StopLoss, | ||
order_id: 3, | ||
order_price: SpotOrderPrice { | ||
base_denom: "ltc".to_owned(), | ||
quote_denom: "usdc".to_owned(), | ||
rate: Decimal::from_atomics(Uint128::new(15), 1).unwrap(), | ||
}, | ||
order_amount: coin(75, "ltc"), | ||
owner_address: Addr::unchecked("userD"), | ||
order_target_denom: "ltc".to_owned(), | ||
order_amm_routes: vec![], | ||
}, | ||
SpotOrder { | ||
order_type: SpotOrderType::LimitBuy, | ||
order_id: 4, | ||
order_price: SpotOrderPrice { | ||
base_denom: "ada".to_owned(), | ||
quote_denom: "usdt".to_owned(), | ||
rate: Decimal::from_atomics(Uint128::new(3), 1).unwrap(), | ||
}, | ||
order_amount: coin(200, "ada"), | ||
owner_address: Addr::unchecked("userE"), | ||
order_target_denom: "ada".to_owned(), | ||
order_amm_routes: vec![], | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::Binary; | ||
|
||
#[cw_serde] | ||
pub struct PageRequest { | ||
pub key: Option<Binary>, | ||
pub offset: Option<u64>, | ||
pub limit: u64, | ||
pub count_total: bool, | ||
pub reverse: bool, | ||
} | ||
|
||
impl PageRequest { | ||
pub fn new(limit: u64) -> Self { | ||
Self { | ||
key: None, | ||
limit, | ||
offset: None, | ||
count_total: false, | ||
reverse: false, | ||
} | ||
} | ||
|
||
pub fn update(&mut self, key: Option<Binary>) -> () { | ||
self.key = key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::Binary; | ||
|
||
#[cw_serde] | ||
pub struct PageResponse { | ||
pub next_key: Option<Binary>, | ||
pub total: Option<u64>, | ||
} | ||
|
||
impl PageResponse { | ||
pub fn new(next_key: Option<Binary>, total: Option<u64>) -> Self { | ||
Self { next_key, total } | ||
} | ||
|
||
pub fn empty(have_total: bool) -> Self { | ||
Self { | ||
next_key: None, | ||
total: if have_total { Some(0) } else { None }, | ||
} | ||
} | ||
} |