|
| 1 | +/* |
| 2 | + Copyright 2022 Index Cooperative. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +pragma solidity 0.6.10; |
| 18 | +pragma experimental ABIEncoderV2; |
| 19 | + |
| 20 | +import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; |
| 21 | +import { Address } from "@openzeppelin/contracts/utils/Address.sol"; |
| 22 | +import { KeeperCompatibleInterface } from "@chainlink/contracts/src/v0.6/KeeperCompatible.sol"; |
| 23 | +import { IFlexibleLeverageStrategyExtension } from "../interfaces/IFlexibleLeverageStrategyExtension.sol"; |
| 24 | + |
| 25 | +/** |
| 26 | + * @title RebalanceKeeper |
| 27 | + * @author Index Cooperative |
| 28 | + * |
| 29 | + * Chainlink Keeper which automatically rebalances FLI SetTokens. |
| 30 | + */ |
| 31 | +contract FliRebalanceKeeper is Ownable, KeeperCompatibleInterface { |
| 32 | + |
| 33 | + using Address for address; |
| 34 | + |
| 35 | + /* ============ Structs ============ */ |
| 36 | + |
| 37 | + struct LeverageSettings { |
| 38 | + uint256 customMinLeverageRatio; // The minimum leverage ratio |
| 39 | + uint256 customMaxLeverageRatio; // The maximum leverage ratio |
| 40 | + } |
| 41 | + |
| 42 | + /* ============ Modifiers ============ */ |
| 43 | + |
| 44 | + modifier onlyRegistry() { |
| 45 | + require(msg.sender == registryAddress, "Only registry address can call this function"); |
| 46 | + _; |
| 47 | + } |
| 48 | + |
| 49 | + /* ============ State Variables ============ */ |
| 50 | + |
| 51 | + IFlexibleLeverageStrategyExtension public fliExtension; // Address of the fli extension contract |
| 52 | + address public registryAddress; // Address of the chainlink keeper registry |
| 53 | + uint256 public exchangeIndex; // The index of the exchange to use |
| 54 | + LeverageSettings public leverageSettings; // The leverage settings to check whether should rebalance |
| 55 | + |
| 56 | + /* ============ Constructor ============ */ |
| 57 | + |
| 58 | + constructor( |
| 59 | + IFlexibleLeverageStrategyExtension _fliExtension, |
| 60 | + address _registryAddress, |
| 61 | + uint256 _exchangeIndex, |
| 62 | + LeverageSettings memory _leverageSettings |
| 63 | + ) public { |
| 64 | + fliExtension = _fliExtension; |
| 65 | + registryAddress = _registryAddress; |
| 66 | + exchangeIndex = _exchangeIndex; |
| 67 | + leverageSettings = _leverageSettings; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * As checkUpkeep is not a view function, calling this function will actually consume gas. |
| 72 | + * As such if a keeper calls this function, it will always return true so that performUpkeep will be called. |
| 73 | + */ |
| 74 | + function checkUpkeep(bytes calldata /* checkData */) external override returns (bool, bytes memory) { |
| 75 | + (string[] memory exchangeNames, IFlexibleLeverageStrategyExtension.ShouldRebalance[] memory shouldRebalances) = fliExtension.shouldRebalanceWithBounds( |
| 76 | + leverageSettings.customMinLeverageRatio, |
| 77 | + leverageSettings.customMaxLeverageRatio |
| 78 | + ); |
| 79 | + IFlexibleLeverageStrategyExtension.ShouldRebalance shouldRebalance = shouldRebalances[exchangeIndex]; |
| 80 | + bytes memory performData = abi.encode(shouldRebalance, exchangeNames[exchangeIndex]); |
| 81 | + return (shouldRebalance != IFlexibleLeverageStrategyExtension.ShouldRebalance.NONE, performData); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * performUpkeep checks that a rebalance is required. Otherwise the contract call will revert. |
| 86 | + */ |
| 87 | + function performUpkeep(bytes calldata performData) external override onlyRegistry { |
| 88 | + require(performData.length > 0, "Invalid performData"); |
| 89 | + (IFlexibleLeverageStrategyExtension.ShouldRebalance shouldRebalance, string memory exchangeName) = abi.decode( |
| 90 | + performData, |
| 91 | + (IFlexibleLeverageStrategyExtension.ShouldRebalance, string) |
| 92 | + ); |
| 93 | + if (shouldRebalance == IFlexibleLeverageStrategyExtension.ShouldRebalance.REBALANCE) { |
| 94 | + fliExtension.rebalance(exchangeName); |
| 95 | + return; |
| 96 | + } else if (shouldRebalance == IFlexibleLeverageStrategyExtension.ShouldRebalance.ITERATE_REBALANCE) { |
| 97 | + fliExtension.iterateRebalance(exchangeName); |
| 98 | + return; |
| 99 | + } else if (shouldRebalance == IFlexibleLeverageStrategyExtension.ShouldRebalance.RIPCORD) { |
| 100 | + fliExtension.ripcord(exchangeName); |
| 101 | + return; |
| 102 | + } |
| 103 | + revert("FliRebalanceKeeper: invalid shouldRebalance or no rebalance required"); |
| 104 | + } |
| 105 | + function setExchangeIndex(uint256 _exchangeIndex) external onlyOwner { |
| 106 | + exchangeIndex = _exchangeIndex; |
| 107 | + } |
| 108 | + |
| 109 | + function setLeverageSettings(LeverageSettings memory _leverageSettings) external onlyOwner { |
| 110 | + leverageSettings = _leverageSettings; |
| 111 | + } |
| 112 | +} |
0 commit comments