UsdnProtocolActionsUtilsLibrary

Git Source

Functions

liquidate

See liquidate.

function liquidate(bytes calldata currentPriceData) external returns (Types.LiqTickInfo[] memory liquidatedTicks_);

validateActionablePendingActions

See validateActionablePendingActions.

function validateActionablePendingActions(
    Types.PreviousActionsData calldata previousActionsData,
    uint256 maxValidations
) external returns (uint256 validatedActions_);

transferPositionOwnership

See transferPositionOwnership.

function transferPositionOwnership(
    Types.PositionId calldata posId,
    address newOwner,
    bytes calldata delegationSignature,
    bytes32 domainSeparatorV4
) external;

_checkImbalanceLimitWithdrawal

Checks and reverts if the withdrawn value breaks the imbalance limits.

function _checkImbalanceLimitWithdrawal(uint256 withdrawalValue, uint256 totalExpo) external view;

Parameters

NameTypeDescription
withdrawalValueuint256The withdrawal value in asset.
totalExpouint256The current total exposure of the long side.

_checkImbalanceLimitDeposit

Checks and reverts if the deposited value breaks the imbalance limits.

function _checkImbalanceLimitDeposit(uint256 depositValue) external view;

Parameters

NameTypeDescription
depositValueuint256The deposit value in asset.

getLongPosition

See getLongPosition.

function getLongPosition(Types.PositionId memory posId)
    public
    view
    returns (Types.Position memory pos_, uint24 liquidationPenalty_);

_prepareClosePositionData

Updates the protocol state, then prepares the data for the initiate close position action.

Reverts if the imbalance limit is reached, or if any checks in _checkInitiateClosePosition fail. Returns without creating a pending action if the position gets liquidated in this transaction or if there are still positions pending liquidation.

function _prepareClosePositionData(Types.PrepareInitiateClosePositionParams calldata params)
    public
    returns (Types.ClosePositionData memory data_, bool liquidated_);

Parameters

NameTypeDescription
paramsTypes.PrepareInitiateClosePositionParamsThe parameters for the _prepareClosePositionData function.

Returns

NameTypeDescription
data_Types.ClosePositionDataThe close position data.
liquidated_boolIndicates whether the position was liquidated.

_validateMultipleActionable

Validates multiple actionable pending actions.

function _validateMultipleActionable(Types.PreviousActionsData calldata previousActionsData, uint256 maxValidations)
    internal
    returns (uint256 validatedActions_, uint256 amountToRefund_);

Parameters

NameTypeDescription
previousActionsDataTypes.PreviousActionsDataThe data for the actions to validate (price and raw indices).
maxValidationsuint256The maximum number of validations to perform.

Returns

NameTypeDescription
validatedActions_uint256The number of actions successfully validated.
amountToRefund_uint256The total amount of security deposits to be refunded.

_isActionable

Checks whether a pending action is actionable, allowing any user to validate it and claim the security deposit.

Between initiateTimestamp and initiateTimestamp + lowLatencyDeadline, the validator receives the security deposit. Between initiateTimestamp + lowLatencyDelay and initiateTimestamp + lowLatencyDelay + onChainDeadline, the validator also receives the security deposit. Outside of those periods, the security deposit goes to the user validating the pending action.

function _isActionable(
    uint256 initiateTimestamp,
    uint256 lowLatencyDeadline,
    uint256 lowLatencyDelay,
    uint256 onChainDeadline
) internal view returns (bool actionable_);

Parameters

NameTypeDescription
initiateTimestampuint256The timestamp at which the action was initiated.
lowLatencyDeadlineuint256The deadline after which the action is actionable within a low latency oracle.
lowLatencyDelayuint256The amount of time the action can be validated with a low latency oracle.
onChainDeadlineuint256The deadline after which the action is actionable with an on-chain oracle.

Returns

NameTypeDescription
actionable_boolIndicates whether the pending action is actionable.

_checkImbalanceLimitClose

Checks the close vault imbalance limit state.

Ensures that the protocol does not imbalance more than the close limit on the vault side, otherwise revert.

function _checkImbalanceLimitClose(uint256 posTotalExpoToClose, uint256 posValueToClose) internal view;

Parameters

NameTypeDescription
posTotalExpoToCloseuint256The total exposure to remove from the position.
posValueToCloseuint256The value to remove from the position (and the long balance).

_checkInitiateClosePosition

Performs checks for the initiate close position action.

function _checkInitiateClosePosition(
    Types.Position memory pos,
    Types.PrepareInitiateClosePositionParams calldata params
) internal;

Parameters

NameTypeDescription
posTypes.PositionThe position to close.
paramsTypes.PrepareInitiateClosePositionParamsThe parameters for the _prepareClosePositionData function.

_assetToRemove

Calculates how much assets must be removed from the long balance due to a position closing.

The amount is bound by the amount of assets available on the long side.

function _assetToRemove(uint256 balanceLong, uint128 price, uint128 liqPriceWithoutPenalty, uint128 posExpo)
    internal
    pure
    returns (uint256 boundedPosValue_);

Parameters

NameTypeDescription
balanceLonguint256The balance of the long side.
priceuint128The price to use for the position value calculation.
liqPriceWithoutPenaltyuint128The liquidation price without penalty.
posExpouint128The total exposure to remove from the position.

Returns

NameTypeDescription
boundedPosValue_uint256The amount of assets to remove from the long balance.

_verifyInitiateCloseDelegation

Performs the initiateClosePosition EIP712 delegation signature verification.

Reverts if the function arguments don't match those included in the signature and if the signer isn't the owner of the position.

function _verifyInitiateCloseDelegation(Types.PrepareInitiateClosePositionParams calldata params, address positionOwner)
    internal;

Parameters

NameTypeDescription
paramsTypes.PrepareInitiateClosePositionParamsThe parameters for the _prepareClosePositionData function.
positionOwneraddressThe position owner.

_verifyTransferPositionOwnershipDelegation

Performs the transferPositionOwnership EIP712 delegation signature verification.

Reverts if the function arguments don't match those included in the signature and if the signer isn't the owner of the position.

function _verifyTransferPositionOwnershipDelegation(
    Types.PositionId calldata posId,
    address positionOwner,
    address newPositionOwner,
    bytes calldata delegationSignature,
    bytes32 domainSeparatorV4
) internal;

Parameters

NameTypeDescription
posIdTypes.PositionIdThe unique identifier of the position.
positionOwneraddressThe current position owner.
newPositionOwneraddressThe new position owner.
delegationSignaturebytesAn EIP712 signature that proves the caller is authorized by the owner of the position to transfer the ownership to a different address on his behalf.
domainSeparatorV4bytes32The domain separator v4.

Structs

ValidateMultipleActionableData

Data structure for the transient state of the _validateMultipleActionable function.

struct ValidateMultipleActionableData {
    Types.PendingAction pending;
    uint128 frontRawIndex;
    uint128 rawIndex;
    bool executed;
    bool liq;
}

Properties

NameTypeDescription
pendingTypes.PendingActionThe candidate pending action to validate.
frontRawIndexuint128The raw index of the front of the queue.
rawIndexuint128The raw index of the candidate pending action in the queue.
executedboolIndicates whether the pending action has been executed.
liqboolIndicates whether the pending action has been liquidated.