💰Margin Trading
This page describes the margin trading use cases of the TurboSwap protocol, such as deposit, withdraw, trade, and liquidation
Deposit and Withdraw
When a user deposits an amountof theasset, this amount is added to the corresponding reserves. Also, a fixed shareof the deposit amount is added to the capital as a fee, and the remaining amount is added to the user's position:
When a user withdraws an amountof theasset, this amount is subtracted by the user's position. Also, a fixed shareof the withdrawn amount is added to the capital as a fee, and the remaining amount is subtracted from the corresponding reserves and is paid to the user:
Note, that a withdrawal operation may turn a positive (long) position into a negative (short) one. This way withdrawal could be used to borrow an asset from the protocol. However, the protocol will allow such withdrawal only in case the loan is sufficiently collateralized.
Trade
When a user trades an amountofasset for an amountofasset, this is basically equivalent to an atomic combination of deposit and withdraw, though fees could be different:
Hereis the sell fee share, andis the buy fee share.
Margin Call and Default
For every asset, a margin quotient is set. The margin quotient for theasset is denoted as. For a particular () account, the marginal value () is the sum of the marginal values of all positions denominated in the base currency. For a positive (long) position, the marginal value is the position value divided by. For a negative (short) position, the marginal value is the position value multiplied by. Thus, the marginal value of an account could be calculated as:
When, theaccount is in margin call. No operation is allowed to bring an account into margin call. If an account is already in margin call, no withdrawals are allowed and only those trades are allowed that:
don't turn any positive (long) position into negative (short), and
reduce the absolute value of some negative (short) position
For a particular () account, the net value () is the sum of the values of all positions denominated in the base currency:
When, the account is in default. Note, that default state of an account implies margin call state.
Liquidation
When an account is in margin call, the protocol allows forcefully to liquidate negative (short) positions of this account. A liquidation could be triggered by anybody and is rewarded by the protocol. A liquidation could be performed in three ways: on exchange, via peer-to-peer trade, or via cross liquidation trade.
Liquidation On Exchange
In order to perform a liquidation on exchange, a liquidator performs a trade on behalf on an account whose negative (short) position is being liquidated. The trade should exchange some asset whose position in the account is positive (long) for an asset whose position is negative (short), but should not turn the positive position to become negative or negative positions to become positive. The liquidator receives a part of the trade fees as a reward. The rewards are added to the liquidator's account.
More formally, letbe the ID of the liquidator's account, andbe the ID of the account which is in margin call. Letbe the ID of an asset whose position in theaccount is positive (long), so, andbe the ID of an asset whose position in theaccount is negative (short), so . The liquidator trades the amountof theasset for the amountof theasset on behalf of theaccount:
A part of the trade fees is added to the liquidator's positions in corresponding assets:
Hereis the share of trade fees being paid to a liquidator as a reward.
The protocol ensures, that the positions of the account being liquidated didn't flip and that the marginal value of the account didn't become positive:
Peer-to-Peer Liquidation
Instead of performing a liquidation on exchange, a liquidator may perform a trade between the account whose position is being liquidated and the liquidator's own account:
The trade is performed at the current market prices, so:
thus
As usual, the protocol ensured, that the positions of the account being liquidated didn't flip, the marginal value didn't become positive, and that the liquidator account didn't fall into a margin call.
Cross Liquidation
When two accounts are in margin call, and these accounts have opposite positions in certain assets; In such a case a liquidator can perform a cross trade between these accounts in order to liquidate negative (short) positions on both of them.
More formally, letbe the ID of the liquidator's account,be the ID of the first account which is in margin call, andbe the ID of the second margin call account. The margin call accounts have opposite positions in theandassets:
The validator executes a cross trade:
and receives his share in the fees:
The magic quotientis basically the effective fee that was charged:
The trade is performed at the current market price:
As usual, the protocol ensures, that positions of the margin call accounts didn't flip during the trade and the marginal values didn't became positive:
Liquidation Against The Capital
In case the capital is not properly balanced, it could be possible to liquidate a margin call position against the capital.
More formally, letbe the ID of the liquidator's account, andbe the ID of the account which is in margin call. Letbe the ID of an asset whose position in theaccount is positive (long), so, andbe the ID of an asset whose position in theaccount is negative (short), so . The liquidator initiates a trade between the margin call account and the capital:
A part of the trade fees is added to the liquidator's positions in corresponding assets:
The trade is performed at the current market prices, so:
thus
As usual, the protocol ensured, that the positions of the account being liquidated didn't flip and the marginal value didn't become positive.
Write Off
The TurboSwap protocol uses marginal quotients to liquidate positions at risk before they go underwater, however in extreme market conditions a margin could not be wide enough, and an account may go default before all its short positions are liquidated.
So when an account is in default, it is very unlikely that short positions will ever be paid in full, as the collateral value is less than the debt, so the debtor doesn't have economical incentive to relay. In order to fix this, the protocol writes off some of the debt, to make the rest of the debt worth repaying. Writes off happens during liquidation. Below is a formal description of how write off works.
Lets assume that theis in margin call, its position in theasset is positive (long), and position in theasset is negative (short):
Letandbe the corresponding positions after the liquidation:
Letandbe the cumulative values of all the positive (long) and the negative (short) positions in theaccount:
The idea is that the relative change in the total value of the short positions during the liquidation shouldn't be lower than the relative change in the total value of the long positions:
If this constraint is violated, then the protocol writes of a portionof the short position in order to make the constraint hold:
After the write off, the protocol ensures that the short position didn't flip:
Last updated
Was this helpful?