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:
Name Type Description _factory contract IChromaticMarketFactory The address of the Chromatic Market Factory contract. _earningDistributor contract IVaultEarningDistributor The 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:
Name Type Description settlementToken address The settlement token address. positionId uint256 The ID of the opened position. takerMargin uint256 The margin amount provided by the taker for the position. tradingFee uint256 The trading fee associated with the position. protocolFee uint256 The 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:
Name Type Description settlementToken address The settlement token address. positionId uint256 The ID of the claimed position. recipient address The address that will receive the settlement amount. takerMargin uint256 The margin amount provided by the taker for the position. settlementAmount uint256 The 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:
Name Type Description settlementToken address The settlement token address. amount uint256 The 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:
Name Type Description settlementToken address The settlement token address. pendingDeposit uint256 The amount of pending deposits being settled. pendingWithdrawal uint256 The 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:
Name Type Description settlementToken address The settlement token address. recipient address The address that will receive the withdrawn liquidity. amount uint256 The 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:
Name Type Description settlementToken address The settlement token address. keeper address The address of the keeper to receive the fee. fee uint256 The amount of the fee to transfer as native token. margin uint256 The margin amount used for the fee payment. Return Values:
Name Type Description usedFee uint256 The 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:
Name Type Description token address The address of the settlement token. keeper address The address of the keeper to receive the fee. fee uint256 The amount of the fee to transfer as native token. margin uint256 The margin amount used for the fee payment. Return Values:
Name Type Description usedFee uint256 The 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:
Name Type Description market address The address of the market contract. settlementToken address The address of the settlement token. positionId uint256 The ID of the position. amount uint256 The 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:
Name Type Description token address The address of the token for the flash loan. amount uint256 The amount of the flash loan. recipient address The address to receive the flash loan. data bytes Additional 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:
Name Type Description market address The address of the market. settlementToken address The settlement token address. binBalance uint256 The balance of funds in the bin. Return Values:
Name Type Description [0] uint256 The 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:
Name Type Description token address The address of the settlement token.
cancelMakerEarningDistributionTask
function cancelMakerEarningDistributionTask(address token) external
Cancels a maker earning distribution task for a token.
Parameters:
Name Type Description token address The 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:
Name Type Description token address The address of the settlement token. fee uint256 The keeper fee amount. keeper address The keeper address to receive fee.
createMarketEarningDistributionTask
function createMarketEarningDistributionTask(address market) external virtual
Creates a market earning distribution task for a market.
Parameters:
Name Type Description market address The address of the market.
cancelMarketEarningDistributionTask
function cancelMarketEarningDistributionTask(address market) external
Cancels a market earning distribution task for a market.
Parameters:
Name Type Description market address The 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:
Name Type Description market address The address of the market. fee uint256 The fee amount. keeper address The 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