Position

Git Source

Keeps track of LP positions. Stores the lower and upper Ticks of a user's range order, and tracks the pro-rata exposure of the order.

Functions

keyHash

Returns the position key hash for self

function keyHash(Key memory self) internal pure returns (bytes32);

keyHash

Returns the position key hash for self

function keyHash(KeyInternal memory self) internal pure returns (bytes32);

toKeyInternal

Returns the internal position key for self

function toKeyInternal(Key memory self, UD60x18 strike, bool isCall) internal pure returns (KeyInternal memory);

Parameters

NameTypeDescription
selfKey
strikeUD60x18The strike of the option (18 decimals)
isCallbool

isShort

Returns true if the position orderType is short

function isShort(OrderType orderType) internal pure returns (bool);

isLong

Returns true if the position orderType is long

function isLong(OrderType orderType) internal pure returns (bool);

pieceWiseLinear

Returns the percentage by which the market price has passed through the lower and upper prices from left to right.

Usage:
CS order: f(x) defines the amount of shorts of a CS order holding one unit of liquidity.
LC order: (1 - f(x)) defines the amount of longs of a LC order holding one unit of liquidity.
Function definition:
case 1. f(x) = 0                                for x < lower
case 2. f(x) = (x - lower) / (upper - lower)    for lower <= x <= upper
case 3. f(x) = 1                                for x > upper
function pieceWiseLinear(KeyInternal memory self, UD50x28 price) internal pure returns (UD50x28);

pieceWiseQuadratic

Returns the amount of 'bid-side' collateral associated to a range order with one unit of liquidity.

Usage:
CS order: bid-side collateral defines the premiums generated from selling options.
LC order: bid-side collateral defines the collateral used to pay for buying long options.
Function definition:
case 1. f(x) = 0                                            for x < lower
case 2. f(x) = (price**2 - lower) / [2 * (upper - lower)]   for lower <= x <= upper
case 3. f(x) = (upper + lower) / 2                          for x > upper
function pieceWiseQuadratic(KeyInternal memory self, UD50x28 price) internal pure returns (UD50x28);

collateralToContracts

Converts _collateral to the amount of contracts normalized to 18 decimals

function collateralToContracts(UD60x18 _collateral, UD60x18 strike, bool isCall) internal pure returns (UD60x18);

Parameters

NameTypeDescription
_collateralUD60x18
strikeUD60x18The strike price (18 decimals)
isCallbool

contractsToCollateral

Converts _contracts to the amount of collateral normalized to 18 decimals

WARNING: Decimals needs to be scaled before using this amount for collateral transfers

function contractsToCollateral(UD60x18 _contracts, UD60x18 strike, bool isCall) internal pure returns (UD60x18);

Parameters

NameTypeDescription
_contractsUD60x18
strikeUD60x18The strike price (18 decimals)
isCallbool

collateralToContracts

Converts _collateral to the amount of contracts normalized to 28 decimals

function collateralToContracts(UD50x28 _collateral, UD60x18 strike, bool isCall) internal pure returns (UD50x28);

Parameters

NameTypeDescription
_collateralUD50x28
strikeUD60x18The strike price (18 decimals)
isCallbool

contractsToCollateral

Converts _contracts to the amount of collateral normalized to 28 decimals

WARNING: Decimals needs to be scaled before using this amount for collateral transfers

function contractsToCollateral(UD50x28 _contracts, UD60x18 strike, bool isCall) internal pure returns (UD50x28);

Parameters

NameTypeDescription
_contractsUD50x28
strikeUD60x18The strike price (18 decimals)
isCallbool

liquidityPerTick

Returns the per-tick liquidity phi (delta) for a specific position key self

function liquidityPerTick(KeyInternal memory self, UD60x18 size) internal pure returns (UD50x28);

Parameters

NameTypeDescription
selfKeyInternal
sizeUD60x18The contract amount (18 decimals)

bid

Returns the bid collateral (18 decimals) either used to buy back options or revenue/ income generated from underwriting / selling options.

For a <= p <= b we have:
bid(p; a, b) = [ (p - a) / (b - a) ] * [ (a + p)  / 2 ]
= (p^2 - a^2) / [2 * (b - a)]
function bid(KeyInternal memory self, UD60x18 size, UD50x28 price) internal pure returns (UD60x18);

Parameters

NameTypeDescription
selfKeyInternalThe internal position key
sizeUD60x18The contract amount (18 decimals)
priceUD50x28The current market price (28 decimals)

collateral

Returns the total collateral (18 decimals) held by the position key self. Note that here we do not distinguish between ask- and bid-side collateral. This increases the capital efficiency of the range order

function collateral(KeyInternal memory self, UD60x18 size, UD50x28 price) internal pure returns (UD60x18 _collateral);

Parameters

NameTypeDescription
selfKeyInternal
sizeUD60x18The contract amount (18 decimals)
priceUD50x28The current market price (28 decimals)

contracts

Returns the total contracts (18 decimals) held by the position key self

function contracts(KeyInternal memory self, UD60x18 size, UD50x28 price) internal pure returns (UD60x18);

Parameters

NameTypeDescription
selfKeyInternal
sizeUD60x18The contract amount (18 decimals)
priceUD50x28The current market price (28 decimals)

long

Returns the number of long contracts (18 decimals) held in position self at current price

function long(KeyInternal memory self, UD60x18 size, UD50x28 price) internal pure returns (UD60x18);

Parameters

NameTypeDescription
selfKeyInternal
sizeUD60x18The contract amount (18 decimals)
priceUD50x28The current market price (28 decimals)

short

Returns the number of short contracts (18 decimals) held in position self at current price

function short(KeyInternal memory self, UD60x18 size, UD50x28 price) internal pure returns (UD60x18);

Parameters

NameTypeDescription
selfKeyInternal
sizeUD60x18The contract amount (18 decimals)
priceUD50x28The current market price (28 decimals)

calculatePositionUpdate

Calculate the update for the Position. Either increments them in case withdraw is False (i.e. in case there is a deposit) and otherwise decreases them. Returns the change in collateral, longs, shorts. These are transferred to (withdrawal)or transferred from (deposit) Position.operator.

function calculatePositionUpdate(KeyInternal memory self, UD60x18 currentBalance, SD59x18 amount, UD50x28 price)
    internal
    pure
    returns (Delta memory delta);

Parameters

NameTypeDescription
selfKeyInternal
currentBalanceUD60x18The current balance of tokens (18 decimals)
amountSD59x18The number of tokens deposited or withdrawn (18 decimals)
priceUD50x28The current market price, used to compute the change in collateral, long and shorts due to the change in tokens (28 decimals)

Returns

NameTypeDescription
deltaDeltaAbsolute change in collateral / longs / shorts due to change in tokens

revertIfLowerGreaterOrEqualUpper

Revert if lower is greater or equal to upper

function revertIfLowerGreaterOrEqualUpper(UD60x18 lower, UD60x18 upper) internal pure;

Structs

Key

struct Key {
    address owner;
    address operator;
    UD60x18 lower;
    UD60x18 upper;
    OrderType orderType;
}

KeyInternal

All the data used to calculate the key of the position

struct KeyInternal {
    address owner;
    address operator;
    UD60x18 lower;
    UD60x18 upper;
    OrderType orderType;
    bool isCall;
    UD60x18 strike;
}

Data

All the data required to be saved in storage

struct Data {
    SD49x28 lastFeeRate;
    UD60x18 claimableFees;
    uint256 lastDeposit;
}

Delta

struct Delta {
    SD59x18 collateral;
    SD59x18 longs;
    SD59x18 shorts;
}

Enums

OrderType

The order type of a position

enum OrderType {
    CSUP,
    CS,
    LC
}