From 4d93fbe4f25cd1586f2b27ef4d79ad6c0c09d175 Mon Sep 17 00:00:00 2001 From: crispymangoes Date: Fri, 27 Oct 2023 09:19:25 -0700 Subject: [PATCH] Fix L2 --- src/base/ERC4626SharePriceOracle.sol | 31 +++++++++++++++++-- .../ERC4626SharePriceOracle.t.sol | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/base/ERC4626SharePriceOracle.sol b/src/base/ERC4626SharePriceOracle.sol index 8fea2716..4bfc5877 100644 --- a/src/base/ERC4626SharePriceOracle.sol +++ b/src/base/ERC4626SharePriceOracle.sol @@ -71,6 +71,12 @@ contract ERC4626SharePriceOracle is AutomationCompatibleInterface { */ address public automationForwarder; + /** + * @notice keccak256 hash of the parameters used to create this upkeep. + * @dev Only set if `initialize` leads to a pending upkeep. + */ + bytes32 public pendingUpkeepParamHash; + //============================== ERRORS =============================== error ERC4626SharePriceOracle__OnlyCallableByAutomationForwarder(); @@ -80,7 +86,7 @@ contract ERC4626SharePriceOracle is AutomationCompatibleInterface { error ERC4626SharePriceOracle__SharePriceTooLarge(); error ERC4626SharePriceOracle__FuturePerformData(); error ERC4626SharePriceOracle__ContractKillSwitch(); - error ERC4626SharePriceOracle__ForwarderAlreadySet(); + error ERC4626SharePriceOracle__AlreadyInitialized(); //============================== EVENTS =============================== @@ -102,8 +108,16 @@ contract ERC4626SharePriceOracle is AutomationCompatibleInterface { bool isNotSafeToUse ); + /** + * @notice Emitted when the oracles kill switch is activated. + * @dev If this happens, then the proposed performData lead to extremely volatile share price, + * so we need to investigate why that happened, mitigate it, then launch a new share price oracle. + */ event KillSwitchActivated(uint256 reportedAnswer, uint256 minAnswer, uint256 maxAnswer); + event UpkeepRegistered(uint256 upkeepId, address forwarder); + event UpkeepPending(bytes32 upkeepParamHash); + //============================== IMMUTABLES =============================== /** @@ -240,7 +254,8 @@ contract ERC4626SharePriceOracle is AutomationCompatibleInterface { */ function initialize(uint96 initialUpkeepFunds) external { // This function is only callable once. - if (automationForwarder != address(0)) revert ERC4626SharePriceOracle__ForwarderAlreadySet(); + if (automationForwarder != address(0) || pendingUpkeepParamHash != bytes32(0)) + revert ERC4626SharePriceOracle__AlreadyInitialized(); link.safeTransferFrom(msg.sender, address(this), initialUpkeepFunds); @@ -262,7 +277,17 @@ contract ERC4626SharePriceOracle is AutomationCompatibleInterface { link.safeApprove(automationRegistrar, initialUpkeepFunds); uint256 upkeepID = registrar.registerUpkeep(params); - automationForwarder = registry.getForwarder(upkeepID); + if (upkeepID > 0) { + // Upkeep was successfully registered. + address forwarder = registry.getForwarder(upkeepID); + automationForwarder = forwarder; + emit UpkeepRegistered(upkeepID, forwarder); + } else { + // Upkeep is pending. + bytes32 paramHash = keccak256(abi.encode(params)); + pendingUpkeepParamHash = paramHash; + emit UpkeepPending(paramHash); + } } //============================== CHAINLINK AUTOMATION =============================== diff --git a/test/ERC4626SharePriceOracle/ERC4626SharePriceOracle.t.sol b/test/ERC4626SharePriceOracle/ERC4626SharePriceOracle.t.sol index 7e476e36..e8264b7d 100644 --- a/test/ERC4626SharePriceOracle/ERC4626SharePriceOracle.t.sol +++ b/test/ERC4626SharePriceOracle/ERC4626SharePriceOracle.t.sol @@ -1128,7 +1128,7 @@ contract ERC4626SharePriceOracleTest is MainnetStarterTest, AdaptorHelperFunctio // Trying to inialtize again should revert. vm.expectRevert( - bytes(abi.encodeWithSelector(ERC4626SharePriceOracle.ERC4626SharePriceOracle__ForwarderAlreadySet.selector)) + bytes(abi.encodeWithSelector(ERC4626SharePriceOracle.ERC4626SharePriceOracle__AlreadyInitialized.selector)) ); sharePriceOracle.initialize(initialUpkeepFunds);