Skip to content

Commit

Permalink
fix: fixed spot price calculation to account for direction
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Jun 27, 2024
1 parent 46e815d commit 6637ec7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions contracts/sumtree-orderbook/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use crate::{
error::ContractResult,
msg::{
CalcOutAmtGivenInResponse, DenomsResponse, GetSwapFeeResponse,
GetTotalPoolLiquidityResponse, GetUnrealizedCancelsResponse, OrdersResponse, SpotPriceResponse,
TickIdAndState, TickUnrealizedCancels, TicksResponse, UnrealizedCancels,
GetTotalPoolLiquidityResponse, GetUnrealizedCancelsResponse, OrdersResponse,
SpotPriceResponse, TickIdAndState, TickUnrealizedCancels, TicksResponse, UnrealizedCancels,
},
order,
state::{
get_directional_liquidity, get_orders_by_owner, orders, IS_ACTIVE, ORDERBOOK, TICK_STATE,
},
sudo::ensure_swap_fee,
sumtree::tree::{get_prefix_sum, get_root_node},
tick_math::tick_to_price,
tick_math::{amount_to_value, tick_to_price, RoundingDirection},
types::{FilterOwnerOrders, LimitOrder, MarketOrder, OrderDirection, TickState},
ContractError,
};
Expand Down Expand Up @@ -59,8 +59,10 @@ 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 = amount_to_value(direction, Uint128::one(), price, RoundingDirection::Down)?;

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

Expand Down
2 changes: 1 addition & 1 deletion contracts/sumtree-orderbook/src/tests/test_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn test_query_spot_price() {
],
base_denom: QUOTE_DENOM.to_string(),
quote_denom: BASE_DENOM.to_string(),
expected_price: Decimal::percent(50),
expected_price: Decimal::percent(200),
expected_error: None,
},
SpotPriceTestCase {
Expand Down

0 comments on commit 6637ec7

Please sign in to comment.