Skip to content

Commit

Permalink
test: Improve tests of base_order_utils (#507)
Browse files Browse the repository at this point in the history
* update base_order_utils tests

* fix panic tests to then_fails format
  • Loading branch information
tevrat-aksoy authored Oct 11, 2023
1 parent e179caf commit d8a4928
Show file tree
Hide file tree
Showing 3 changed files with 417 additions and 80 deletions.
60 changes: 13 additions & 47 deletions src/order/base_order_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn validate_order_trigger_price(
};

if !ok {
panic_invalid_prices(primary_price, trigger_price, order_type);
OrderError::INVALID_ORDER_PRICE(primary_price, trigger_price, order_type);
}

return;
Expand All @@ -276,7 +276,7 @@ fn validate_order_trigger_price(
};

if !ok {
panic_invalid_prices(primary_price, trigger_price, order_type);
OrderError::INVALID_ORDER_PRICE(primary_price, trigger_price, order_type);
}

return;
Expand All @@ -296,7 +296,7 @@ fn validate_order_trigger_price(
};

if !ok {
panic_invalid_prices(primary_price, trigger_price, order_type);
OrderError::INVALID_ORDER_PRICE(primary_price, trigger_price, order_type);
}

return;
Expand Down Expand Up @@ -340,7 +340,7 @@ fn get_execution_price_for_increase(
// it may also be possible for users to prevent the execution of orders from other users
// by manipulating the price impact, though this should be costly

panic_unfulfillable(execution_price, acceptable_price);
OrderError::ORDER_NOT_FULFILLABLE_AT_ACCEPTABLE_PRICE(execution_price, acceptable_price);
0 // doesn't compile otherwise
}

Expand Down Expand Up @@ -416,18 +416,11 @@ fn get_execution_price_for_decrease(

if adjusted_price_impact_usd < 0
&& calc::to_unsigned(-adjusted_price_impact_usd) > size_delta_usd {
panic(
array![
OrderError::PRICE_IMPACT_LARGER_THAN_ORDER_SIZE,
adjusted_price_impact_usd.into(),
size_delta_usd.into()
]
OrderError::PRICE_IMPACT_LARGER_THAN_ORDER_SIZE(
adjusted_price_impact_usd, size_delta_usd
);
}

// error: Trait has no implementation in context: core::traits::Div::<core::integer::i128>
// TODO: uncomment this when i128 division available
// let adjustment = precision::mul_div_inum(position_size_in_usd, adjusted_price_impact_usd, position_size_in_tokens) / size_delta_usd.try_into().unwrap();
let numerator = precision::mul_div_inum(
position_size_in_usd, adjusted_price_impact_usd, position_size_in_tokens
);
Expand All @@ -436,15 +429,12 @@ fn get_execution_price_for_decrease(
let _execution_price: i128 = calc::to_signed(price, true) + adjustment;

if _execution_price < 0 {
panic(
array![
OrderError::NEGATIVE_EXECUTION_PRICE,
_execution_price.into(),
price.into(),
position_size_in_usd.into(),
adjusted_price_impact_usd.into(),
size_delta_usd.into()
]
OrderError::NEGATIVE_EXECUTION_PRICE(
_execution_price,
price,
position_size_in_usd,
adjusted_price_impact_usd,
size_delta_usd
);
}

Expand Down Expand Up @@ -482,7 +472,7 @@ fn get_execution_price_for_decrease(
//
// it may also be possible for users to prevent the execution of orders from other users
// by manipulating the price impact, though this should be costly
panic_unfulfillable(execution_price, acceptable_price);
OrderError::ORDER_NOT_FULFILLABLE_AT_ACCEPTABLE_PRICE(execution_price, acceptable_price);
0
}

Expand All @@ -497,27 +487,3 @@ fn validate_non_empty_order(order: @Order) {
OrderError::EMPTY_ORDER
);
}

#[inline(always)]
fn panic_invalid_prices(primary_price: Price, trigger_price: u128, order_type: OrderType) {
panic(
array![
OrderError::INVALID_ORDER_PRICES,
primary_price.min.into(),
primary_price.max.into(),
trigger_price.into(),
order_type.into(),
]
);
}

#[inline(always)]
fn panic_unfulfillable(execution_price: u128, acceptable_price: u128) {
panic(
array![
OrderError::ORDER_NOT_FULFILLABLE_AT_ACCEPTABLE_PRICE,
execution_price.into(),
acceptable_price.into()
]
);
}
53 changes: 47 additions & 6 deletions src/order/error.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
mod OrderError {
use satoru::order::order::OrderType;
use satoru::price::price::Price;

const EMPTY_ORDER: felt252 = 'empty_order';
const INVALID_ORDER_PRICES: felt252 = 'invalid_order_prices';
const INVALID_KEEPER_FOR_FROZEN_ORDER: felt252 = 'invalid_keeper_for_frozen_order';
const UNSUPPORTED_ORDER_TYPE: felt252 = 'unsupported_order_type';
const INVALID_ORDER_PRICES: felt252 = 'invalid_order_prices';
const INVALID_FROZEN_ORDER_KEEPER: felt252 = 'invalid_frozen_order_keeper';
const ORDER_NOT_FOUND: felt252 = 'order_not_found';
const ORDER_INDEX_NOT_FOUND: felt252 = 'order_index_not_found';
const CANT_BE_ZERO: felt252 = 'order account cant be 0';
const ORDER_NOT_FULFILLABLE_AT_ACCEPTABLE_PRICE: felt252 =
'order_unfulfillable_at_price'; // TODO: unshorten value
const NEGATIVE_EXECUTION_PRICE: felt252 = 'negative_execution_price';
const PRICE_IMPACT_LARGER_THAN_ORDER_SIZE: felt252 =
'price_impact_too_large'; // TODO: unshorten value
const EMPTY_SIZE_DELTA_IN_TOKENS: felt252 = 'empty_size_delta_in_tokens';

fn INVALID_ORDER_PRICE(primary_price: Price, trigger_price: u128, order_type: OrderType) {
let mut data: Array<felt252> = array![];
data.append('invalid_order_price');
data.append(primary_price.min.into());
data.append(primary_price.max.into());
data.append(trigger_price.into());
data.append(order_type.into());
panic(data);
}

fn ORDER_NOT_FULFILLABLE_AT_ACCEPTABLE_PRICE(execution_price: u128, acceptable_price: u128) {
let mut data: Array<felt252> = array![];
data.append('order_unfulfillable_at_price');
data.append(execution_price.into());
data.append(acceptable_price.into());
panic(data);
}

fn PRICE_IMPACT_LARGER_THAN_ORDER_SIZE(price_impact_usd: i128, size_delta_usd: u128) {
let mut data: Array<felt252> = array![];
data.append('price_impact_too_large');
data.append(price_impact_usd.into());
data.append(size_delta_usd.into());
panic(data);
}

fn NEGATIVE_EXECUTION_PRICE(
execution_price: i128,
price: u128,
position_size_in_usd: u128,
adjusted_price_impact_usd: i128,
size_delta_usd: u128
) {
let mut data: Array<felt252> = array![];
data.append('negative_execution_price');
data.append(execution_price.into());
data.append(price.into());
data.append(position_size_in_usd.into());
data.append(adjusted_price_impact_usd.into());
data.append(size_delta_usd.into());
panic(data);
}
}
Loading

0 comments on commit d8a4928

Please sign in to comment.