Skip to main content
External library for helper functions commonly used in many places

getPnlPercent

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

NameTypeDescription
_openPriceuint64trade open price (1e10 precision)
_currentPriceuint64trade current price (1e10 precision)
_longbooltrue for long, false for short
_leverageuint24trade leverage (1e3 precision)

getPositionSizeCollateral

function getPositionSizeCollateral(uint120 _collateralAmount, uint24 _leverage) public pure returns (uint256)
Returns position size of trade in collateral tokens (avoids overflow from uint120 collateralAmount)

Parameters

NameTypeDescription
_collateralAmountuint120collateral of trade
_leverageuint24leverage of trade (1e3)

getMarketExecutionPrice

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

NameTypeDescription
_priceuint256price of the asset (1e10)
_spreadPuint256spread percentage (1e10)
_longbooltrue if long, false if short
_openbool
_contractsVersionenum ITradingStorage.ContractsVersion

convertCollateralToUsd

function convertCollateralToUsd(uint256 _collateralAmount, uint128 _collateralPrecisionDelta, uint256 _collateralPriceUsd) public pure returns (uint256)
Converts collateral value to USD (1e18 precision)

Parameters

NameTypeDescription
_collateralAmountuint256amount of collateral (collateral precision)
_collateralPrecisionDeltauint128precision delta of collateral (10^18/10^decimals)
_collateralPriceUsduint256price of collateral in USD (1e8)

convertCollateralToGns

function convertCollateralToGns(uint256 _collateralAmount, uint128 _collateralPrecisionDelta, uint256 _gnsPriceCollateral) internal pure returns (uint256)
Converts collateral value to GNS (1e18 precision)

Parameters

NameTypeDescription
_collateralAmountuint256amount of collateral (collateral precision)
_collateralPrecisionDeltauint128precision delta of collateral (10^18/10^decimals)
_gnsPriceCollateraluint256price of GNS in collateral (1e10)

getTradeValuePure

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

NameTypeDescription
_collateraluint256amount of collateral (collateral precision)
_percentProfitint256profit percentage (1e10)
_feesCollateraluint256borrowing fee + closing fee in collateral tokens (collateral precision)
_collateralPrecisionDeltauint128precision delta of collateral (10^18/10^decimals)

getLiqPnlThresholdP

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

NameTypeDescription
_paramsIPairsStorage.GroupLiquidationParamstrade liquidation params
_leverageuint256trade leverage (1e3 precision)

getMinPositionSizeCollateral

function getMinPositionSizeCollateral(uint8 _collateralIndex, uint256 _pairIndex) public view returns (uint256)
Returns minimum position size in collateral tokens for a pair (collateral precision)

Parameters

NameTypeDescription
_collateralIndexuint8collateral index
_pairIndexuint256pair index

getPositionSizeCollateralBasis

function getPositionSizeCollateralBasis(uint8 _collateralIndex, uint256 _pairIndex, uint256 _positionSizeCollateral) public view returns (uint256)
Returns position size to use when charging fees

Parameters

NameTypeDescription
_collateralIndexuint8collateral index
_pairIndexuint256pair index
_positionSizeCollateraluint256trade position size in collateral tokens (collateral precision)

isWithinExposureLimits

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

NameTypeDescription
_collateralIndexuint8index of collateral
_pairIndexuint16index of pair
_longbooltrue if long, false if short
_positionSizeCollateralDeltauint256position size delta in collateral tokens (collateral precision)

getTradeBorrowingFeeCollateral

function getTradeBorrowingFeeCollateral(struct ITradingStorage.Trade _trade) public view returns (uint256)
Convenient wrapper to return trade borrowing fee in collateral tokens (collateral precision)

Parameters

NameTypeDescription
_tradeITradingStorage.Tradetrade input

getTradeLiquidationPrice

function getTradeLiquidationPrice(struct ITradingStorage.Trade _trade, bool _useBorrowingFees) public view returns (uint256)
Convenient wrapper to return trade liquidation price (1e10)

Parameters

NameTypeDescription
_tradeITradingStorage.Tradetrade input
_useBorrowingFeesbool

getTradeValueCollateral

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

NameTypeDescription
_tradeITradingStorage.Tradetrade data
_percentProfitint256profit percentage (1e10)
_closingFeesCollateraluint256closing fees in collateral tokens (collateral precision)
_collateralPrecisionDeltauint128precision delta of collateral (10^18/10^decimals)

getTradeOpeningPriceImpact

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

NameTypeDescription
_inputITradingCommonUtils.TradePriceImpactInputinput data
_contractsVersionenum ITradingStorage.ContractsVersioncontracts version

getTradeClosingPriceImpact

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

NameTypeDescription
_inputITradingCommonUtils.TradePriceImpactInputinput data

getTradeLiqPnlThresholdP

function getTradeLiqPnlThresholdP(struct ITradingStorage.Trade _trade) public view returns (uint256)
Returns a trade’s liquidation threshold % (1e10)

Parameters

NameTypeDescription
_tradeITradingStorage.Tradetrade struct

getTotalTradeFeesCollateral

function getTotalTradeFeesCollateral(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint256 _positionSizeCollateral) public view returns (uint256)
Returns all fees for a trade in collateral tokens

Parameters

NameTypeDescription
_collateralIndexuint8collateral index
_traderaddressaddress of trader
_pairIndexuint16index of pair
_positionSizeCollateraluint256position size in collateral tokens (collateral precision)

getTradeFeesCollateral

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

NameTypeDescription
_collateralIndexuint8collateral index
_traderaddressaddress of trader
_pairIndexuint16index of pair
_collateralAmountuint256trade collateral amount (collateral precision)
_positionSizeCollateraluint256trade position size in collateral tokens (collateral precision)
_orderTypeenum ITradingStorage.PendingOrderTypecorresponding order type

getMinGovFeeCollateral

function getMinGovFeeCollateral(uint8 _collateralIndex, address _trader, uint16 _pairIndex) public view returns (uint256)

revertIfTradeHasPendingMarketOrder

function revertIfTradeHasPendingMarketOrder(address _user, uint32 _index) public view
Reverts if user initiated any kind of pending market order on his trade

Parameters

NameTypeDescription
_useraddresstrade user
_indexuint32trade index

getGToken

function getGToken(uint8 _collateralIndex) public view returns (contract IGToken)
Returns gToken contract for a collateral index

Parameters

NameTypeDescription
_collateralIndexuint8collateral index

transferCollateralFrom

function transferCollateralFrom(uint8 _collateralIndex, address _from, uint256 _amountCollateral) public
Transfers collateral from trader

Parameters

NameTypeDescription
_collateralIndexuint8index of the collateral
_fromaddresssending address
_amountCollateraluint256amount of collateral to receive (collateral precision)

transferCollateralTo

function transferCollateralTo(uint8 _collateralIndex, address _to, uint256 _amountCollateral) internal
Transfers collateral to trader

Parameters

NameTypeDescription
_collateralIndexuint8index of the collateral
_toaddressreceiving address
_amountCollateraluint256amount of collateral to transfer (collateral precision)

transferGnsTo

function transferGnsTo(address _to, uint256 _amountGns) internal
Transfers GNS to address

Parameters

NameTypeDescription
_toaddressreceiving address
_amountGnsuint256amount of GNS to transfer (1e18)

transferGnsFrom

function transferGnsFrom(address _from, uint256 _amountGns) internal
Transfers GNS from address

Parameters

NameTypeDescription
_fromaddresssending address
_amountGnsuint256amount of GNS to receive (1e18)

sendCollateralToVault

function sendCollateralToVault(uint8 _collateralIndex, uint256 _amountCollateral, address _trader) public
Sends collateral to gToken vault for negative pnl

Parameters

NameTypeDescription
_collateralIndexuint8collateral index
_amountCollateraluint256amount of collateral to send to vault (collateral precision)
_traderaddresstrader address

handleTradePnl

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

NameTypeDescription
_tradeITradingStorage.Tradetrade struct
_collateralSentToTraderint256total amount to send to trader (collateral precision)
_availableCollateralInDiamondint256part of _collateralSentToTrader available in diamond balance (collateral precision)
_borrowingFeeCollateraluint256

updateFeeTierPoints

function updateFeeTierPoints(uint8 _collateralIndex, address _trader, uint256 _pairIndex, uint256 _positionSizeCollateral) public
Updates a trader’s fee tiers points based on his trade size

Parameters

NameTypeDescription
_collateralIndexuint8collateral index
_traderaddressaddress of trader
_pairIndexuint256index of pair
_positionSizeCollateraluint256position size in collateral tokens (collateral precision)

distributeVaultFeeCollateral

function distributeVaultFeeCollateral(uint8 _collateralIndex, address _trader, uint256 _valueCollateral) public
Distributes fee to gToken vault

Parameters

NameTypeDescription
_collateralIndexuint8index of collateral
_traderaddressaddress of trader
_valueCollateraluint256fee in collateral tokens (collateral precision)

distributeExactGovFeeCollateral

function distributeExactGovFeeCollateral(uint8 _collateralIndex, address _trader, uint256 _govFeeCollateral) public
Distributes gov fees exact amount

Parameters

NameTypeDescription
_collateralIndexuint8index of collateral
_traderaddressaddress of trader
_govFeeCollateraluint256position size in collateral tokens (collateral precision)

distributeGnsOtcFeeCollateral

function distributeGnsOtcFeeCollateral(uint8 _collateralIndex, address _trader, uint256 _amountCollateral) public
Increases OTC balance to be distributed once OTC is executed

Parameters

NameTypeDescription
_collateralIndexuint8collateral index
_traderaddresstrader address
_amountCollateraluint256amount of collateral tokens to distribute (collateral precision)

distributeTriggerFeeGns

function distributeTriggerFeeGns(address _trader, uint8 _collateralIndex, uint256 _triggerFeeCollateral, uint256 _gnsPriceCollateral, uint128 _collateralPrecisionDelta) public
Distributes trigger fee in GNS tokens

Parameters

NameTypeDescription
_traderaddressaddress of trader
_collateralIndexuint8index of collateral
_triggerFeeCollateraluint256trigger fee in collateral tokens (collateral precision)
_gnsPriceCollateraluint256gns/collateral price (1e10 precision)
_collateralPrecisionDeltauint128collateral precision delta (10^18/10^decimals)

processFees

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

NameTypeDescription
_tradeITradingStorage.Tradetrade struct
_positionSizeCollateraluint256position size in collateral tokens (collateral precision)
_orderTypeenum ITradingStorage.PendingOrderTypetrade order type

distributeReferralFeeCollateral

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

NameTypeDescription
_collateralIndexuint8collateral index
_traderaddressaddress of trader
_positionSizeCollateraluint256position size in collateral tokens (collateral precision)
_referralFeeCollateraluint256referral fee in collateral tokens (collateral precision)
_gnsPriceCollateraluint256gns/collateral price (1e10 precision)

updateOi

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

NameTypeDescription
_tradeITradingStorage.Tradetrade struct
_positionSizeCollateraluint256position size in collateral tokens (collateral precision)
_openboolwhether it corresponds to a trade opening or closing
_isPnlPositiveboolwhether it corresponds to a positive pnl trade (only relevant when _open = false)

updateOiTrade

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

NameTypeDescription
_tradeITradingStorage.Tradetrade struct
_openboolwhether it corresponds to a trade opening or closing
_isPnlPositiveboolwhether it corresponds to a positive pnl trade (only relevant when _open = false)

handleOiDelta

function handleOiDelta(struct ITradingStorage.Trade _trade, uint256 _newPositionSizeCollateral, bool _isPnlPositive) external
Handles OI delta for an existing trade (for trade updates)

Parameters

NameTypeDescription
_tradeITradingStorage.Tradetrade struct
_newPositionSizeCollateraluint256new position size in collateral tokens (collateral precision)
_isPnlPositiveboolwhether it corresponds to a positive pnl trade (only relevant when closing)

_getMultiCollatDiamond

function _getMultiCollatDiamond() internal view returns (contract IGNSMultiCollatDiamond)
Returns current address as multi-collateral diamond interface to call other facets functions.