Skip to content

Commit 72ce4b5

Browse files
authored
Merge pull request #191 from osmosis-labs/connor/e2e-testing
[Sumtree]: E2E and Fuzz Testing
2 parents cd637d0 + 5c1911a commit 72ce4b5

File tree

23 files changed

+4573
-424
lines changed

23 files changed

+4573
-424
lines changed

Cargo.lock

Lines changed: 2137 additions & 258 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/sumtree-orderbook/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ rpath = false
3131
backtraces = ["cosmwasm-std/backtraces"]
3232
# use library feature to disable all instantiate/execute/query exports
3333
library = []
34+
skip-integration-tests = []
3435

3536
[package.metadata.scripts]
3637
optimize = """docker run --rm -v "$(pwd)":/code \
@@ -52,12 +53,13 @@ schemars = "0.8.15"
5253
serde = { version = "1.0.189", default-features = false, features = ["derive"] }
5354
thiserror = { version = "1.0.49" }
5455
osmosis-std-derive = "0.15.3"
55-
osmosis-std = "0.16.0"
56+
osmosis-std = "0.25.0"
5657
prost = { version = "0.11.2", default-features = false, features = [
5758
"prost-derive",
5859
] }
5960

6061

6162
[dev-dependencies]
6263
cw-multi-test = "0.18.0"
64+
osmosis-test-tube = { version = "25.0.0", features = ["wasm-sudo"] }
6365
rand = "0.8.4"

contracts/sumtree-orderbook/src/contract.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
143143
} => Ok(to_json_binary(&query::orders_by_owner(
144144
deps, owner, start_from, end_at, limit,
145145
)?)?),
146+
QueryMsg::OrderbookState {} => Ok(to_json_binary(&query::orderbook_state(deps)?)?),
146147
QueryMsg::TicksById { tick_ids } => {
147148
Ok(to_json_binary(&query::ticks_by_id(deps, tick_ids)?)?)
148149
}

contracts/sumtree-orderbook/src/msg.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub enum QueryMsg {
109109
#[returns(bool)]
110110
IsActive {},
111111

112-
#[returns(Vec<crate::types::LimitOrder>)]
112+
#[returns(OrdersResponse)]
113113
OrdersByOwner {
114114
// The address of the order maker
115115
owner: Addr,
@@ -129,6 +129,9 @@ pub enum QueryMsg {
129129
limit: Option<u64>,
130130
},
131131

132+
#[returns(crate::types::Orderbook)]
133+
OrderbookState {},
134+
132135
#[returns(DenomsResponse)]
133136
Denoms {},
134137

contracts/sumtree-orderbook/src/order.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ pub fn place_limit(
106106
.with_placed_at(env.block.time);
107107

108108
let quant_dec256 = Decimal256::from_ratio(limit_order.quantity.u128(), Uint256::one());
109-
110109
// Save the order to the orderbook
111110
orders().save(deps.storage, &(tick_id, order_id), &limit_order)?;
112111

@@ -166,7 +165,6 @@ pub fn cancel_limit(
166165
.effective_total_amount_swapped,
167166
)?;
168167

169-
// Ensure the order has not been filled.
170168
let tick_state = TICK_STATE.load(deps.storage, tick_id).unwrap_or_default();
171169
let tick_values = tick_state.get_values(order.order_direction);
172170
ensure!(
@@ -213,10 +211,10 @@ pub fn cancel_limit(
213211
);
214212

215213
orders().remove(deps.storage, &(order.tick_id, order.order_id))?;
216-
217214
curr_tick_values.total_amount_of_liquidity = curr_tick_values
218215
.total_amount_of_liquidity
219216
.checked_sub(Decimal256::from_ratio(order.quantity, Uint256::one()))?;
217+
220218
curr_tick_state.set_values(order.order_direction, curr_tick_values);
221219
TICK_STATE.save(deps.storage, order.tick_id, &curr_tick_state)?;
222220
subtract_directional_liquidity(deps.storage, order.order_direction, quant_dec256)?;
@@ -503,6 +501,11 @@ pub(crate) fn run_market_order_internal(
503501
let current_tick_id = maybe_current_tick?;
504502
let mut current_tick = TICK_STATE.load(storage, current_tick_id)?;
505503
let mut current_tick_values = current_tick.get_values(order.order_direction.opposite());
504+
505+
if current_tick_values.total_amount_of_liquidity.is_zero() {
506+
continue;
507+
}
508+
506509
let tick_price = tick_to_price(current_tick_id)?;
507510
last_tick_price = tick_price;
508511

@@ -634,6 +637,7 @@ pub(crate) fn claim_order(
634637
// Sync the tick the order is on to ensure correct ETAS
635638
let bid_tick_values = tick_state.get_values(OrderDirection::Bid);
636639
let ask_tick_values = tick_state.get_values(OrderDirection::Ask);
640+
637641
sync_tick(
638642
storage,
639643
tick_id,

contracts/sumtree-orderbook/src/query.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
sudo::ensure_swap_fee,
1919
sumtree::tree::{get_prefix_sum, get_root_node},
2020
tick_math::tick_to_price,
21-
types::{FilterOwnerOrders, LimitOrder, MarketOrder, OrderDirection, TickState},
21+
types::{FilterOwnerOrders, MarketOrder, OrderDirection, Orderbook, TickState},
2222
ContractError,
2323
};
2424

@@ -60,8 +60,8 @@ pub(crate) fn spot_price(
6060
let price = tick_to_price(next_tick)?;
6161

6262
let spot_price = match direction {
63-
OrderDirection::Ask => price.inv().unwrap(),
64-
OrderDirection::Bid => price,
63+
OrderDirection::Ask => price,
64+
OrderDirection::Bid => price.inv().unwrap(),
6565
};
6666

6767
Ok(SpotPriceResponse {
@@ -205,15 +205,25 @@ pub(crate) fn orders_by_owner(
205205
start_from: Option<(i64, u64)>,
206206
end_at: Option<(i64, u64)>,
207207
limit: Option<u64>,
208-
) -> ContractResult<Vec<LimitOrder>> {
208+
) -> ContractResult<OrdersResponse> {
209+
let count = orders()
210+
.idx
211+
.owner
212+
.prefix(owner.clone())
213+
.keys(deps.storage, None, None, Order::Ascending)
214+
.count();
209215
let orders = get_orders_by_owner(
210216
deps.storage,
211217
FilterOwnerOrders::all(owner),
212218
start_from,
213219
end_at,
214220
limit,
215221
)?;
216-
Ok(orders)
222+
223+
Ok(OrdersResponse {
224+
count: count as u64,
225+
orders,
226+
})
217227
}
218228

219229
pub(crate) fn denoms(deps: Deps) -> ContractResult<DenomsResponse> {
@@ -224,6 +234,11 @@ pub(crate) fn denoms(deps: Deps) -> ContractResult<DenomsResponse> {
224234
})
225235
}
226236

237+
pub(crate) fn orderbook_state(deps: Deps) -> ContractResult<Orderbook> {
238+
let orderbook = ORDERBOOK.load(deps.storage)?;
239+
Ok(orderbook)
240+
}
241+
227242
pub(crate) fn ticks_by_id(deps: Deps, tick_ids: Vec<i64>) -> ContractResult<TicksResponse> {
228243
let mut ticks: Vec<TickIdAndState> = vec![];
229244
for tick_id in tick_ids {

contracts/sumtree-orderbook/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn get_orders_by_owner(
8383
page_size: Option<u64>,
8484
) -> StdResult<Vec<LimitOrder>> {
8585
let page_size = page_size.unwrap_or(DEFAULT_PAGE_SIZE) as usize;
86-
let min = min.map(Bound::exclusive);
86+
let min = min.map(Bound::inclusive);
8787
let max = max.map(Bound::inclusive);
8888

8989
// Define the prefix iterator based on the filter

contracts/sumtree-orderbook/src/sumtree/tree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub fn get_prefix_sum(
6464
// Since the longest path this function can walk is from the root to a leaf, it runs in O(log(N)) time. Given
6565
// how it is able to terminate early using our sumtree's range properties, in many cases it will likely run
6666
// in much less.
67+
6768
fn prefix_sum_walk(
6869
storage: &dyn Storage,
6970
node: &TreeNode,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod test_fuzz;
2+
mod test_orders;

0 commit comments

Comments
 (0)