Skip to main content
Interface for GNSTradingInteractions facet (inherits types and also contains functions, events, and custom errors)

initializeTrading

function initializeTrading(uint16 _marketOrdersTimeoutBlocks, address[] _usersByPassTriggerLink) external
Initializes the trading facet

Parameters

NameTypeDescription
_marketOrdersTimeoutBlocksuint16The number of blocks after which a market order is considered timed out
_usersByPassTriggerLinkaddress[]

updateMarketOrdersTimeoutBlocks

function updateMarketOrdersTimeoutBlocks(uint16 _valueBlocks) external
Updates marketOrdersTimeoutBlocks

Parameters

NameTypeDescription
_valueBlocksuint16blocks after which a market order times out
function updateByPassTriggerLink(address[] _users, bool[] _shouldByPass) external
Updates the users that can bypass the link cost of triggerOrder

Parameters

NameTypeDescription
_usersaddress[]array of addresses that can bypass the link cost of triggerOrder
_shouldByPassbool[]whether each user should bypass the link cost

setTradingDelegate

function setTradingDelegate(address _delegate) external
_Sets delegate as the new delegate of caller (can call delegatedAction)

Parameters

NameTypeDescription
_delegateaddressthe new delegate address

removeTradingDelegate

function removeTradingDelegate() external
Removes the delegate of caller (can’t call delegatedAction)

delegatedTradingAction

function delegatedTradingAction(address _trader, bytes _callData) external returns (bytes)
_Caller executes a trading action on behalf of trader using delegatecall

Parameters

NameTypeDescription
_traderaddressthe trader address to execute the trading action for
_callDatabytesthe data to be executed (open trade/close trade, etc.)

openTrade

function openTrade(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) external
Opens a new trade/limit order/stop order

Parameters

NameTypeDescription
_tradeITradingStorage.Tradethe trade to be opened
_maxSlippagePuint16the maximum allowed slippage % when open the trade (1e3 precision)
_referreraddressthe address of the referrer (can only be set once for a trader)

openTradeNative

function openTradeNative(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) external payable
Wraps native token and opens a new trade/limit order/stop order

Parameters

NameTypeDescription
_tradeITradingStorage.Tradethe trade to be opened
_maxSlippagePuint16the maximum allowed slippage % when open the trade (1e3 precision)
_referreraddressthe address of the referrer (can only be set once for a trader)

updateMaxClosingSlippageP

function updateMaxClosingSlippageP(uint32 _index, uint16 _maxSlippageP) external
Updates existing trade’s max closing slippage % for caller

Parameters

NameTypeDescription
_indexuint32index of trade
_maxSlippagePuint16new max closing slippage % (1e3 precision)

closeTradeMarket

function closeTradeMarket(uint32 _index, uint64 _expectedPrice) external
Closes an open trade (market order) for caller

Parameters

NameTypeDescription
_indexuint32the index of the trade of caller
_expectedPriceuint64expected closing price, used to check max slippage (1e10 precision)

updateOpenOrder

function updateOpenOrder(uint32 _index, uint64 _triggerPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) external
Updates an existing limit/stop order for caller

Parameters

NameTypeDescription
_indexuint32index of limit/stop order of caller
_triggerPriceuint64new trigger price of limit/stop order (1e10 precision)
_tpuint64new tp of limit/stop order (1e10 precision)
_sluint64new sl of limit/stop order (1e10 precision)
_maxSlippagePuint16new max slippage % of limit/stop order (1e3 precision)

cancelOpenOrder

function cancelOpenOrder(uint32 _index) external
Cancels an open limit/stop order for caller

Parameters

NameTypeDescription
_indexuint32index of limit/stop order of caller

updateTp

function updateTp(uint32 _index, uint64 _newTp) external
Updates the tp of an open trade for caller

Parameters

NameTypeDescription
_indexuint32index of open trade of caller
_newTpuint64new tp of open trade (1e10 precision)

updateSl

function updateSl(uint32 _index, uint64 _newSl) external
Updates the sl of an open trade for caller

Parameters

NameTypeDescription
_indexuint32index of open trade of caller
_newSluint64new sl of open trade (1e10 precision)

triggerOrder

function triggerOrder(uint256 _packed) external
Initiates a new trigger order (for tp/sl/liq/limit/stop orders)

Parameters

NameTypeDescription
_packeduint256the packed data of the trigger order (orderType, trader, index)

cancelOrderAfterTimeout

function cancelOrderAfterTimeout(uint32 _orderIndex) external
Safety function in case oracles don’t answer in time, allows caller to cancel a pending order and if relevant claim back any stuck collateral Only allowed for MARKET_OPEN, MARKET_CLOSE, UPDATE_LEVERAGE, MARKET_PARTIAL_OPEN, and MARKET_PARTIAL_CLOSE orders

Parameters

NameTypeDescription
_orderIndexuint32the id of the pending order to cancel

updateLeverage

function updateLeverage(uint32 _index, uint24 _newLeverage) external
Update trade leverage

Parameters

NameTypeDescription
_indexuint32index of trade
_newLeverageuint24new leverage (1e3)

increasePositionSize

function increasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice, uint16 _maxSlippageP) external
Increase trade position size

Parameters

NameTypeDescription
_indexuint32index of trade
_collateralDeltauint120collateral to add (collateral precision)
_leverageDeltauint24partial trade leverage (1e3)
_expectedPriceuint64expected price of execution (1e10 precision)
_maxSlippagePuint16max slippage % (1e3)

decreasePositionSize

function decreasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice) external
Decrease trade position size

Parameters

NameTypeDescription
_indexuint32index of trade
_collateralDeltauint120collateral to remove (collateral precision)
_leverageDeltauint24leverage to reduce by (1e3)
_expectedPriceuint64expected closing price, used to check max slippage (1e10 precision)

getWrappedNativeToken

function getWrappedNativeToken() external view returns (address)
Returns the wrapped native token or address(0) if the current chain, or the wrapped token, is not supported.

isWrappedNativeToken

function isWrappedNativeToken(address _token) external view returns (bool)
Returns true if the token is the wrapped native token for the current chain, where supported.

Parameters

NameTypeDescription
_tokenaddresstoken address

getTradingDelegate

function getTradingDelegate(address _trader) external view returns (address)
Returns the address a trader delegates his trading actions to

Parameters

NameTypeDescription
_traderaddressaddress of the trader

getMarketOrdersTimeoutBlocks

function getMarketOrdersTimeoutBlocks() external view returns (uint16)
Returns the current marketOrdersTimeoutBlocks value
function getByPassTriggerLink(address _user) external view returns (bool)
Returns whether a user bypasses trigger link costs

Parameters

NameTypeDescription
_useraddressaddress of the user

MarketOrdersTimeoutBlocksUpdated

event MarketOrdersTimeoutBlocksUpdated(uint256 newValueBlocks)
Emitted when marketOrdersTimeoutBlocks is updated

Parameters

NameTypeDescription
newValueBlocksuint256the new value of marketOrdersTimeoutBlocks

ByPassTriggerLinkUpdated

event ByPassTriggerLinkUpdated(address user, bool bypass)
Emitted when a user is allowed/disallowed to bypass the link cost of triggerOrder

Parameters

NameTypeDescription
useraddressaddress of the user
bypassboolwhether the user can bypass the link cost of triggerOrder

MarketOrderInitiated

event MarketOrderInitiated(struct ITradingStorage.Id orderId, address trader, uint16 pairIndex, bool open)
Emitted when a market order is initiated

Parameters

NameTypeDescription
orderIdstruct ITradingStorage.Idprice aggregator order id of the pending market order
traderaddressaddress of the trader
pairIndexuint16index of the trading pair
openboolwhether the market order is for opening or closing a trade

OpenOrderPlaced

event OpenOrderPlaced(address trader, uint16 pairIndex, uint32 index)
Emitted when a new limit/stop order is placed

Parameters

NameTypeDescription
traderaddressaddress of the trader
pairIndexuint16index of the trading pair
indexuint32index of the open limit order for caller

OpenLimitUpdated

event OpenLimitUpdated(address trader, uint16 pairIndex, uint32 index, uint64 newPrice, uint64 newTp, uint64 newSl, uint64 maxSlippageP)

Parameters

NameTypeDescription
traderaddressaddress of the trader
pairIndexuint16index of the trading pair
indexuint32index of the open limit/stop order for caller
newPriceuint64new trigger price (1e10 precision)
newTpuint64new tp (1e10 precision)
newSluint64new sl (1e10 precision)
maxSlippagePuint64new max slippage % (1e3 precision)

OpenLimitCanceled

event OpenLimitCanceled(address trader, uint16 pairIndex, uint32 index)
Emitted when a limit/stop order is canceled (collateral sent back to trader)

Parameters

NameTypeDescription
traderaddressaddress of the trader
pairIndexuint16index of the trading pair
indexuint32index of the open limit/stop order for caller

TriggerOrderInitiated

event TriggerOrderInitiated(struct ITradingStorage.Id orderId, address trader, uint16 pairIndex, bool byPassesLinkCost)
Emitted when a trigger order is initiated (tp/sl/liq/limit/stop orders)

Parameters

NameTypeDescription
orderIdstruct ITradingStorage.Idprice aggregator order id of the pending trigger order
traderaddressaddress of the trader
pairIndexuint16index of the trading pair
byPassesLinkCostboolwhether the caller bypasses the link cost

ChainlinkCallbackTimeout

event ChainlinkCallbackTimeout(struct ITradingStorage.Id pendingOrderId, uint256 pairIndex)
Emitted when a pending market order is canceled due to timeout

Parameters

NameTypeDescription
pendingOrderIdstruct ITradingStorage.Idid of the pending order
pairIndexuint256index of the trading pair

CouldNotCloseTrade

event CouldNotCloseTrade(address trader, uint16 pairIndex, uint32 index)
Emitted when a pending market order is canceled due to timeout and new closeTradeMarket() call failed

Parameters

NameTypeDescription
traderaddressaddress of the trader
pairIndexuint16index of the trading pair
indexuint32index of the open trade for caller

NativeTokenWrapped

event NativeTokenWrapped(address trader, uint256 nativeTokenAmount)
Emitted when a native token is wrapped

Parameters

NameTypeDescription
traderaddressaddress of the trader
nativeTokenAmountuint256amount of native token wrapped

NotWrappedNativeToken

error NotWrappedNativeToken()

DelegateNotApproved

error DelegateNotApproved()

PriceZero

error PriceZero()

AboveExposureLimits

error AboveExposureLimits()

CollateralNotActive

error CollateralNotActive()

PriceImpactTooHigh

error PriceImpactTooHigh()

NoTrade

error NoTrade()

NoOrder

error NoOrder()

AlreadyBeingMarketClosed

error AlreadyBeingMarketClosed()

ConflictingPendingOrder

error ConflictingPendingOrder(enum ITradingStorage.PendingOrderType)

WrongLeverage

error WrongLeverage()

WrongTp

error WrongTp()

WrongSl

error WrongSl()

WaitTimeout

error WaitTimeout()

PendingTrigger

error PendingTrigger()

NoSl

error NoSl()

NoTp

error NoTp()

NotYourOrder

error NotYourOrder()

DelegatedActionNotAllowed

error DelegatedActionNotAllowed()

InsufficientCollateral

error InsufficientCollateral()