Comment on page
DeltaNeutralBasisTradingStrategy
Strategy smart contract that transforms a SetToken into an on-chain delta neutral basis trading token that earns yield by shorting assets on PerpV2 and collecting funding, while maintaing delta neutral exposure to the short asset by holding an equal amount of spot asset. This extension is paired with the PerpV2BasisTradingModule from Set Protocol where module interactions are invoked via the IBaseManager contract. Any basis trading token can be constructed as long as the market is listed on Perp V2 and there is enough liquidity for the corresponding spot asset on UniswapV3. This extension contract also allows the operator to set an ETH reward to incentivize keepers calling the rebalance function at different leverage thresholds.
function engage()
OPERATOR ONLY: Engage to enter delta neutral position for the first time. SetToken will use 50% of the collateral token to acquire spot asset on Uniswapv3, and deposit rest of the collateral token to PerpV2 to open a new short base token position on PerpV2 such that net exposure to the spot assetis zero. If total rebalance notional is above max trade size, then TWAP is kicked off. To complete engage if TWAP, any valid caller must call iterateRebalance until target is met.
Note: Unlike PerpV2LeverageStrategyExtension,
deposit()
should NOT be called before engage()
.function rebalance()
ONLY EOA AND ALLOWED CALLER: Rebalance product. If |min leverage ratio| < |current leverage ratio| < |max leverage ratio|, then rebalance can only be called once the rebalance interval has elapsed since last timestamp. If outside the max and min but below incentivized leverage ratio, rebalance can be called anytime to bring leverage ratio back to the max or min bounds. The methodology will determine whether to delever or lever. If levering up, SetToken increases the short position on PerpV2, withdraws collateral asset from PerpV2 and uses it to acquire more spot asset to keep the position delta-neutral. If delevering, SetToken decreases the short position on PerpV2, sells spot asset on UniswapV3 and deposits the returned collateral token to PerpV2 to collateralize the PerpV2 position.
Note: If the calculated current leverage ratio is above the incentivized leverage ratio or in TWAP then rebalance cannot be called. Instead, you must call ripcord() which is incentivized with a reward in Ether or iterateRebalance().
function iterateRebalance()
Allowed caller and EOA only: Iterate a rebalance when in TWAP. TWAP cooldown period must have elapsed. If price moves advantageously, then exit without rebalancing and clear TWAP state. This function can only be called when |current leverage ratio| < |incentivized leverage ratio| and in TWAP state.
function reinvest()
ONLY EOA AND ALLOWED CALLER: Reinvests tracked settled funding to increase position. SetToken withdraws funding as collateral token using PerpV2BasisTradingModule. It uses the collateral token to acquire more spot asset and deposit the rest to PerpV2 to increase short perp position. It can only be called once the reinvest interval has elapsed since last reinvest timestamp. TWAP is not supported because reinvestment amounts would be generally small.
NOTE: Rebalance is prioritized over reinvestment. This function can not be called when leverage ratio is out of bounds. Call
rebalance()
instead.This prioritization is important when the shouldRebalance function returns `REINVEST` in the current block and `REBALANCE` in the next block.
function ripcord()
ONLY EOA: In case |current leverage ratio| > |incentivized leverage ratio|, the ripcord function can be called by anyone to return leverage ratio back to the max leverage ratio. This function typically would only be called during times of high downside/upside volatility and / or normal keeper malfunctions. The caller of ripcord() will receive a reward in Ether. The ripcord function uses it's own TWAP cooldown period, slippage tolerance and TWAP max trade size which are typically looser than in regular rebalances. If chunk rebalance size is above max incentivized trade size, then caller must continue to call this function to pull the leverage ratio under the incentivized leverage ratio. Incentivized TWAP cooldown period must have elapsed. The function iterateRebalance will not work.
function deposit()
Operator only: Deposits specified units of current USDC tokens not already being used as collateral into Perpetual Protocol.
Parameter Name | Type | Description |
_collateralUnits | uint256 | Collateral to deposit in position units |
function withdraw()
Operator only: Withdraws specified units of USDC tokens from Perpetual Protocol and adds it as default position on the SetToken
Parameter Name | Type | Description |
_collateralUnits | uint256 | Collateral to withdraw in position units |
function setMethodologySettings()
Operator only: Set methodology settings and check new settings are valid. Note: Need to pass in existing parameters if only changing a few settings. Must not be in a rebalance.
Parameter Name | Type | Description |
_newMethodologySettings | MethodologySettings | New Struct containing methodology parameters address |
function setExecutionSettings()
Operator only: Set execution settings and check new settings are valid. Note: Need to pass in existing parameters if only changing a few settings. Must not be in a rebalance.
Parameter Name | Type | Description |
_newExecutionSettings | ExecutionSettings | New Struct containing execution parameters |
function setIncentiveSettings()
Operator only: Set incentive settings and check new settings are valid. Note: Need to pass in existing parameters if only changing a few settings. Must not be in a rebalance.
Parameter Name | Type | Description |
_newIncentiveSettings | IncentiveSettings | New Struct containing incentive parameters |
function setExchangeSettings()
Operator only: Set exchange settings and check new settings are valid.Updating exchange settings during rebalances is allowed, as it is not possible to enter an unexpected state while doing so.
Note: Need to pass in existing parameters if only changing a few settings.
Parameter Name | Type | Description |
_newExchangeSettings | ExchangeSettings | New Struct containing exchange settings |
function getCurrentLeverageRatio() view returns (int256)
Get current leverage ratio. Current leverage ratio is defined as the sum of USD values of all SetToken open positions on Perp V2 divided by its account value on PerpV2. Prices for base and quote asset are retrieved from the Chainlink Price Oracle.
Return Name | Type | Description |
currentLeverageRatio | int256 | Current leverage ratio in precise units (10e18) |
function shouldRebalance() view returns (ShouldRebalance)
Helper that checks if conditions are met for rebalance or ripcord.
Returns an enum with 0 = no rebalance, 1 = call rebalance(), 2 = call iterateRebalance(), 3 = call ripcord() and 4 = call reinvest().
Return Name | Type | Description |
shouldRebalance | ShouldRebalance | Enum representing whether should rebalance |
function shouldRebalanceWithBounds() view returns (ShouldRebalance)
Helper that checks if conditions are met for rebalance or ripcord with custom max and min bounds specified by caller. This function simplifies the logic for off-chain keeper bots to determine what threshold to call rebalance when leverage exceeds max or drops below min.
Returns an enum with 0 = no rebalance, 1 = call rebalance(), 2 = call iterateRebalance(), 3 = call ripcord() and 4 = call reinvest().
Parameter Name | Type | Description |
_customMinLeverageRatio | int256 | Min leverage ratio passed in by caller |
_customMaxLeverageRatio | int256 | Max leverage ratio passed in by caller |
Return Name | Type | Description |
shouldRebalance | ShouldRebalance | Enum representing whether should rebalance |
Last modified 1yr ago