From 2ae6901eeb2e7a9c977d5aaf8492cd0aaa23a7b9 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Wed, 14 Feb 2024 16:52:03 -0500 Subject: [PATCH 1/3] Forwarder update for VRF sub balance monitor --- .../VRFSubscriptionBalanceMonitor.sol | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol b/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol index 52b1bc2c543..d6d68d238c3 100644 --- a/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol +++ b/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.6; +pragma solidity 0.8.20; import "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol"; import "@chainlink/contracts/src/v0.8/automation/interfaces/KeeperCompatibleInterface.sol"; @@ -27,6 +27,7 @@ contract VRFSubscriptionBalanceMonitor is event TopUpSucceeded(uint64 indexed subscriptionId); event TopUpFailed(uint64 indexed subscriptionId); event KeeperRegistryAddressUpdated(address oldAddress, address newAddress); + event ForwarderAddressUpdated(address oldAddress, address newAddress); event VRFCoordinatorV2AddressUpdated( address oldAddress, address newAddress @@ -40,6 +41,7 @@ contract VRFSubscriptionBalanceMonitor is error InvalidWatchList(); error OnlyKeeperRegistry(); + error OnlyForwarder(); error DuplicateSubcriptionId(uint64 duplicate); struct Target { @@ -50,6 +52,7 @@ contract VRFSubscriptionBalanceMonitor is } address public s_keeperRegistryAddress; // the address of the keeper registry + address public s_forwarderAddress; // the address of the upkeep's forwarder uint256 public s_minWaitPeriodSeconds; // minimum time to wait between top-ups uint64[] public s_watchList; // the watchlist on which subscriptions are stored mapping(uint64 => Target) internal s_targets; @@ -58,17 +61,20 @@ contract VRFSubscriptionBalanceMonitor is * @param linkTokenAddress the Link token address * @param coordinatorAddress the address of the vrf coordinator contract * @param keeperRegistryAddress the address of the keeper registry contract + * @param forwarderAddress the address of the upkeep's forwarder * @param minWaitPeriodSeconds the minimum wait period for addresses between funding */ constructor( address linkTokenAddress, address coordinatorAddress, address keeperRegistryAddress, + address forwarderAddress, uint256 minWaitPeriodSeconds ) ConfirmedOwner(msg.sender) { setLinkTokenAddress(linkTokenAddress); setVRFCoordinatorV2Address(coordinatorAddress); setKeeperRegistryAddress(keeperRegistryAddress); + setForwarderAddress(forwarderAddress); setMinWaitPeriodSeconds(minWaitPeriodSeconds); } @@ -218,7 +224,7 @@ contract VRFSubscriptionBalanceMonitor is */ function performUpkeep( bytes calldata performData - ) external override onlyKeeperRegistry whenNotPaused { + ) external override onlyForwarder whenNotPaused { uint64[] memory needsFunding = abi.decode(performData, (uint64[])); topUp(needsFunding); } @@ -274,6 +280,17 @@ contract VRFSubscriptionBalanceMonitor is s_keeperRegistryAddress = keeperRegistryAddress; } + /** + * @notice Sets the upkeep's unique forwarder address + * for upkeeps in Automation versions 2.0 and later + * https://docs.chain.link/chainlink-automation/guides/forwarder + */ + function setForwarderAddress(address forwarderAddress) public onlyOwner { + require(forwarderAddress != address(0)); + emit ForwarderAddressUpdated(s_forwarderAddress, forwarderAddress); + s_forwarderAddress = forwarderAddress; + } + /** * @notice Sets the minimum wait period (in seconds) for subscription ids between funding. */ @@ -327,9 +344,9 @@ contract VRFSubscriptionBalanceMonitor is _unpause(); } - modifier onlyKeeperRegistry() { - if (msg.sender != s_keeperRegistryAddress) { - revert OnlyKeeperRegistry(); + modifier onlyForwarder() { + if (msg.sender != s_forwarderAddress) { + revert OnlyForwarder(); } _; } From eb9cd79d85a2462e5ec8c86dc6cb227ef60dbfef Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Thu, 15 Feb 2024 12:05:11 -0500 Subject: [PATCH 2/3] Remove other refs to keeper registry --- .../VRFSubscriptionBalanceMonitor.sol | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol b/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol index d6d68d238c3..d3acff2dc64 100644 --- a/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol +++ b/public/samples/Automation/tutorials/VRFSubscriptionBalanceMonitor.sol @@ -26,7 +26,6 @@ contract VRFSubscriptionBalanceMonitor is event FundsWithdrawn(uint256 amountWithdrawn, address payee); event TopUpSucceeded(uint64 indexed subscriptionId); event TopUpFailed(uint64 indexed subscriptionId); - event KeeperRegistryAddressUpdated(address oldAddress, address newAddress); event ForwarderAddressUpdated(address oldAddress, address newAddress); event VRFCoordinatorV2AddressUpdated( address oldAddress, @@ -51,7 +50,6 @@ contract VRFSubscriptionBalanceMonitor is uint56 lastTopUpTimestamp; } - address public s_keeperRegistryAddress; // the address of the keeper registry address public s_forwarderAddress; // the address of the upkeep's forwarder uint256 public s_minWaitPeriodSeconds; // minimum time to wait between top-ups uint64[] public s_watchList; // the watchlist on which subscriptions are stored @@ -60,20 +58,17 @@ contract VRFSubscriptionBalanceMonitor is /** * @param linkTokenAddress the Link token address * @param coordinatorAddress the address of the vrf coordinator contract - * @param keeperRegistryAddress the address of the keeper registry contract * @param forwarderAddress the address of the upkeep's forwarder * @param minWaitPeriodSeconds the minimum wait period for addresses between funding */ constructor( address linkTokenAddress, address coordinatorAddress, - address keeperRegistryAddress, address forwarderAddress, uint256 minWaitPeriodSeconds ) ConfirmedOwner(msg.sender) { setLinkTokenAddress(linkTokenAddress); setVRFCoordinatorV2Address(coordinatorAddress); - setKeeperRegistryAddress(keeperRegistryAddress); setForwarderAddress(forwarderAddress); setMinWaitPeriodSeconds(minWaitPeriodSeconds); } @@ -266,20 +261,6 @@ contract VRFSubscriptionBalanceMonitor is COORDINATOR = VRFCoordinatorV2Interface(coordinatorAddress); } - /** - * @notice Sets the keeper registry address. - */ - function setKeeperRegistryAddress( - address keeperRegistryAddress - ) public onlyOwner { - require(keeperRegistryAddress != address(0)); - emit KeeperRegistryAddressUpdated( - s_keeperRegistryAddress, - keeperRegistryAddress - ); - s_keeperRegistryAddress = keeperRegistryAddress; - } - /** * @notice Sets the upkeep's unique forwarder address * for upkeeps in Automation versions 2.0 and later From f1f9706a4297a2d8d84256dcd17a989193c8ee88 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Thu, 15 Feb 2024 12:23:20 -0500 Subject: [PATCH 3/3] Apply similar update to EthBalanceMonitor --- .../tutorials/EthBalanceMonitor.sol | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/public/samples/Automation/tutorials/EthBalanceMonitor.sol b/public/samples/Automation/tutorials/EthBalanceMonitor.sol index 0f50a5d4d4f..99b061960c8 100644 --- a/public/samples/Automation/tutorials/EthBalanceMonitor.sol +++ b/public/samples/Automation/tutorials/EthBalanceMonitor.sol @@ -29,14 +29,14 @@ contract EthBalanceMonitor is event FundsWithdrawn(uint256 amountWithdrawn, address payee); event TopUpSucceeded(address indexed recipient); event TopUpFailed(address indexed recipient); - event KeeperRegistryAddressUpdated(address oldAddress, address newAddress); + event ForwarderAddressUpdated(address oldAddress, address newAddress); event MinWaitPeriodUpdated( uint256 oldMinWaitPeriod, uint256 newMinWaitPeriod ); error InvalidWatchList(); - error OnlyKeeperRegistry(); + error OnlyForwarder(); error DuplicateAddress(address duplicate); struct Target { @@ -46,20 +46,20 @@ contract EthBalanceMonitor is uint56 lastTopUpTimestamp; // enough space for 2 trillion years } - address private s_keeperRegistryAddress; + address private s_forwarderAddress; uint256 private s_minWaitPeriodSeconds; address[] private s_watchList; mapping(address => Target) internal s_targets; /** - * @param keeperRegistryAddress The address of the Chainlink Automation registry contract + * @param forwarderAddress The address of the Chainlink Automation upkeep's forwarder contract * @param minWaitPeriodSeconds The minimum wait period for addresses between funding */ constructor( - address keeperRegistryAddress, + address forwarderAddress, uint256 minWaitPeriodSeconds ) ConfirmedOwner(msg.sender) { - setKeeperRegistryAddress(keeperRegistryAddress); + setForwarderAddress(forwarderAddress); setMinWaitPeriodSeconds(minWaitPeriodSeconds); } @@ -193,7 +193,7 @@ contract EthBalanceMonitor is */ function performUpkeep( bytes calldata performData - ) external override onlyKeeperRegistry whenNotPaused { + ) external override onlyForwarder whenNotPaused { address[] memory needsFunding = abi.decode(performData, (address[])); topUp(needsFunding); } @@ -220,17 +220,14 @@ contract EthBalanceMonitor is } /** - * @notice Sets the Chainlink Automation registry address + * @notice Sets the upkeep's unique forwarder address + * for upkeeps in Automation versions 2.0 and later + * https://docs.chain.link/chainlink-automation/guides/forwarder */ - function setKeeperRegistryAddress( - address keeperRegistryAddress - ) public onlyOwner { - require(keeperRegistryAddress != address(0)); - emit KeeperRegistryAddressUpdated( - s_keeperRegistryAddress, - keeperRegistryAddress - ); - s_keeperRegistryAddress = keeperRegistryAddress; + function setForwarderAddress(address forwarderAddress) public onlyOwner { + require(forwarderAddress != address(0)); + emit ForwarderAddressUpdated(s_forwarderAddress, forwarderAddress); + s_forwarderAddress = forwarderAddress; } /** @@ -242,14 +239,14 @@ contract EthBalanceMonitor is } /** - * @notice Gets the Chainlink Automation registry address + * @notice Gets the forwarder address of the Chainlink Automation upkeep */ - function getKeeperRegistryAddress() + function getForwarderAddress() external view - returns (address keeperRegistryAddress) + returns (address forwarderAddress) { - return s_keeperRegistryAddress; + return s_forwarderAddress; } /** @@ -304,9 +301,9 @@ contract EthBalanceMonitor is _unpause(); } - modifier onlyKeeperRegistry() { - if (msg.sender != s_keeperRegistryAddress) { - revert OnlyKeeperRegistry(); + modifier onlyForwarder() { + if (msg.sender != s_forwarderAddress) { + revert OnlyForwarder(); } _; }