From 93b30d531d1bb1d766bdf2105cd04fa50fef12aa Mon Sep 17 00:00:00 2001 From: John Kane Date: Mon, 19 Aug 2024 14:25:24 +0100 Subject: [PATCH] fix: return no accounts if `eth_accounts` is deprecated Erigon now responds that `eth_accounts` is deprecated when we make an RPC call. Instead of erroring we now return an empty set of accounts from `getSigners` if `eth_accounts` is deprecated. Fixes #5572. --- .changeset/popular-ways-whisper.md | 5 +++++ .../hardhat-ethers/src/internal/helpers.ts | 15 ++++++++++++++- packages/hardhat-ethers/test/index.ts | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .changeset/popular-ways-whisper.md 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 () {