User actions

Description of all user-facing and internal auxiliary functions available inside the Marginly protocol

Function
Parameter
Description

deposit base

account_id

qty longQty

deposit ETH:

  • accrue system-wide interest, margincall position if needed

  • update user position: reduce debt or increase collateral,

  • update contract parameters

  • if longQty is not zero, then makes long call

deposit quote

account_id

qty shortQty

deposit USDC:

  • accrue system-wide interest, margincall position if needed

  • update user position: reduce debt or increase collateral

  • update contract parameters

  • if shortQty is not zero, then makes short call

withdraw base

account_id

qty

withdraw ETH:

  • accrue system-wide interest, margincall position if needed

  • calculate new user L, if L <=max leverage:

    • update user position: reduce ETH collateral

    • update contract parameters

withdraw quote

account_id

qty

withdraw USDC:

  • accrue system-wide interest, margincall position if needed

  • calculate new user L, if L <=max leverage:

    • update user position: reduce USDC collateral

    • update contract parameters

Long

account_id

qty (L)

This is an atomic sequence of actions: if one of the steps fails, all of the transaction is reverted:

  • reinit user (accrue interest from USDC debt), margincall user if needed.

  • check that user account's L < max leverage, if false - margincall user, all next steps are skipped

  • fetch price P_trade from an external AMM / router to buy qty ETH

  • take from the pool P_trade * qty USDC, revert if not enough USDC in the pool

  • sell USDC, buy ETH in uniswap

  • put qty ETH into the pool

  • update user collateral: base_collater += qty

  • update user debt: quote_debt += qty * P_trade * (1 + swap fee) / accrued rate

  • check that user account's L < max_leverage, if false - revert

  • update system aggregates

Short

account_id

qty (L)

This is an atomic sequence of actions: if one of the steps fails, all of the transaction is reverted:

  • reinit user (accrue interest from ETH debt), margincall user if needed.

  • check that user account's L < max_leverage, if false - margincall user, all next steps are skipped

  • fetch price P_trade from an external AMM / router to sell qty ETH

  • take from the pool qty ETH, revert if not enough ETH in the pool

  • sell ETH, buy USDC in uniswap

  • put qty * P_trade USDC into the pool

  • update user collateral: quote collateral += qty * P_trade * (1 - swap fee) / accrued rate

  • update user debt: base_debt += qty

  • check that user account's L < max leverage, if false - revert

  • update system aggregates

close position

account_id

This is an atomic sequence of actions: if one of the steps fails, all of the transaction is reverted:

  • reinit user (accrue interest from ETH debt), margincall user if needed.

  • swap part of collateral enough to cover position’s debt

  • reduce position debts to zero

  • update system leverage

  • remove position from leverage heap

reinit

Accrue interest rates, syncs balances and liquidate riskiest position if needed:

Marginly tracks leverages of user long and short positions sorted from highest to lowest (using heaps), each time interest rates are accrued, system checks if the leverage of the most risky position (heap root) is above max leverage it gets liquidated automatically. All of the collateral is sold and the liquidated user effectively pays a penalty of <=5%. All the penalty is distributed to liquidity providers

This function is free for any user to call as it's in the best interest of the pool participants to call it to collect liquidation fees.

This function is called automatically inside any user facing function.

Receive position

account_id

Provide liquidity and absorb liquidated position, effectively realising its net position value.

This function is useful when there is no collateral to sell in the system, but liquidations still need to happen.

Emergency withdraw

Available when the system parameter mode is EmergencyShort or EmergencyLong. Theses scenarios are only possible in the system when there is not enough ETH collateral to cover USDC debt and vice versa: not enough USDC collateral to cover ETH debt.

Emergency Short mode:

There is no liquidity in the system and the insurance pool to liquidate bad short positions. In that mode all positions of type Lend and Long could withdraw their collateral minus amounts needed to reduce short positions debts.

Emergency Long mode:

There is no liquidity in the system and the insurance pool to liquidate bad long positions. In that mode all positions of type Lend and Short could withdraw their collateral minus amounts needed to reduce short positions debts.

Last updated