Margin Trading Safety - laminar-protocol/flow-protocol-ethereum GitHub Wiki

The safety contract handles all functions regarding margin calls and liquidations of traders and pools that run too low in their liquidity.

External Functions

payTraderDeposits

Can be used by traders to pay for their deposits for a given pool. It's automatically called when opening the first position. The deposits are required for the case of a trader getting margin called or liquidated. All deposits are put into the money market and gain interest.

Full details can be seen at MarginFlowProtocolSafety.payTraderDeposits.

withdrawTraderDeposits

Allows to withdraw the trader deposits for a given pool. It's automatically called when withdrawing all funds from the protocol. The requirement is not having any positions open anymore in that pool.

Full details can be seen at MarginFlowProtocolSafety.withdrawTraderDeposits.

marginCallTrader

Margin call a trader if his liquidity is getting too low. Margin called traders cannot open new positions, they can only close positions. To open new ones again, they need to provide sufficient liquidity and then call makeTraderSafe. The caller of marginCallTrader receives the given trader margin call deposit as incentive.

Full details can be seen at MarginFlowProtocolSafety.marginCallTrader.

makeTraderSafe

A trader can make himself safe again after getting margin called. When given sufficient liquidity, he can use this function to pay again the trader margin call deposit and mark himself as safe. This allows him to open new positions again.

Full details can be seen at MarginFlowProtocolSafety.makeTraderSafe.

marginCallLiquidityPool

Margin call a pool if its liquidity is getting too low. Margin called pools cannot have new positions opened, traders can only close positions. To open new ones again, the pool needs to provide sufficient liquidity (more than getEstimatedRequiredDepositForPool) and then call makeLiquidityPoolSafe. The caller of marginCallLiquidityPool receives the given pool margin call deposit as incentive.

Full details can be seen at MarginFlowProtocolSafety.marginCallLiquidityPool.

makeLiquidityPoolSafe

A pool can make himself safe again after getting margin called. When given sufficient liquidity, he can use this function to pay again the pool margin call deposit and mark himself as safe. This allow traders to open new positions again.

Full details can be seen at MarginFlowProtocolSafety.makeLiquidityPoolSafe.

liquidateTrader

When a trader falls below the liquidation threshold, he can get liquidated. On liquidation all current market prices are stored and all trader's positions cannot be closed regularly anymore. To receive any leftover money, a trader has to close all his positions using closePositionForLiquidatedTrader.

Full details can be seen at MarginFlowProtocolSafety.liquidateTrader.

liquidateLiquidityPool

When a pool falls below the liquidation threshold, he can get liquidated. On liquidation all current market prices are stored and all pool's positions cannot be closed regularly anymore. To receive any leftover money, a trader has to close all his positions using closePositionForLiquidatedPool, unless the trader himself was already liquidated, then he still needs to use closePositionForLiquidatedTrader.

Full details can be seen at MarginFlowProtocolSafety.liquidateLiquidityPool.

isTraderSafe

Checks if a trader is safe or not. A trader is considered safe when his equity is high enough with regards to his net positions, see getMarginLevel.

Full details can be seen at MarginFlowProtocolSafety.isTraderSafe.

isPoolSafe

Checks if a pool is safe or not. A pool is considered safe when both the ENP and ELL are above the specified thresholds.

Full details can be seen at MarginFlowProtocolSafety.isPoolSafe.

getMarginLevel

The margin level of a trader is equity / sumOfNetPositions where the net of a position is evaluated as the USD value of the leveraged debits.

Full details can be seen at MarginFlowProtocolSafety.getMarginLevel.

getEnpAndEll

ENP and ELL. If new_position is None, return the ENP & ELL based on current positions, else based on current positions plus this new one. If equity_delta is None, return the ENP & ELL based on current equity of pool, else based on current equity of pool plus the equity_delta.

Full details can be seen at MarginFlowProtocolSafety.getEnpAndEll.

getEquityOfPool

Get the equity of a pool based on the iToken liquidity and balances mapping in the protocol.

Full details can be seen at MarginFlowProtocolSafety.getEquityOfPool.

getEstimatedRequiredDepositForPool

Return the estimated required amount for a pool to deposit to become safe again. Actual amount will be slightly different as the estimate doesn't include the swap rates.

Full details can be seen at MarginFlowProtocolSafety.getEstimatedRequiredDepositForPool.