UsdnProtocolActionsLongLibrary

Git Source

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

See initiateClosePosition.

function initiateClosePosition(
    Types.InitiateClosePositionParams memory params,
    bytes calldata currentPriceData,
    Types.PreviousActionsData calldata previousActionsData,
    bytes calldata delegationSignature
) external returns (Types.LongActionOutcome outcome_);

validateClosePosition

See 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

NameTypeDescription
pendingTypes.PendingActionThe pending action's data.
priceDatabytesThe current price data.

Returns

NameTypeDescription
isValidated_boolWhether the action is validated.
isLiquidated_boolWhether the pending action is liquidated.
posId_Types.PositionIdThe (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

NameTypeDescription
paramsTypes.InitiateOpenPositionParamsThe parameters for the open position initiation.
currentPriceDatabytesThe current price data.

Returns

NameTypeDescription
posId_Types.PositionIdThe unique index of the opened position.
amountToRefund_uint256If there are pending liquidations we'll refund the securityDepositValue, else we'll only refund the security deposit value of the stale pending action.
isInitiated_boolWhether 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

NameTypeDescription
validatoraddressThe address of the validator.
priceDatabytesThe price data for the pending action to validate.

Returns

NameTypeDescription
securityDepositValue_uint256The value of the security deposit to refund.
isValidated_boolWhether the action is validated.
isLiquidated_boolWhether the pending action is liquidated.
posId_Types.PositionIdThe (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

NameTypeDescription
newPosValueuint256The new value of the position.
oldPosValueuint256The 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

NameTypeDescription
pendingTypes.PendingActionThe pending action data.
priceDatabytesThe price data for the pending action.

Returns

NameTypeDescription
data_Types.ValidateOpenPositionDataThe ValidateOpenPositionData data structure.
isLiquidated_boolWhether 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

NameTypeDescription
paramsTypes.InitiateClosePositionParamsThe parameters for the close position initiation.
currentPriceDatabytesThe current price data.
delegationSignaturebytesAn EIP712 signature that proves the caller is authorized by the owner of the position to close it on their behalf.

Returns

NameTypeDescription
amountToRefund_uint256If there are pending liquidations we'll refund the securityDepositValue, else we'll only refund the security deposit value of the stale pending action.
isInitiated_boolWhether the action is initiated.
isLiquidated_boolWhether 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

NameTypeDescription
validatoraddressThe validator of the pending action.
priceDatabytesThe price data for the validator's pending action.

Returns

NameTypeDescription
securityDepositValue_uint256The value of the security deposit of the pending action.
isValidated_boolWhether the action is validated.
isLiquidated_boolWhether 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

NameTypeDescription
pendingTypes.PendingActionThe pending action data.
priceDatabytesThe price data for the action to validate.

Returns

NameTypeDescription
isValidated_boolWhether the action is validated.
isLiquidated_boolWhether the pending action is liquidated.

Structs

ValidateClosePositionWithActionData

Data structure for the _validateClosePositionWithAction function.

struct ValidateClosePositionWithActionData {
    bool isLiquidationPending;
    uint128 priceWithFees;
    uint128 liquidationPrice;
    int256 positionValue;
}

Properties

NameTypeDescription
isLiquidationPendingboolWhether a liquidation is pending.
priceWithFeesuint128The price of the position with fees.
liquidationPriceuint128The liquidation price of the position.
positionValueint256The 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

NameTypeDescription
currentLiqPenaltyuint24The current liquidation penalty parameter value.
newPosIdTypes.PositionIdThe new position id.
liquidationPenaltyuint24The liquidation penalty of the tick we are considering.