/ contracts / interfaces / external / d8x / ID8xPerpetualsContractInterface.sol
ID8xPerpetualsContractInterface.sol
  1  pragma solidity >=0.6.0;
  2  pragma experimental ABIEncoderV2;
  3  
  4  
  5  interface ID8xPerpetualsContractInterface {
  6  
  7      //     iPerpetualId          global id for perpetual
  8      //     traderAddr            address of trader
  9      //     brokerSignature       signature of broker (or 0)
 10      //     brokerFeeTbps         broker can set their own fee
 11      //     fAmount               amount in base currency to be traded
 12      //     fLimitPrice           limit price
 13      //     fTriggerPrice         trigger price. Non-zero for stop orders.
 14      //     iDeadline             deadline for price (seconds timestamp)
 15      //     traderMgnTokenAddr    address of the compatible margin token the user likes to use,
 16      //                           0 if same token as liquidity pool's margin token
 17      //     flags                 trade flags
 18      struct ClientOrder {
 19          uint32 flags;
 20          uint24 iPerpetualId;
 21          uint16 brokerFeeTbps;
 22          address traderAddr;
 23          address brokerAddr;
 24          address referrerAddr;
 25          bytes brokerSignature;
 26          int128 fAmount;
 27          int128 fLimitPrice;
 28          int128 fTriggerPrice;
 29          int128 fLeverage; // 0 if deposit and trade separate
 30          uint64 iDeadline;
 31          uint64 createdTimestamp;
 32          //uint64 submittedBlock <- will be set by LimitOrderBook
 33          bytes32 parentChildDigest1;
 34          bytes32 parentChildDigest2;
 35      }
 36  
 37      struct LiquidityPoolData {
 38          bool isRunning; // state
 39          uint8 iPerpetualCount; // state
 40          uint8 id; // parameter: index, starts from 1
 41          uint16 iTargetPoolSizeUpdateTime; //parameter: timestamp in seconds. How often we update the pool's target size
 42          address marginTokenAddress; //parameter: address of the margin token
 43          // -----
 44          int128 fFundAllocationNormalizationCC; // state: sum of all perpetual weights during fund allocation (cheaper than re-normalizing w/each trade)
 45          int128 fDefaultFundCashCC; // state: profit/loss
 46          // -----
 47          uint64 prevAnchor; // state: keep track of timestamp since last withdrawal was initiated
 48          int32 fRedemptionRate; // state: used for settlement in case of AMM default
 49          address shareTokenAddress; // parameter
 50          // -----
 51          int128 fPnLparticipantsCashCC; // state: addLiquidity/withdrawLiquidity + profit/loss - rebalance
 52          int128 fAMMFundCashCC; // state: profit/loss - rebalance (sum of cash in individual perpetuals)
 53          // -----
 54          int128 fTargetAMMFundSize; // state: target AMM pool size for all perpetuals in pool (sum)
 55          int128 fTargetDFSize; // state: target default fund size for all perpetuals in pool
 56          // -----
 57          int128 fMaxTransferPerConvergencePeriod; // param: how many funds can be transferred in FUND_TRANSFER_CONVERGENCE_HOURS hours
 58          int128 fBrokerCollateralLotSize; // param:how much collateral do brokers deposit when providing "1 lot" (not trading lot)
 59          // -----
 60          uint128 prevTokenAmount; // state
 61          uint128 nextTokenAmount; // state
 62          // -----
 63          uint128 totalSupplyShareToken; // state
 64      }
 65      
 66      /**
 67       * @notice  D8X Perpetual Data structure to store user margin information.
 68       */
 69      struct MarginAccount {
 70          int128 fLockedInValueQC; // unrealized value locked-in when trade occurs in
 71          int128 fCashCC; // cash in collateral currency (base, quote, or quanto)
 72          int128 fPositionBC; // position in base currency (e.g., 1 BTC for BTCUSD)
 73          int128 fUnitAccumulatedFundingStart; // accumulated funding rate
 74          uint64 iLastOpenTimestamp; // timestamp in seconds when the position was last opened/increased
 75          uint16 feeTbps; // exchange fee in tenth of a basis point
 76          uint16 brokerFeeTbps; // broker fee in tenth of a basis point
 77          bytes16 positionId; // unique id for the position (for given trader, and perpetual). Current position, zero otherwise.
 78      }
 79  
 80      /**
 81       * @notice  Data structure to return simplified and relevant margin information.
 82       */
 83      struct D18MarginAccount {
 84          int256 lockedInValueQCD18; // unrealized value locked-in when trade occurs in
 85          int256 cashCCD18; // cash in collateral currency (base, quote, or quanto)
 86          int256 positionSizeBCD18; // position in base currency (e.g., 1 BTC for BTCUSD)
 87          bytes16 positionId; // unique id for the position (for given trader, and perpetual). Current position, zero otherwise.
 88      }
 89      
 90      function postOrder(ClientOrder calldata _order, bytes calldata _signature)
 91          external;
 92  
 93      function getMarginAccount(uint24 _perpetualId, address _traderAddress)
 94          external
 95          view
 96          returns (MarginAccount memory);
 97  
 98      function getMaxSignedOpenTradeSizeForPos(
 99          uint24 _perpetualId,
100          int128 _fCurrentTraderPos,
101          bool _isBuy
102      ) external view returns (int128);
103  
104      function getPriceInfo(uint24 _perpetualId) external view returns (bytes32[] memory, bool[] memory);
105  
106      function getPoolCount() external view returns (uint8);
107     
108      /**
109  
110       * Query liquidity pool data for given indices
111  
112       * @param _poolFromIdx start from (>=1)
113  
114       * @param _poolToIdx up to (can be larger than number of pools)
115  
116       * @return array with liquidity pool data
117  
118       */
119  
120      function getLiquidityPools(uint8 _poolFromIdx, uint8 _poolToIdx) external view returns (LiquidityPoolData[] memory);
121  
122      function getPerpetualCountInPool(uint8 _poolId) external view returns (uint8);
123  
124      function getPerpetualId(uint8 _poolId, uint8 _perpetualIndex) external view returns (uint24);
125  
126  }