Skip to main content
Facet #7: Trading (user interactions)

constructor

constructor() public

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)

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)

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

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