IRebalancer

Git Source

Inherits: IBaseRebalancer, IRebalancerErrors, IRebalancerEvents, IRebalancerTypes

Functions

MULTIPLIER_FACTOR

Gets the value of the multiplier at 1x.

Also helps to normalize the result of multiplier calculations.

function MULTIPLIER_FACTOR() external view returns (uint256 factor_);

Returns

NameTypeDescription
factor_uint256The multiplier factor.

MAX_ACTION_COOLDOWN

The maximum cooldown time between actions.

function MAX_ACTION_COOLDOWN() external view returns (uint256 cooldown_);

Returns

NameTypeDescription
cooldown_uint256The maximum cooldown time between actions.

INITIATE_CLOSE_TYPEHASH

The EIP712 initiateClosePosition typehash.

By including this hash into the EIP712 message for this domain, this can be used together with ECDSA-recover to obtain the signer of a message.

function INITIATE_CLOSE_TYPEHASH() external view returns (bytes32 typehash_);

Returns

NameTypeDescription
typehash_bytes32The EIP712 initiateClosePosition typehash.

MAX_CLOSE_DELAY

Gets the maximum amount of seconds to wait to execute a initiateClosePosition since a new rebalancer long position has been created.

function MAX_CLOSE_DELAY() external view returns (uint256 closeDelay_);

Returns

NameTypeDescription
closeDelay_uint256The max close delay value.

getAsset

Returns the address of the asset used by the USDN protocol.

function getAsset() external view returns (IERC20Metadata asset_);

Returns

NameTypeDescription
asset_IERC20MetadataThe address of the asset used by the USDN protocol.

getUsdnProtocol

Returns the address of the USDN protocol.

function getUsdnProtocol() external view returns (IUsdnProtocol protocol_);

Returns

NameTypeDescription
protocol_IUsdnProtocolThe address of the USDN protocol.

getPositionVersion

Returns the version of the current position (0 means no position open).

function getPositionVersion() external view returns (uint128 version_);

Returns

NameTypeDescription
version_uint128The version of the current position.

getPositionMaxLeverage

Returns the maximum leverage the rebalancer position can have.

In some edge cases during the calculation of the rebalancer position's tick, this value might be exceeded by a slight margin.

Returns the max leverage of the USDN Protocol if it's lower than the rebalancer's.

function getPositionMaxLeverage() external view returns (uint256 maxLeverage_);

Returns

NameTypeDescription
maxLeverage_uint256The maximum leverage.

getPendingAssetsAmount

Returns the amount of assets deposited and waiting for the next version to be opened.

function getPendingAssetsAmount() external view returns (uint128 pendingAssetsAmount_);

Returns

NameTypeDescription
pendingAssetsAmount_uint128The amount of pending assets.

getPositionData

Returns the data of the provided version of the position.

function getPositionData(uint128 version) external view returns (PositionData memory positionData_);

Parameters

NameTypeDescription
versionuint128The version of the position.

Returns

NameTypeDescription
positionData_PositionDataThe data for the provided version of the position.

getTimeLimits

Gets the time limits for the action validation process.

function getTimeLimits() external view returns (TimeLimits memory timeLimits_);

Returns

NameTypeDescription
timeLimits_TimeLimitsThe time limits.

increaseAssetAllowance

Increases the allowance of assets for the USDN protocol spender by addAllowance.

function increaseAssetAllowance(uint256 addAllowance) external;

Parameters

NameTypeDescription
addAllowanceuint256Amount to add to the allowance of the USDN Protocol.

getLastLiquidatedVersion

Returns the version of the last position that got liquidated.

0 means no liquidated version yet.

function getLastLiquidatedVersion() external view returns (uint128 version_);

Returns

NameTypeDescription
version_uint128The version of the last position that got liquidated.

getNonce

Gets the nonce a user can use to generate a delegation signature.

This is to prevent replay attacks when using an EIP712 delegation signature.

function getNonce(address user) external view returns (uint256 nonce_);

Parameters

NameTypeDescription
useraddressThe user address of the deposited amount in the rebalancer.

Returns

NameTypeDescription
nonce_uint256The user's nonce.

domainSeparatorV4

Gets the domain separator v4 used for EIP-712 signatures.

function domainSeparatorV4() external view returns (bytes32 domainSeparator_);

Returns

NameTypeDescription
domainSeparator_bytes32The domain separator v4.

getCloseLockedUntil

Gets the timestamp by which a user must wait to perform a initiateClosePosition.

function getCloseLockedUntil() external view returns (uint256 timestamp_);

Returns

NameTypeDescription
timestamp_uint256The timestamp until which the position cannot be closed.

initiateDepositAssets

Deposits assets into this contract to be included in the next position after validation

The user must call validateDepositAssets between _timeLimits.validationDelay and. _timeLimits.validationDeadline seconds after this action.

function initiateDepositAssets(uint88 amount, address to) external;

Parameters

NameTypeDescription
amountuint88The amount in assets that will be deposited into the rebalancer.
toaddressThe address which will need to validate and which will own the position.

validateDepositAssets

Validates a deposit to be included in the next position version.

The to from the initiateDepositAssets must call this function between _timeLimits.validationDelay and _timeLimits.validationDeadline seconds after the initiate action. After that, the user must wait until _timeLimits.actionCooldown seconds has elapsed, and then can call resetDepositAssets to retrieve their assets.

function validateDepositAssets() external;

resetDepositAssets

Retrieves the assets for a failed deposit due to waiting too long before calling validateDepositAssets.

The user must wait _timeLimits.actionCooldown since the initiateDepositAssets before calling this function.

function resetDepositAssets() external;

initiateWithdrawAssets

Withdraws assets that were not yet included in a position.

The user must call validateWithdrawAssets between _timeLimits.validationDelay and _timeLimits.validationDeadline seconds after this action.

function initiateWithdrawAssets() external;

validateWithdrawAssets

Validates a withdrawal of assets that were not yet included in a position.

The user must call this function between _timeLimits.validationDelay and _timeLimits.validationDeadline seconds after initiateWithdrawAssets. After that, the user must wait until the cooldown has elapsed, and then can call initiateWithdrawAssets again or wait to be included in the next position.

function validateWithdrawAssets(uint88 amount, address to) external;

Parameters

NameTypeDescription
amountuint88The amount of assets to withdraw.
toaddressThe recipient of the assets.

initiateClosePosition

Closes the provided amount from the current rebalancer's position.

The rebalancer allows partially closing its position to withdraw the user's assets + PnL. The remaining amount needs to be above _minAssetDeposit. The validator is always the msg.sender, which means the user must call validateClosePosition on the protocol side after calling this function.

function initiateClosePosition(
    uint88 amount,
    address to,
    address payable validator,
    uint256 userMinPrice,
    uint256 deadline,
    bytes calldata currentPriceData,
    Types.PreviousActionsData calldata previousActionsData,
    bytes calldata delegationData
) external payable returns (Types.LongActionOutcome outcome_);

Parameters

NameTypeDescription
amountuint88The amount to close relative to the amount deposited.
toaddressThe recipient of the assets.
validatoraddress payableThe address that should validate the open position.
userMinPriceuint256The minimum price at which the position can be closed.
deadlineuint256The deadline of the close position to be initiated.
currentPriceDatabytesThe current price data.
previousActionsDataTypes.PreviousActionsDataThe data needed to validate actionable pending actions.
delegationDatabytesAn optional delegation data that include the depositOwner and an EIP712 signature to provide when closing a position on the owner's behalf. If used, it needs to be encoded with abi.encode(depositOwner, abi.encodePacked(r, s, v)).

Returns

NameTypeDescription
outcome_Types.LongActionOutcomeThe outcome of the UsdnProtocol's initiateClosePosition call, check initiateClosePosition for more details.

setPositionMaxLeverage

Updates the max leverage a position can have.

newMaxLeverage must be between the min and max leverage of the USDN protocol. This function can only be called by the owner of the contract.

function setPositionMaxLeverage(uint256 newMaxLeverage) external;

Parameters

NameTypeDescription
newMaxLeverageuint256The new max leverage.

setTimeLimits

Sets the various time limits in seconds.

This function can only be called by the owner of the contract.

function setTimeLimits(uint64 validationDelay, uint64 validationDeadline, uint64 actionCooldown, uint64 closeDelay)
    external;

Parameters

NameTypeDescription
validationDelayuint64The amount of time to wait before an initiate can be validated.
validationDeadlineuint64The amount of time a user has to validate an initiate.
actionCooldownuint64The amount of time to wait after the deadline has passed before trying again.
closeDelayuint64The close delay that will be applied to the next long position opening.