Skip to content

Commit

Permalink
1967 Proxy Helper functions in abstract Test Class (#24)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tom2o17 authored Nov 13, 2024
1 parent c951978 commit 029e6be
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/FraxTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~~~~~~~~~~~~~~~~~~~~~
// ========================================================================
Expand All @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ========================================================================
Expand Down

0 comments on commit 029e6be

Please sign in to comment.