External library for helper functions commonly used in many places
getPnlPercent
Copy function getPnlPercent(uint64 _openPrice, uint64 _currentPrice, bool _long, uint24 _leverage) public pure returns (int256 p)
Returns the current percent profit of a trade (1e10 precision)
Parameters
getPositionSizeCollateral
Copy function getPositionSizeCollateral ( uint120 _collateralAmount , uint24 _leverage ) public pure returns ( uint256 )
Returns position size of trade in collateral tokens (avoids overflow from uint120 collateralAmount)
Parameters
getMarketExecutionPrice
Copy function getMarketExecutionPrice(uint256 _price, uint256 _spreadP, bool _long, bool _open, enum ITradingStorage.ContractsVersion _contractsVersion) public pure returns (uint256)
Calculates market execution price for a trade (1e10 precision)
Parameters
convertCollateralToUsd
Copy function convertCollateralToUsd(uint256 _collateralAmount, uint128 _collateralPrecisionDelta, uint256 _collateralPriceUsd) public pure returns (uint256)
Converts collateral value to USD (1e18 precision)
Parameters
convertCollateralToGns
Copy function convertCollateralToGns(uint256 _collateralAmount, uint128 _collateralPrecisionDelta, uint256 _gnsPriceCollateral) internal pure returns (uint256)
Converts collateral value to GNS (1e18 precision)
Parameters
getTradeValuePure
Copy function getTradeValuePure(uint256 _collateral, int256 _percentProfit, uint256 _feesCollateral, uint128 _collateralPrecisionDelta) public pure returns (uint256)
Calculates trade value (useful when closing a trade) Important: does not calculate if trade can be liquidated or not, has to be done by calling function
Parameters
getLiqPnlThresholdP
Copy function getLiqPnlThresholdP(struct IPairsStorage.GroupLiquidationParams _params, uint256 _leverage) public pure returns (uint256)
Pure function that returns the liquidation pnl % threshold for a trade (1e10)
Parameters
getMinPositionSizeCollateral
Copy function getMinPositionSizeCollateral ( uint8 _collateralIndex , uint256 _pairIndex ) public view returns ( uint256 )
Returns minimum position size in collateral tokens for a pair (collateral precision)
Parameters
getPositionSizeCollateralBasis
Copy function getPositionSizeCollateralBasis(uint8 _collateralIndex, uint256 _pairIndex, uint256 _positionSizeCollateral) public view returns (uint256)
Returns position size to use when charging fees
Parameters
isWithinExposureLimits
Copy function isWithinExposureLimits(uint8 _collateralIndex, uint16 _pairIndex, bool _long, uint256 _positionSizeCollateralDelta) external view returns (bool)
Checks if total position size is not higher than maximum allowed open interest for a pair
Parameters
getTradeBorrowingFeeCollateral
Copy function getTradeBorrowingFeeCollateral (struct ITradingStorage . Trade _trade ) public view returns ( uint256 )
Convenient wrapper to return trade borrowing fee in collateral tokens (collateral precision)
Parameters
getTradeLiquidationPrice
Copy function getTradeLiquidationPrice(struct ITradingStorage.Trade _trade, bool _useBorrowingFees) public view returns (uint256)
Convenient wrapper to return trade liquidation price (1e10)
Parameters
getTradeValueCollateral
Copy function getTradeValueCollateral(struct ITradingStorage.Trade _trade, int256 _percentProfit, uint256 _closingFeesCollateral, uint128 _collateralPrecisionDelta) public view returns (uint256 valueCollateral, uint256 borrowingFeesCollateral)
Returns trade value and borrowing fee in collateral tokens
Parameters
getTradeOpeningPriceImpact
Copy function getTradeOpeningPriceImpact(struct ITradingCommonUtils.TradePriceImpactInput _input, enum ITradingStorage.ContractsVersion _contractsVersion) external view returns (uint256 priceImpactP, uint256 priceAfterImpact)
Returns price impact % (1e10), price after spread and impact (1e10)
Parameters
getTradeClosingPriceImpact
Copy function getTradeClosingPriceImpact(struct ITradingCommonUtils.TradePriceImpactInput _input) external view returns (uint256 priceImpactP, uint256 priceAfterImpact, uint256 tradeValueCollateralNoFactor)
Returns price impact % (1e10), price after spread and impact (1e10), and trade value used to know if pnl is positive (collateral precision)
Parameters
getTradeLiqPnlThresholdP
Copy function getTradeLiqPnlThresholdP (struct ITradingStorage . Trade _trade ) public view returns ( uint256 )
Returns a trade's liquidation threshold % (1e10)
Parameters
getTotalTradeFeesCollateral
Copy function getTotalTradeFeesCollateral(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint256 _positionSizeCollateral) public view returns (uint256)
Returns all fees for a trade in collateral tokens
Parameters
getTradeFeesCollateral
Copy function getTradeFeesCollateral(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint256 _collateralAmount, uint256 _positionSizeCollateral, enum ITradingStorage.PendingOrderType _orderType) public view returns (struct IPairsStorage.TradeFees tradeFees)
Returns all fees for a trade in collateral tokens
Parameters
getMinGovFeeCollateral
Copy function getMinGovFeeCollateral(uint8 _collateralIndex, address _trader, uint16 _pairIndex) public view returns (uint256)
revertIfTradeHasPendingMarketOrder
Copy function revertIfTradeHasPendingMarketOrder ( address _user , uint32 _index ) public view
Reverts if user initiated any kind of pending market order on his trade
Parameters
getGToken
Copy function getGToken ( uint8 _collateralIndex ) public view returns (contract IGToken )
Returns gToken contract for a collateral index
Parameters
transferCollateralFrom
Copy function transferCollateralFrom ( uint8 _collateralIndex , address _from , uint256 _amountCollateral ) public
Transfers collateral from trader
Parameters
transferCollateralTo
Copy function transferCollateralTo ( uint8 _collateralIndex , address _to , uint256 _amountCollateral ) internal
Transfers collateral to trader
Parameters
transferGnsTo
Copy function transferGnsTo ( address _to , uint256 _amountGns ) internal
Transfers GNS to address
Parameters
transferGnsFrom
Copy function transferGnsFrom ( address _from , uint256 _amountGns ) internal
Transfers GNS from address
Parameters
sendCollateralToVault
Copy function sendCollateralToVault ( uint8 _collateralIndex , uint256 _amountCollateral , address _trader ) public
Sends collateral to gToken vault for negative pnl
Parameters
handleTradePnl
Copy function handleTradePnl(struct ITradingStorage.Trade _trade, int256 _collateralSentToTrader, int256 _availableCollateralInDiamond, uint256 _borrowingFeeCollateral) external returns (uint256 traderDebt)
Handles pnl transfers when (fully or partially) closing a trade
Parameters
updateFeeTierPoints
Copy function updateFeeTierPoints(uint8 _collateralIndex, address _trader, uint256 _pairIndex, uint256 _positionSizeCollateral) public
Updates a trader's fee tiers points based on his trade size
Parameters
distributeVaultFeeCollateral
Copy function distributeVaultFeeCollateral ( uint8 _collateralIndex , address _trader , uint256 _valueCollateral ) public
Distributes fee to gToken vault
Parameters
distributeExactGovFeeCollateral
Copy function distributeExactGovFeeCollateral ( uint8 _collateralIndex , address _trader , uint256 _govFeeCollateral ) public
Distributes gov fees exact amount
Parameters
distributeGnsOtcFeeCollateral
Copy function distributeGnsOtcFeeCollateral ( uint8 _collateralIndex , address _trader , uint256 _amountCollateral ) public
Increases OTC balance to be distributed once OTC is executed
Parameters
distributeTriggerFeeGns
Copy function distributeTriggerFeeGns(address _trader, uint8 _collateralIndex, uint256 _triggerFeeCollateral, uint256 _gnsPriceCollateral, uint128 _collateralPrecisionDelta) public
Distributes trigger fee in GNS tokens
Parameters
processFees
Copy function processFees(struct ITradingStorage.Trade _trade, uint256 _positionSizeCollateral, enum ITradingStorage.PendingOrderType _orderType) external returns (uint256)
Distributes opening fees for trade and returns the trade fees charged in collateral tokens
Parameters
distributeReferralFeeCollateral
Copy function distributeReferralFeeCollateral(uint8 _collateralIndex, address _trader, uint256 _positionSizeCollateral, uint256 _referralFeeCollateral, uint256 _gnsPriceCollateral) public
Distributes referral rewards and returns the amount charged in collateral tokens
Parameters
updateOi
Copy function updateOi(struct ITradingStorage.Trade _trade, uint256 _positionSizeCollateral, bool _open, bool _isPnlPositive) public
_Update protocol open interest (any amount) CAREFUL: this will reset the trade's borrowing fees to 0 when open = true
Parameters
updateOiTrade
Copy function updateOiTrade (struct ITradingStorage . Trade _trade , bool _open , bool _isPnlPositive ) external
_Update protocol open interest (trade position size) CAREFUL: this will reset the trade's borrowing fees to 0 when open = true
Parameters
handleOiDelta
Copy function handleOiDelta(struct ITradingStorage.Trade _trade, uint256 _newPositionSizeCollateral, bool _isPnlPositive) external
Handles OI delta for an existing trade (for trade updates)
Parameters
_getMultiCollatDiamond
Copy function _getMultiCollatDiamond () internal view returns (contract IGNSMultiCollatDiamond )
Returns current address as multi-collateral diamond interface to call other facets functions.