Skip to main content

Position.sol

Position

struct Position {
uint256 id;
uint256 openVersion;
uint256 closeVersion;
int256 qty;
uint256 openTimestamp;
uint256 closeTimestamp;
uint256 takerMargin;
address owner;
address liquidator;
uint16 _protocolFeeRate;
BinMargin[] _binMargins;
}

The Position struct represents a trading position.

NameTypeDescription
iduint256The position identifier
openVersionuint256The version of the oracle when the position was opened
closeVersionuint256The version of the oracle when the position was closed
qtyint256The quantity of the position
openTimestampuint256The timestamp when the position was opened
closeTimestampuint256The timestamp when the position was closed
takerMarginuint256The amount of collateral that a trader must provide
owneraddressThe owner of the position, usually it is the account address of trader
liquidatoraddressThe liquidator contract address
_binMarginsBinMargin[]The bin margins for the position, it represents the amount of collateral for each bin
_protocolFeeRateuint16The protocol fee rate for the market

PositionLib

Provides functions that operate on the Position struct

entryPrice

function entryPrice(struct Position self, struct LpContext ctx) internal view returns (uint256)

Calculates the entry price of the position based on the position's open oracle version

It fetches oracle price from IOracleProvider at the settle version calculated based on the position's open oracle version

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
    ctxstruct LpContextThe context object for this transaction
  • Return Values:

    NameTypeDescription
    [0]uint256uint256 The entry price

exitPrice

function exitPrice(struct Position self, struct LpContext ctx) internal view returns (uint256)

Calculates the exit price of the position based on the position's close oracle version

It fetches oracle price from IOracleProvider at the settle version calculated based on the position's close oracle version

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
    ctxstruct LpContextThe context object for this transaction
  • Return Values:

    NameTypeDescription
    [0]uint256uint256 The exit price

pnl

function pnl(struct Position self, struct LpContext ctx) internal view returns (int256)

Calculates the profit or loss of the position based on the close oracle version and the qty

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
    ctxstruct LpContextThe context object for this transaction
  • Return Values:

    NameTypeDescription
    [0]int256int256 The profit or loss

makerMargin

function makerMargin(struct Position self) internal pure returns (uint256 margin)

Calculates the total margin required for the makers of the position

The maker margin is calculated by summing up the amounts of all bin margins in the _binMargins array

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
  • Return Values:

    NameTypeDescription
    marginuint256The maker margin

tradingFee

function tradingFee(struct Position self) internal pure returns (uint256 fee)

Calculates the total trading fee for the position

The trading fee is calculated by summing up the trading fees of all bin margins in the _binMargins array

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
  • Return Values:

    NameTypeDescription
    feeuint256The trading fee

protocolFee

function protocolFee(struct Position self) internal pure returns (uint256 fee)

Calculates the total protocol fee for a position.

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe Position struct representing the position.
  • Return Values:

    NameTypeDescription
    feeuint256The total protocol fee amount.

binMargins

function binMargins(struct Position self) internal pure returns (struct BinMargin[] margins)

Returns an array of BinMargin instances representing the bin margins for the position

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
  • Return Values:

    NameTypeDescription
    marginsstruct BinMargin[]The bin margins for the position

setBinMargins

function setBinMargins(struct Position self, struct BinMargin[] margins) internal pure

Sets the _binMargins array for the position

  • Parameters:

    NameTypeDescription
    selfstruct PositionThe memory instance of the Position struct
    marginsstruct BinMargin[]The bin margins for the position