From 029e6be48a4c534f8d0f57a6eb9c4d1beced7b42 Mon Sep 17 00:00:00 2001 From: Thomas Clement <88335455+tom2o17@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:04:09 -0500 Subject: [PATCH] 1967 Proxy Helper functions in abstract Test Class (#24) * Init Helper functions in abstract Test Class I find myself rewriting this code across repos often. It would be nice if we supported in base FraxTest Class. We can add Test Coverage once we add a generic 1967Proxy to this repo --- src/FraxTest.sol | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/FraxTest.sol b/src/FraxTest.sol index a28014c..2595bc8 100644 --- a/src/FraxTest.sol +++ b/src/FraxTest.sol @@ -6,9 +6,14 @@ import { VmHelper } from "./VmHelper.sol"; import { Strings } from "./StringsHelper.sol"; abstract contract FraxTest is VmHelper, Test { + /// @notice Differential State Storage uint256[] internal snapShotIds; function()[] internal setupFunctions; + /// @notice EIP-1967 Slots + bytes32 internal IMPLEMENTATION_SLOT = bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1); + bytes32 internal ADMIN_SLOT = bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1); + // ======================================================================== // ~~~~~~~~~~~~~~~~~~ Different State Testing Helpers ~~~~~~~~~~~~~~~~~~~~~ // ======================================================================== @@ -34,6 +39,34 @@ abstract contract FraxTest is VmHelper, Test { } } + // ======================================================================== + // ~~~~~~~~~~~~~~~~~~~~~~~ EIP-1967 Proxy Helpers ~~~~~~~~~~~~~~~~~~~~~~~~~ + // ======================================================================== + + /// @notice Helper function to efficiently return the address type located at the implementation + /// and admin slot on an EIP-1967 Proxy + /// @param _proxyToCheck The proxy to fetch the implementation and admin of + /// @return implementation The Implmentation of the `_proxyToCheck` + /// @return admin The Admin of the `_proxyToCheck` + function get1967ProxyImplAndAdmin( + address _proxyToCheck + ) internal view returns (address implementation, address admin) { + implementation = address(uint160(uint(vm.load(_proxyToCheck, IMPLEMENTATION_SLOT)))); + admin = address(uint160(uint(vm.load(_proxyToCheck, ADMIN_SLOT)))); + } + + /// @notice Variant of the above function but the returns will be logged to the console + /// @param _proxyToCheck The proxy to fetch the implementation and admin of + /// @return implementation The Implmentation of the `_proxyToCheck` + /// @return admin The Admin of the `_proxyToCheck` + function get1967ProxyImplAndAdminWithLog( + address _proxyToCheck + ) internal view returns (address implementation, address admin) { + (implementation, admin) = get1967ProxyImplAndAdmin(_proxyToCheck); + console.log(" get1967ProxyImplAndAdminWithLog: Implementation - ", implementation); + console.log(" get1967ProxyImplAndAdminWithLog: ProxyAdmin - ", admin); + } + // ======================================================================== // ~~~~~~~~~~~~~~~~~~~~~~~ Storage Slot Helpers ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ========================================================================