Rebalancing
The Rebalancing contract provides functionalities to maintain and adjust the portfolio weights of tokens in a "vault". This allows a vault manager to ensure the portfolio adheres to its target allocations. Here's a step-by-step breakdown of the rebalancing process:
Initialization:
The rebalancing module is initialized with the main IndexSwap contract address and the AccessController address, which manages roles and permissions.
Setting the Pause State:
The asset manager can pause or unpause the IndexSwap contract's functionalities using the setPause function. This can be useful during rebalancing to make sure thereβs no deposits/withdrawals during rebalancing.
If the system is paused, it checks if the last pause exceeded 15 minutes. After 15 minutes anyone can unpause the system.
Selling Overweighted Tokens:
If a token's weight in the portfolio exceeds its target weight, it needs to be sold.
The sellTokens function calculates the difference between current and target weights and sells the excessive amount. The sold tokens are converted to ETH (or BNB).
The total weight of the tokens that have to be bought (underweighted tokens) is returned.
Buying Underweighted Tokens:
The buyTokens function uses the ETH (or BNB) obtained from selling overweighted tokens to buy tokens that are under their target weight. This process ensures the portfolio meets its desired allocations.
Executing Rebalance:
The rebalance function orchestrates the selling of overweighted tokens and the buying of underweighted tokens. It also updates the last rebalance timestamp and sets the redeemed state to false.
Updating Token Weights:
The asset manager can use the updateWeights function to modify the target weights of tokens in the portfolio.
After updating the weights, the rebalance function is invoked to ensure the portfolio reflects the new weights.
Adding or Removing Tokens:
The asset manager can add new tokens or remove existing tokens from the portfolio using the updateTokens function.
Tokens are validated, old tokens that are not in the new list are sold, and the index's token list is updated. The portfolio is then rebalanced to the updated list and weights.
Swapping Reward Tokens:
The asset manager can swap reward tokens for any token in the index using the swapRewardToken function. This can be useful in scenarios where reward tokens are earned and need to be converted to a more standard token.
Internal Helpers:
There are several internal functions that assist in the rebalancing process, such as _pullAndSwap, which handles the actual token swap operations, or getTokenBalance, which retrieves the balance of a given token.
Receiving ETH (or BNB):
The contract has a receive function to accept ETH, which is crucial for operations involving WETH or when rebalancing requires ETH.
Additional notes
The rebalancing process ensures the portfolio remains aligned with its strategic objectives by adjusting token weights.
Certain operations, such as rebalancing or updating weights, are restricted to the asset manager role.
The system can be paused or unpaused during the rebalancing to avoid incorrect deposit/withdrawal calculations
In off-chain functions, we also have a revert function mechanism to protect asset managers from funds stuck in contract during rebalancing.
revertEnableRebalancing function can be called to revert the first transaction of the off-chain function and send back the tokens to the vault. During rebalance the deposit/withdrawal are paused. If the asset manager does not revert back the transaction in 15 mins, any user can then revert back the transaction by calling revertEnableRebalancingByUser.
revertSellTokens function can be called to revert the second transaction of the off-chain function in which the tokens sold for BNB will be sent to vault and token list will be updated. If asset manager does not revert back the transaction in 15 mins, any user can then revert back the transaction by calling revertSellByUser