Skip to main content

ChromaticVault.sol

ChromaticVault

A contract that implements the ChromaticVault interface and provides functionality for managing positions, liquidity, and fees in Chromatic markets.

factory

contract IChromaticMarketFactory factory

earningDistributor

contract IVaultEarningDistributor earningDistributor

makerBalances

mapping(address => uint256) makerBalances

takerBalances

mapping(address => uint256) takerBalances

makerMarketBalances

mapping(address => uint256) makerMarketBalances

takerMarketBalances

mapping(address => uint256) takerMarketBalances

pendingMakerEarnings

mapping(address => uint256) pendingMakerEarnings

pendingMarketEarnings

mapping(address => uint256) pendingMarketEarnings

pendingDeposits

mapping(address => uint256) pendingDeposits

pendingWithdrawals

mapping(address => uint256) pendingWithdrawals

_tradingLockOwner

address _tradingLockOwner

OnlyAccessableByDao

error OnlyAccessableByDao()

Throws an error indicating that the caller is not the DAO.

OnlyAccessableByFactoryOrDao

error OnlyAccessableByFactoryOrDao()

Throws an error indicating that the caller is nether the chormatic factory contract nor the DAO.

OnlyAccessableByMarket

error OnlyAccessableByMarket()

Throws an error indicating that the caller is not a registered market.

OnlyAccessableByEarningDistributor

error OnlyAccessableByEarningDistributor()

Throws an error indicating that the caller is not the Vault earning distribute contract.

NotEnoughBalance

error NotEnoughBalance()

Throws an error indicating that the flash loan amount exceeds the available balance in the vault.

NotEnoughFeePaid

error NotEnoughFeePaid()

Throws an error indicating that the recipient has not paid the sufficient flash loan fee.

TradingLockAlreadyAcquired

error TradingLockAlreadyAcquired()

NotTradingLockOwner

error NotTradingLockOwner()

onlyDao

modifier onlyDao()

Modifier to restrict access to only the DAO. Throws an OnlyAccessableByDao error if the caller is not the DAO.

onlyFactoryOrDao

modifier onlyFactoryOrDao()

Modifier to restrict access to only the factory or the DAO. Throws an OnlyAccessableByFactoryOrDao error if the caller is nether the chormatic factory contract nor the DAO.

onlyEarningDistributor

modifier onlyEarningDistributor()

Modifier to restrict access to only the Vault earning distribute contract. Throws an OnlyAccessableByEarningDistributor error if the caller is not the Vault earning distribute contract.

requireUnacquiredTradingLock

modifier requireUnacquiredTradingLock()

requireTradingLock

modifier requireTradingLock()

constructor

constructor(contract IChromaticMarketFactory _factory, contract IVaultEarningDistributor _earningDistributor) public

Constructs a new ChromaticVault instance.

  • Parameters:

    NameTypeDescription
    _factorycontract IChromaticMarketFactoryThe address of the Chromatic Market Factory contract.
    _earningDistributorcontract IVaultEarningDistributorThe address of the Vault earning distribute contract.

onOpenPosition

function onOpenPosition(address settlementToken, uint256 positionId, uint256 takerMargin, uint256 tradingFee, uint256 protocolFee) external

Called when a position is opened by a market contract.

This function can only be called by a market contract.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe settlement token address.
    positionIduint256The ID of the opened position.
    takerMarginuint256The margin amount provided by the taker for the position.
    tradingFeeuint256The trading fee associated with the position.
    protocolFeeuint256The protocol fee associated with the position.

onClaimPosition

function onClaimPosition(address settlementToken, uint256 positionId, address recipient, uint256 takerMargin, uint256 settlementAmount) external

Called when a position is claimed by a market contract.

This function can only be called by a market contract.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe settlement token address.
    positionIduint256The ID of the claimed position.
    recipientaddressThe address that will receive the settlement amount.
    takerMarginuint256The margin amount provided by the taker for the position.
    settlementAmountuint256The amount to be settled for the position.

onAddLiquidity

function onAddLiquidity(address settlementToken, uint256 amount) external

Called when liquidity is added to the vault by a market contract.

This function can only be called by a market contract.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe settlement token address.
    amountuint256The amount of liquidity being added.

onSettlePendingLiquidity

function onSettlePendingLiquidity(address settlementToken, uint256 pendingDeposit, uint256 pendingWithdrawal) external

Called when pending liquidity is settled in the vault by a market contract.

This function can only be called by a market contract.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe settlement token address.
    pendingDeposituint256The amount of pending deposits being settled.
    pendingWithdrawaluint256The amount of pending withdrawals being settled.

onWithdrawLiquidity

function onWithdrawLiquidity(address settlementToken, address recipient, uint256 amount) external

Called when liquidity is withdrawn from the vault by a market contract.

This function can only be called by a market contract.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe settlement token address.
    recipientaddressThe address that will receive the withdrawn liquidity.
    amountuint256The amount of liquidity to be withdrawn.

transferKeeperFee

function transferKeeperFee(address settlementToken, address keeper, uint256 fee, uint256 margin) external returns (uint256 usedFee)

Transfers the keeper fee from the market to the specified keeper.

This function can only be called by a market contract.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe settlement token address.
    keeperaddressThe address of the keeper to receive the fee.
    feeuint256The amount of the fee to transfer as native token.
    marginuint256The margin amount used for the fee payment.
  • Return Values:

    NameTypeDescription
    usedFeeuint256The actual settlement token amount of fee used for the transfer.

_transferKeeperFee

function _transferKeeperFee(address token, address keeper, uint256 fee, uint256 margin) internal returns (uint256 usedFee)

Internal function to transfer the keeper fee.

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the settlement token.
    keeperaddressThe address of the keeper to receive the fee.
    feeuint256The amount of the fee to transfer as native token.
    marginuint256The margin amount used for the fee payment.
  • Return Values:

    NameTypeDescription
    usedFeeuint256The actual settlement token amount of fee used for the transfer.

transferProtocolFee

function transferProtocolFee(address market, address settlementToken, uint256 positionId, uint256 amount) internal

Transfers the protocol fee to the DAO treasury address.

  • Parameters:

    NameTypeDescription
    marketaddressThe address of the market contract.
    settlementTokenaddressThe address of the settlement token.
    positionIduint256The ID of the position.
    amountuint256The amount of the protocol fee to transfer.

flashLoan

function flashLoan(address token, uint256 amount, address recipient, bytes data) external

Executes a flash loan.

_Throws a NotEnoughBalance error if the loan amount exceeds the available balance. Throws a NotEnoughFeePaid error if the fee has not been paid by the recipient.

Requirements:

  • The loan amount must not exceed the available balance after considering pending deposits and withdrawals.
  • The fee for the flash loan must be paid by the recipient.
  • The total amount paid must be distributed between the taker pool and maker pool according to their balances.
  • The amount paid to the taker pool must be transferred to the DAO treasury address.
  • The amount paid to the maker pool must be added to the pending maker earnings.

Emits a FlashLoan event with details of the flash loan execution._

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the token for the flash loan.
    amountuint256The amount of the flash loan.
    recipientaddressThe address to receive the flash loan.
    databytesAdditional data for the flash loan.

getPendingBinShare

function getPendingBinShare(address market, address settlementToken, uint256 binBalance) external view returns (uint256)

Retrieves the pending share of earnings for a specific bin (subset) of funds in a market.

The pending share of earnings is calculated based on the bin balance, maker balances, and market balances.

  • Parameters:

    NameTypeDescription
    marketaddressThe address of the market.
    settlementTokenaddressThe settlement token address.
    binBalanceuint256The balance of funds in the bin.
  • Return Values:

    NameTypeDescription
    [0]uint256The pending share of earnings for the specified bin.

migrateEarningDistributionTasks

function migrateEarningDistributionTasks(contract IVaultEarningDistributor oldEarningDistributor) external

setVaultEarningDistributor

function setVaultEarningDistributor(address _earningDistributor) external

This function can only be called by the DAO address.

createMakerEarningDistributionTask

function createMakerEarningDistributionTask(address token) external virtual

Creates a maker earning distribution task for a token.

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the settlement token.

cancelMakerEarningDistributionTask

function cancelMakerEarningDistributionTask(address token) external

Cancels a maker earning distribution task for a token.

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the settlement token.

distributeMakerEarning

function distributeMakerEarning(address token, uint256 fee, address keeper) external

Distributes the maker earning for a token to the each markets.

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the settlement token.
    feeuint256The keeper fee amount.
    keeperaddressThe keeper address to receive fee.

createMarketEarningDistributionTask

function createMarketEarningDistributionTask(address market) external virtual

Creates a market earning distribution task for a market.

  • Parameters:

    NameTypeDescription
    marketaddressThe address of the market.

cancelMarketEarningDistributionTask

function cancelMarketEarningDistributionTask(address market) external

Cancels a market earning distribution task for a market.

  • Parameters:

    NameTypeDescription
    marketaddressThe address of the market.

distributeMarketEarning

function distributeMarketEarning(address market, uint256 fee, address keeper) external

Distributes the market earning for a market to the each bins.

  • Parameters:

    NameTypeDescription
    marketaddressThe address of the market.
    feeuint256The fee amount.
    keeperaddressThe keeper address to receive fee.

acquireTradingLock

function acquireTradingLock() external

releaseTradingLock

function releaseTradingLock() external

_requireUnacquiredTradingLock

function _requireUnacquiredTradingLock() internal view

_requireTradingLock

function _requireTradingLock() internal view

_checkMarket

function _checkMarket() internal view