Skip to content

Commit

Permalink
Make eth_accounts return impersonated accounts (#5734)
Browse files Browse the repository at this point in the history
* Return impersonated accounts

* Return unique accounts in deterministic order
  • Loading branch information
MartinquaXD committed Aug 27, 2023
1 parent 7d21c64 commit bff4ed9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use foundry_evm::{
};
use futures::channel::mpsc::Receiver;
use parking_lot::RwLock;
use std::{sync::Arc, time::Duration};
use std::{collections::HashSet, sync::Arc, time::Duration};
use tracing::{trace, warn};

use super::{backend::mem::BlockRequest, sign::build_typed_transaction};
Expand Down Expand Up @@ -528,10 +528,18 @@ impl EthApi {
/// Handler for ETH RPC call: `eth_accounts`
pub fn accounts(&self) -> Result<Vec<Address>> {
node_info!("eth_accounts");
let mut unique = HashSet::new();
let mut accounts = Vec::new();
for signer in self.signers.iter() {
accounts.append(&mut signer.accounts());
accounts.extend(signer.accounts().into_iter().filter(|acc| unique.insert(*acc)));
}
accounts.extend(
self.backend
.cheats()
.impersonated_accounts()
.into_iter()
.filter(|acc| unique.insert(*acc)),
);
Ok(accounts)
}

Expand Down
5 changes: 5 additions & 0 deletions crates/anvil/src/eth/backend/cheats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ impl CheatsManager {
trace!(target: "cheats", "Auto impersonation set to {:?}", enabled);
self.state.write().auto_impersonate_accounts = enabled
}

/// Returns all accounts that are currently being impersonated.
pub fn impersonated_accounts(&self) -> HashSet<Address> {
self.state.read().impersonated_accounts.clone()
}
}

/// Container type for all the state variables
Expand Down
1 change: 1 addition & 0 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async fn can_impersonate_account() {
res.unwrap_err();

api.anvil_impersonate_account(impersonate).await.unwrap();
assert!(api.accounts().unwrap().contains(&impersonate));

let res = provider.send_transaction(tx.clone(), None).await.unwrap().await.unwrap().unwrap();
assert_eq!(res.from, impersonate);
Expand Down

0 comments on commit bff4ed9

Please sign in to comment.