|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {Test, console} from "forge-std/Test.sol"; |
| 5 | +import {AlchemyProxyRegistry} from "../src/AlchemyProxyRegistry.sol"; |
| 6 | +import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; |
| 7 | + |
| 8 | +contract AlchemyProxyRegistryTest is Test { |
| 9 | + AlchemyProxyRegistry public proxyRegistry; |
| 10 | + bytes32 internal immutable _PROXY_IMPL_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; |
| 11 | + |
| 12 | + function setUp() public { |
| 13 | + proxyRegistry = new AlchemyProxyRegistry(address(this)); |
| 14 | + } |
| 15 | + |
| 16 | + function test_deploys() public { |
| 17 | + // check that owner is test contract |
| 18 | + assertEq(address(proxyRegistry.deployer()), address(this)); |
| 19 | + |
| 20 | + address proxy = address(new ERC1967Proxy(address(proxyRegistry), "")); |
| 21 | + |
| 22 | + // instance of implementation to test upgradeTo |
| 23 | + address toUpgradeTo = address(new AlchemyProxyRegistry(address(this))); |
| 24 | + |
| 25 | + assertEq(_getImplementation(proxy), address(proxyRegistry)); |
| 26 | + AlchemyProxyRegistry(proxy).upgradeToAndCall(toUpgradeTo, ""); |
| 27 | + |
| 28 | + // check that implementation is now the new implementation |
| 29 | + assertEq(_getImplementation(proxy), address(toUpgradeTo)); |
| 30 | + } |
| 31 | + |
| 32 | + function test_deployBad() public { |
| 33 | + // check that owner is test contract |
| 34 | + assertEq(address(proxyRegistry.deployer()), address(this)); |
| 35 | + |
| 36 | + address proxy = address(new ERC1967Proxy(address(proxyRegistry), "")); |
| 37 | + |
| 38 | + // instance of implementation to test upgradeTo |
| 39 | + address toUpgradeTo = address(new AlchemyProxyRegistry(address(this))); |
| 40 | + |
| 41 | + assertEq(_getImplementation(proxy), address(proxyRegistry)); |
| 42 | + vm.startPrank(address(1)); |
| 43 | + vm.expectRevert("Only deployer can upgrade"); |
| 44 | + AlchemyProxyRegistry(proxy).upgradeToAndCall(toUpgradeTo, ""); |
| 45 | + |
| 46 | + // check that implementation is still the old implementation |
| 47 | + assertEq(_getImplementation(proxy), address(proxyRegistry)); |
| 48 | + } |
| 49 | + |
| 50 | + function _getImplementation(address proxy) internal view returns (address) { |
| 51 | + return address(uint160(uint256(vm.load(address(proxy), _PROXY_IMPL_SLOT)))); |
| 52 | + } |
| 53 | +} |
0 commit comments