UsdnProtocolVaultLibrary

Git Source

Functions

initiateDeposit

See initiateDeposit.

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

validateDeposit

See validateDeposit.

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

initiateWithdrawal

See initiateWithdrawal.

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

validateWithdrawal

See validateWithdrawal.

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

getActionablePendingActions

See getActionablePendingActions.

function getActionablePendingActions(address currentUser, uint256 lookAhead, uint256 maxIter)
    external
    view
    returns (Types.PendingAction[] memory actions_, uint128[] memory rawIndices_);

usdnPrice

See usdnPrice.

function usdnPrice(uint128 currentPrice) external view returns (uint256 price_);

usdnPrice

See usdnPrice.

function usdnPrice(uint128 currentPrice, uint128 timestamp) public view returns (uint256 price_);

vaultAssetAvailableWithFunding

See vaultAssetAvailableWithFunding.

function vaultAssetAvailableWithFunding(uint128 currentPrice, uint128 timestamp)
    public
    view
    returns (uint256 available_);

_executePendingActionOrRevert

Executes the first actionable pending action.

Will revert if the corresponding price data is invalid.

function _executePendingActionOrRevert(Types.PreviousActionsData calldata data)
    public
    returns (uint256 securityDepositValue_);

Parameters

NameTypeDescription
dataTypes.PreviousActionsDataThe price data and corresponding raw indices.

Returns

NameTypeDescription
securityDepositValue_uint256The security deposit value of the executed action.

_executePendingAction

Executes the first actionable pending action and reports the outcome.

function _executePendingAction(Types.PreviousActionsData calldata data)
    public
    returns (bool success_, bool executed_, bool liquidated_, uint256 securityDepositValue_);

Parameters

NameTypeDescription
dataTypes.PreviousActionsDataThe price data and corresponding raw indices.

Returns

NameTypeDescription
success_boolWhether the price data is valid.
executed_boolWhether the pending action was executed (false if the queue has no actionable item).
liquidated_boolWhether the position corresponding to the pending action was liquidated.
securityDepositValue_uint256The security deposit value of the executed action.

_getActionablePendingAction

This is the mutating version of getActionablePendingActions, where empty items at the front of the list are removed.

function _getActionablePendingAction() internal returns (Types.PendingAction memory action_, uint128 rawIndex_);

Returns

NameTypeDescription
action_Types.PendingActionThe first actionable pending action if any, otherwise a struct with all fields set to zero and ProtocolAction's None action.
rawIndex_uint128The raw index in the queue for the returned pending action, or zero if empty.

_prepareInitiateDepositData

Prepares the data for the initiateDeposit function.

Updates the protocol's balances if the price is fresh.

function _prepareInitiateDepositData(
    address validator,
    uint128 amount,
    uint256 sharesOutMin,
    bytes calldata currentPriceData
) internal returns (InitiateDepositData memory data_);

Parameters

NameTypeDescription
validatoraddressThe address that is supposed to validate the deposit and receive the security deposit.
amountuint128The amount of asset to deposit.
sharesOutMinuint256The minimum amount of USDN shares to receive.
currentPriceDatabytesThe current price data.

Returns

NameTypeDescription
data_InitiateDepositDataThe transient data for the deposit action.

_createDepositPendingAction

Prepares the pending action struct for a deposit and adds it to the queue.

function _createDepositPendingAction(
    address to,
    address validator,
    uint64 securityDepositValue,
    uint128 amount,
    InitiateDepositData memory data
) internal returns (uint256 amountToRefund_);

Parameters

NameTypeDescription
toaddressThe recipient of the minted USDN.
validatoraddressThe address that is supposed to validate the deposit and receive the security deposit.
securityDepositValueuint64The value of the security deposit for the newly created pending action.
amountuint128The amount of assets to deposit (before fees).
dataInitiateDepositDataThe deposit action data.

Returns

NameTypeDescription
amountToRefund_uint256The security deposit value of a stale pending action.

_initiateDeposit

Attempts to initiate a deposit of assets into the vault to mint USDN.

Consults the current oracle middleware implementation to know the expected format for the price data, using the ProtocolAction's InitiateDeposit action. The price validation might require payment according to the return value of the validationCost function of the middleware.

function _initiateDeposit(InitiateDepositParams memory params, bytes calldata currentPriceData)
    internal
    returns (uint256 amountToRefund_, bool isInitiated_);

Parameters

NameTypeDescription
paramsInitiateDepositParamsThe parameters for the deposit.
currentPriceDatabytesThe current price data.

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.

_validateDeposit

Attempts to validate the deposit pending action assigned to the given validator.

If successful, the pending action will be cleared from the queue.

function _validateDeposit(address validator, bytes calldata priceData)
    internal
    returns (uint256 securityDepositValue_, bool isValidated_);

Parameters

NameTypeDescription
validatoraddressThe address that is supposed to validate the deposit and receive the security deposit.
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.

_validateDepositWithAction

Attempts to validate the given deposit pending action.

function _validateDepositWithAction(Types.PendingAction memory pending, bytes calldata priceData)
    internal
    returns (bool isValidated_);

Parameters

NameTypeDescription
pendingTypes.PendingActionThe pending action to validate.
priceDatabytesThe corresponding price data.

Returns

NameTypeDescription
isValidated_boolWhether the action is validated.

_prepareWithdrawalData

Prepares the data for the initiateWithdrawal function.

Updates the protocol's balances if the price is fresh.

function _prepareWithdrawalData(
    address validator,
    uint152 usdnShares,
    uint256 amountOutMin,
    bytes calldata currentPriceData
) internal returns (WithdrawalData memory data_);

Parameters

NameTypeDescription
validatoraddressThe address that is supposed to validate the withdrawal and receive the security deposit.
usdnSharesuint152The amount of USDN shares to burn.
amountOutMinuint256The estimated minimum amount of assets to receive.
currentPriceDatabytesThe current price data.

Returns

NameTypeDescription
data_WithdrawalDataThe transient data for the withdrawal action.

_initiateWithdrawal

Initiates a withdrawal of assets from the vault by providing USDN tokens.

Consults the current oracle middleware implementation to know the expected format for the price data, using the ProtocolAction's InitiateWithdrawal action. The price validation might require payment according to the return value of the validationCost function of the middleware.

function _initiateWithdrawal(WithdrawalParams memory params, bytes calldata currentPriceData)
    internal
    returns (uint256 amountToRefund_, bool isInitiated_);

Parameters

NameTypeDescription
paramsWithdrawalParamsThe parameters for the withdrawal.
currentPriceDatabytesThe current price data.

Returns

NameTypeDescription
amountToRefund_uint256If there are pending liquidations we'll refund the sent security deposit, else we'll only refund the security deposit value of the stale pending action.
isInitiated_boolWhether the action is initiated.

_validateWithdrawal

Attempts to validate the withdrawal pending action assigned to the given validator.

If successful, the pending action will be cleared from the queue.

function _validateWithdrawal(address validator, bytes calldata priceData)
    internal
    returns (uint256 securityDepositValue_, bool isValidated_);

Parameters

NameTypeDescription
validatoraddressThe address that is supposed to validate the withdrawal and receive the security deposit.
priceDatabytesThe corresponding price data.

Returns

NameTypeDescription
securityDepositValue_uint256The value of the security deposit.
isValidated_boolWhether the action is validated.

_validateWithdrawalWithAction

Attempts to validate the given withdrawal pending action.

function _validateWithdrawalWithAction(Types.PendingAction memory pending, bytes calldata priceData)
    internal
    returns (bool isValidated_);

Parameters

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

Returns

NameTypeDescription
isValidated_boolWhether the action is validated.

_isActionable

Checks whether a pending action is actionable, i.e any user can validate it and retrieve 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 with 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_boolWhether the pending action is actionable.

_vaultAssetAvailable

Calculates the available balance in the vault if the price moves to currentPrice.

The funding is not taken into account.

function _vaultAssetAvailable(uint128 currentPrice) internal view returns (int256 available_);

Parameters

NameTypeDescription
currentPriceuint128The current or predicted price.

Returns

NameTypeDescription
available_int256The available balance in the vault.

_calcUsdnPrice

Calculates the price of the USDN token as a function of its total supply, the vault balance and the underlying asset price.

function _calcUsdnPrice(uint256 vaultBalance, uint128 assetPrice, uint256 usdnTotalSupply, uint8 assetDecimals)
    internal
    pure
    returns (uint256 price_);

Parameters

NameTypeDescription
vaultBalanceuint256The vault balance.
assetPriceuint128The price of the asset.
usdnTotalSupplyuint256The total supply of the USDN token.
assetDecimalsuint8The number of decimals of the underlying asset.

Returns

NameTypeDescription
price_uint256The price of the USDN token.

_calcWithdrawalAmountLSB

Calculates the lower 24 bits of the withdrawal amount (USDN shares).

function _calcWithdrawalAmountLSB(uint152 usdnShares) internal pure returns (uint24 sharesLSB_);

Parameters

NameTypeDescription
usdnSharesuint152The amount of USDN shares.

Returns

NameTypeDescription
sharesLSB_uint24The 24 least significant bits of the USDN shares.

_calcWithdrawalAmountMSB

Calculates the higher 128 bits of the withdrawal amount (USDN shares).

function _calcWithdrawalAmountMSB(uint152 usdnShares) internal pure returns (uint128 sharesMSB_);

Parameters

NameTypeDescription
usdnSharesuint152The amount of USDN shares.

Returns

NameTypeDescription
sharesMSB_uint128The 128 most significant bits of the USDN shares.

Structs

InitiateDepositParams

Parameters for the internal _initiateDeposit function. The user's input for the minimum amount of shares to receive is not guaranteed due to the price difference between the initiate and validate actions.

struct InitiateDepositParams {
    address user;
    address to;
    address validator;
    uint128 amount;
    uint256 sharesOutMin;
    uint64 securityDepositValue;
}

Properties

NameTypeDescription
useraddressThe address of the user initiating the deposit.
toaddressThe recipient of the USDN tokens.
validatoraddressThe address that is supposed to validate the deposit and receive the security deposit.
amountuint128The amount of assets to deposit.
sharesOutMinuint256The minimum amount of USDN shares to receive.
securityDepositValueuint64The value of the security deposit for the newly created deposit.

InitiateDepositData

Structure to hold the transient data during _initiateDeposit.

struct InitiateDepositData {
    uint128 lastPrice;
    bool isLiquidationPending;
    uint16 feeBps;
    uint256 totalExpo;
    uint256 balanceLong;
    uint256 balanceVault;
    uint256 usdnTotalShares;
    uint256 sdexToBurn;
}

Properties

NameTypeDescription
lastPriceuint128The last known price of the asset.
isLiquidationPendingboolWhether some liquidations still need to be performed.
feeBpsuint16The vault deposit fee (in basis points).
totalExpouint256The total exposure of the long side.
balanceLonguint256The balance of the long side.
balanceVaultuint256The balance of the vault including the funding.
usdnTotalSharesuint256Total minted shares of USDN.
sdexToBurnuint256The required amount of SDEX to burn for the deposit.

WithdrawalParams

Parameters for the internal _initiateWithdrawal function.

struct WithdrawalParams {
    address user;
    address to;
    address validator;
    uint152 usdnShares;
    uint256 amountOutMin;
    uint64 securityDepositValue;
}

Properties

NameTypeDescription
useraddressThe address of the user initiating the withdrawal.
toaddressThe recipient of the assets.
validatoraddressThe address that is supposed to validate the withdrawal and receive the security deposit.
usdnSharesuint152The amount of USDN shares to withdraw.
amountOutMinuint256The minimum amount of assets to receive.
securityDepositValueuint64The value of the security deposit for the newly created withdrawal.

WithdrawalData

Structure to hold the transient data during _initiateWithdrawal.

struct WithdrawalData {
    uint256 usdnTotalShares;
    uint256 totalExpo;
    uint256 balanceLong;
    uint256 balanceVault;
    uint256 withdrawalAmountAfterFees;
    uint128 lastPrice;
    uint16 feeBps;
    bool isLiquidationPending;
}

Properties

NameTypeDescription
usdnTotalSharesuint256The total supply of USDN shares.
totalExpouint256The current total exposure.
balanceLonguint256The balance of the long side.
balanceVaultuint256The balance of the vault including the funding.
withdrawalAmountAfterFeesuint256The predicted amount of assets that will be withdrawn after fees.
lastPriceuint128The last known price of the asset.
feeBpsuint16The vault deposit fee (in basis points).
isLiquidationPendingboolWhether some liquidations still need to be performed.