Logo

dev-resources.site

for different kinds of informations.

How Binary Heaps Are Utilized In A Leveraged Trading Protocol On EVM

Published at
8/2/2023
Categories
ethereum
evm
blockchain
Author
eqlab_io
Categories
3 categories in total
ethereum
open
evm
open
blockchain
open
Author
8 person written this
eqlab_io
open
How Binary Heaps Are Utilized In A Leveraged Trading Protocol On EVM

EQ Lab develops a variety of different products on multiple blockchain platforms. Among others, there is am EVM-based decentralized lending protocol that allows users to take up to 20x leveraged long and short positions on crypto assets across different DEX-es and AMMs. In this article we will explore how this product utilizes binary heaps to handle margin calls.

Pools

The protocol is comprised of liquidity pools. Each pool consists of a quote asset (stablecoin) and a base asset (risk asset). For each pool there is a corresponding AMM pool where quote and base assets can be exchanged. Lending pools allow to deposit base or quote asset, go long or short (borrow the other asset with up to 20x leverage), withdraw base or quote asset, and close long/short positions.

Four indexes are calculated for each pool. They are baseCollateralCoeff, baseDebtCoeff, quoteCollateralCoeff, quoteDebtCoeff. These are used to determine interest rates.

Positions

There are three types of positions: Lend, Long, and Short When a user creates any of these positions a discounted balance is stored instead of the “real” balance. This is how it is calculated:

discountedBalance = realBalance / coeff

There are separate coefficients for Lend, Long and Short positions. For example if a Lend position is created by depositing quote asset the following calculation applies:

discountedQuoteAmount = realAmount / quoteCollateralCoeff

The calculation for Short and Long positions is as follows:

discountedBaseAmount = realAmount / quoteDebtCoeff

Short and Long positions are then stored in two separate collections. They are sorted in descending order of debtAmount/collateralAmount. For short positions this is calculated as baseAmount/quoteAmount, and for long positions the calculation is quoteAmount/baseAmount.

These sorted positions are stored in a Max binary heap.

Any user action that changes a position’s quoteAmount/baseAmount ratio triggers a recalculation of the binary heap such that the first element is always the riskiest position.

Interest rates

Interest rates are scaled proportionally to asset volatility and leverage. Total long leverage and total short leverage is calculated for each pool. Users who take up long or short positions pay interest fees to collateral providers. Fee accrual is done by changing baseCollateralCoeff, quoteCollateralCoeff coefficients. Fee write-off occurs by modifying the baseDebtCoeff and quoteDebtCoeff.

Margin calls

User action triggers a heap check of long and short positions leverages. Leverage is calculated for the riskiest position (heap root). It is then compared to the max leverage pool parameter. If the position’s leverage exceeds max leverage, the position is liquidated. In this case collateral is swapped and used to cover liabilities. The rest is then distributed between lenders. If there is not enough collateral to settle, the lenders cover the difference. These actions are performed by modifying baseCollateralCoeff or quoteCollateralCoeff. This way individual balances don’t need to be changed.

This design allows to run an automated liquidator as well as potentially implement no-liquidation systems in future versions of the protocol. It is more capital efficient compared to designs that implement collateral, debt and balances positions for each user because liquidity can be reused: the same capital can be used to go both long and short simultaneously. This could ultimately result in a superior product and user experience.

evm Article's
30 articles in total
Favicon
Ever wonder what happens when you send a transaction on Ethereum? đź‘€
Favicon
TEEs: The Secret Sauce Making Ethereum Rollups Faster and Simpler
Favicon
How to List Held Tokens by an Address Using the Moralis API
Favicon
Ethereum Transaction Calls and State Changes
Favicon
Creating a Toy Solidity compiler and running it in a Toy EVM
Favicon
The delegatecall Function in Solidity
Favicon
The delegatecall Function in Solidity
Favicon
ERC-4337 Shared Mempool: Official Launch on Ethereum Mainnet, Arbitrum and Optimism
Favicon
Understanding Fallback and Receive Functions in Solidity
Favicon
vyper挺好玩的
Favicon
Understanding EVM(Ethereum Virtual Machine)
Favicon
designing the skeleton
Favicon
building free speech forever
Favicon
I've made Huff docs Simple Storage page simpler for newcomers
Favicon
Polygon Nodes: Types and Usage
Favicon
BasilicaEVM: A modern dApp Stack
Favicon
EVM Reverse Engineering Challenge 0x02
Favicon
EVM Reverse Engineering Challenge 0x03
Favicon
Implementing Earned Value Management EVM in Government Projects
Favicon
EVM Reverse Engineering Challenge 0x01
Favicon
EVM Reverse Engineering Challenge 0x00
Favicon
Monad: Diving Deep into the L1 Choice
Favicon
Confidential Smart Contracts & Building w/Oasis Sapphire
Favicon
How To Setup A Berachain React Native Expo dApp With WalletConnect
Favicon
Chainsight Hands-on: Subscribe to On-Chain Events & Collect
Favicon
Creating a multi-chain voting in 30 minutes with Chainsight
Favicon
Oráculos Descentralizados em Blockchain: Conectando o Mundo Real à Blockchain.
Favicon
Swisstronick's Innovative Use Cases
Favicon
How Binary Heaps Are Utilized In A Leveraged Trading Protocol On EVM
Favicon
Deploy a Smart Contract on Polygon (MATIC)

Featured ones: