Skip to content

Commit

Permalink
fix: inverted base/quote denoms for spot price to be correct
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Jun 27, 2024
1 parent 6637ec7 commit 9d6f82a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 25 deletions.
11 changes: 7 additions & 4 deletions contracts/sumtree-orderbook/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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::{
Expand All @@ -17,7 +17,7 @@ use crate::{
},
sudo::ensure_swap_fee,
sumtree::tree::{get_prefix_sum, get_root_node},
tick_math::{amount_to_value, tick_to_price, RoundingDirection},
tick_math::tick_to_price,
types::{FilterOwnerOrders, LimitOrder, MarketOrder, OrderDirection, TickState},
ContractError,
};
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,7 +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)?;
let spot_price = match direction {
OrderDirection::Ask => price.inv().unwrap(),
OrderDirection::Bid => price,
};

Ok(SpotPriceResponse {
spot_price: Decimal::from_str(&spot_price.to_string())?,
Expand Down
88 changes: 67 additions & 21 deletions contracts/sumtree-orderbook/src/tests/test_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{
};

use crate::{
constants::EXPECTED_SWAP_FEE,
constants::{EXPECTED_SWAP_FEE, MIN_TICK},
orderbook::create_orderbook,
query,
state::IS_ACTIVE,
Expand Down Expand Up @@ -45,8 +45,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 +81,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 +108,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,8 +140,8 @@ 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,
},
Expand All @@ -156,8 +156,8 @@ fn test_query_spot_price() {
Decimal256::zero(),
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 @@ -192,8 +192,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 +219,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 +251,57 @@ 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(
MIN_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::from_ratio(1000000000000u128, 1u128),
expected_error: None,
},
SpotPriceTestCase {
name: "invalid: duplicate denom",
pre_operations: vec![],
Expand All @@ -274,8 +320,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 +331,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

0 comments on commit 9d6f82a

Please sign in to comment.