wighawag / hardhat-deploy-ethers

MIT License
69 stars 25 forks source link

'getContract' function does not exist on HardhatRuntimeEnvironment.ethers #1

Closed naddison36 closed 3 years ago

naddison36 commented 3 years ago

I've installed the hardhat-deploy-ethers extension into my TyoeScript project and imported it in my hardhat.config.ts file

import "hardhat-deploy-ethers"

When I try and use the getContract function in a deploy script like the following

import { DeployFunction } from "hardhat-deploy/dist/types"
import { HardhatRuntimeEnvironment } from "hardhat/types"
import { CurvedMasset } from "types/generated"

const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
    const mBTC = (await hre.ethers.getContract("mBTC")) as CurvedMasset
}

I get the following TypeScript error

Property 'getContract' does not exist on type '{ provider: JsonRpcProvider; getContractFactory: { (name: string, signerOrOptions?: Signer | FactoryOptions): Promise<ContractFactory>; (abi: any[], bytecode: BytesLike, signer?: Signer): Promise<...>; (name: string, signerOrOptions?: string | ... 1 more ... | FactoryOptions): Promise<...>; (abi: any[], bytecode: By...'. Did you mean 'getContractAt'?ts(2551)

If I put in a // @ts-ignore before the line with the getContract function, the deploy scripts work fine.

My tsconfig.ts file looks like

{
  "compilerOptions": {
    "outDir": "./transpiled/",
    "sourceMap": true,
    "noImplicitAny": false,
    "lib": ["dom", "es2015", "es2016", "es2017"],
    "module": "CommonJS",
    "baseUrl": ".",
    "rootDir": ".",
    "paths": {
      "@utils/*": ["test-utils/*"],
    },
    "allowJs": true,
    "target": "ES5",
    "skipLibCheck": true,
    "typeRoots": [
      "node_modules/@0x/typescript-typings/types",
      "node_modules/@types"
    ],
    "esModuleInterop": true
  },
  "include": [
    "./deploy/**/*.ts",
    "./scripts/**/*.ts",
    "./types/**/*.ts",
    "./test/**/*.ts",
    "./test-utils/**/*.ts"
  ],
  "files": ["./hardhat.config.ts"]
}
wighawag commented 3 years ago

Could it be that you also use a plugin that depends on @nomiclabs/hardhat-ethers ?

If so, you ll need to alias @nomiclabs/hardhat-ethers to hardhat-ethers-deploy This might not solve the typing issue, but worth a try

With npm : npm i -D @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers

Recently hardhat team made @nomiclabs/hardhat-ethers itself extensible so my plan is to update hardhat-deploy-ethers to extend it instead of being a fork of it. No timeline yet though.

naddison36 commented 3 years ago

Thanks, @wighawag. Using the above alias worked

tennox commented 3 years ago

It didn't fully work for me though... yarn add @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers

warning Pattern ["@nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers"] is trying to unpack in the same destination "/home/manu/.cache/yarn/v6/npm-@nomiclabs-hardhat-ethers-0.3.0-beta.7-860d86b84ed3c4fdef64283ccf1e8d5623454d02-integrity/node_modules/@nomiclabs/hardhat-ethers" as pattern ["hardhat-deploy-ethers@^0.3.0-beta.7"]. This could result in non-deterministic behavior, skipping. ... warning " > @nomiclabs/hardhat-waffle@2.0.1" has incorrect peer dependency "@nomiclabs/hardhat-ethers@^2.0.0".

yarn upgrade-interactive

error Couldn't find any versions for "@nomiclabs/hardhat-ethers" that matches "^0.3.0-beta.7"

package.json

{
  "dependencies": {
    "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
    "hardhat": "^2.1.2",
    "hardhat-deploy": "^0.7.0-beta.56",
    "hardhat-deploy-ethers": "^0.3.0-beta.7"
  }
}

Any ideas?

wighawag commented 3 years ago

you need to remove that line "hardhat-deploy-ethers": "^0.3.0-beta.7"

and then you need to import @nomiclabs/hardhat-ethers in your hardhat.config.js instead of hardhat-deploy-ethers. I need to add that to the doc actually.

rodrigoherrerai commented 2 years ago

For people experiencing this issue in the future, you can implement this workaround instead of using ethers.getContract(), and it should be the same.


 await deployments.fixture(["MyContract"]);

 const myContract = await deployments.get("MyContract");

 const contract = await ethers.getContractAt(
      myContract.abi,
      myContract.address
    );
  });
dumindapium commented 2 years ago

For people experiencing this issue in the future, you can implement this workaround instead of using ethers.getContract(), and it should be the same.

 await deployments.fixture(["MyContract"]);

 const myContract = await deployments.get("MyContract");

 const contract = await ethers.getContractAt(
      myContract.abi,
      myContract.address
    );
  });

Thanks rodrigoherrerai, this worked perfectly.

mauricedesaxe commented 1 year ago

This:

With npm : npm i -D @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers

With this:

yarn upgrade-interactive

Fixed it for me in 2 repos already 🙏🏻

usman571 commented 1 year ago

npm i -D @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers

Thanks bro you save me

PrAtikDubariya commented 10 months ago

you need to remove that line "hardhat-deploy-ethers": "^0.3.0-beta.7"

and then you need to import @nomiclabs/hardhat-ethers in your hardhat.config.js instead of hardhat-deploy-ethers. I need to add that to the doc actually.

Thanks bro

mohamed-Decentralized commented 6 months ago
   deployer = (await getNamedAccounts()).deployer;
    await deployments.fixture(["all"]);
    console.log('deployer',deployer)
    // fundMe = await ethers.getContract("FundMe", deployer);
     const contract = await deployments.get("FundMe");

  fundMe = await ethers.getContractAt(
      contract.abi,
      contract.address
    );
  });
how to apply the deployer though