GNSTradingInteractions

Facet #7: Trading (user interactions)

Methods

cancelOpenOrder

function cancelOpenOrder(uint32 _index) external nonpayable

Cancels an open limit/stop order for caller

Parameters

NameTypeDescription

_index

uint32

index of limit/stop order of caller

closeTradeMarket

function closeTradeMarket(uint32 _index) external nonpayable

Closes an open trade (market order) for caller

Parameters

NameTypeDescription

_index

uint32

the index of the trade of caller

closeTradeMarketTimeout

function closeTradeMarketTimeout(ITradingStorage.Id _orderId) external nonpayable

Safety function in case oracles don't answer in time, allows caller to initiate another market close order for the same open trade

Parameters

NameTypeDescription

_orderId

the id of the pending close market order to be canceled

decreasePositionSize

function decreasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta) external;

Decrease trade position size for trade with _index by either decreasing collateral by _collateralDelta or leverage by _leverageDelta.

Parameters

NameTypeDescription

_index

uint32

index of trade

_collateralDelta

uint120

collateral to remove (collateral precision)

_leverageDelta

uint24

leverage to reduce by (1e3)

delegatedTradingAction

function delegatedTradingAction(address _trader, bytes _callData) external nonpayable returns (bytes)

Caller executes a trading action on behalf of _trader using delegatecall

Parameters

NameTypeDescription

_trader

address

the trader address to execute the trading action for

_callData

bytes

the data to be executed (open trade/close trade, etc.)

Returns

NameTypeDescription

_0

bytes

function getByPassTriggerLink(address _user) external view returns (bool)

Returns whether a user bypasses trigger link costs

Parameters

NameTypeDescription

_user

address

address of the user

Returns

NameTypeDescription

_0

bool

getMarketOrdersTimeoutBlocks

function getMarketOrdersTimeoutBlocks() external view returns (uint16)

Returns the current marketOrdersTimeoutBlocks value

Returns

NameTypeDescription

_0

uint16

getTradingDelegate

function getTradingDelegate(address _trader) external view returns (address)

Returns the address a trader delegates his trading actions to

Parameters

NameTypeDescription

_trader

address

address of the trader

Returns

NameTypeDescription

_0

address

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.

Returns

NameTypeDescription

_0

address

increasePositionSize

function increasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice, uint16 _maxSlippageP) external;

Increase trade position size for trade with _index by increasing collateral amount by collateralDelta and increasing leverage by _leverageDelta

Parameters

NameTypeDescription

_index

uint32

index of trade

_collateralDelta

uint120

collateral to remove (collateral precision)

_leverageDelta

uint24

leverage to reduce by (1e3)

_expectedPrice

uint64

expected price of execution (1e10 precision)

_maxSlippageP

uint16

max slippage % (1e3)

initializeTrading

function initializeTrading(uint16 _marketOrdersTimeoutBlocks, address[] _usersByPassTriggerLink) external nonpayable

Initializes the trading facet

Parameters

NameTypeDescription

_marketOrdersTimeoutBlocks

uint16

The number of blocks after which a market order is considered timed out

_usersByPassTriggerLink

address[]

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

_token

address

token address

Returns

NameTypeDescription

_0

bool

openTrade

function openTrade(ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) external nonpayable

Opens a new trade/limit order/stop order

Parameters

NameTypeDescription

_trade

the trade to be opened

_maxSlippageP

uint16

the maximum allowed slippage % when open the trade (1e3 precision)

_referrer

address

the address of the referrer (can only be set once for a trader)

openTradeMarketTimeout

function openTradeMarketTimeout(ITradingStorage.Id _orderId) external nonpayable

Safety function in case oracles don't answer in time, allows caller to claim back the collateral of a pending open market order

Parameters

NameTypeDescription

_orderId

the id of the pending open market order to be canceled

openTradeNative

function openTradeNative(ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) external payable

Wraps native token and opens a new trade/limit order/stop order

Parameters

NameTypeDescription

_trade

the trade to be opened

_maxSlippageP

uint16

the maximum allowed slippage % when open the trade (1e3 precision)

_referrer

address

the address of the referrer (can only be set once for a trader)

removeTradingDelegate

function removeTradingDelegate() external nonpayable

Removes the delegate of caller (can't call delegatedAction)

setTradingDelegate

function setTradingDelegate(address _delegate) external nonpayable

Sets _delegate as the new delegate of caller (can call delegatedAction)

Parameters

NameTypeDescription

_delegate

address

the new delegate address

triggerOrder

function triggerOrder(uint256 _packed) external nonpayable

Initiates a new trigger order (for tp/sl/liq/limit/stop orders)

Parameters

NameTypeDescription

_packed

uint256

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

function updateByPassTriggerLink(address[] _users, bool[] _shouldByPass) external nonpayable

Updates the users that can bypass the link cost of triggerOrder

Parameters

NameTypeDescription

_users

address[]

array of addresses that can bypass the link cost of triggerOrder

_shouldByPass

bool[]

whether each user should bypass the link cost

updateLeverage

function updateLeverage(uint32 _index, uint24 _newLeverage) external;

Sets trade with _index leverage to _newLeverage while keeping trade position size the same.

Parameters

NameTypeDescription

_index

uint32

index of trade

_newLeverage

uint24

new leverage (1e3)

updateMarketOrdersTimeoutBlocks

function updateMarketOrdersTimeoutBlocks(uint16 _valueBlocks) external nonpayable

Updates marketOrdersTimeoutBlocks

Parameters

NameTypeDescription

_valueBlocks

uint16

blocks after which a market order times out

updateOpenOrder

function updateOpenOrder(uint32 _index, uint64 _triggerPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) external nonpayable

Updates an existing limit/stop order for caller

Parameters

NameTypeDescription

_index

uint32

index of limit/stop order of caller

_triggerPrice

uint64

new trigger price of limit/stop order (1e10 precision)

_tp

uint64

new tp of limit/stop order (1e10 precision)

_sl

uint64

new sl of limit/stop order (1e10 precision)

_maxSlippageP

uint16

new max slippage % of limit/stop order (1e3 precision)

updateSl

function updateSl(uint32 _index, uint64 _newSl) external nonpayable

Updates the sl of an open trade for caller

Parameters

NameTypeDescription

_index

uint32

index of open trade of caller

_newSl

uint64

new sl of open trade (1e10 precision)

updateTp

function updateTp(uint32 _index, uint64 _newTp) external nonpayable

Updates the tp of an open trade for caller

Parameters

NameTypeDescription

_index

uint32

index of open trade of caller

_newTp

uint64

new tp of open trade (1e10 precision)

Events

ByPassTriggerLinkUpdated

event ByPassTriggerLinkUpdated(address indexed user, bool bypass)

Emitted when a user is allowed/disallowed to bypass the link cost of triggerOrder

Parameters

NameTypeDescription

user indexed

address

address of the user

bypass

bool

whether the user can bypass the link cost of triggerOrder

ChainlinkCallbackTimeout

event ChainlinkCallbackTimeout(ITradingStorage.Id pendingOrderId, uint256 indexed pairIndex)

Emitted when a pending market order is canceled due to timeout

Parameters

NameTypeDescription

pendingOrderId

id of the pending order

pairIndex indexed

uint256

index of the trading pair

CouldNotCloseTrade

event CouldNotCloseTrade(address indexed trader, uint16 indexed pairIndex, uint32 index)

Emitted when a pending market order is canceled due to timeout and new closeTradeMarket() call failed

Parameters

NameTypeDescription

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

index

uint32

index of the open trade for caller

LeverageUpdateInitiated

event LeverageUpdateInitiated(ITradingStorage.Id orderId, address indexed trader, uint256 indexed pairIndex, uint256 index, bool isIncrease, uint256 newLeverage);

Emitted when a leverage update is initiated

Parameters

NameTypeDescription

orderId

price aggregator order id of the pending market order

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

index

uint256

index of user trades

isIncrease

bool

true if increase leverage, false if decrease

newLeverage

uint256

new leverage value (1e3)

MarketOrderInitiated

event MarketOrderInitiated(ITradingStorage.Id orderId, address indexed trader, uint16 indexed pairIndex, bool open)

Emitted when a market order is initiated

Parameters

NameTypeDescription

orderId

price aggregator order id of the pending market order

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

open

bool

whether the market order is for opening or closing a trade

MarketOrdersTimeoutBlocksUpdated

event MarketOrdersTimeoutBlocksUpdated(uint256 newValueBlocks)

Emitted when marketOrdersTimeoutBlocks is updated

Parameters

NameTypeDescription

newValueBlocks

uint256

the new value of marketOrdersTimeoutBlocks

NativeTokenWrapped

event NativeTokenWrapped(address indexed trader, uint256 nativeTokenAmount)

Emitted when a native token is wrapped

Parameters

NameTypeDescription

trader indexed

address

address of the trader

nativeTokenAmount

uint256

amount of native token wrapped

OpenLimitCanceled

event OpenLimitCanceled(address indexed trader, uint16 indexed pairIndex, uint32 index)

Emitted when a limit/stop order is canceled (collateral sent back to trader)

Parameters

NameTypeDescription

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

index

uint32

index of the open limit/stop order for caller

OpenLimitUpdated

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

Parameters

NameTypeDescription

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

index

uint32

index of the open limit/stop order for caller

newPrice

uint64

new trigger price (1e10 precision)

newTp

uint64

new tp (1e10 precision)

newSl

uint64

new sl (1e10 precision)

maxSlippageP

uint64

new max slippage % (1e3 precision)

OpenOrderPlaced

event OpenOrderPlaced(address indexed trader, uint16 indexed pairIndex, uint32 index)

Emitted when a new limit/stop order is placed

Parameters

NameTypeDescription

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

index

uint32

index of the open limit order for caller

PositionSizeUpdateInitiated

event PositionSizeUpdateInitiated(ITradingStorage.Id orderId, address indexed trader, uint256 indexed pairIndex, uint256 index, bool isIncrease, uint256 collateralDelta, uint256 leverageDelta);

Emitted when a position size update is initiated

Parameters

NameTypeDescription

orderId

price aggregator order id of the pending market order

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

index

uint256

index of user trades

isIncrease

bool

true if increase position size, false if decrease

collateralDelta

uint256

collateral delta (collateral precision)

leverageDelta

uint256

leverage delta (1e3)

TriggerOrderInitiated

event TriggerOrderInitiated(ITradingStorage.Id orderId, address indexed trader, uint16 indexed pairIndex, bool byPassesLinkCost)

Emitted when a trigger order is initiated (tp/sl/liq/limit/stop orders)

Parameters

NameTypeDescription

orderId

price aggregator order id of the pending trigger order

trader indexed

address

address of the trader

pairIndex indexed

uint16

index of the trading pair

byPassesLinkCost

bool

whether the caller bypasses the link cost

Errors

AboveGroupMaxOi

error AboveGroupMaxOi()

AboveMax

error AboveMax()

AbovePairMaxOi

error AbovePairMaxOi()

AlreadyBeingMarketClosed

error AlreadyBeingMarketClosed()

AlreadyExists

error AlreadyExists()

BelowMin

error BelowMin()

BelowMinPositionSizeUsd

error BelowMinPositionSizeUsd()

BlockOrder

error BlockOrder()

CollateralNotActive

error CollateralNotActive()

DelegateNotApproved

error DelegateNotApproved()

DelegatedActionNotAllowed

error DelegatedActionNotAllowed()

DoesntExist

error DoesntExist()

InitError

error InitError()

InvalidAddresses

error InvalidAddresses()

InvalidCollateralIndex

error InvalidCollateralIndex()

InvalidDecreasePositionSizeInput

error InvalidDecreasePositionSizeInput()

InvalidIncreasePositionSizeInput

error InvalidIncreasePositionSizeInput()

InvalidInputLength

error InvalidInputLength()

InsufficientCollateral

error InsufficientCollateral()

NewPositionSizeSmaller

error NewPositionSizeSmaller()

NoOrder

error NoOrder()

NoSl

error NoSl()

NoTp

error NoTp()

NoTrade

error NoTrade()

NotAllowed

error NotAllowed()

NotAuthorized

error NotAuthorized()

NotWrappedNativeToken

error NotWrappedNativeToken()

NotYourOrder

error NotYourOrder()

Overflow

error Overflow()

Paused

error Paused()

PendingTrigger

error PendingTrigger()

PriceImpactTooHigh

error PriceImpactTooHigh()

PriceZero

error PriceZero()

WaitTimeout

error WaitTimeout()

WrongAccess

error WrongAccess()

WrongIndex

error WrongIndex()

WrongLength

error WrongLength()

WrongLeverage

error WrongLeverage()

WrongOrder

error WrongOrder()

WrongOrderType

error WrongOrderType()

WrongParams

error WrongParams()

WrongSl

error WrongSl()

WrongTp

error WrongTp()

WrongTradeType

error WrongTradeType()

ZeroAddress

error ZeroAddress()

ZeroValue

error ZeroValue()

Last updated