0

I am unable to deploy any contract, including the example contract shown below:

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

// Import Ownable from the OpenZeppelin Contracts library
import "@openzeppelin/contracts/access/Ownable.sol";

contract Box is Ownable{
    uint256 private _value;

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

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

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

I am trying to deploy to localhost using the script below:

    const { ethers } = require("hardhat");

// scripts/deploy.js
async function main () {
  // We get the contract to deploy
  const Box = await ethers.getContractFactory('Box');
  console.log('Deploying Box...');
  const box = await Box.deploy();
  await box.deployed();
  console.log('Box deployed to:', box.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

I execute the script using the following command:

npx hardhat run --network localhost scripts/deplobox.js

My Config File looks like this

    //LOCAL CONFIG

require("@nomiclabs/hardhat-waffle");
require('@nomiclabs/hardhat-ethers');

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});


// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.20",
     
};

The error message I receive is the following:

    hardhat_addCompilationResult
web3_clientVersion
eth_chainId
eth_accounts
eth_blockNumber
eth_chainId (2)
eth_estimateGas
  Contract deployment: Box
  From:                0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
  Value:               0 ETH

  Error: Transaction reverted and Hardhat couldn't infer the reason. Please report this to help us improve Hardhat.
      at Box._msgSender (@openzeppelin/contracts/utils/Context.sol:17)
      at Box.constructor (@openzeppelin/contracts/access/Ownable.sol:29)
      at EthModule._estimateGasAction (C:\Users\Admin\hhtest\node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:429:7)
      at HardhatNetworkProvider._sendWithLogging (C:\Users\Admin\hhtest\node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:139:22)
      at HardhatNetworkProvider.request (C:\Users\Admin\hhtest\node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:116:18)
      at JsonRpcHandler._handleRequest (C:\Users\Admin\hhtest\node_modules\hardhat\src\internal\hardhat-network\jsonrpc\handler.ts:188:20)
      at JsonRpcHandler._handleSingleRequest (C:\Users\Admin\hhtest\node_modules\hardhat\src\internal\hardhat-network\jsonrpc\handler.ts:167:17)
      at Server.JsonRpcHandler.handleHttp (C:\Users\Admin\hhtest\node_modules\hardhat\src\internal\hardhat-network\jsonrpc\handler.ts:52:21)

I deployed many scripts in several testnets already. Now it just stopped working. Please help me

2
  • 1
    Check which version of Ownable you're using - you may need to initialize the constructor with an initial owner, which wasn't the case before.
    – Ape Toshi
    Nov 1 at 17:40
  • yes - this was one of the problems. unfortunately, the compiler stopped me from implementing a consctructor. After reinstalling the whole project, the compiler allowed me to implement constructor arguments and the deployment worked. thanks for your suggestion!
    – PupSt4r
    Nov 13 at 6:32

2 Answers 2

0

The script does not deploy to testnets, rather to your localhost. Which testnet are you attempting to deploy this to? If you aren't running a localhost like Ganache for your testing locally, then it will fail to deploy the contract to that testnet.

1
  • i consider "localhost" as a testnet. i apologize if this naming caused confusion. thanks for your suggestion!
    – PupSt4r
    Nov 13 at 6:35
0

I reinstalled the whole project folder - this led the compiler to accept certain changes which it didnt accept before (most notable change was the addition of constructor arguments for Ownable.sol)

Now everything works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.