-
Notifications
You must be signed in to change notification settings - Fork 1
Margin Trading
Margin trading pairs with leverage are implemented as smart contracts, which will be made ERC-20 compliant with re-balance to achieve tokenized margin positions in future release. The Flow Margin Protocol is the control protocol for opening and closing margin positions. There can be multiple liquidity pool available for each fToken supporting corresponding margin trading pairs, and users can choose to trade with whichever liquidity pool at their own discretion. Liquidity pool can choose to opt in or out trading certain pairs as well as adding their own swap rate markup and spreads per pair.
All calculations in the protocol are based internally on USD, but externally in iUSD. In the background every deposited USD token will be transferred into iUSD. iUSD gain interest over time. All functions return values in iUSD.
The deployment can be configured in the respective config files at https://github.com/laminar-protocol/flow-protocol-ethereum/tree/master/migrations/config.
The openPosition function opens a position for a given trading pair e.g. long EURUSD with 10x leverage, transfers user's amount into collateral. The exchange rate is the ask price from the given pool. All collaterals are converted to iToken to be managed under the money market to earn interest.
Trader must approve TRADER_MARGIN_CALL_FEE + TRADER_LIQUIDATION_FEE
to be paid as fees for the first opening of a position.
Full details can be seen at MarginFlowProtocol.openPosition.
The closePosition function closes a given position. The owner of the position can close it at any point in time. The amount exchanged back would return to the owner, and the additional collaterals would return to the pool. The exchange rate is the bid price from the given pool.
Full details can be seen at MarginFlowProtocol.closePosition.
The deposit function can be used by traders to deposit new funds for a given margin liquidity pool. The amount provided represents the USD value.
Full details can be seen at MarginFlowProtocol.deposit.
The withdraw function can be used by traders to withdraw funds from a given margin liquidity pool. The amount provided represents the USD value.
Full details can be seen at MarginFlowProtocol.withdraw.
The withdraw function can be used by liquidity pool smart contracts to withdraw from their balance.
Full details can be seen at MarginFlowProtocol.withdrawForPool.
The function returns the free margin of a trader in a given margin liquidity pool. The free margin is calculated as freeMargin = equityOfTrader - marginHeld
and represents the amount the trader can use to open new positions or to withdraw.
Full details can be seen at MarginFlowProtocol.getExactFreeMargin.
Full details can be seen at MarginFlowProtocol.getExactEquityOfTrader.
The function returns the margin held of a trader in a given margin liquidity pool. The margin held represents the sum of all margin held of a given trader.
Full details can be seen at MarginFlowProtocol.getMarginHeld.
The unrealized profit-loss of a position represents the occured profits or losses from a position after it has been opened and the market prices have changed. The function can be passed a max/min price which is useful when opening a new position to ensure paying roughly the expected price. It returns the unrealized PL of the position along with the current market price.
Full details can be seen at MarginFlowProtocol.getUnrealizedPlOfPosition.
A swap rate defined the fee for holding the debit on a daily basis. For technical reasons we cannot change the swap rate once a position has been opened, so once a position is open, the swap rate will be fixed. But this allows us to compute the accumulated swap rates easily as accSwapRate = swapRate * daysSincePositionIsOpen
where the days since opened are counted only for fully passed 24 hours.
The exact swap rate that is used is calculated by swapRateForPair - (abs(swapRateForPair) * poolSwapRateMarkup)
.
Full details can be seen at MarginFlowProtocol.getAccumulatedSwapRateOfPosition.
- There is a fixed swap rate when opening a position. Once a position is opened, the swap rate cannot be changed anymore.
- There is a maximum amount of trading pairs that can be allowed due to the gas costs.
- Margin liquidity pool implementation must be either white-listed or manually verified.
- Estimations in safety level checks: Due to gas limitations, the checks for sufficient liquidity of traders and pools use estimates which don't include the swap rates. Margin call and liquidation thresholds should be chosen accordingly.
- The estimated equity of a trader on closing a position is not including the swap rates. In edge cases this can result in the
maxRealizable
being slightly too high and thus the transfer to the pool is slightly too high. This case should be avoided by liquidating traders in time.