BasisStrategy
Inherits: Initializable, PausableUpgradeable, OwnableUpgradeable, IBasisStrategy, AutomationCompatibleInterface
Author: Logarithm Labs
BasisStrategy implements a delta-neutral basis trading strategy. By simultaneously buying a spot asset and selling a perpetual contract, the strategy seeks to hedge the price risk of the spot position while generating revenue from funding payments. The contract allows depositors to provide capital through the connected vault, which is then deployed across both the spot and perpetual markets. Profits are derived from the funding payments collected from the short perpetual position, aiming for yield independent of price direction.
SpotManager and HedgeManager are connected as separated smart contracts to manage spot and hedge positions. BasisStrategy is an upgradeable smart contract, deployed through a beacon proxy pattern.
State Variables
BasisStrategyStorageLocation
Functions
_getBasisStrategyStorage
authCaller
Authorize caller if it is authorized one.
onlyOwnerOrVault
Authorize caller if it is owner and vault.
whenIdle
Validates if strategy is in IDLE status, otherwise reverts calling.
initialize
_setLeverages
_setOperator
setSpotManager
Sets the spot manager.
setHedgeManager
Sets the hedge manager.
setOperator
Sets the operator.
setLeverages
Sets the leverages.
pause
Pauses strategy, disabling utilizing and deutilizing for withdraw requests, while all logics related to keeping are still available.
unpause
Unpauses strategy.
stop
Pauses strategy while swapping all products back to assets and closing the hedge position.
utilize
Utilizes assets to increase the spot size. Right after the increase, the hedge position is also increased as the same amount as the spot size increased.
Uses assets in vault. Callable only by the operator.
Parameters
amount
uint256
The underlying asset amount to be utilized.
swapType
ISpotManager.SwapType
The swap type in which the underlying asset is swapped.
swapData
bytes
The data used in swapping.
deutilize
Deutilizes products to decrease the spot size. Right after the decrease, the hedge position is also decreased as the same amount as the spot size decreased.
Called when processing withdraw requests, when deleveraging the position, and when there are funding risks. Callable only by the operator.
Parameters
amount
uint256
The product amount to be deutilized.
swapType
ISpotManager.SwapType
The swap type in which the product is swapped.
swapData
bytes
The data used in swapping.
processAssetsToWithdraw
Processes assets in Strategy for the withdraw requests.
Callable by anyone and only when strategy is in the IDLE status.
checkUpkeep
method that is simulated by the keepers to see if any work actually needs to be performed. This method does does not actually need to be executable, and since it is only ever simulated it can consume lots of gas.
To ensure that it is never called, you may want to add the cannotExecute modifier from KeeperBase to your implementation of this method.
Parameters
<none>
bytes
Returns
upkeepNeeded
bool
boolean to indicate whether the keeper should call performUpkeep or not.
performData
bytes
bytes that the keeper should call performUpkeep with, if upkeep is needed. If you would like to encode data to decode later, try abi.encode
.
performUpkeep
method that is actually executed by the keepers, via the registry. The data returned by the checkUpkeep simulation will be passed into this method to actually be executed.
The input to this method should not be trusted, and the caller of the method should not even be restricted to any single registry. Anyone should be able call it, and the input should be validated, there is no guarantee that the data passed in is the performData returned from checkUpkeep. This could happen due to malicious keepers, racing keepers, or simply a state change while the performUpkeep transaction is waiting for confirmation. Always validate the data passed in.
Parameters
<none>
bytes
spotBuyCallback
Called after product is bought. Increases the hedge position size if the swap operation is for utilizing.
spotSellCallback
Called after product is sold. Decreases the hedge position if the swap operation is not for reverting.
afterAdjustPosition
Callback function dispatcher of the hedge position adjustment.
pendingUtilizations
Returns available pending utilization and deutilization amounts.
The operator uses these values on offchain side to decide the parameters for calling utilize or deutilize functions. Both of those values can’t be none-zero at the same time.
Returns
pendingUtilizationInAsset
uint256
The available pending utilization amount in asset.
pendingDeutilizationInProduct
uint256
The available pending deutilzation amount in product. The calculation of this amount depends on the goal of deutilizing whether it is for processing withdraw requests or for rebalancing down.
utilizedAssets
The total underlying asset amount that has been utilized by this strategy.
Includes the product balance, the position net balance, and the asset balance of this strategy.
assetsToWithdraw
The asset balance of this strategy.
This value should be transferred to the vault after finishing strategy operations.
assetsToDeutilize
The total asset amount that is needed to be withdrawn from strategy to vault to process withdraw requests.
_adjustPosition
Validate the position adjustment parameters before requesting.
_checkUpkeep
Common function of checkUpkeep and performUpkeep.
_afterIncreasePosition
Called after the hedge position is increased.
_afterDecreasePosition
Called after the hedge position is decreased.
_processAssetsToWithdraw
Processes assetsToWithdraw for the withdraw requests
_pendingUtilization
This return value should be 0 when rebalancing down or when paused or when the totalSupply is 0.
_pendingDeutilization
This return value should be 0 when paused and not processing rebalance down.
_clamp
_checkDeviation
Should be called under the condition that denominator != 0. Note: check if response of position adjustment is in the allowed deviation
_checkNeedRebalance
Checks if current leverage is not near to the target leverage
_checkRebalance
Checks if current leverage is out of the min and max leverage
_checkHedgeDeviation
Checks the difference between spot and hedge sizes if it is over the configured threshold.
_calculateDeltaCollateralForRebalance
collateral adjustment for rebalancing currentLeverage = notional / collateral notional = currentLeverage collateral targetLeverage = notional / targetCollateral targetCollateral = notional / targetLeverage targetCollateral = collateral * currentLeverage / targetLeverage*
_validateStrategyStatus
Validates the strategy status if it is desired one.
_setStrategyStatus
Sets the strategy status.
vault
The address of connected vault.
spotManager
The address of the spot manager which buys and sells product in spot markets.
hedgeManager
The address of the position manager which hedges the spot by opening perpetual positions.
oracle
The address of system oracle.
operator
The address of operator which is responsible for calling utilize/deutilize.
asset
The address of underlying asset.
product
The address of product.
config
The address of Config smart contract that is used throughout all strategies for their configurations.
strategyStatus
The strategy status.
targetLeverage
The target leverage at which the hedge position is increased.
minLeverage
The minimum leverage value to which the hedge position can be reached down.
maxLeverage
The maximum leverage value to which the hedge position can be reached up.
safeMarginLeverage
The maximum leverage value where normal rebalancing down is applied. If the leverage overshoots it, emergency rebalancing down is executed.
pendingDecreaseCollateral
The value that couldn’t be decreased due to size limits. Accumulated overtime and executed to decrease collateral by keeping logic once the size satisfies the conditions.
processingRebalanceDown
Tells if strategy is in rebalancing down.
Events
Utilize
Emitted when assets are utilized.
Deutilize
Emitted when assets are deutilized.
PositionAdjusted
Emitted when the hedge position gets adjusted.
LeverageConfigUpdated
Emitted when leverage config gets changed.
SpotManagerUpdated
Emitted when the spot manager is changed.
HedgeManagerUpdated
Emitted when the position manager is changed.
OperatorUpdated
Emitted when the operator is changed.
Stopped
Emitted when strategy is stopped.
Structs
InternalPendingDeutilization
Used internally to optimize params of deutilization.
InternalCheckUpkeepResult
Used internally as a result of checkUpkeep function.
BasisStrategyStorage
Enums
StrategyStatus
Used to specify strategy’s operations.
Last updated