1

This JavaScript test code is from a course I'm undertaking:

before(async () => {
        const Collectible = await ethers.getContractFactory("Collectible");
        collectible = await Collectible.deploy();
        
        [owner, a2, a3] = await ethers.provider.listAccounts();
        oneEther = parseEther("1.0");
        halfEther = parseEther("0.5");
    });

And works fine in their environment. We just use a web view and I can't see what they are doing behind the scenes.

I tried to bring this in to my IDE but even running npx hardhat test gives the error:

     TypeError: ethers.provider.listAccounts is not a function

So I started again and just used the provided contract/Lock.sol and test/Lock.js with:

mkdir testDir && cd testDir
npm init -y
npm i -D hardhat
npx hardhat init  # and said yes to everything

I updated the test to contain:

async function deployOneYearLockFixture() {
    const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
    const ONE_GWEI = 1_000_000_000;

    const lockedAmount = ONE_GWEI;
    const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;

    // Contracts are deployed using the first signer/account by default
    const [owner, otherAccount] = await ethers.getSigners();

    const Lock = await ethers.getContractFactory("Lock");
    const lock = await Lock.deploy(unlockTime, { value: lockedAmount });

    const [owner2, b2, b3] = await ethers.provider.listAccounts();
    console.log(`owner: ${owner2}, address: ${owner2.getAddress()}`)

    return { lock, unlockTime, lockedAmount, owner, otherAccount };
  }

(Adding the await ethers.provider.listAccounts() and console.log lines).

Running this also gives that error:

 1) Lock
       Deployment
         Should set the right unlockTime:
     TypeError: ethers.provider.listAccounts is not a function
      at deployOneYearLockFixture (test/Lock.js:26:56)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at loadFixture (node_modules/@nomicfoundation/hardhat-network-helpers/src/loadFixture.ts:59:18)
      at Context.<anonymous> (test/Lock.js:34:36)

It is not the provider as such since this Lock.js test also contains:

      expect(await ethers.provider.getBalance(lock.target)).to.equal(
        lockedAmount
      );

It is like something is missing from the configuration like the following but nothing is standing out in the Hardhat documentation:

extendEnvironment((hre) => {
  const Web3 = require("web3");
  hre.Web3 = Web3;

  // hre.network.provider is an EIP1193-compatible provider.
  hre.web3 = new Web3(hre.network.provider);
});

This is from https://hardhat-lime.vercel.app/hardhat-runner/docs/advanced/hardhat-runtime-environment#extending-the-hre but then why is ethers.provider.getBalance() working ok - so it doesn't seem to be the provider.

Am I missing something in the configuration, or just misunderstanding something?

I dug a bit deeper:

npx hardhat console
> ethers
...

  wordlists: {
    cz: LangCz { locale: 'cz' },
    en: LangEn { locale: 'en' },
    es: LangEs { locale: 'es' },
    fr: LangFr { locale: 'fr' },
    it: LangIt { locale: 'it' },
    pt: LangPt { locale: 'pt' },
    ja: LangJa { locale: 'ja' },
    ko: LangKo { locale: 'ko' },
    zh_cn: LangZh { locale: 'zh_cn' },
    zh_tw: LangZh { locale: 'zh_tw' }
  },
  provider: HardhatEthersProvider {
    _hardhatProvider: LazyInitializationProviderAdapter {
      _providerFactory: [AsyncFunction (anonymous)],
      _emitter: [EventEmitter],
      _initializingPromise: [Promise],
      provider: [BackwardsCompatibilityProviderAdapter]
    },
    _networkName: 'hardhat',
    _blockListeners: [],
    _transactionHashListeners: Map(0) {},
    _eventListeners: []
  },
  getSigner: [Function: getSigner],
  getSigners: [Function: getSigners],
...

Maybe the LazyInitializationProviderAdapter is not working properly?

Configuration

  • Node: v18.17.1
  • Hardhat: 2.18.3 and also 2.17.4 tested
  • Hardhat brings in ethers: 6.8.0 for both

I found https://github.com/ethers-io/ethers.js/issues/3778 "Ethers v6: missing listAccounts() function for browserProvider" but according to that issue it was fixed in 6.1. Of course I"m not trying to use the BrowserProvider. Is this the problem - that I'm meant to select the local default provider?

Any help appreciated.


More info:

  • indeed it seems like the default Hardhat provider does not have a listAccounts() function.

IDE right-clicking on ethers gets me to:

node_modules/@nomicfoundation/hardhat-ethers/src/internal/type-extensions.ts

Then to:

node_modules/@nomicfoundation/hardhat-ethers/src/types/index.ts

export interface HardhatEthersHelpers {
  provider: HardhatEthersProvider;

  ...
}

Then to:

node_modules/@nomicfoundation/hardhat-ethers/src/internal/hardhat-ethers-provider.ts

export class HardhatEthersProvider implements ethers.Provider {
  private _isHardhatNetworkCached: boolean | undefined;

  ...

  public async getBalance(
    address: AddressLike,
    blockTag?: BlockTag | undefined
  ): Promise<bigint> {
    ...
  }

}

But there is no listAccounts() in that. So that answers part of the question.

So where is it ..

The only location that function is found is:

node_modules/ethers/src.ts/providers/provider-jsonrpc.ts

export abstract class JsonRpcApiProvider extends AbstractProvider {

  ...
}

And the only places that extend this are:

provider-browser.js
* 12:class BrowserProvider extends provider_jsonrpc_js_1.JsonRpcApiPollingProvider {

provider-socket.js
155:class SocketProvider extends provider_jsonrpc_js_1.JsonRpcApiProvider {

provider-jsonrpc.js
* 787:class JsonRpcProvider extends JsonRpcApiPollingProvider {

OK, so it seems I need to create one of these providers. Let me work on that ...

NUP can't get that working either.

I tried adding this to hardhat.config.js

require("@nomicfoundation/hardhat-ethers");

But make no difference.

Any ideas would be much appreciated!!!!!!!!!!!!!!!!!!!!!!

1
  • I created the sample Hardhat typescript project, thus giving my IDE a bit more insight. It agrees that there is no ethers.provider.listAccounts(). However it obviously exists and I'm curious as to how to get there.
    – HankCa
    Oct 30, 2023 at 23:41

2 Answers 2

1

Ok, egg on face (palm) time!

listAccounts() is Ethers5 only - https://docs.ethers.org/v5/search/?search=listAccounts and not available under v6, which I'm trying to only use.

Mon in the Alchemy Discord suggested using ethers.provider.getSigners() [in v6].


BTW I had spent some time upgrading the code Alchemy provided us to Ethers6, which was a good exercise but took way too much time (when done over and over for each exercise and project).

I moved to installing the hardhat that uses ether's 5:

npm i [email protected]
1

I'm totally new to Ethereum and I just faced the same issue in ethers v6.10.0

ethers.provider.listAccounts is not a function

I was able to get the list of accounts by using:

const provider = new ethers.JsonRpcProvider();
const accounts = await provider.listAccounts();
1
  • That's awesome. Most of times I feel myself a POW miner that searching correct information in the God damn old documents. Mar 27 at 14:08

Your Answer

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

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