UsdnProtocolActions

Git Source

Inherits: IUsdnProtocolActions, InitializableReentrancyGuard, PausableUpgradeable, EIP712Upgradeable

Functions

initiateOpenPosition

Initiates an open position action.

Consult the current oracle middleware for price data format and possible oracle fee. Requires _securityDepositValue to be included in the transaction value. In case of pending liquidations, this function will not initiate the position (isInitiated_ would be false). The user's input for price and leverage is not guaranteed due to the price difference between the initiate and validate actions.

function initiateOpenPosition(
    uint128 amount,
    uint128 desiredLiqPrice,
    uint128 userMaxPrice,
    uint256 userMaxLeverage,
    address to,
    address payable validator,
    uint256 deadline,
    bytes calldata currentPriceData,
    PreviousActionsData calldata previousActionsData
) external payable whenNotPaused initializedAndNonReentrant returns (bool isInitiated_, PositionId memory posId_);

Parameters

NameTypeDescription
amountuint128The amount of assets to deposit.
desiredLiqPriceuint128The desired liquidation price, including the penalty.
userMaxPriceuint128The user's wanted maximum price at which the position can be opened.
userMaxLeverageuint256The user's wanted maximum leverage for the new position.
toaddressThe address that will owns of the position.
validatoraddress payableThe address that is supposed to validate the opening and receive the security deposit. If not an EOA, it must be a contract that implements a receive function.
deadlineuint256The deadline for initiating the open position.
currentPriceDatabytesThe price data used for temporary leverage and entry price computations.
previousActionsDataPreviousActionsDataThe data needed to validate actionable pending actions.

Returns

NameTypeDescription
isInitiated_boolWhether the position was successfully initiated. If false, the security deposit was refunded
posId_PositionIdThe unique position identifier. If the position was not initiated, the tick number will be NO_POSITION_TICK.

validateOpenPosition

Validates a pending open position action.

Consult the current oracle middleware for price data format and possible oracle fee. It is possible for this operation to change the tick, tick version and index of the position, in which case we emit the LiquidationPriceUpdated event. This function always sends the security deposit to the validator. So users wanting to earn the corresponding security deposit must use validateActionablePendingActions. In case liquidations are pending (outcome_ == LongActionOutcome.PendingLiquidations), the pending action will not be removed from the queue, and the user will have to try again. In case the position was liquidated by this call (outcome_ == LongActionOutcome.Liquidated), this function will refund the security deposit and remove the pending action from the queue.

function validateOpenPosition(
    address payable validator,
    bytes calldata openPriceData,
    PreviousActionsData calldata previousActionsData
)
    external
    payable
    whenNotPaused
    initializedAndNonReentrant
    returns (LongActionOutcome outcome_, PositionId memory posId_);

Parameters

NameTypeDescription
validatoraddress payableThe address associated with the pending open position. If not an EOA, it must be a contract that implements a receive function.
openPriceDatabytesThe price data for the pending open position.
previousActionsDataPreviousActionsDataThe data needed to validate actionable pending actions.

Returns

NameTypeDescription
outcome_LongActionOutcomeThe effect on the pending action (processed, liquidated, or pending liquidations).
posId_PositionIdThe position ID after validation (or NO_POSITION_TICK if liquidated).

initiateClosePosition

Initiates a close position action.

Consult the current oracle middleware for price data format and possible oracle fee. Requires _securityDepositValue to be included in the transaction value. 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 the transaction will revert. In case liquidations are pending (outcome_ == LongActionOutcome.PendingLiquidations), the pending action will not be removed from the queue, and the user will have to try again. In case the position was liquidated by this call (outcome_ == LongActionOutcome.Liquidated), this function will refund the security deposit and remove the pending action from the queue. The user's input for the price is not guaranteed due to the price difference between the initiate and validate actions.

function initiateClosePosition(
    PositionId calldata posId,
    uint128 amountToClose,
    uint256 userMinPrice,
    address to,
    address payable validator,
    uint256 deadline,
    bytes calldata currentPriceData,
    PreviousActionsData calldata previousActionsData,
    bytes calldata delegationSignature
) external payable whenNotPaused initializedAndNonReentrant returns (LongActionOutcome outcome_);

Parameters

NameTypeDescription
posIdPositionIdThe unique identifier of the position to close.
amountToCloseuint128The amount of collateral to remove.
userMinPriceuint256The user's wanted minimum price for closing the position.
toaddressThe address that will receive the assets.
validatoraddress payableThe address that is supposed to validate the closing and receive the security deposit. If not an EOA, it must be a contract that implements a receive function.
deadlineuint256The deadline for initiating the close position.
currentPriceDatabytesThe price data for temporary calculations.
previousActionsDataPreviousActionsDataThe data needed to validate actionable pending actions.
delegationSignaturebytesOptional EIP712 signature for delegated action.

Returns

NameTypeDescription
outcome_LongActionOutcomeThe effect on the pending action (processed, liquidated, or pending liquidations).

validateClosePosition

Validates a pending close position action.

Consult the current oracle middleware for price data format and possible oracle fee. This function calculates the final exit price, determines the profit of the long position, and performs the payout. This function always sends the security deposit to the validator. So users wanting to earn the corresponding security deposit must use validateActionablePendingActions. In case liquidations are pending (outcome_ == LongActionOutcome.PendingLiquidations), the pending action will not be removed from the queue, and the user will have to try again. In case the position was liquidated by this call (outcome_ == LongActionOutcome.Liquidated), this function will refund the security deposit and remove the pending action from the queue.

function validateClosePosition(
    address payable validator,
    bytes calldata closePriceData,
    PreviousActionsData calldata previousActionsData
) external payable whenNotPaused initializedAndNonReentrant returns (LongActionOutcome outcome_);

Parameters

NameTypeDescription
validatoraddress payableThe address associated with the pending close position. If not an EOA, it must be a contract that implements a receive function.
closePriceDatabytesThe price data for the pending close position action.
previousActionsDataPreviousActionsDataThe data required to validate actionable pending actions.

Returns

NameTypeDescription
outcome_LongActionOutcomeThe outcome of the action (processed, liquidated, or pending liquidations).

initiateDeposit

Initiates a deposit of assets into the vault to mint USDN.

Consult the current oracle middleware for price data format and possible oracle fee. Requires _securityDepositValue to be included in the transaction value. In case liquidations are pending, this function might not initiate the deposit, and success_ would be false. The user's input for the shares is not guaranteed due to the price difference between the initiate and validate actions.

function initiateDeposit(
    uint128 amount,
    uint256 sharesOutMin,
    address to,
    address payable validator,
    uint256 deadline,
    bytes calldata currentPriceData,
    PreviousActionsData calldata previousActionsData
) external payable whenNotPaused initializedAndNonReentrant returns (bool success_);

Parameters

NameTypeDescription
amountuint128The amount of assets to deposit.
sharesOutMinuint256The minimum amount of USDN shares to receive.
toaddressThe address that will receive the USDN tokens.
validatoraddress payableThe address that is supposed to validate the deposit and receive the security deposit. If not an EOA, it must be a contract that implements a receive function.
deadlineuint256The deadline for initiating the deposit.
currentPriceDatabytesThe current price data.
previousActionsDataPreviousActionsDataThe data required to validate actionable pending actions.

Returns

NameTypeDescription
success_boolIndicates whether the deposit was successfully initiated.

validateDeposit

Validates a pending deposit action.

Consult the current oracle middleware for price data format and possible oracle fee. This function always sends the security deposit to the validator. So users wanting to earn the corresponding security deposit must use validateActionablePendingActions. If liquidations are pending, the validation may fail, and success_ would be false.

function validateDeposit(
    address payable validator,
    bytes calldata depositPriceData,
    PreviousActionsData calldata previousActionsData
) external payable whenNotPaused initializedAndNonReentrant returns (bool success_);

Parameters

NameTypeDescription
validatoraddress payableThe address associated with the pending deposit action. If not an EOA, it must be a contract that implements a receive function.
depositPriceDatabytesThe price data for the pending deposit action.
previousActionsDataPreviousActionsDataThe data required to validate actionable pending actions.

Returns

NameTypeDescription
success_boolIndicates whether the deposit was successfully validated.

initiateWithdrawal

Initiates a withdrawal of assets from the vault using USDN tokens.

Consult the current oracle middleware for price data format and possible oracle fee. Requires _securityDepositValue to be included in the transaction value. Note that in case liquidations are pending, this function might not initiate the withdrawal, and success_ would be false. The user's input for the minimum amount is not guaranteed due to the price difference between the initiate and validate actions.

function initiateWithdrawal(
    uint152 usdnShares,
    uint256 amountOutMin,
    address to,
    address payable validator,
    uint256 deadline,
    bytes calldata currentPriceData,
    PreviousActionsData calldata previousActionsData
) external payable whenNotPaused initializedAndNonReentrant returns (bool success_);

Parameters

NameTypeDescription
usdnSharesuint152The amount of USDN shares to burn.
amountOutMinuint256The minimum amount of assets to receive.
toaddressThe address that will receive the assets.
validatoraddress payableThe address that is supposed to validate the withdrawal and receive the security deposit. If not an EOA, it must be a contract that implements a receive function.
deadlineuint256The deadline for initiating the withdrawal.
currentPriceDatabytesThe current price data.
previousActionsDataPreviousActionsDataThe data required to validate actionable pending actions.

Returns

NameTypeDescription
success_boolIndicates whether the withdrawal was successfully initiated.

validateWithdrawal

Validates a pending withdrawal action.

Consult the current oracle middleware for price data format and possible oracle fee. This function always sends the security deposit to the validator. So users wanting to earn the corresponding security deposit must use validateActionablePendingActions. In case liquidations are pending, this function might not validate the withdrawal, and success_ would be false.

function validateWithdrawal(
    address payable validator,
    bytes calldata withdrawalPriceData,
    PreviousActionsData calldata previousActionsData
) external payable whenNotPaused initializedAndNonReentrant returns (bool success_);

Parameters

NameTypeDescription
validatoraddress payableThe address associated with the pending withdrawal action. If not an EOA, it must be a contract that implements a receive function.
withdrawalPriceDatabytesThe price data for the pending withdrawal action.
previousActionsDataPreviousActionsDataThe data required to validate actionable pending actions.

Returns

NameTypeDescription
success_boolIndicates whether the withdrawal was successfully validated.

liquidate

Liquidates positions based on the provided asset price.

Consult the current oracle middleware for price data format and possible oracle fee. Each tick is liquidated in constant time. The tick version is incremented for each liquidated tick.

function liquidate(bytes calldata currentPriceData)
    external
    payable
    whenNotPaused
    initializedAndNonReentrant
    returns (LiqTickInfo[] memory liquidatedTicks_);

Parameters

NameTypeDescription
currentPriceDatabytesThe price data.

Returns

NameTypeDescription
liquidatedTicks_LiqTickInfo[]Information about the liquidated ticks.

validateActionablePendingActions

Manually validates actionable pending actions.

Consult the current oracle middleware for price data format and possible oracle fee. The timestamp for each pending action is calculated by adding the OracleMiddleware.validationDelay to its initiation timestamp.

function validateActionablePendingActions(PreviousActionsData calldata previousActionsData, uint256 maxValidations)
    external
    payable
    whenNotPaused
    initializedAndNonReentrant
    returns (uint256 validatedActions_);

Parameters

NameTypeDescription
previousActionsDataPreviousActionsDataThe data required to validate actionable pending actions.
maxValidationsuint256The maximum number of actionable pending actions to validate. At least one validation will be performed.

Returns

NameTypeDescription
validatedActions_uint256The number of successfully validated actions.

transferPositionOwnership

Transfers the ownership of a position to another address.

This function reverts if the caller is not the position owner, if the position does not exist, or if the new owner's address is the zero address. If the new owner is a contract that implements the IOwnershipCallback interface, its ownershipCallback function will be invoked after the transfer.

function transferPositionOwnership(PositionId calldata posId, address newOwner, bytes calldata delegationSignature)
    external
    whenNotPaused
    initializedAndNonReentrant;

Parameters

NameTypeDescription
posIdPositionIdThe unique identifier of the position.
newOwneraddressThe address of the new position owner.
delegationSignaturebytesAn optional EIP712 signature to authorize the transfer on the owner's behalf.

domainSeparatorV4

Retrieves the domain separator used in EIP-712 signatures.

function domainSeparatorV4() external view returns (bytes32);

Returns

NameTypeDescription
<none>bytes32domainSeparatorV4_ The domain separator compliant with EIP-712.