Skip to content

Commit

Permalink
Feat/is liquidable func (#689)
Browse files Browse the repository at this point in the history
* refactor integration tests

* add is_position_liquidable in reader

* fix coding style
  • Loading branch information
sparqet committed Jul 3, 2024
1 parent 75b73a1 commit 9ea6b2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
29 changes: 29 additions & 0 deletions src/reader/reader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ trait IReader<TContractState> {
is_long: bool,
prices: MarketPrices
) -> (u64, bool, i256, u256);

fn is_position_liquidable(
self: @TContractState,
data_store: IDataStoreDispatcher,
referral_storage: IReferralStorageDispatcher,
position: Position,
market: Market,
prices: MarketPrices,
should_validate_min_collateral_usd: bool,
) -> (bool, felt252);
}

#[starknet::contract]
Expand Down Expand Up @@ -856,6 +866,25 @@ mod Reader {
);
(latest_adl_block, should_enabled_ald, pnl_to_pool_factor, max_pnl_factor)
}

fn is_position_liquidable(
self: @ContractState,
data_store: IDataStoreDispatcher,
referral_storage: IReferralStorageDispatcher,
position: Position,
market: Market,
prices: MarketPrices,
should_validate_min_collateral_usd: bool
) -> (bool, felt252) {
position_utils::is_position_liquiditable(
data_store,
referral_storage,
position,
market,
prices,
should_validate_min_collateral_usd
)
}
}
}

21 changes: 12 additions & 9 deletions tests/integration/test_long_integration.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3713,9 +3713,10 @@ fn test_long_liquidation() {
short_token_price: Price { min: 1, max: 1, },
};

let (is_liquiditable, reason) = position_utils::is_position_liquiditable(
data_store, referal_storage, first_position, market, market_prices, false
);
let (is_liquiditable, reason) = reader
.is_position_liquidable(
data_store, referal_storage, first_position, market, market_prices, true
);

assert(is_liquiditable == true, 'Position is liquidable');

Expand All @@ -3727,9 +3728,10 @@ fn test_long_liquidation() {
short_token_price: Price { min: 1, max: 1, },
};

let (is_liquiditable, reason) = position_utils::is_position_liquiditable(
data_store, referal_storage, first_position, market, market_prices, false
);
let (is_liquiditable, reason) = reader
.is_position_liquidable(
data_store, referal_storage, first_position, market, market_prices, true
);

assert(is_liquiditable == false, 'Position is not liquidable');

Expand Down Expand Up @@ -4637,9 +4639,10 @@ fn test_long_leverage_liquidation() {
short_token_price: Price { min: 1, max: 1, },
};

let (is_liquiditable, reason) = position_utils::is_position_liquiditable(
data_store, referal_storage, first_position, market, market_prices, false
);
let (is_liquiditable, reason) = reader
.is_position_liquidable(
data_store, referal_storage, first_position, market, market_prices, true
);
// position x10 leverage is liquidable at 3000$, position x1 leverage is not liquidable at 3000$
assert(is_liquiditable == true, 'Position is liquidable');

Expand Down

0 comments on commit 9ea6b2d

Please sign in to comment.