UsdnProtocolActionsLongLibrary
Functions
initiateOpenPosition
See initiateOpenPosition.
function initiateOpenPosition(
Types.InitiateOpenPositionParams memory params,
bytes calldata currentPriceData,
Types.PreviousActionsData calldata previousActionsData
) external returns (bool isInitiated_, Types.PositionId memory posId_);
validateOpenPosition
See validateOpenPosition.
function validateOpenPosition(
address payable validator,
bytes calldata openPriceData,
Types.PreviousActionsData calldata previousActionsData
) external returns (Types.LongActionOutcome outcome_, Types.PositionId memory posId_);
initiateClosePosition
function initiateClosePosition(
Types.InitiateClosePositionParams memory params,
bytes calldata currentPriceData,
Types.PreviousActionsData calldata previousActionsData,
bytes calldata delegationSignature
) external returns (Types.LongActionOutcome outcome_);
validateClosePosition
function validateClosePosition(
address payable validator,
bytes calldata closePriceData,
Types.PreviousActionsData calldata previousActionsData
) external returns (Types.LongActionOutcome outcome_);
_validateOpenPositionWithAction
Validates an open position action.
function _validateOpenPositionWithAction(Types.PendingAction memory pending, bytes calldata priceData)
public
returns (bool isValidated_, bool isLiquidated_, Types.PositionId memory posId_);
Parameters
Name | Type | Description |
---|---|---|
pending | Types.PendingAction | The pending action's data. |
priceData | bytes | The current price data. |
Returns
Name | Type | Description |
---|---|---|
isValidated_ | bool | Whether the action is validated. |
isLiquidated_ | bool | Whether the pending action is liquidated. |
posId_ | Types.PositionId | The (potentially updated) position ID, or NO_POSITION_TICK in the tick field if the position was liquidated. |
_initiateOpenPosition
Initiates an open position action.
Consult the current oracle middleware implementation to know the expected format for the price data, using
the ProtocolAction's InitiateOpenPosition
action.
The price validation might require payment according to the return value of the
validationCost function of the middleware.
The position is immediately included in the protocol calculations with a temporary entry price (and thus
leverage). The validation operation then updates the entry price and leverage with fresher data.
function _initiateOpenPosition(Types.InitiateOpenPositionParams memory params, bytes calldata currentPriceData)
internal
returns (Types.PositionId memory posId_, uint256 amountToRefund_, bool isInitiated_);
Parameters
Name | Type | Description |
---|---|---|
params | Types.InitiateOpenPositionParams | The parameters for the open position initiation. |
currentPriceData | bytes | The current price data. |
Returns
Name | Type | Description |
---|---|---|
posId_ | Types.PositionId | The unique index of the opened position. |
amountToRefund_ | uint256 | If there are pending liquidations we'll refund the securityDepositValue , else we'll only refund the security deposit value of the stale pending action. |
isInitiated_ | bool | Whether the action is initiated. |
_validateOpenPosition
Retrieves the pending action data of the owner, try to validate it and clear it if successful.
function _validateOpenPosition(address validator, bytes calldata priceData)
internal
returns (uint256 securityDepositValue_, bool isValidated_, bool isLiquidated_, Types.PositionId memory posId_);
Parameters
Name | Type | Description |
---|---|---|
validator | address | The address of the validator. |
priceData | bytes | The price data for the pending action to validate. |
Returns
Name | Type | Description |
---|---|---|
securityDepositValue_ | uint256 | The value of the security deposit to refund. |
isValidated_ | bool | Whether the action is validated. |
isLiquidated_ | bool | Whether the pending action is liquidated. |
posId_ | Types.PositionId | The (potentially updated) position ID, or NO_POSITION_TICK in the tick field if the position was liquidated. |
_validateOpenPositionUpdateBalances
Updates the protocol balances during validateOpenPosition to reflect the new entry price of the position.
We need to adjust the balances because the position that was created during the initiateOpenPosition might
have gained or lost some value, and we need to reflect that the position value is now newPosValue
.
Any potential PnL on that temporary position must be "cancelled" so that it doesn't affect the other positions
and the vault.
function _validateOpenPositionUpdateBalances(uint256 newPosValue, uint256 oldPosValue) internal;
Parameters
Name | Type | Description |
---|---|---|
newPosValue | uint256 | The new value of the position. |
oldPosValue | uint256 | The value of the position at the current price, using its old parameters. |
_prepareValidateOpenPositionData
Updates protocol balances, liquidate positions if necessary, then validate the open position action.
function _prepareValidateOpenPositionData(Types.PendingAction memory pending, bytes calldata priceData)
internal
returns (Types.ValidateOpenPositionData memory data_, bool isLiquidated_);
Parameters
Name | Type | Description |
---|---|---|
pending | Types.PendingAction | The pending action data. |
priceData | bytes | The price data for the pending action. |
Returns
Name | Type | Description |
---|---|---|
data_ | Types.ValidateOpenPositionData | The ValidateOpenPositionData data structure. |
isLiquidated_ | bool | Whether the position is liquidated. |
_initiateClosePosition
Initiates a close position action.
Consult the current oracle middleware implementation to know the expected format for the price data, using
the ProtocolAction's InitiateClosePosition
action.
The price validation might require payment according to the return value of the
validationCost function of the middleware.
If the current tick version is greater than the tick version of the position (when it was opened), then the
position has been liquidated and this function will return 0.
The position is taken out of the tick and put in a pending state during this operation. Thus, calculations don't
consider this position anymore. The exit price (and thus profit) is not yet set definitively and will be done
during the validate
action.
function _initiateClosePosition(
Types.InitiateClosePositionParams memory params,
bytes calldata currentPriceData,
bytes calldata delegationSignature
) internal returns (uint256 amountToRefund_, bool isInitiated_, bool isLiquidated_);
Parameters
Name | Type | Description |
---|---|---|
params | Types.InitiateClosePositionParams | The parameters for the close position initiation. |
currentPriceData | bytes | The current price data. |
delegationSignature | bytes | An EIP712 signature that proves the caller is authorized by the owner of the position to close it on their behalf. |
Returns
Name | Type | Description |
---|---|---|
amountToRefund_ | uint256 | If there are pending liquidations we'll refund the securityDepositValue , else we'll only refund the security deposit value of the stale pending action. |
isInitiated_ | bool | Whether the action is initiated. |
isLiquidated_ | bool | Whether the position got liquidated by this call. |
_validateClosePosition
Retrieves the pending action data of the validator, try to validate it and clear it if successful.
function _validateClosePosition(address validator, bytes calldata priceData)
internal
returns (uint256 securityDepositValue_, bool isValidated_, bool isLiquidated_);
Parameters
Name | Type | Description |
---|---|---|
validator | address | The validator of the pending action. |
priceData | bytes | The price data for the validator's pending action. |
Returns
Name | Type | Description |
---|---|---|
securityDepositValue_ | uint256 | The value of the security deposit of the pending action. |
isValidated_ | bool | Whether the action is validated. |
isLiquidated_ | bool | Whether the pending action is liquidated. |
_validateClosePositionWithAction
Updates protocol balances, liquidate positions if necessary, then validate the close position action.
function _validateClosePositionWithAction(Types.PendingAction memory pending, bytes calldata priceData)
internal
returns (bool isValidated_, bool isLiquidated_);
Parameters
Name | Type | Description |
---|---|---|
pending | Types.PendingAction | The pending action data. |
priceData | bytes | The price data for the action to validate. |
Returns
Name | Type | Description |
---|---|---|
isValidated_ | bool | Whether the action is validated. |
isLiquidated_ | bool | Whether the pending action is liquidated. |
Structs
ValidateClosePositionWithActionData
Data structure for the _validateClosePositionWithAction function.
struct ValidateClosePositionWithActionData {
bool isLiquidationPending;
uint128 priceWithFees;
uint128 liquidationPrice;
int256 positionValue;
}
Properties
Name | Type | Description |
---|---|---|
isLiquidationPending | bool | Whether a liquidation is pending. |
priceWithFees | uint128 | The price of the position with fees. |
liquidationPrice | uint128 | The liquidation price of the position. |
positionValue | int256 | The value of the position. The amount the user will receive when closing the position. |
MaxLeverageData
Data structure for the _validateOpenPositionWithAction function.
struct MaxLeverageData {
uint24 currentLiqPenalty;
Types.PositionId newPosId;
uint24 liquidationPenalty;
}
Properties
Name | Type | Description |
---|---|---|
currentLiqPenalty | uint24 | The current liquidation penalty parameter value. |
newPosId | Types.PositionId | The new position id. |
liquidationPenalty | uint24 | The liquidation penalty of the tick we are considering. |