Getting error when trying to forceImport ABIs for upgrades

Hello, I'm trying to make an upgrade of my contracts, in order to do that I want to import the oldAbis so that I can later propose the upgrade with OZ defender.

I have the old abis (the ones that are currently deployed) on a folder in my repository called "oldAbis".
My code looks something like this:

export async function prepareUpgradeContract(name: string, address: string) {
  const abi = require('../oldabis/' + (await ethers.provider?.getNetwork())?.name + '/' + name + '.sol/' + name + '.json')['abi'];
  const bytecode = require('../oldabis/' + (await ethers.provider?.getNetwork())?.name + '/' + name + '.sol/' + name + '.json')['bytecode'];
  const old = await upgrades.forceImport(address, await ethers.getContractFactory(abi, bytecode, undefined));

But when running the script, I'm getting the following error:

Error: The requested contract was not found. Make sure the source code is available for compilation
    at getContractNameAndRunValidation (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\upgrades-core\src\validate\query.ts:75:11)
    at getStorageLayout (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\upgrades-core\src\validate\query.ts:83:45)
    at getDeployData (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\hardhat-upgrades\src\utils\deploy-impl.ts:55:34)
    at async getSimulatedData (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\hardhat-upgrades\src\utils\simulate-deploy.ts:51:22)
    at async simulateDeployImpl (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\hardhat-upgrades\src\utils\simulate-deploy.ts:38:42)
    at async addImplToManifest (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\hardhat-upgrades\src\force-import.ts:79:3)
    at async importProxyToManifest (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\hardhat-upgrades\src\force-import.ts:65:3)
    at async Proxy.forceImport (C:\Users\HP\Desktop\ethereum-dev\test\contracts\node_modules\@openzeppelin\hardhat-upgrades\src\force-import.ts:41:7)

But the address points to the deployed contract (the proxy contract) on the right network.

Any help will be greatly appreciated!

The actual source code needs to be available to the ContractFactory. The ABIs aren't sufficient for forceImport because the source code is needed to determine the storage layout of the existing implementation, and the storage layout is used for upgrade safety validations when you propose an upgrade.

Thanks for your answer, how can I make the source code available to the ContractFactory in the code?

Simply changing the source code on the project to the deployed version and updating the call to getContractFactory worked:

await ethers.getContractFactory(name)

Thanks for your help, the code I originally submitted seemed to work on previous upgrades but the reasoning you provided makes sense.

best regards.

1 Like