Position
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
Name | Type | Description |
---|---|---|
self | Key | |
strike | UD60x18 | The strike of the option (18 decimals) |
isCall | bool |
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
Name | Type | Description |
---|---|---|
_collateral | UD60x18 | |
strike | UD60x18 | The strike price (18 decimals) |
isCall | bool |
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
Name | Type | Description |
---|---|---|
_contracts | UD60x18 | |
strike | UD60x18 | The strike price (18 decimals) |
isCall | bool |
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
Name | Type | Description |
---|---|---|
_collateral | UD50x28 | |
strike | UD60x18 | The strike price (18 decimals) |
isCall | bool |
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
Name | Type | Description |
---|---|---|
_contracts | UD50x28 | |
strike | UD60x18 | The strike price (18 decimals) |
isCall | bool |
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
Name | Type | Description |
---|---|---|
self | KeyInternal | |
size | UD60x18 | The 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
Name | Type | Description |
---|---|---|
self | KeyInternal | The internal position key |
size | UD60x18 | The contract amount (18 decimals) |
price | UD50x28 | The 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
Name | Type | Description |
---|---|---|
self | KeyInternal | |
size | UD60x18 | The contract amount (18 decimals) |
price | UD50x28 | The 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
Name | Type | Description |
---|---|---|
self | KeyInternal | |
size | UD60x18 | The contract amount (18 decimals) |
price | UD50x28 | The 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
Name | Type | Description |
---|---|---|
self | KeyInternal | |
size | UD60x18 | The contract amount (18 decimals) |
price | UD50x28 | The 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
Name | Type | Description |
---|---|---|
self | KeyInternal | |
size | UD60x18 | The contract amount (18 decimals) |
price | UD50x28 | The 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
Name | Type | Description |
---|---|---|
self | KeyInternal | |
currentBalance | UD60x18 | The current balance of tokens (18 decimals) |
amount | SD59x18 | The number of tokens deposited or withdrawn (18 decimals) |
price | UD50x28 | The current market price, used to compute the change in collateral, long and shorts due to the change in tokens (28 decimals) |
Returns
Name | Type | Description |
---|---|---|
delta | Delta | Absolute 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
}