Skip to content

Conversation

crispheaney
Copy link
Member

@crispheaney crispheaney commented Jul 20, 2025

No description provided.

Some(meets_margin_requirement) => meets_margin_requirement,
None => meets_settle_pnl_maintenance_margin_requirement(
Some(meets_margin_requirement) if !isolated_position_market_index.is_some() => meets_margin_requirement,
// TODO check margin for isolate position
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to redo margin check?

total_withdraws_after,
market_index: spot_market_index,
explanation: DepositExplanation::None,
transfer_user: None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we planning on tracking isolated positions deposit/withdraws? might need something here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good question for lukas/kev

Comment on lines 215 to 219
Ok(self
.margin_requirement_plus_buffer
.cast::<i128>()?
.safe_sub(self.get_total_collateral_plus_buffer())?
.unsigned_abs())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think how this function is used is safe, but it's semantically confusing since 'shortage' implies it should return a negative number that's abs'd. But since it abs everything, it can technically give a wrong result if there was a surplus. Same applies to the other impl of margin_shortage

This might be more safer:

Suggested change
Ok(self
.margin_requirement_plus_buffer
.cast::<i128>()?
.safe_sub(self.get_total_collateral_plus_buffer())?
.unsigned_abs())
Ok(self
.margin_requirement_plus_buffer
.cast::<i128>()?
.safe_sub(self.get_total_collateral_plus_buffer())?
.max(0).cast::<u128>()?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min?

Comment on lines 419 to 424
for (_, isolated_margin_calculation) in &self.isolated_margin_calculations
{
if !isolated_margin_calculation.meets_margin_requirement() {
return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this use both cross and isolated? looks like it's used in liquidate_perp, so if the isolated position is still in liquidation it would prevent the user from exiting liquidation?

) -> DriftResult<(AssetTier, ContractTier)> {
let contract_tier = perp_market_map.get_ref(&self.market_index)?.contract_tier;

Ok((AssetTier::default(), contract_tier))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this correct @0xbigz ?

perp_position,
false,
)?;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to do this for settle_expired_position too? or will it settle to the user's spot pos without a problem?

&perp_market_map,
&spot_market_map,
oracle_map,
MarginRequirementType::Initial,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if these post-transfer margin checks should be maintenance rather than initial. I think using initial prevents the user from transfering from their iso->cross to improve the liquidation level on their main account, unless the transfer is large enough to meet initial margin requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants