// SPDX-License-Identifier: GPL-2.0-or-later pragma solidity ^0.8.0; interface IMargin { struct Position { int256 quoteSize; //quote amount of position int256 baseSize; //margin + fundingFee + unrealizedPnl + deltaBaseWhenClosePosition uint256 tradeSize; //if quoteSize>0 unrealizedPnl = baseValueOfQuoteSize - tradeSize; if quoteSize<0 unrealizedPnl = tradeSize - baseValueOfQuoteSize; } event AddMargin(address indexed trader, uint256 depositAmount, Position position); event RemoveMargin( address indexed trader, address indexed to, uint256 withdrawAmount, int256 fundingFee, uint256 withdrawAmountFromMargin, Position position ); event OpenPosition( address indexed trader, uint8 side, uint256 baseAmount, uint256 quoteAmount, int256 fundingFee, Position position ); event ClosePosition( address indexed trader, uint256 quoteAmount, uint256 baseAmount, int256 fundingFee, Position position ); event Liquidate( address indexed liquidator, address indexed trader, address indexed to, uint256 quoteAmount, uint256 baseAmount, uint256 bonus, int256 fundingFee, Position position ); event UpdateCPF(uint256 timeStamp, int256 cpf); /// @notice only factory can call this function /// @param baseToken_ margin's baseToken. /// @param quoteToken_ margin's quoteToken. /// @param amm_ amm address. function initialize( address baseToken_, address quoteToken_, address amm_ ) external; /// @notice add margin to trader /// @param trader . /// @param depositAmount base amount to add. function addMargin(address trader, uint256 depositAmount) external; /// @notice remove margin to msg.sender /// @param withdrawAmount base amount to withdraw. function removeMargin( address trader, address to, uint256 withdrawAmount ) external; /// @notice open position with side and quoteAmount by msg.sender /// @param side long or short. /// @param quoteAmount quote amount. function openPosition( address trader, uint8 side, uint256 quoteAmount ) external returns (uint256 baseAmount); /// @notice close msg.sender's position with quoteAmount /// @param quoteAmount quote amount to close. function closePosition(address trader, uint256 quoteAmount) external returns (uint256 baseAmount); /// @notice liquidate trader function liquidate(address trader, address to) external returns ( uint256 quoteAmount, uint256 baseAmount, uint256 bonus ); function updateCPF() external returns (int256); /// @notice get factory address function factory() external view returns (address); /// @notice get config address function config() external view returns (address); /// @notice get base token address function baseToken() external view returns (address); /// @notice get quote token address function quoteToken() external view returns (address); /// @notice get amm address of this margin function amm() external view returns (address); /// @notice get all users' net position of quote function netPosition() external view returns (int256 netQuotePosition); /// @notice get all users' net position of quote function totalPosition() external view returns (uint256 totalQuotePosition); /// @notice get trader's position function getPosition(address trader) external view returns ( int256 baseSize, int256 quoteSize, uint256 tradeSize ); /// @notice get withdrawable margin of trader function getWithdrawable(address trader) external view returns (uint256 amount); /// @notice check if can liquidate this trader's position function canLiquidate(address trader) external view returns (bool); /// @notice calculate the latest funding fee with current position function calFundingFee(address trader) external view returns (int256 fundingFee); /// @notice calculate the latest debt ratio with Pnl and funding fee function calDebtRatio(address trader) external view returns (uint256 debtRatio); function calUnrealizedPnl(address trader) external view returns (int256); function getNewLatestCPF() external view returns (int256); function querySwapBaseWithAmm(bool isLong, uint256 quoteAmount) external view returns (uint256); }