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

[Sumtree]: Spot Price Adjustment for Order Direction #198

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 10 additions & 5 deletions contracts/sumtree-orderbook/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::str::FromStr;

use cosmwasm_std::{coin, ensure, Addr, Coin, Decimal, Decimal256, Deps, Order, Uint128};
use cosmwasm_std::{coin, ensure, Addr, Coin, Decimal, Decimal256, Deps, Fraction, Order, Uint128};
use cw_storage_plus::Bound;

use crate::{
constants::{MAX_TICK, MIN_TICK},
error::ContractResult,
msg::{
CalcOutAmtGivenInResponse, DenomsResponse, GetSwapFeeResponse,
GetTotalPoolLiquidityResponse, GetUnrealizedCancelsResponse, OrdersResponse, SpotPriceResponse,
TickIdAndState, TickUnrealizedCancels, TicksResponse, UnrealizedCancels,
GetTotalPoolLiquidityResponse, GetUnrealizedCancelsResponse, OrdersResponse,
SpotPriceResponse, TickIdAndState, TickUnrealizedCancels, TicksResponse, UnrealizedCancels,
},
order,
state::{
Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) fn spot_price(
// Fetch orderbook to retrieve tick info
let orderbook = ORDERBOOK.load(deps.storage)?;
// Determine the order direction by denom pairing
let direction = orderbook.direction_from_pair(quote_asset_denom, base_asset_denom)?;
let direction = orderbook.direction_from_pair(base_asset_denom, quote_asset_denom)?;

// Determine next tick based on desired order direction
let next_tick = match direction {
Expand All @@ -59,8 +59,13 @@ pub(crate) fn spot_price(
// Generate spot price based on current active tick for desired order direction
let price = tick_to_price(next_tick)?;

let spot_price = match direction {
OrderDirection::Ask => price.inv().unwrap(),
OrderDirection::Bid => price,
};

Ok(SpotPriceResponse {
spot_price: Decimal::from_str(&price.to_string())?,
spot_price: Decimal::from_str(&spot_price.to_string())?,
})
}

Expand Down
133 changes: 112 additions & 21 deletions contracts/sumtree-orderbook/src/tests/test_query.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::str::FromStr;

use cosmwasm_std::{
coin,
testing::{mock_env, mock_info},
Addr, Coin, Decimal, Decimal256, Uint128,
};

use crate::{
constants::EXPECTED_SWAP_FEE,
constants::{EXPECTED_SWAP_FEE, MAX_TICK, MIN_TICK},
orderbook::create_orderbook,
query,
state::IS_ACTIVE,
Expand Down Expand Up @@ -45,8 +47,8 @@ fn test_query_spot_price() {
Decimal256::zero(),
None,
))],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::one(),
expected_error: None,
},
Expand Down Expand Up @@ -81,8 +83,8 @@ fn test_query_spot_price() {
None,
)),
],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::one(),
expected_error: None,
},
Expand All @@ -108,8 +110,8 @@ fn test_query_spot_price() {
None,
)),
],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::one(),
expected_error: None,
},
Expand Down Expand Up @@ -140,24 +142,56 @@ fn test_query_spot_price() {
sender.clone(),
)),
],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::percent(200),
expected_error: None,
},
SpotPriceTestCase {
name: "ASK: basic price 1 query",
name: "BID: max tick",
pre_operations: vec![OrderOperation::PlaceLimit(LimitOrder::new(
MAX_TICK,
0,
OrderDirection::Ask,
sender.clone(),
Uint128::one(),
Decimal256::zero(),
None,
))],
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::from_ratio(340282300000000000000u128, 1u128),
expected_error: None,
},
SpotPriceTestCase {
name: "BID: min tick",
pre_operations: vec![OrderOperation::PlaceLimit(LimitOrder::new(
MIN_TICK,
0,
OrderDirection::Bid,
OrderDirection::Ask,
sender.clone(),
Uint128::one(),
Decimal256::zero(),
None,
))],
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::from_str("0.000000000001").unwrap(),
expected_error: None,
},
SpotPriceTestCase {
name: "ASK: basic price 1 query",
pre_operations: vec![OrderOperation::PlaceLimit(LimitOrder::new(
0,
0,
OrderDirection::Bid,
sender.clone(),
Uint128::one(),
Decimal256::zero(),
None,
))],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::one(),
expected_error: None,
},
Expand Down Expand Up @@ -192,8 +226,8 @@ fn test_query_spot_price() {
None,
)),
],
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::one(),
expected_error: None,
},
Expand All @@ -219,8 +253,8 @@ fn test_query_spot_price() {
None,
)),
],
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::one(),
expected_error: None,
},
Expand Down Expand Up @@ -251,11 +285,68 @@ fn test_query_spot_price() {
sender.clone(),
)),
],
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::percent(200),
expected_error: None,
},
SpotPriceTestCase {
name: "ASK: large positive tick",
pre_operations: vec![
OrderOperation::PlaceLimit(LimitOrder::new(
LARGE_POSITIVE_TICK,
0,
OrderDirection::Bid,
sender.clone(),
Uint128::MAX,
Decimal256::zero(),
None,
)),
OrderOperation::RunMarket(MarketOrder::new(
Uint128::from(100u128),
OrderDirection::Ask,
sender.clone(),
)),
],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::percent(50),
expected_error: None,
},
SpotPriceTestCase {
name: "ASK: max tick",
pre_operations: vec![OrderOperation::PlaceLimit(LimitOrder::new(
MAX_TICK,
0,
OrderDirection::Bid,
sender.clone(),
Uint128::MAX,
Decimal256::zero(),
None,
))],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
// At max tick the price is 2.9387365e-21 which is outside the range of the `Decimal` type
// As such the returned price is zero
expected_price: Decimal::zero(),
expected_error: None,
},
SpotPriceTestCase {
name: "ASK: min tick",
pre_operations: vec![OrderOperation::PlaceLimit(LimitOrder::new(
MIN_TICK,
0,
OrderDirection::Bid,
sender.clone(),
Uint128::MAX,
Decimal256::zero(),
None,
))],
base_denom: BASE_DENOM.to_string(),
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::from_ratio(1000000000000u128, 1u128),
expected_error: None,
},
SpotPriceTestCase {
name: "invalid: duplicate denom",
pre_operations: vec![],
Expand All @@ -274,8 +365,8 @@ fn test_query_spot_price() {
quote_denom: QUOTE_DENOM.to_string(),
expected_price: Decimal::percent(50),
expected_error: Some(ContractError::InvalidPair {
token_in_denom: QUOTE_DENOM.to_string(),
token_out_denom: "notadenom".to_string(),
token_out_denom: QUOTE_DENOM.to_string(),
token_in_denom: "notadenom".to_string(),
}),
},
SpotPriceTestCase {
Expand All @@ -285,8 +376,8 @@ fn test_query_spot_price() {
quote_denom: "notadenom".to_string(),
expected_price: Decimal::percent(50),
expected_error: Some(ContractError::InvalidPair {
token_out_denom: BASE_DENOM.to_string(),
token_in_denom: "notadenom".to_string(),
token_in_denom: BASE_DENOM.to_string(),
token_out_denom: "notadenom".to_string(),
}),
},
];
Expand Down
Loading