# GTE Perps
### Account + Subaccount = Perp Account
Positions and margin are stored in an address account + uint256 subaccount mapping. If a subaccount has multiple open positions, it's treated as a cross-margin account. Margin is shared within — but not outside of — a given subaccount
### Margining
After a trade a base margin delta is determined. For opens that's `opened notional / leverage`. For closes, it's `-(closedOpenNotional / leverage)`.
After rebalancing the final margin delta an open / increase is `MIN(marginDelta, MAX(intendedMargin - equity, 0))` and the final margin delta on a a decrease (including a position closed in a cross margin account with at least 1 more position) is is `MAX(marginDelta, MIN(intendedMargin - equity, 0))`.
Where equity is `margin + rpnl + upnl`. The margin remaining after the rebalancing is `margin + rpnl + marginDelta`
### Liquidations
GTE has 4 types of liquidations: standard, backstop, delist, and deleveraging.
• A standard liquidation is executed when an account fails to meet the minimum margin requirement. The liquidaiton runs through the book for the entire position (if under partialLiquidationThreshold) or position.amount * partialLiquidationRate (if above partialLiquidaitionThreshold). The partialLiquidaitonThreshold is compared to the notional value of the position to determine partial liquidations.
If, after rpnl and fee, the account meets the minimum margin requirment, they can keep the remaining margin / position.
• A backstop liquidation is executed when an account fails to meet 1/3rd of the minimum margin requirement. The backstop liquidaiton runs through the backstop book for the entire position. The margin — prorated based on notional value of positions & amount closed — is used is a reward for backstop liquidators. Backstop liqidators split the reward based on a 50/50 blended weight of volume and liquidator points, which are points we reward to liquidators on the backstop book.
• Deleveraging is a liqiudation of last resort. It will run with the insurance fund is unable to cover bad debt. In profit 'takers' are matched with a 'maker' in bad debt, and closed on the bankruptcy price of the deleverage maker.
• Delist liquidations are run when a market is delisted. The remaining open positions are cloed on mark price
### Low level trade flow
During trades we hold the entire account (a positions array and a Solady dynamic array of the assets that positions belong to) in memory. We use these data types to get funding, rebalance, and run a liquidatability check