Skip to content

Commit

Permalink
feat: implement decrease_position_collateral_utils (#465)
Browse files Browse the repository at this point in the history
* feat: implement decrease_position_collateral_utils

* fixes: changes after review

---------

Co-authored-by: Michel <[email protected]>
  • Loading branch information
zarboq and Sk8erboi84 authored Oct 4, 2023
1 parent ca0e1a4 commit 55c97b4
Show file tree
Hide file tree
Showing 14 changed files with 1,012 additions and 268 deletions.
27 changes: 14 additions & 13 deletions src/event/event_emitter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ use starknet::{ContractAddress, ClassHash};
// Local imports.
use satoru::deposit::deposit::Deposit;
use satoru::withdrawal::withdrawal::Withdrawal;
use satoru::position::position::Position;
use satoru::market::market_pool_value_info::MarketPoolValueInfo;
use satoru::pricing::swap_pricing_utils::SwapFees;
use satoru::position::position_event_utils::PositionIncreaseParams;
use satoru::position::position_utils::DecreasePositionCollateralValues;
use satoru::order::order::OrderType;
use satoru::position::{
position::Position, position_event_utils::PositionIncreaseParams,
position_utils::DecreasePositionCollateralValues
};
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, I128Store, I128Serde};
use satoru::order::order::{Order, SecondaryOrderType, OrderType};
use satoru::utils::{
i128::{I128Div, I128Mul, I128Store, I128Serde}, span32::{Span32, DefaultSpan32}
};


//TODO: OrderCollatDeltaAmountAutoUpdtd must be renamed back to OrderCollateralDeltaAmountAutoUpdated when string will be allowed as event argument
Expand Down Expand Up @@ -57,7 +58,7 @@ trait IEventEmitter<TContractState> {

/// Emits the `PositionImpactPoolAmountUpdated` event.
fn emit_position_impact_pool_amount_updated(
ref self: TContractState, market: ContractAddress, delta: u128, next_value: u128,
ref self: TContractState, market: ContractAddress, delta: i128, next_value: u128,
);

/// Emits the `SwapImpactPoolAmountUpdated` event.
Expand Down Expand Up @@ -182,7 +183,7 @@ trait IEventEmitter<TContractState> {
ref self: TContractState,
order_key: felt252,
position_collateral_amount: u128,
base_pnl_usd: u128,
base_pnl_usd: i128,
remaining_cost_usd: u128
);

Expand Down Expand Up @@ -800,7 +801,7 @@ mod EventEmitter {
#[derive(Drop, starknet::Event)]
struct PositionImpactPoolAmountUpdated {
market: ContractAddress,
delta: u128,
delta: i128,
next_value: u128,
}

Expand Down Expand Up @@ -988,7 +989,7 @@ mod EventEmitter {
struct InsolventClose {
order_key: felt252,
position_collateral_amount: u128,
base_pnl_usd: u128,
base_pnl_usd: i128,
remaining_cost_usd: u128
}

Expand Down Expand Up @@ -1607,7 +1608,7 @@ mod EventEmitter {

/// Emits the `PositionImpactPoolAmountUpdated` event.
fn emit_position_impact_pool_amount_updated(
ref self: ContractState, market: ContractAddress, delta: u128, next_value: u128,
ref self: ContractState, market: ContractAddress, delta: i128, next_value: u128,
) {
self.emit(PositionImpactPoolAmountUpdated { market, delta, next_value, });
}
Expand Down Expand Up @@ -1947,7 +1948,7 @@ mod EventEmitter {
ref self: ContractState,
order_key: felt252,
position_collateral_amount: u128,
base_pnl_usd: u128,
base_pnl_usd: i128,
remaining_cost_usd: u128
) {
self
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ mod utils {
mod error_utils;
mod starknet_utils;
mod traits;
mod default;
}

// `liquidation` function to help with liquidations.
Expand Down
7 changes: 3 additions & 4 deletions src/market/market_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ fn get_max_open_interest(
/// * `delta` - The amount to increment by.
fn increment_claimable_collateral_amount(
data_store: IDataStoreDispatcher,
chain: IChainDispatcher,
event_emitter: IEventEmitterDispatcher,
market_address: ContractAddress,
token: ContractAddress,
Expand All @@ -507,7 +506,7 @@ fn increment_claimable_collateral_amount(
let divisor = data_store.get_u128(keys::claimable_collateral_time_divisor());
error_utils::check_division_by_zero(divisor, 'increment_claimable_collateral');
// Get current timestamp.
let current_timestamp = chain.get_block_timestamp().into();
let current_timestamp = get_block_timestamp().into();
let time_key = current_timestamp / divisor;

// Increment the collateral amount for the account.
Expand Down Expand Up @@ -870,11 +869,11 @@ fn apply_delta_to_position_impact_pool(
data_store: IDataStoreDispatcher,
event_emitter: IEventEmitterDispatcher,
market_address: ContractAddress,
delta: u128
delta: i128
) -> u128 {
// Increment the position impact pool amount.
let next_value = data_store
.increment_u128(keys::position_impact_pool_amount_key(market_address), delta);
.apply_bounded_delta_to_u128(keys::position_impact_pool_amount_key(market_address), delta);

// Emit event.
event_emitter.emit_position_impact_pool_amount_updated(market_address, delta, next_value);
Expand Down
3 changes: 2 additions & 1 deletion src/order/order.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ enum OrderType {
}

/// To help further differentiate orders.
#[derive(Drop, Copy, starknet::Store, Serde)]
#[derive(Drop, starknet::Store, Serde, PartialEq, Copy, Default)]
enum SecondaryOrderType {
#[default]
None,
Adl,
}
Expand Down
Loading

0 comments on commit 55c97b4

Please sign in to comment.