Hardhat – Comprehensive Guide to Testing Proxies

hardhatsoliditytestingupgrading

I have a smart contract that was inherited from UUPSUpgradeable smart contract. I am deploying it first as v1 and then deploying my proxy contract. Here is my proxy contract:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.11;

import "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Proxy is UUPSUpgradeable, Ownable {

    constructor(address _impl) {
        _upgradeTo(_impl);
    }
    
    function _authorizeUpgrade(address) internal override onlyOwner {}
}

I am giving the v1.address as constructor parameter to the proxy. Am I doing the right thing? If I am, how can I send tx to proxy to delegate? I am using hardhat for testing, as you can see in the title.

Best Answer

Use attach to test an already deployed contract with some specific contract ABI. Assuming your v1 contract is named V1, you may test the proxy contract with hardhat-waffle like this:

const V1 = await ethers.getContractFactory("V1");
const v1 = await V1.deploy();

const Proxy = await ethers.getContractFactory("Proxy");
const proxy = await Proxy.deploy(v1.address);

const proxiedV1 = await V1.attach(proxy.address);

you can then use the proxiedV1 object to call V1's methods with the proxy contract. For example if V1 is an ERC20Upgradeable contract with an initialize method (as per the UUPS patten):

await proxiedV1.initialize();
await proxiedV1.transfer(addr1, value1);
console.log(await proxiedV1.balanceOf(addr1));
Related Topic