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

feat(anvil): Anvil --unlocked for auto-impersonation on instance creation #5335

Merged
merged 4 commits into from
Jul 8, 2023

Conversation

Sabnock01
Copy link
Contributor

@Sabnock01 Sabnock01 commented Jul 8, 2023

Motivation

It should be possible to auto-impersonate accounts upon the initial spawning of Anvil instances.

Solution

This PR introduces an --auto-impersonate flag which enables auto impersonation of accounts when the anvil instance is created. This helps avoid the need to use curl or cast rpc to set anvil_autoImpersonateAccount to true when the Anvil instance is spawned. Resolves #5334

anvil/src/cmd.rs Outdated

/// Enable autoImpersonate on startup
#[clap(long, short, visible_alias = "unlocked")]
pub unlocked: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--unlocked already has a slightly different meaning in cast,
perhaps we rename this to --auto-impersonate instead?

@mattsse mattsse added the C-anvil Command: anvil label Jul 8, 2023
@Sabnock01
Copy link
Contributor Author

Question, would the call to the EthApi::anvil_auto_impersonate_account method be done in spawn or somewhere else?

@mattsse
Copy link
Member

mattsse commented Jul 8, 2023

we can either call the rpc handler after creating the EthApi type:

foundry/anvil/src/lib.rs

Lines 148 to 159 in c78a811

// create the cloneable api wrapper
let api = EthApi::new(
Arc::clone(&pool),
Arc::clone(&backend),
Arc::new(signers),
fee_history_cache,
fee_history_service.fee_history_limit(),
miner.clone(),
logger,
filters.clone(),
transaction_order,
);

foundry/anvil/src/eth/api.rs

Lines 1382 to 1389 in c78a811

/// If set to true will make every account impersonated
///
/// Handler for ETH RPC call: `anvil_autoImpersonateAccount`
pub async fn anvil_auto_impersonate_account(&self, enabled: bool) -> Result<()> {
node_info!("anvil_autoImpersonateAccount");
self.backend.auto_impersonate_account(enabled).await?;
Ok(())
}

or directly on the backend

/// If set to true will make every account impersonated
pub async fn auto_impersonate_account(&self, enabled: bool) -> DatabaseResult<()> {
self.cheats.set_auto_impersonate_account(enabled);
Ok(())
}

these are actually infallible and I think we should remove the result, at least for the backend function

so lets do:

  1. remove result return type from backend::auto_impersonate_account
  2. call backend::auto_impersonate_account(true) if config.auto_impersonate

@Sabnock01 Sabnock01 marked this pull request as ready for review July 8, 2023 12:36
@Sabnock01
Copy link
Contributor Author

Sabnock01 commented Jul 8, 2023

Thanks! This was very helpful. I've tested this locally through cast send commands and it works. Let me know if there's anything else I should add!

@Sabnock01 Sabnock01 changed the title [WIP]: Anvil --unlocked for auto-impersonation on instance creation feat(anvil): Anvil --unlocked for auto-impersonation on instance creation Jul 8, 2023
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, one nit

anvil/src/lib.rs Outdated Show resolved Hide resolved
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
@mattsse mattsse merged commit 60e1a74 into foundry-rs:master Jul 8, 2023
@Sabnock01 Sabnock01 deleted the sabnock/anvil-unlocked branch July 8, 2023 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-anvil Command: anvil
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: --unlocked flag for Anvil which enables autoImpersonate on startup
2 participants