NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.3k stars 1.41k forks source link

Invalid contract address when deploying to Mumbai #2162

Closed bolpol closed 2 years ago

bolpol commented 2 years ago

Edit by @alcuadrado: This is an issue with https://rpc-mumbai.maticvigil.com and I have already reported it. In the meantime, using alchemy is a valid workaround.


Incorrect address calculation or set in contract instance. I got this bug only on mumbai test network from polygon.

Expected result: token.address - the same deployed address on block explorer.

Result: token.address - not equal to real deployed contract address.

./scripts/Deploy.ts

import hre, {ethers} from "hardhat";

async function main() {
    const [owner] = await hre.ethers.getSigners();
    console.log(owner.address)

    const ERC721Mock = await hre.ethers.getContractFactory("ERC721Mock");
    const token = await ERC721Mock.deploy()

    await token .deployed()

    console.log("token deployed to:", token.address);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

./contracts/ERC721Mock.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0 <1.0.0;

import { ERC721, ERC721Enumerable } from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "../interfaces/ICollectible.sol";

contract ERC721Mock is ERC721Enumerable, ICollectible {
    constructor() ERC721("ERC721 NFT Mock", "MOCK") {}

    function mint(address _to, uint256 _amount) external override {
        for (uint256 i; i < _amount; i++) {
            _mint(_to, totalSupply());
        }
    }
}

package.json

{
  "name": "hardhat-project",
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.1",
    "@nomiclabs/hardhat-etherscan": "^2.1.8",
    "@nomiclabs/hardhat-solhint": "^2.0.0",
    "@nomiclabs/hardhat-waffle": "^2.0.1",
    "@nomiclabs/hardhat-web3": "^2.0.0",
    "@openzeppelin/test-environment": "^0.1.5",
    "@openzeppelin/test-helpers": "^0.5.10",
    "@typechain/ethers-v5": "^7.2.0",
    "@typechain/hardhat": "^2.3.1",
    "@types/chai": "^4.2.22",
    "@types/mocha": "^9.0.0",
    "chai": "^4.3.0",
    "chai-bignumber": "^2.0.2",
    "eslint": "^7.30.0",
    "eslint-config-standard": "^14.1.1",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-standard": "^4.0.0",
    "ethereum-waffle": "^3.4.0",
    "ethers": "^5.4.5",
    "hardhat": "^2.6.1",
    "hardhat-abi-exporter": "^2.3.1",
    "hardhat-gas-reporter": "^1.0.4",
    "mocha": "^9.0.2",
    "prettier": "^2.3.2",
    "prettier-plugin-solidity": "^1.0.0-beta.15",
    "solhint": "^3.3.6",
    "solidity-coverage": "^0.7.17",
    "ts-node": "^10.2.1",
    "typechain": "^6.0.4",
    "typescript": "^4.3.5"
  },
  "dependencies": {
    "@openzeppelin/contracts": "^4.3.1"
  },
  "scripts": {
    "build": "npx hardhat compile",
    "dev": "npx hardhat node",
    "xxxx": "echo 'rm -rfv ./cache && rm -rfv ./artifacts && yarn build'",
    "pretest": "rm -rfv ./cache && rm -rfv ./artifacts && yarn build",
    "test": "npx hardhat test",
    "lint": "solhint 'contracts/**/*.sol' --ignore-path .solhintignore",
    "prettier": "npx prettier --write 'contracts/**/*.sol'",
    "lint:js": "eslint .",
    "lint:js:fix": "eslint . --fix",
    "dist": "npx waffle flatten"
  }
}

hardhat.config.ts

import { HardhatUserConfig, task } from "hardhat/config";

import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";

const config = require("./config.json");

task("accounts", "Prints the list of accounts", async (args, hre) => {
  const accounts = await hre.ethers.getSigners();

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

module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    hardhat: {
    },
    polygon: {
      networkId: 137,
      url: `https://polygon-rpc.com`,
      accounts: [config.privateKey]
    },
    mumbai: {
      networkId: 80001,
      url: "https://rpc-mumbai.maticvigil.com/",
      accounts: [config.privateKey]
    }
  },
  solidity: {
    version: "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  mocha: {
    timeout: 80000
  }
};
dohomi commented 2 years ago

@SwaroopH I can confirm that Mumbai works now.

alcuadrado commented 2 years ago

Thanks @SwaroopH for fixing this bug, and everyone for helping!

I'm closing this issue now

whitenode32 commented 2 years ago

Hi Everyone!

I just ran into the spoken issue on the polygon's MAINNET.

It worked correctly on the testnet, but I got the issue on the mainnet. Actually I was wondering if any of the other developers here deployed to MANINET. I would bet they did not, otherwise they would have gotten the same issue...

Should the solution not have been the same for both testnet(Mumbai) and MAINNET ?

whitenode32 commented 2 years ago

All mumbai nodes behind MaticVigil RPC are on the new version. Can someone confirm by testing their mumbai endpoint?

If it works well, we'll deploy it to mainnet next week.

Has it been deployed to mainnet?

SwaroopH commented 2 years ago

All mumbai nodes behind MaticVigil RPC are on the new version. Can someone confirm by testing their mumbai endpoint? If it works well, we'll deploy it to mainnet next week.

Has it been deployed to mainnet?

@whitenode32 Yes, it should be live on our mainnet nodes now. Also, best to ping us via a ticket - the team cannot track external Github issues.

Deathwing commented 2 years ago

The issue seems not to be fixed on the polygon mainnet, I still get a tx hash, but most of the time I can't even see that TX on polygonscan, but if I see it, the from is still the null address...

mswezey23 commented 2 years ago

I just ran into this issue on Polygon mainnet as well. The same script works on Mumbai without flaw.

Going to make sure I've updated my NPM packages.

I cleared out my node_modules folder and updated to the latest version of hardhat and ethers. Same result. Tried a different RPC URL. Same result.

Tried the patch above about getting the tx hash instead, but it just hangs for 5+ minutes doing nothing.

Going to attempt the deploy plugin.

The hardhat deploy plugin works!

toledoroy commented 2 years ago

Same here. Tried using infura and alchemy. Hardhat script finished successfully and returns a contract instance with an address but there's nothing on the chain explorer. Works just fine on Rinkeby.