Upgrades Plugin fails with the latest Hardhat ("contractFactory.attach is not a function")

I am trying to deploy the OpenZeppelin Upgrades as described in several tutorials, including your own (https://docs.openzeppelin.com/upgrades-plugins/1.x/hardhat-upgrades):

// scripts/create-box.js
const { ethers, upgrades } = require("hardhat");

async function main() {
  const Box = await ethers.getContractFactory("Box");
  const box = await upgrades.deployProxy(Box, [42]);
  await box.waitForDeployment();
  console.log("Box deployed to:", await box.getAddress());
}

main();

and receive the following message:

hardhat run scripts/1.deploy_box.ts

Compiled 1 Solidity file successfully (evm target: paris).
D:\Code\Proxies\solproxy\node_modules@openzeppelin\hardhat-upgrades\src\utils\ethers.ts:7
return contractFactory.attach(address) as Contract; // Needed because ethers attach returns a BaseContract.
^
TypeError: contractFactory.attach is not a function
at attach (D:\Code\Proxies\solproxy\node_modules@openzeppelin\hardhat-upgrades\src\utils\ethers.ts:7:26)
at getContractInstance (D:\Code\Proxies\solproxy\node_modules@openzeppelin\hardhat-upgrades\src\utils\contract-instance.ts:28:26)
at Proxy.deployProxy (D:\Code\Proxies\solproxy\node_modules@openzeppelin\hardhat-upgrades\src\deploy-proxy.ts:87:31)
at async main (D:\Code\Proxies\solproxy\scripts\1.deploy_box.ts:5:15)

==================================
Can anybody point out a working example/tutorial, which would compile and deploy with the latest libraries and tools?

Can you check whether you are using the latest version of the ethers (should be version 6.x) and @nomicfoundation/hardhat-ethers packages?

You can get a sample project by going to https://wizard.openzeppelin.com/, enabling Upgradeability, then Download -> Development Package (Hardhat), then follow the steps in the downloaded package's readme.

For future readers: updating ethers and adding @nomicfoundation/hardhat-ethers worked for me. Please be advised that when downloading the Development Package in the wizard section this templete is missing both these dependencies and thus resulted in the same error. Good luck.

1 Like