LogoLogo
  • Introduction
  • App
    • How to
      • Connect a wallet
      • Swap
      • Approve Spending
      • Open a Long Position
      • Open a Short Position
      • Withdraw Funds
      • Add Funds
      • Close Position
      • Increase Leverage
      • Pay Off Debt
      • Deposit Liquidity
      • Withdraw Liquidity
      • Add Liquidity
    • Smart contract addresses
      • Arbitrum
      • Blast
    • FAQ
  • Protocol mechanics
    • Providing Liquidity
    • Trading
    • Marginly MAX leverage specifics
  • Protocol architecture
    • Overview
    • Pools
      • Pool variables
      • Pool parameters
      • User positions
      • User actions
      • Pool Factory API
      • Pool API
    • TWAP oracle
    • Loan pricing
    • Errors
    • Marginly SDK
  • Router
    • Router architecture
    • Adapters
      • ApeSwapAdapter
      • BalancerAdapter
      • CamelotAdapter
      • KyberSwapClassicAdapter
      • KyberSwapElasticAdapter
      • UniswapV2Adapter
      • UniswapV3Adapter
      • WooFiAdapter
  • Risk Management
    • Risk Management Overview
    • Keeper service and smart contract description
    • Keeper contract architecture
    • Liquidations and Deleveraging
    • Volatility as risk proxy
    • Insurance pool
    • Shutdown mode
  • Economics
    • Marginly economics
  • Future plans
    • Beyond Marginly v1
    • Some ideas for Marginly v2
  • Trading Contest FAQ
  • Github
  • Audit
Powered by GitBook
On this page
  1. Protocol architecture

Overview

PreviousMarginly MAX leverage specificsNextPools

Last updated 1 year ago

Marginly architecture consists of the following contracts:

  • marginly pool implementation

  • factory

  • pool

  • router

Marginly pool implementation (contracts/contract/MarginlyPool.sol) is a pool bytecode. This deployed contract is basically an uninitialized version of marginly pool, which is cloned during creation of a new pool. The cloning approach is chosen in order for our factory contract size to fit with ethereum evm limit of 24 KiB.

Factory (contracts/contracts/MarginlyFactory.sol) creates new Marginly pools via 'createPool' method. This method clones marginly pool implementation and calls 'initialize' method, which sets pool variables and parameters that can be found and .

Factory stores the following info common for all our pools:

  • router address

  • fee holder address

  • tech position owner address

Though 'createPool' method is 'ownerOnly' right now, we are planning to make it permissionless in the future versions.

Router (router/contracts/MarginlyRouter.sol) is a contract that makes swaps on different DEXs of user's choice. It has two main methods:

  • swapExactInput -- makes swap with exact amount of swapped tokens

  • swapExactOutput -- makes swap with exact amount of tokens, received after swap

The user's choice is defined by a swapCalldata argument, which is decoded to an array of DEX indexes and their swap ratio. Default value for this argument is 0. It represents swap on UniswapV3 and is used during liquidation. Such swaps can be performed by any wallet or contract not limited to Marginly pools.

DEXs Swap implementations are located in router/contract/abstract/dex directory.

here
here