UUPS Upgrades using Foundry

I am wondering if any of you have tried to do UUPS smart contract upgrade using Foundry ? Similarly the same way you write a script in hardhat. If so, how to do it properly.

Hello @kapitankot, we currently don't have support for Foundry in upgrades plugins but we're considering allocating effort for it.

A rough example of how to test an UUPS deployment in Foundry would be:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;

import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract TestUUPS is OwnableUpgradeable, UUPSUpgradeable {
    uint256 public value;

    function initialize(uint256 value_) public initializer {
        value = value_;
        __Ownable_init();
    }

    function _authorizeUpgrade(
        address newImplementation
    ) internal override onlyOwner {}
}
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;

import "forge-std/Test.sol";
import {TestUUPS} from "../src/UUPS.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract UUPSTest is Test {
    function testDeployAndInitialize() internal {
        address implementation = address(new TestUUPS());
        uint256 value = 42;

        bytes memory data = abi.encodeCall(TestUUPS.initialize, value);
        address proxy = address(new ERC1967Proxy(implementation, data));

        TestUUPS testUUPS = TestUUPS(proxy);
        assertEq(testUUPS.value(), value);
    }
}

However, we always recommend to check for update safetiness.

2 Likes

I'll give it a try, thanks!

you can find in my project , https://github.com/phenix3443/contract_starter/tree/main/script/