|
| 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 | + SPDX-License-Identifier: Apache License, Version 2.0 |
| 17 | +*/ |
| 18 | + |
| 19 | +pragma solidity 0.6.10; |
| 20 | +pragma experimental ABIEncoderV2; |
| 21 | + |
| 22 | +import { Address } from "@openzeppelin/contracts/utils/Address.sol"; |
| 23 | +import { SafeCast } from "@openzeppelin/contracts/utils/SafeCast.sol"; |
| 24 | + |
| 25 | +import { BaseExtension } from "../lib/BaseExtension.sol"; |
| 26 | +import { PreciseUnitMath } from "../lib/PreciseUnitMath.sol"; |
| 27 | +import { IBaseManager } from "../interfaces/IBaseManager.sol"; |
| 28 | +import { ISetToken } from "../interfaces/ISetToken.sol"; |
| 29 | +import { IAirdropModule } from "../interfaces/IAirdropModule.sol"; |
| 30 | +import { ITradeModule } from "../interfaces/ITradeModule.sol"; |
| 31 | + |
| 32 | +/** |
| 33 | + * @title StakeWiseReinvestmentExtension |
| 34 | + * @author FlattestWhite |
| 35 | + * |
| 36 | + * Smart contract that enables reinvesting the accrued rETH2 into a SetToken into sETH2. |
| 37 | + */ |
| 38 | +contract StakeWiseReinvestmentExtension is BaseExtension { |
| 39 | + |
| 40 | + using Address for address; |
| 41 | + using SafeCast for int256; |
| 42 | + |
| 43 | + /* ============ Constants ============= */ |
| 44 | + |
| 45 | + address public constant S_ETH2 = 0xFe2e637202056d30016725477c5da089Ab0A043A; |
| 46 | + address public constant R_ETH2 = 0x20BC832ca081b91433ff6c17f85701B6e92486c5; |
| 47 | + |
| 48 | + /* ========== Structs ================= */ |
| 49 | + |
| 50 | + struct ExecutionSettings { |
| 51 | + // Name of the exchange adapter stored in the IntegrationRegistry. Typically the name of the contract |
| 52 | + // such as UniswapV3ExchangeAdapterV2. |
| 53 | + string exchangeName; |
| 54 | + // The callData that needs to be passed along to the exchange adapter. This is usually generated with |
| 55 | + // a external view call on the adapter contract using the function generateDataParam. |
| 56 | + bytes exchangeCallData; |
| 57 | + } |
| 58 | + |
| 59 | + /* ========== State Variables ========= */ |
| 60 | + |
| 61 | + ISetToken public immutable setToken; |
| 62 | + IAirdropModule public immutable airdropModule; |
| 63 | + ITradeModule public immutable tradeModule; |
| 64 | + ExecutionSettings public settings; |
| 65 | + |
| 66 | + /* ============ Constructor ============ */ |
| 67 | + /** |
| 68 | + * Sets state variables |
| 69 | + * |
| 70 | + * @param _manager // The manager contract. Used to invoke calls on the underlying SetToken. |
| 71 | + * @param _airdropModule // The airdropModule contract. Used to absorb tokens into the SetToken so that it's part of SetToken's accounting. |
| 72 | + * @param _tradeModule // The tradeModule contract. Used to trade the absorbed rETH2 into sETH2. |
| 73 | + * @param _settings // Determines which exchange adapter is used to execute the trade through the TradeModule contract. |
| 74 | + */ |
| 75 | + constructor( |
| 76 | + IBaseManager _manager, |
| 77 | + IAirdropModule _airdropModule, |
| 78 | + ITradeModule _tradeModule, |
| 79 | + ExecutionSettings memory _settings |
| 80 | + ) public BaseExtension(_manager) { |
| 81 | + setToken = _manager.setToken(); |
| 82 | + airdropModule = _airdropModule; |
| 83 | + tradeModule = _tradeModule; |
| 84 | + settings = _settings; |
| 85 | + } |
| 86 | + |
| 87 | + /* ============ External Functions ============ */ |
| 88 | + |
| 89 | + /** |
| 90 | + * Initializes the extension by: |
| 91 | + * 1. Calling initialize on the AirdropModule for the SetToken with required airdrop settings. |
| 92 | + * 2. Initializing the TradeModule for the SetToken to allow trading trading with the SetToken. |
| 93 | + */ |
| 94 | + function initialize() external onlyOperator { |
| 95 | + address[] memory tokens = new address[](1); |
| 96 | + tokens[0] = R_ETH2; |
| 97 | + IAirdropModule.AirdropSettings memory airdropSettings = IAirdropModule.AirdropSettings ({ |
| 98 | + airdrops: tokens, |
| 99 | + feeRecipient: address(setToken), |
| 100 | + airdropFee: 0, |
| 101 | + anyoneAbsorb: false |
| 102 | + }); |
| 103 | + bytes memory airdropModuleData = abi.encodeWithSelector(airdropModule.initialize.selector, setToken, airdropSettings); |
| 104 | + invokeManager(address(airdropModule), airdropModuleData); |
| 105 | + |
| 106 | + bytes memory tradeModuleData = abi.encodeWithSelector(tradeModule.initialize.selector, setToken); |
| 107 | + invokeManager(address(tradeModule), tradeModuleData); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * |
| 112 | + * 1. Absorbs rETH2 into the SetToken |
| 113 | + * 2. Trades rETH2 into sETH2 with _minReceivedQuantity |
| 114 | + * |
| 115 | + * We considered removing the _minReceivedQuantity parameter and storing the slippage parameter as part of |
| 116 | + * ExecutionSettings. However, in the event of a black swan event where rETH2 de-pegs, we'd need to updateExecutionSettings |
| 117 | + * which would involve a multi-sig txn. Once rETH2 can be redeemed for sETH2 directly, the exchange rate is guaranteed to be at least 1:1. |
| 118 | + * Therefore, _minReceiveQuantity can be removed and this function can be made public. |
| 119 | + */ |
| 120 | + function reinvest(uint256 _minReceiveQuantity) external onlyAllowedCaller(msg.sender) { |
| 121 | + bytes memory absorbCallData = abi.encodeWithSelector( |
| 122 | + IAirdropModule.absorb.selector, |
| 123 | + setToken, |
| 124 | + R_ETH2 |
| 125 | + ); |
| 126 | + invokeManager(address(airdropModule), absorbCallData); |
| 127 | + |
| 128 | + uint256 rEthUnits = uint256(setToken.getTotalComponentRealUnits(R_ETH2)); |
| 129 | + require(rEthUnits > 0, "rETH2 units must be greater than zero"); |
| 130 | + bytes memory tradeCallData = abi.encodeWithSelector( |
| 131 | + ITradeModule.trade.selector, |
| 132 | + setToken, |
| 133 | + settings.exchangeName, |
| 134 | + R_ETH2, |
| 135 | + rEthUnits, |
| 136 | + S_ETH2, |
| 137 | + _minReceiveQuantity, |
| 138 | + settings.exchangeCallData |
| 139 | + ); |
| 140 | + invokeManager(address(tradeModule), tradeCallData); |
| 141 | + } |
| 142 | + |
| 143 | + function updateExecutionSettings(ExecutionSettings memory _settings) external onlyAllowedCaller(msg.sender) { |
| 144 | + settings = _settings; |
| 145 | + } |
| 146 | +} |
0 commit comments