💰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 amountxix_iof thei-thi\text{-th}asset, this amount is added to the corresponding reserves. Also, a fixed shareϕd\phi_dof the deposit amount is added to the capital as a fee, and the remaining amount is added to the user's position:

ai=ai+xivij=vij+(1ϕd)xi\begin{array}{rcl} a_i' & = & a_i + x_i \\[1em] v_{ij}' & = & v_{ij} + (1 - \phi_d) x_i \end{array}

When a user withdraws an amountxxof thei-thi\text{-th}asset, this amount is subtracted by the user's position. Also, a fixed shareϕw\phi_wof 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:

vij=vijxiai=ai(1ϕw)xi\begin{array}{rcl} v_{ij}' & = & v_{ij} - x_i \\[1em] a_i' & = & a_i - (1 - \phi_w) x_i \end{array}

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 amountxi1x_{i_1}ofi1-thi_1\text{-th}asset for an amountxi2x_{i_2}ofi2-thi_2\text{-th}asset, this is basically equivalent to an atomic combination of deposit and withdraw, though fees could be different:

vi1,j=vi1,jxi1ai1=ai1(1ϕs)xi1ai2=ai2+xi2vi2,j=vi2,j+(1ϕb)xi2\begin{array}{rcl} v_{i_1,j}' & = & v_{i_1,j} - x_{i_1} \\[1em] a_{i_1}' & = & a_{i_1} - (1 - \phi_s)x_{i_1} \\[1em] a_{i_2}' & = & a_{i_2} + x_{i_2} \\[1em] v_{i_2, j}' & = & v_{i_2,j} + (1 - \phi_b)x_{i_2} \end{array}

Hereϕs\phi_sis the sell fee share, andϕb\phi_bis the buy fee share.

Margin Call and Default

For every asset, a margin quotient is set. The margin quotient for thei-thi\text{-th}asset is denoted asmim_i. For a particular (j-thj\text{-th}) account, the marginal value (μj\mu_j) 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 by1+mi1 + m_i. For a negative (short) position, the marginal value is the position value multiplied by1+mi1 + m_i. Thus, the marginal value of an account could be calculated as:

μj=ipi(11+mimax(vij,0)2+(1+mi)min(vij,0)2)\mu_j = \sum_i p_i \left( \frac{1}{1 + m_i} \cdot \frac{\max (v_{ij}, 0)}{2} + (1 + m_i) \cdot \frac{\min (v_{ij}, 0)}{2} \right)

Whenμj<0\mu_j < 0, thej-thj\text{-th}account 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:

  1. don't turn any positive (long) position into negative (short), and

  2. reduce the absolute value of some negative (short) position

For a particular (j-thj\text{-th}) account, the net value (νj\nu_j) is the sum of the values of all positions denominated in the base currency:

νj=ipivij\nu_j = \sum_i p_i v_{ij}

Whenνj<0\nu_j < 0, the j-thj\text{-th}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, letj1j_1be the ID of the liquidator's account, andj2j_2be the ID of the account which is in margin call. Leti1i_1be the ID of an asset whose position in thej2-thj_2\text{-th}account is positive (long), sovi1,j2>0v_{i_1,j_2} > 0, andi2i_2be the ID of an asset whose position in thej2-thj_2\text{-th}account is negative (short), so vi2,j2<0v_{i_2,j_2} < 0. The liquidator trades the amountxi1x_{i_1}of thei1-thi_1\text{-th}asset for the amountxi2x_{i_2}of thei2-thi_2\text{-th}asset on behalf of thej2-thj_2\text{-th}account:

vi1,j2=vi1,j2xi1ai1=ai1(1ϕs)xi1ai2=ai2+xi2vi2,j2=vi2,j2+(1ϕb)xi2\begin{array}{rcl} v_{i_1,j_2}' & = & v_{i_1,j_2} - x_{i_1} \\[1em] a_{i_1}' & = & a_{i_1} - (1 - \phi_s)x_{i_1} \\[1em] a_{i_2}' & = & a_{i_2} + x_{i_2} \\[1em] v_{i_2, j_2}' & = & v_{i_2,j_2} + (1 - \phi_b)x_{i_2} \end{array}

A part of the trade fees is added to the liquidator's positions in corresponding assets:

vi1,j1=vi1,j1+λϕsxi1vi2,j1=vi2,j1+λϕbxi2\begin{array}{rcl} v_{i_1,j_1}' & = & v_{i_1,j_1} + \lambda \phi_s x_{i_1} \\[1em] v_{i_2,j_1}' & = & v_{i_2,j_1} + \lambda \phi_b x_{i_2} \end{array}

Hereλ\lambdais 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:

vi1,j20vi2,j20μj20v_{i_1,j_2}' \geqslant 0 \\[1em] v_{i_2,j_2}' \leqslant 0 \\[1em] \mu_{j_2}' \leqslant 0

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:

vi1,j2=vi1,j2xi1vi2,j2=vi2,j2+(1ϕb)xi2vi1,j1=vi1,j1+(1ϕs)xi1+λϕsxi1vi2,j1=vi2,j1xi2+λϕbxi2\begin{array}{rcl} v_{i_1,j_2}' & = & v_{i_1,j_2} - x_{i_1} \\[1em] v_{i_2,j_2}' & = & v_{i_2,j_2} + (1 - \phi_b) x_{i_2} \\[1em] v_{i_1,j_1}' & = & v_{i_1,j_1} + (1 - \phi_s) x_{i_1} + \lambda \phi_s x_{i_1} \\[1em] v_{i_2,j_1}' & = & v_{i_2,j_1} - x_{i_2} + \lambda \phi_b x_{i_2} \end{array}

The trade is performed at the current market prices, so:

(1ϕs)xi1xi2=pi2pi1\frac{(1 - \phi_s) x_{i_1}}{x_{i_2}} = \frac{p_{i_2}}{p_{i_1}}

thus

xi2=(1ϕs)xi1pi1pi2x_{i_2} = \frac{(1 - \phi_s) x_{i_1} p_{i_1}}{p_{i_2}}

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, letj1j_1be the ID of the liquidator's account,j2j_2be the ID of the first account which is in margin call, andj3j_3be the ID of the second margin call account. The margin call accounts have opposite positions in thei1-thi_1\text{-th}andi2-thi_2\text{-th}assets:

vi1,j2>0,vi2,j2<0vi1,j3<0,vi2,j3>0\begin{array}{rcl} v_{i_1,j_2} > 0, v_{i_2,j_2} < 0 \\[1em] v_{i_1,j_3} < 0, v_{i_2,j_3} > 0 \end{array}

The validator executes a cross trade:

vi1,j2=vi1,j2xi1vi1,j3=vi1,j3+(1ϕs)(1ϕb)xi1vi2,j3=vi2,j3xi2vi2,j2=vi2,j2+(1ϕs)(1ϕb)xi2\begin{array}{rcl} v_{i_1,j_2}' & = & v_{i_1,j_2} - x_{i_1} \\[1em] v_{i_1,j_3}' & = & v_{i_1,j_3} + (1 - \phi_s)(1 - \phi_b)x_{i_1} \\[1em] v_{i_2,j_3}' & = & v_{i_2,j_3} - x_{i_2} \\[1em] v_{i_2,j_2}' & = & v_{i_2,j_2} + (1 - \phi_s)(1 - \phi_b)x_{i_2} \end{array}

and receives his share in the fees:

vi1,j1=vi1,j1+λ(ϕs+ϕbϕsϕb)xi1vi2,j1=vi2,j1+λ(ϕs+ϕbϕsϕb)xi2\begin{array}{rcl} v_{i_1,j_1}' & = & v_{i_1,j_1} + \lambda (\phi_s + \phi_b - \phi_s \phi_b) x_{i_1} \\[1em] v_{i_2,j_1}' & = & v_{i_2,j_1} + \lambda (\phi_s + \phi_b - \phi_s \phi_b) x_{i_2} \end{array}

The magic quotientϕs+ϕbϕsϕb\phi_s + \phi_b - \phi_s \phi_bis basically the effective fee that was charged:

1(1ϕs)(1ϕb)=1(1ϕsϕb+ϕsϕb)=ϕs+ϕbϕsϕb1 - (1 - \phi_s)(1 - \phi_b) = 1 - (1 - \phi_s - \phi_b + \phi_s \phi_b) = \phi_s + \phi_b - \phi_s \phi_b

The trade is performed at the current market price:

xi1xi2=pi2pi1xi2=pi1pi2xi1\frac{x_{i_1}}{x_{i_2}} = \frac{p_{i_2}}{p_{i_1}} \\[1em] x_{i_2} = \frac{p_{i_1}}{p_{i_2}} x_{i_1}

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:

vi1,j20,vi2,j20,μj20vi1,j30,vi2,j30,μj30\begin{array}{rcl} v_{i_1,j_2}' \geqslant 0, v_{i_2,j_2}' \leqslant 0, \mu_{j_2} \leqslant 0 \\[1em] v_{i_1,j_3}' \leqslant 0, v_{i_2,j_3}' \geqslant 0, \mu_{j_3} \leqslant 0 \end{array}

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, letj1j_1be the ID of the liquidator's account, andj2j_2be the ID of the account which is in margin call. Leti1i_1be the ID of an asset whose position in thej2-thj_2\text{-th}account is positive (long), sovi1,j2>0v_{i_1,j_2} > 0, andi2i_2be the ID of an asset whose position in thej2-thj_2\text{-th}account is negative (short), so vi2,j2<0v_{i_2,j_2} < 0. The liquidator initiates a trade between the margin call account and the capital:

vi1,j2=vi1,j2xi1vi2,j2=vi2,j2+(1ϕb)xi2\begin{array}{rcl} v_{i_1,j_2}' & = & v_{i_1,j_2} - x_{i_1} \\[1em] v_{i_2,j_2}' & = & v_{i_2,j_2} + (1 - \phi_b)x_{i_2} \end{array}

A part of the trade fees is added to the liquidator's positions in corresponding assets:

vi1,j1=vi1,j1+λϕsxi1vi2,j1=vi2,j1+λϕbxi2\begin{array}{rcl} v_{i_1,j_1}' & = & v_{i_1,j_1} + \lambda \phi_s x_{i_1} \\[1em] v_{i_2,j_1}' & = & v_{i_2,j_1} + \lambda \phi_b x_{i_2} \end{array}

The trade is performed at the current market prices, so:

(1ϕs)xi1xi2=pi2pi1\frac{(1 - \phi_s) x_{i_1}}{x_{i_2}} = \frac{p_{i_2}}{p_{i_1}}

thus

xi2=(1ϕs)xi1pi1pi2x_{i_2} = \frac{(1 - \phi_s) x_{i_1} p_{i_1}}{p_{i_2}}

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 thej-thj\text{-th}is in margin call, its position in thei1-thi_1\text{-th}asset is positive (long), and position in thei2-thi_2\text{-th}asset is negative (short):

μj<0vi1,j>0vi2,j<0\begin{array}{rcl} \mu_j & < & 0 \\[1em] v_{i_1,j} & > & 0 \\[1em] v_{i_2,j} & < & 0 \end{array}

Letvi1,jv_{i_1,j}'andvi2,jv_{i_2,j}'be the corresponding positions after the liquidation:

0vi1,j<vi1,j0vi2,j>vi2,j\begin{array}{rcccl} 0 & \leqslant & v_{i_1,j}' & < & v_{i_1,j} \\[1em] 0 & \geqslant & v_{i_2,j}' & > & v_{i_2,j} \end{array}

Letνj+\nu^+_jandνj\nu^-_jbe the cumulative values of all the positive (long) and the negative (short) positions in thej-thj\text{-th}account:

νj+=ipimax(vij,0)νj=ipimin(vij,0)\nu^+_j = \sum_i p_i \max \left(v_{ij}, 0\right) \\[1em] \nu^-_j = \sum_i p_i \min \left(v_{ij}, 0\right)

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:

vi2,jvi2,jνjvi1,jvi1,jνj+\frac{v_{i_2,j} - v_{i_2,j}'}{\nu^-_j} \geqslant \frac{v_{i_1,j} - v_{i_1,j}'}{\nu^+_j}

If this constraint is violated, then the protocol writes of a portionxi2x_{i_2}of the short position in order to make the constraint hold:

vi2,j=vi2,j+xi2vi2,jvi2,jνj=vi1,jvi1,jνj+vi2,jvi2,jxi2νj=vi1,jvi1,jνj+xi2=vi2,jvi2,jνjνj+(vi1,jvi1,j)v_{i_2,j}'' = v_{i_2,j}' + x_{i_2} \\[1em] \frac{v_{i_2,j} - v_{i_2,j}''}{\nu^-_j} = \frac{v_{i_1,j} - v_{i_1,j}'}{\nu^+_j} \\[1em] \frac{v_{i_2,j} - v_{i_2,j}' - x_{i_2}}{\nu^-_j} = \frac{v_{i_1,j} - v_{i_1,j}'}{\nu^+_j} \\[1em] x_{i_2} = v_{i_2,j} - v_{i_2,j}' - \frac{\nu^-_j}{\nu^+_j} \left(v_{i_1,j} - v_{i_1,j}'\right)

After the write off, the protocol ensures that the short position didn't flip:

vi2,j0v_{i_2,j}'' \leqslant 0

Last updated