Function: completeRebalance(BasketManagerStorage self, ExternalTrade[] externalTrades, address[] baskets, uint64[][] basketTargetWeights)
This function allows the completion of a rebalance for the given baskets. The rebalance can be completed if it has been more than 15 minutes since the last action.
Inputs
self
Control: Fully controlled by the calling contract.
Constraints: None.
Impact: The storage reference.
externalTrades
Control: Fully controlled by the calling contract.
Constraints: Ensured that the hash of the external trades matches the stored hash.
Impact: The external trades to be processed.
baskets
Control: Fully controlled by the calling contract.
Constraints: Baskets' hash is validated.
Impact: The baskets to complete the rebalance for.
basketTargetWeights
Control: Fully controlled by the calling contract.
Constraints: Validated against the baskets that are to be rebalanced.
Impact: The target weights for each basket.
Branches and code coverage
Intended branches
Validate the basket hash against the storage one, so that no arbitrary trades are processed.
Process the external trades if the rebalance status is
TOKEN_SWAP_EXECUTED
. This assumes that theexternalTrades
have already been validated by this point.Initialize the basket data.
Validate the target weights if the retry count is less than
_MAX_RETRIES
. Otherwise, take the risk and continue with possibly unmet target weights.Increment the retry count, update the timestamp, reset the external trades hash, and set the status to
REBALANCE_PROPOSED
so that a new swap can be proposed again — in the case of unmet target weights.Finalize the rebalance if all checks pass.
Negative behavior
Should not allow completing the rebalance if the status is not
TOKEN_SWAP_PROPOSED
,REBALANCE_PROPOSED
orTOKEN_SWAP_EXECUTED
.Should not allow a
block.timestamp - rebalanceStatus.timestamp
to be less than 15 minutes.Should revert if the rebalance status is
NOT_STARTED
.Should revert if the external trades hash does not match the stored hash.
Should return early if the target weights are met and the retry count is less than
_MAX_RETRIES
.