OpenZeppelin / docs.openzeppelin.com

Source for the OpenZeppelin documentation site
https://docs.openzeppelin.com
45 stars 87 forks source link

upgrading-smart-contracts.adoc HardhatError: HH701: There are multiple artifacts for contract "Box", please use a fully qualified name. #280

Closed paulvi closed 3 years ago

paulvi commented 3 years ago

The page at https://docs.openzeppelin.com/learn/upgrading-smart-contracts

says to use

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

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

main();

However when there is also BoxV2.sol is added, as suggested later on the page, this script now fails:

npx hardhat run --network localhost scripts/deploy_upgradeable_box.js
(node:38342) UnhandledPromiseRejectionWarning: HardhatError: HH701: There are multiple artifacts for contract "Box", please use a fully qualified name.

Please replace Box for one of these options wherever you are trying to read its artifact:

contracts/Box.sol:Box
contracts/BoxV2.sol:Box

    at Artifacts._getArtifactPathFromFiles (/Users/paul/Workspaces/Ethereum/ERC20/hardhat-tutorial-ERC20/node_modules/hardhat/src/internal/artifacts.ts:401:13)
    at Artifacts._getArtifactPath (/Users/paul/Workspaces/Ethereum/ERC20/hardhat-tutorial-ERC20/node_modules/hardhat/src/internal/artifacts.ts:316:17)
    at Artifacts.readArtifact (/Users/paul/Workspaces/Ethereum/ERC20/hardhat-tutorial-ERC20/node_modules/hardhat/src/internal/artifacts.ts:49:26)
    at getContractFactoryByName (/Users/paul/Workspaces/Ethereum/ERC20/hardhat-tutorial-ERC20/node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:100:20)
    at main (/Users/paul/Workspaces/Ethereum/ERC20/hardhat-tutorial-ERC20/scripts/deploy_upgradeable_box.js:5:15)

Please suggest deploy_upgradeable_box.js that would work regardless of what are other files in contracts/

P.S. using file name as await ethers.getContractFactory("Box.sol"); fails with the same error.

doc source is at: https://github.com/OpenZeppelin/docs.openzeppelin.com/blob/master/components/learn/modules/ROOT/pages/upgrading-smart-contracts.adoc

abcoathup commented 3 years ago

Hi @paulvi,

I’m sorry that you had this issue.

Unfortunately, I wasn’t able to reproduce this issue. I tried the following:

Box.sol

// contracts/Box.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

contract Box {
    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }
}

BoxV2.sol

// contracts/BoxV2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

contract BoxV2 {
    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }

    // Increments the stored value by 1
    function increment() public {
        value = value + 1;
        emit ValueChanged(value);
    }
}

Deploy

$ npx hardhat run --network localhost scripts/deploy_upgradeable_box.js
Compiling 2 files with 0.7.3
Compilation finished successfully
Deploying Box...
Box deployed to: 0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0

The Learn guide uses Box.sol with a contract named Box and BoxV2.sol named BoxV2.

It looks like your contract in BoxV2.sol is named Box rather than BoxV2. Can you try renaming this to BoxV2

paulvi commented 3 years ago

Thank you so much, it was all mine error

The warning by tool is correct, I had BoxV2 file not fully updated. (It had Box inside )