Skip to main content
Facet #5: Trading storage

constructor

constructor() public

initializeTradingStorage

function initializeTradingStorage(address _gns, address _gnsStaking, address[] _collaterals, address[] _gTokens) external
Initializes the trading storage facet

Parameters

NameTypeDescription
_gnsaddressaddress of the gns token
_gnsStakingaddressaddress of the gns staking contract
_collateralsaddress[]
_gTokensaddress[]

updateTradingActivated

function updateTradingActivated(enum ITradingStorage.TradingActivated _activated) external
Updates the trading activated state

Parameters

NameTypeDescription
_activatedenum ITradingStorage.TradingActivatedthe new trading activated state

addCollateral

function addCollateral(address _collateral, address _gToken) external
Adds a new supported collateral

Parameters

NameTypeDescription
_collateraladdressthe address of the collateral
_gTokenaddressthe gToken contract of the collateral

toggleCollateralActiveState

function toggleCollateralActiveState(uint8 _collateralIndex) external
Toggles the active state of a supported collateral

Parameters

NameTypeDescription
_collateralIndexuint8index of the collateral

updateGToken

function updateGToken(address _collateral, address _gToken) external
Updates the contracts of a supported collateral trading stack

Parameters

NameTypeDescription
_collateraladdressaddress of the collateral
_gTokenaddressthe gToken contract of the collateral

storeTrade

function storeTrade(struct ITradingStorage.Trade _trade, struct ITradingStorage.TradeInfo _tradeInfo) external virtual returns (struct ITradingStorage.Trade)
Stores a new trade (trade/limit/stop)

Parameters

NameTypeDescription
_tradeITradingStorage.Tradetrade to be stored
_tradeInfoITradingStorage.TradeInfotrade info to be stored

updateTradeMaxClosingSlippageP

function updateTradeMaxClosingSlippageP(struct ITradingStorage.Id _tradeId, uint16 _maxSlippageP) external virtual
Updates an existing trade max closing slippage %

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idid of the trade
_maxSlippagePuint16new max slippage % (1e3 precision)

updateTradeCollateralAmount

function updateTradeCollateralAmount(struct ITradingStorage.Id _tradeId, uint120 _collateralAmount) external virtual
Updates an open trade collateral

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idid of updated trade
_collateralAmountuint120new collateral amount value (collateral precision)

updateTradePosition

function updateTradePosition(struct ITradingStorage.Id _tradeId, uint120 _collateralAmount, uint24 _leverage, uint64 _openPrice, bool _isPartialIncrease, bool _isPnlPositive) external virtual
Updates an open trade collateral

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idid of updated trade
_collateralAmountuint120new collateral amount value (collateral precision)
_leverageuint24new leverage value
_openPriceuint64new open price value
_isPartialIncreaseboolrefreshes trade liquidation params if true
_isPnlPositiveboolwhether the pnl is positive (only relevant when closing)

updateOpenOrderDetails

function updateOpenOrderDetails(struct ITradingStorage.Id _tradeId, uint64 _openPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) external virtual
Updates an open order details (limit/stop)

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idid of updated trade
_openPriceuint64new open price (1e10)
_tpuint64new take profit price (1e10)
_sluint64new stop loss price (1e10)
_maxSlippagePuint16new max slippage % value (1e3)

updateTradeTp

function updateTradeTp(struct ITradingStorage.Id _tradeId, uint64 _newTp) external virtual
Updates the take profit of an open trade

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idthe trade id
_newTpuint64the new take profit (1e10 precision)

updateTradeSl

function updateTradeSl(struct ITradingStorage.Id _tradeId, uint64 _newSl) external virtual
Updates the stop loss of an open trade

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idthe trade id
_newSluint64the new sl (1e10 precision)

closeTrade

function closeTrade(struct ITradingStorage.Id _tradeId, bool _isPnlPositive) external virtual
Marks an open trade/limit/stop as closed

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idthe trade id
_isPnlPositiveboolwhether the pnl is positive

storePendingOrder

function storePendingOrder(struct ITradingStorage.PendingOrder _pendingOrder) external virtual returns (struct ITradingStorage.PendingOrder)
Stores a new pending order

Parameters

NameTypeDescription
_pendingOrderITradingStorage.PendingOrderthe pending order to be stored

closePendingOrder

function closePendingOrder(struct ITradingStorage.Id _orderId) external virtual
Closes a pending order

Parameters

NameTypeDescription
_orderIdITradingStorage.Idthe id of the pending order to be closed

getCollateral

function getCollateral(uint8 _index) external view returns (struct ITradingStorage.Collateral)
Returns collateral data by index

Parameters

NameTypeDescription
_indexuint8the index of the supported collateral

isCollateralActive

function isCollateralActive(uint8 _index) external view returns (bool)
Returns whether can open new trades with a collateral

Parameters

NameTypeDescription
_indexuint8the index of the collateral to check

isCollateralListed

function isCollateralListed(uint8 _index) external view returns (bool)
Returns whether a collateral has been listed

Parameters

NameTypeDescription
_indexuint8the index of the collateral to check

getCollateralsCount

function getCollateralsCount() external view returns (uint8)
Returns the number of supported collaterals

getCollaterals

function getCollaterals() external view returns (struct ITradingStorage.Collateral[])
Returns the supported collaterals

getCollateralIndex

function getCollateralIndex(address _collateral) external view returns (uint8)
Returns the index of a supported collateral

Parameters

NameTypeDescription
_collateraladdressthe address of the collateral

getTradingActivated

function getTradingActivated() external view returns (enum ITradingStorage.TradingActivated)
Returns the trading activated state

getTraderStored

function getTraderStored(address _trader) external view returns (bool)
Returns whether a trader is stored in the traders array

Parameters

NameTypeDescription
_traderaddresstrader to check

getTradersCount

function getTradersCount() external view returns (uint256)
Returns the length of the traders array

getTraders

function getTraders(uint32 _offset, uint32 _limit) external view returns (address[])
Returns all traders that have open trades using a pagination system

Parameters

NameTypeDescription
_offsetuint32start index in the traders array
_limituint32end index in the traders array

getTrade

function getTrade(address _trader, uint32 _index) external view returns (struct ITradingStorage.Trade)
Returns open trade/limit/stop order

Parameters

NameTypeDescription
_traderaddressaddress of the trader
_indexuint32index of the trade for trader

getTrades

function getTrades(address _trader) external view returns (struct ITradingStorage.Trade[])
Returns all open trades/limit/stop orders for a trader

Parameters

NameTypeDescription
_traderaddressaddress of the trader

getAllTradesForTraders

function getAllTradesForTraders(address[] _traders, uint256 _offset, uint256 _limit) external view returns (struct ITradingStorage.Trade[])
Returns all trade/limit/stop orders using a pagination system

Parameters

NameTypeDescription
_tradersaddress[]list of traders to return trades for
_offsetuint256index of first trade to return
_limituint256index of last trade to return

getAllTrades

function getAllTrades(uint256 _offset, uint256 _limit) external view returns (struct ITradingStorage.Trade[])
Returns all trade/limit/stop orders using a pagination system. Calls getAllTradesForTraders internally with all traders.

Parameters

NameTypeDescription
_offsetuint256index of first trade to return
_limituint256index of last trade to return

getTradeInfo

function getTradeInfo(address _trader, uint32 _index) external view returns (struct ITradingStorage.TradeInfo)
Returns trade info of an open trade/limit/stop order

Parameters

NameTypeDescription
_traderaddressaddress of the trader
_indexuint32index of the trade for trader

getTradeInfos

function getTradeInfos(address _trader) external view returns (struct ITradingStorage.TradeInfo[])
Returns all trade infos of open trade/limit/stop orders for a trader

Parameters

NameTypeDescription
_traderaddressaddress of the trader

getAllTradeInfosForTraders

function getAllTradeInfosForTraders(address[] _traders, uint256 _offset, uint256 _limit) external view returns (struct ITradingStorage.TradeInfo[])
Returns all trade infos of open trade/limit/stop orders using a pagination system

Parameters

NameTypeDescription
_tradersaddress[]list of traders to return tradeInfo for
_offsetuint256index of first tradeInfo to return
_limituint256index of last tradeInfo to return

getAllTradeInfos

function getAllTradeInfos(uint256 _offset, uint256 _limit) external view returns (struct ITradingStorage.TradeInfo[])
Returns all trade infos of open trade/limit/stop orders using a pagination system. Calls getAllTradeInfosForTraders internally with all traders.

Parameters

NameTypeDescription
_offsetuint256index of first tradeInfo to return
_limituint256index of last tradeInfo to return

getPendingOrder

function getPendingOrder(struct ITradingStorage.Id _orderId) external view returns (struct ITradingStorage.PendingOrder)
Returns a pending ordeer

Parameters

NameTypeDescription
_orderIdITradingStorage.Idid of the pending order

getPendingOrders

function getPendingOrders(address _user) external view returns (struct ITradingStorage.PendingOrder[])
Returns all pending orders for a trader

Parameters

NameTypeDescription
_useraddressaddress of the trader

getAllPendingOrdersForTraders

function getAllPendingOrdersForTraders(address[] _traders, uint256 _offset, uint256 _limit) external view returns (struct ITradingStorage.PendingOrder[])
Returns all pending orders using a pagination system

Parameters

NameTypeDescription
_tradersaddress[]list of traders to return pendingOrder for
_offsetuint256index of first pendingOrder to return
_limituint256index of last pendingOrder to return

getAllPendingOrders

function getAllPendingOrders(uint256 _offset, uint256 _limit) external view returns (struct ITradingStorage.PendingOrder[])
Returns all pending orders using a pagination system Calls getAllPendingOrdersForTraders internally with all traders.

Parameters

NameTypeDescription
_offsetuint256index of first pendingOrder to return
_limituint256index of last pendingOrder to return

getTradePendingOrderBlock

function getTradePendingOrderBlock(struct ITradingStorage.Id _tradeId, enum ITradingStorage.PendingOrderType _orderType) external view returns (uint256)
Returns the block number of the pending order for a trade (0 = doesn’t exist)

Parameters

NameTypeDescription
_tradeIdITradingStorage.Idid of the trade
_orderTypeenum ITradingStorage.PendingOrderTypepending order type to check

getCounters

function getCounters(address _trader, enum ITradingStorage.CounterType _type) external view returns (struct ITradingStorage.Counter)
Returns the counters of a trader (currentIndex / open count for trades/tradeInfos and pendingOrders mappings)

Parameters

NameTypeDescription
_traderaddressaddress of the trader
_typeenum ITradingStorage.CounterTypethe counter type (trade/pending order)

getCountersForTraders

function getCountersForTraders(address[] _traders, enum ITradingStorage.CounterType _type) external view returns (struct ITradingStorage.Counter[])
Returns the counters for a list of traders

Parameters

NameTypeDescription
_tradersaddress[]the list of traders
_typeenum ITradingStorage.CounterTypethe counter type (trade/pending order)

getGToken

function getGToken(uint8 _collateralIndex) external view returns (address)
Returns the address of the gToken for a collateral stack

Parameters

NameTypeDescription
_collateralIndexuint8the index of the supported collateral

getTradeLiquidationParams

function getTradeLiquidationParams(address _trader, uint32 _index) external view returns (struct IPairsStorage.GroupLiquidationParams)
Returns the liquidation params for a trade

Parameters

NameTypeDescription
_traderaddressaddress of the trader
_indexuint32index of the trade for trader

getTradesLiquidationParams

function getTradesLiquidationParams(address _trader) external view returns (struct IPairsStorage.GroupLiquidationParams[])
Returns all trade liquidation params of open trade/limit/stop orders for a trader

Parameters

NameTypeDescription
_traderaddressaddress of the trader

getAllTradesLiquidationParamsForTraders

function getAllTradesLiquidationParamsForTraders(address[] _traders, uint256 _offset, uint256 _limit) external view returns (struct IPairsStorage.GroupLiquidationParams[])
Returns all trade liquidation params of open trade/limit/stop orders using a pagination system

Parameters

NameTypeDescription
_tradersaddress[]list of traders to return liq params for
_offsetuint256index of first liq param to return
_limituint256index of last liq param to return

getAllTradesLiquidationParams

function getAllTradesLiquidationParams(uint256 _offset, uint256 _limit) external view returns (struct IPairsStorage.GroupLiquidationParams[])
Returns all trade liquidation params of open trade/limit/stop orders using a pagination system Calls getAllTradesLiquidationParamsForTraders internally with all traders.

Parameters

NameTypeDescription
_offsetuint256index of first liq param to return
_limituint256index of last liq param to return

getCurrentContractsVersion

function getCurrentContractsVersion() external pure returns (enum ITradingStorage.ContractsVersion)
Returns the current contracts version