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

Feat: Implement the functions in the swap_pricing_utils library #446

Merged
merged 20 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
6 changes: 3 additions & 3 deletions src/event/event_emitter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use satoru::price::price::Price;
use satoru::pricing::position_pricing_utils::PositionFees;
use satoru::order::order::{Order, SecondaryOrderType};
use satoru::utils::span32::{Span32, DefaultSpan32};
use satoru::utils::i128::{I128Div, I128Mul, I128Serde};
use satoru::utils::i128::{I128Div, I128Mul, StoreI128, I128Serde};


//TODO: OrderCollatDeltaAmountAutoUpdtd must be renamed back to OrderCollateralDeltaAmountAutoUpdated when string will be allowed as event argument
Expand Down Expand Up @@ -641,7 +641,7 @@ mod EventEmitter {
use satoru::pricing::position_pricing_utils::PositionFees;
use satoru::order::order::{Order, SecondaryOrderType};
use satoru::utils::span32::{Span32, DefaultSpan32};
use satoru::utils::i128::{I128Div, I128Mul, I128Serde};
use satoru::utils::i128::{I128Div, I128Mul, StoreI128, I128Serde};

// *************************************************************************
// STORAGE
Expand Down Expand Up @@ -2587,7 +2587,7 @@ mod EventEmitter {
amount_in_after_fees,
amount_out,
price_impact_usd,
price_impact_amount
price_impact_amount,
}
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ mod pricing {
mod position_pricing_utils;
mod pricing_utils;
mod swap_pricing_utils;
mod error;
}

// `referral` contains referral logic.
Expand Down Expand Up @@ -309,11 +310,12 @@ mod tests {
mod price {
mod test_price;
}

mod pricing {
mod test_swap_pricing_utils;
}
mod reader {
mod test_reader;
}

mod role {
mod test_role_module;
mod test_role_store;
Expand Down
71 changes: 56 additions & 15 deletions src/market/market_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use satoru::market::{
use satoru::oracle::oracle::{IOracleDispatcher, IOracleDispatcherTrait};
use satoru::price::price::{Price, PriceTrait};
use satoru::utils::span32::Span32;
use satoru::utils::i128::{StoreI128, u128_to_i128, I128Serde, I128Div, I128Mul};
use satoru::utils::i128::{StoreI128, u128_to_i128, I128Serde, I128Div, I128Mul, I128Default};
/// Struct to store the prices of tokens of a market.
/// # Params
/// * `indexTokenPrice` - Price of the market's index token.
Expand Down Expand Up @@ -73,11 +73,11 @@ fn get_cached_token_price(token: ContractAddress, market: Market, prices: Market
}

fn get_swap_impact_amount_with_cap(
dataStore: IDataStoreDispatcher,
data_store: IDataStoreDispatcher,
market: ContractAddress,
token: ContractAddress,
tokenPrice: Price,
priceImpactUsd: i128 //TODO : check u128
token_price: Price,
price_impact_usd: i128 //TODO : check u128
) -> i128 { //Todo : check u128
//TODO
return 0;
Expand Down Expand Up @@ -694,6 +694,16 @@ fn is_pnl_factor_exceeded_direct(
(true, 0, 0)
}

fn get_ui_fee_factor(data_store: IDataStoreDispatcher, account: ContractAddress) -> u128 {
let max_ui_fee_factor = data_store.get_u128(keys::max_ui_fee_factor());
let ui_fee_factor = data_store.get_u128(keys::ui_fee_factor_key(account));
if ui_fee_factor < max_ui_fee_factor {
ui_fee_factor
} else {
max_ui_fee_factor
}
}

/// Gets the enabled market. This function will revert if the market does not exist or is not enabled.
/// # Arguments
/// * `dataStore` - DataStore
Expand Down Expand Up @@ -952,7 +962,6 @@ fn market_token_amount_to_usd(
0
}


/// Get the borrowing factor per second.
/// # Arguments
/// * `data_store` - The data store to use.
Expand Down Expand Up @@ -1030,18 +1039,50 @@ fn get_open_interest_with_pnl(
0
}


/// Get the virtual inventory for swaps
/// # Arguments
/// * `data_store` - The data store to use.
/// * `market` - The market address.
/// # Returns
/// has virtual inventory, virtual long token inventory, virtual short token inventory
// @dev get the virtual inventory for swaps
// @param data_store DataStore
// @param market the market to check
// @return returns (has virtual inventory, virtual long token inventory, virtual short token inventory)
delaaxe marked this conversation as resolved.
Show resolved Hide resolved
fn get_virtual_inventory_for_swaps(
data_store: IDataStoreDispatcher, market: ContractAddress,
data_store: IDataStoreDispatcher, market: ContractAddress
) -> (bool, u128, u128) {
// TODO
(false, 0, 0)
let virtual_market_id = data_store.get_felt252(keys::virtual_market_id_key(market));
if virtual_market_id.is_zero() {
return (false, 0, 0);
}

return (
true,
data_store.get_u128(keys::virtual_inventory_for_swaps_key(virtual_market_id, true)),
data_store.get_u128(keys::virtual_inventory_for_swaps_key(virtual_market_id, false))
);
}

fn get_adjusted_swap_impact_factor(
data_store: IDataStoreDispatcher, market: ContractAddress, is_positive: bool
) -> u128 {
let (positive_impact_factor, negative_impact_factor) = get_adjusted_swap_impact_factors(
data_store, market
);
if is_positive {
positive_impact_factor
} else {
negative_impact_factor
}
}

fn get_adjusted_swap_impact_factors(
data_store: IDataStoreDispatcher, market: ContractAddress
) -> (u128, u128) {
let mut positive_impact_factor = data_store
.get_u128(keys::swap_impact_factor_key(market, true));
let negative_impact_factor = data_store.get_u128(keys::swap_impact_factor_key(market, false));
// if the positive impact factor is more than the negative impact factor, positions could be opened
// and closed immediately for a profit if the difference is sufficient to cover the position fees
if positive_impact_factor > negative_impact_factor {
positive_impact_factor = negative_impact_factor;
}
(positive_impact_factor, negative_impact_factor)
}


Expand Down
11 changes: 6 additions & 5 deletions src/order/base_order_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use integer::BoundedInt;
use starknet::ContractAddress;

// Local imports.
use satoru::utils::i128::{I128Div, u128_to_i128, i128_to_u128};
use satoru::data::data_store::{IDataStoreDispatcher, IDataStoreDispatcherTrait};
use satoru::event::event_emitter::{IEventEmitterDispatcher, IEventEmitterDispatcherTrait};
use satoru::oracle::oracle::{IOracleDispatcher, IOracleDispatcherTrait};
Expand All @@ -23,6 +22,8 @@ use satoru::referral::referral_storage::interface::{
IReferralStorageDispatcher, IReferralStorageDispatcherTrait
};
use satoru::utils::span32::Span32;
use satoru::utils::calc;
use satoru::utils::i128::{I128Div, StoreI128, I128Serde};

#[derive(Drop, starknet::Store, Serde)]
struct ExecuteOrderParams {
Expand Down Expand Up @@ -416,7 +417,7 @@ fn get_execution_price_for_decrease(
};

if adjusted_price_impact_usd < 0
&& i128_to_u128(-adjusted_price_impact_usd) > size_delta_usd {
&& calc::to_unsigned(-adjusted_price_impact_usd) > size_delta_usd {
panic(
array![
OrderError::PRICE_IMPACT_LARGER_THAN_ORDER_SIZE,
Expand All @@ -432,9 +433,9 @@ fn get_execution_price_for_decrease(
let numerator = precision::mul_div_inum(
position_size_in_usd, adjusted_price_impact_usd, position_size_in_tokens
);
let adjustment = numerator / u128_to_i128(size_delta_usd);
let adjustment = numerator / calc::to_signed(size_delta_usd, true);

let _execution_price: i128 = u128_to_i128(price) + adjustment;
let _execution_price: i128 = calc::to_signed(price, true) + adjustment;

if _execution_price < 0 {
panic(
Expand All @@ -449,7 +450,7 @@ fn get_execution_price_for_decrease(
);
}

execution_price = i128_to_u128(_execution_price);
execution_price = calc::to_unsigned(_execution_price);
}

// decrease order:
Expand Down
3 changes: 3 additions & 0 deletions src/pricing/error.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod PricingError {
const USD_DELTA_EXCEEDS_POOL_VALUE: felt252 = 'usd_delta_exceeds_pool_value';
}
2 changes: 1 addition & 1 deletion src/pricing/pricing_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn get_price_impact_usd_for_same_side_rebalance(
/// a crossover in balance is for example if the long open interest is larger
/// than the short open interest, and a short position is opened such that the
/// short open interest becomes larger than the long open interest.
fn get_price_impact_usd_for_crossover_side_rebalance(
fn get_price_impact_usd_for_crossover_rebalance(
initial_diff_usd: u128,
next_diff_usd: u128,
positive_impact_factor: u128,
Expand Down
Loading
Loading