You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use an impersonated account via ethers.js with this function:
exportconstimpersonate=async(address: string,provider: ethers.JsonRpcProvider)=>{// Impersonate the accountawaitprovider.send("anvil_impersonateAccount",[address]);// Create a signer for the impersonated accountreturnawaitprovider.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);
The text was updated successfully, but these errors were encountered:
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
Component
Anvil
Have you ensured that all of these are up to date?
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:Which fails with following error (anvil output included as it gives a bit more context):
After digging a bit I think
ethers.js
expects all accounts the node can sign transactions as to be returned byeth_accounts
which conforms with the spec. However, when usinganvil_impersonateAccount
the list of addresses returned byeth_account
does not change which leads to this incompatibility withethers.js
. Although this seems to work when using anvil from rust as it's shown in thecan_impersonate_account
test.I adjusted the test with the provided diff to show that
anvil_impersonateAccount
indeed does not change the list reported byeth_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:
The text was updated successfully, but these errors were encountered: