diff --git a/.changeset/popular-ways-whisper.md b/.changeset/popular-ways-whisper.md new file mode 100644 index 0000000000..e3c319465d --- /dev/null +++ b/.changeset/popular-ways-whisper.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/hardhat-ethers": patch +--- + +Fix for `getSigners` against networks where `eth_accounts` is deprecated. diff --git a/packages/hardhat-ethers/src/internal/helpers.ts b/packages/hardhat-ethers/src/internal/helpers.ts index bd3bc89ff2..3710b05b73 100644 --- a/packages/hardhat-ethers/src/internal/helpers.ts +++ b/packages/hardhat-ethers/src/internal/helpers.ts @@ -40,7 +40,20 @@ function isArtifact(artifact: any): artifact is Artifact { export async function getSigners( hre: HardhatRuntimeEnvironment ): Promise { - const accounts: string[] = await hre.ethers.provider.send("eth_accounts", []); + let accounts: string[]; + + try { + accounts = await hre.ethers.provider.send("eth_accounts", []); + } catch (error) { + if ( + error instanceof Error && + /the method has been deprecated: eth_accounts/.test(error.message) + ) { + return []; + } + + throw error; + } const signersWithAddress = await Promise.all( accounts.map((account) => getSigner(hre, account)) diff --git a/packages/hardhat-ethers/test/index.ts b/packages/hardhat-ethers/test/index.ts index 46afc46c1a..e66915735f 100644 --- a/packages/hardhat-ethers/test/index.ts +++ b/packages/hardhat-ethers/test/index.ts @@ -73,6 +73,25 @@ describe("Ethers plugin", function () { "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" ); }); + + it("should return an empty array of signers if `eth_accounts` is deprecated", async function () { + const originalSend = this.env.ethers.provider.send; + + this.env.ethers.provider.send = async function ( + method: string, + params: any + ) { + if (method === "eth_accounts") { + throw new Error("the method has been deprecated: eth_accounts"); + } + + return originalSend.call(this, method, params); + }; + + const sigs = await this.env.ethers.getSigners(); + + assert.deepStrictEqual(sigs, []); + }); }); describe("getImpersonatedSigner", function () {