Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using .connect() causes type errors #6126

Open
kyrers opened this issue Jan 8, 2025 · 7 comments
Open

Using .connect() causes type errors #6126

kyrers opened this issue Jan 8, 2025 · 7 comments
Assignees
Labels
status:needs-more-info There's not enough information to start working on this issue

Comments

@kyrers
Copy link

kyrers commented Jan 8, 2025

Version of Hardhat

2.22.17

What happened?

I was writing some tests for ERC20 tokens and tried to use .connect() . I immediately got type errors saying that Property 'mint' does not exist on type 'BaseContract'.

I assumed I was doing something wrong, so I set up a new hardhat project using typescript and changed nothing, to my surprise the error was there too.

image

Minimal reproduction steps

Just follow the guide in the docs to create a typescript project and the error will be in the Lock.ts test file.

Search terms

connect, type errors

@schaable
Copy link
Member

schaable commented Jan 9, 2025

Hi @kyrers, have you tried compiling the contract? This issue is typically caused by the types not being available, which often happens when the contracts have not been compiled yet.

When starting a new Hardhat project, the example contract is available, but you still need to compile it. Try running npx hardhat compile and then reloading your VS Code to see if the error is resolved.

@schaable schaable added status:needs-more-info There's not enough information to start working on this issue and removed status:triaging labels Jan 9, 2025
@kyrers
Copy link
Author

kyrers commented Jan 9, 2025

Thank you for the answer @schaable. You are correct that running npx hardhat compile solves the error in the default project, so I assume the problems I am facing are related to ignition. Here is my package.json:

{
  "devDependencies": {
    "@nomicfoundation/hardhat-ignition": "^0.15.9",
    "@nomicfoundation/hardhat-ignition-ethers": "^0.15.9",
    "@nomicfoundation/hardhat-toolbox": "^5.0.0",
    "ethers": "^6.13.5",
    "hardhat": "^2.22.17"
  },
  "dependencies": {
    "@openzeppelin/contracts": "^5.1.0"
  }
}

This is my hardhat-config.ts:

import type { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "@nomicfoundation/hardhat-ignition-ethers";

const config: HardhatUserConfig = {
  solidity: "0.8.28",
  networks: {
    hardhat: {
      chainId: 1337,
    },
  },
};

export default config;

I followed the documentation to the best of my knowledge. I also ran npx hardhat compile again on my project just to be sure, but the original error is still present. Additionally, a new error shows up when I have the typechain-types folder: Property 'getSigners' does not exist on type 'typeof import("/xxx/xxx/xxx/project/node_modules/.pnpm/[email protected]/node_modules/ethers/lib.commonjs/ethers") & HardhatEthersHelpers'.ts(2339).

Here is the fixture I am using for the tests where this error shows up:
image

You'll note that I am using pnpm, however I am not sure how that could be the reason. Please let me know if you need me to provide more information.


EDIT: I tried npm instead of pnpm and the ethers.getSigners error goes away. However, I still have the original error. I probably should've shared this before, but it appears I am not the first person to run into this, see #5112

@kyrers
Copy link
Author

kyrers commented Jan 9, 2025

Ok, more information.

As I said in the previous comment, I managed to get rid of the ethers.getSigners error by switching from pnpm to npm.

Now, I also managed to get rid of the error Property 'mint' does not exist on type 'BaseContract' by not using the ignition modules to deploy the contracts in the fixture I showed above. Instead, if I use factories like below, it works and type errors are gone:

const TokenA = await ethers.getContractFactory("TokenA");
const tokenA = await TokenA.deploy(owner.address);

const TokenB = await ethers.getContractFactory("TokenB");
const tokenB = await TokenB.deploy(owner.address);

const SimpleDex = await ethers.getContractFactory("SimpleDex");
const simpleDex = await SimpleDex.deploy(tokenA.target, tokenB.target);

My new question is - did I make a mistake or are you aware of these issues? I followed these docs https://hardhat.org/ignition/docs/guides/tests#using-ignition-modules-as-fixtures which seem to suggest that what I was doing (using ignition to deploy modules in tests) is possible.

@kanej
Copy link
Member

kanej commented Jan 9, 2025

Is the issue here not that .connect(...) in ethers returns a BaseContract type and not the fully enhanced type from typechain?

So lock.withdraw() types correctly but lock.connect(someAccount).withdraw() does not.

@kyrers
Copy link
Author

kyrers commented Jan 9, 2025

As I see it, there are two issues here:

  1. The .connect(...) issue. I agree with your assessment, that's why I changed the fixture to use the contract factories to deploy instead of the ignition modules. That fixes the type issues, but according to ignition documentation, what I was doing should be possible, unless I made some mistake.

  2. Using pnpm according to the docs also results in errors, like the ethers.getSigners(). I changed back to npm and it went way. Not sure if this second issue is strictly related to hardhat though.

@kanej
Copy link
Member

kanej commented Jan 10, 2025

I am wondering if we are getting a clash in ethers version dependencies.

Can you update your dependencies and reinstall node_modules to:

{
  "devDependencies": {
    "@nomicfoundation/hardhat-toolbox": "^5.0.0",
    "ethers": "^6.13.5",
    "hardhat": "^2.22.17"
  },
  "dependencies": {
    "@openzeppelin/contracts": "^5.1.0"
  }
}

My thinking here is maybe you are getting a clash between the toolbox and the explicit ignition versions (hardhat-ignition-ethers is included in the toolbox).

@kyrers
Copy link
Author

kyrers commented Jan 10, 2025

So, two things happen with the package.json you suggested:

  1. It works for the .connect(...) issue, as long as I use factories instead of ignition modules to deploy when testing. That was already the case with my package.json, so I wouldn't say that is the problem;

  2. If I use your package.json with pnpm I can no longer use ignition modules because this import fails: import { buildModule } from "@nomicfoundation/hardhat-ignition/modules"; Additionally, the ethers.getSigners() issue is back. Weirdly though, if I use npm, the two errors I mentioned above go away.

You can even simplify that package.json further:

{
  "devDependencies": {
    "@nomicfoundation/hardhat-toolbox": "^5.0.0",
    "hardhat": "^2.22.17"
  },
  "dependencies": {
    "@openzeppelin/contracts": "^5.1.0"
  }
}

With this simple package.json I can get past all type errors under 2 conditions:

  1. In tests, I must use factories to deploy, not ignition modules;
  2. I must use npm instead of pnpm;

I guess the real questions here are:

  1. Are we supposed to be able to use ignition modules as fixtures when deploying for tests? If I do that, I inevitably get the .connect(...) error. I am forced to use factories.
  2. Why this npm vs pnpm difference and is this even a hardhat issue?

@kanej kanej assigned kanej and unassigned schaable Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:needs-more-info There's not enough information to start working on this issue
Projects
Status: Backlog
Development

No branches or pull requests

3 participants