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

impersonated accounts do not get returned by eth_accounts #5732

Closed
2 tasks done
MartinquaXD opened this issue Aug 26, 2023 · 0 comments
Closed
2 tasks done

impersonated accounts do not get returned by eth_accounts #5732

MartinquaXD opened this issue Aug 26, 2023 · 0 comments
Labels
T-bug Type: bug

Comments

@MartinquaXD
Copy link
Contributor

Component

Anvil

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (ae3ec74 2023-08-26T00:18:16.173100000Z)

What command(s) is the bug in?

No response

Operating System

macOS (Apple Silicon)

Describe the bug

I'm trying to use an impersonated account via ethers.js with this function:

export const impersonate = async (address: string, provider: ethers.JsonRpcProvider) => {
  // Impersonate the account
  await provider.send("anvil_impersonateAccount", [address]);

  // Create a signer for the impersonated account
  return await provider.getSigner(address);
}

Which fails with following error (anvil output included as it gives a bit more context):

Listening on 127.0.0.1:8545
eth_chainId
anvil_impersonateAccount
eth_accounts
eth_chainId
error: Uncaught (in promise) Error: invalid account
    at JsonRpcProvider.getSigner (file:///Users/martin/work/ethers/node_modules/.deno/ethers@6.7.1/node_modules/ethers/lib.esm/providers/provider-jsonrpc.js:717:15)
    at Object.runMicrotasks (ext:core/01_core.js:838:30)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:53:14)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:5)
    at eventLoopTick (ext:core/01_core.js:189:21)
    at async impersonate (file:///Users/martin/work/ethers/src/utils.ts:61:10)
    at async file:///Users/martin/work/ethers/src/test.ts:10:16

After digging a bit I think ethers.js expects all accounts the node can sign transactions as to be returned by eth_accounts which conforms with the spec. However, when using anvil_impersonateAccount the list of addresses returned by eth_account does not change which leads to this incompatibility with ethers.js. Although this seems to work when using anvil from rust as it's shown in the can_impersonate_account test.

I adjusted the test with the provided diff to show that anvil_impersonateAccount indeed does not change the list reported by eth_account. It looks in the code that impersonated accounts and the accounts managed by the node from the beginning are stored in 2 places (CheatsState::impersonated_accounts vs. EthApi::signers) which would both have to be queried for a fix.

test setup

Run test with: cargo test -- can_impersonate_account --nocapture

Diff:

diff --git a/crates/anvil/tests/it/anvil_api.rs b/crates/anvil/tests/it/anvil_api.rs
index a4213dc4..b4b1a93f 100644
--- a/crates/anvil/tests/it/anvil_api.rs
+++ b/crates/anvil/tests/it/anvil_api.rs
@@ -81,7 +81,10 @@ async fn can_impersonate_account() {
     let res = provider.send_transaction(tx.clone(), None).await;
     res.unwrap_err();
 
+    let before_impersonation = api.accounts().unwrap();
     api.anvil_impersonate_account(impersonate).await.unwrap();
+    let after_impersonation = api.accounts().unwrap();
+    assert_ne!(before_impersonation, after_impersonation);
 
     let res = provider.send_transaction(tx.clone(), None).await.unwrap().await.unwrap().unwrap();
     assert_eq!(res.from, impersonate);
@MartinquaXD MartinquaXD added the T-bug Type: bug label Aug 26, 2023
@gakonst gakonst added this to Foundry Aug 26, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Aug 26, 2023
@MartinquaXD MartinquaXD changed the title anvil_impersonateAccount does not list address in eth_accounts impersonated accounts do not get returned by eth_accounts Aug 26, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

1 participant