Using Hardhat to deploy smart contract to local Polygon node

Viewed 528

I have followed this tutorial to learn how can I use HARDHAT to deploy a Smart Contract on a Polygon testnet (and it worked just fine).

Now I want to run some tests on my local Polygon blockchain instance which is running and working fine on my local computer (with 4 nodes). I know it works because I can operate over it via jsonRPC and gRPG, consulting balances, status, etc.

So, in my hardhat.config.js I have this settings:

require("@nomiclabs/hardhat-ethers");
module.exports = {
  defaultNetwork: "matic",
  networks: {
    hardhat: {
    },
    matic: {
      url: "http://localhost:10002"
    }
  },
  solidity: {
    version: "0.8.0",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  mocha: {
    timeout: 20000
  }
}

I then compiled and tryed to deploy Hardhat's sample script:

$ npx hardhat compile
>Downloading compiler 0.8.0
>Compiled 2 Solidity files successfully



$ npx hardhat run scripts/sample-script.js --network matic

>ProviderError: the method eth_accounts does not exist/is not available
>    at HttpProvider.request (/home/edu/projects/test-hardhat->polygon/node_modules/hardhat/src/internal/core/providers/http.ts:74:19)
>    at GanacheGasMultiplierProvider.request (/home/edu/projects/test-hardhat->polygon/node_modules/hardhat/src/internal/core/providers/gas-providers.ts:312:34)

It seems Hardhat is calling the method eth_accounts which does not exist in my Polygon-Edge local blockchain.

What am I doing wrong?

Thanks in advance

1 Answers

good question... Ran into your question while troubleshooting the same issue. I'm running polygon-edge server --dev... and couldn't deploy smart contracts with hardhat or truffle. An alternative is to use Remix IDE and your wallet to deploy via Injected Web3 per the project's Does polygon-edge support smart contract? #411 discussion.

Deploy & Run Transactions within the Remix IDE, with selected environment with deploying via transaction.

Once completed you'll be able to review and approve the transaction within your wallet which in turn deploys the contract via transaction. Pause for a second and be vigilant of scammers, fake remix IDE clones, or incidentally deploying this transaction to a live network using real funds! With that said: how to import a network to Metamask if you have yet to do so. If you need an account with funds, what worked for me was importing into Metamask an account via private key for which I included premined currency. To retrieve the private key I referred to my validator node's data folder contained the private key at $data-dir/consensus/validator.key file.

Regarding the actual error and web3js.. The error message is accurate. If you revisit the polygon-edge docs referencing JSON RPC Commands note eth_accounts method call is missing. This is problematic for the underlying node modules relying on web3.eth.getAccounts() call setup for contract deployment, and in turn impacting truffle and hardhat.

Related