-
Notifications
You must be signed in to change notification settings - Fork 265
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
[Bug] anvil impersonations fail when using a WalletFiller
in the provider
#1918
Comments
For now, I'm using a wrapper function like: #[inline]
pub async fn anvil_impersonated_tx(
&self,
tx: TransactionRequest,
from: Address,
) -> Result<PendingTransactionBuilder<AnvilTransport, AnvilNetwork>> {
// create a provider, without any `WalletFiller` otherwise there is a bug with impersonations
// see: https://github.com/alloy-rs/alloy/issues/1918
let anvil_provider = ProviderBuilder::new().on_anvil();
anvil_provider.anvil_impersonate_account(from).await?;
let pending_tx = anvil_provider.send_transaction(tx.from(from)).await?;
anvil_provider
.anvil_stop_impersonating_account(from)
.await?;
Ok(pending_tx)
} in my class. The struct here already has a provider btw, but it has |
@erhant //...
.on_anvil_with_wallet_and_config(|anvil| anvil.arg("--auto-impersonate")); |
Thanks @yash-atreya for the suggestion; sadly, it fails with the same error "Missing signing credential ...". To try, check the test at: https://github.com/alloy-rs/alloy/blob/main/crates/provider/src/ext/anvil.rs#L370 and run it with: - let provider = ProviderBuilder::new().on_anvil();
+ let provider = ProviderBuilder::new()
+ .with_recommended_fillers()
+ .on_anvil_with_wallet_and_config(|anvil| anvil.arg("--auto-impersonate")); |
Sorry I misread
The above error is indicative of the waller not being setup properly. It is unrelated to anvil, rather the wallet you pass to When you're using You need to do : let pk = PrivateKeySigner::random();
let wallet = EthereumWallet::from(pk); // `from` used in tx request
let provider = ProviderBuilder::new()
.wallet(wallet)
.on_anvil_with_config(|anvil| anvil.fork(fork_url).arg("--auto-impersonate")); |
WalletFiller
in the providerWalletFiller
in the provider
I know, but the thing is im fork-testing a blockchain and I need to impersonate an account that I do not have the key for, and therefore can not create the wallet for; thats the whole point of why I want to impersonate as well. Naturally, I am unable to create the signer to the |
Aah, my bad. Yeah this is not ideal. |
On second thought, @erhant. Unsure why you want to use the Like you suggested earlier, you could just use let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_config(|anvil| anvil.fork(url).arg("--auto-impersonate")); without the need to instantiate the provider with a wallet. Agreed that we could improve docs here |
I have a provider that has some wallet, and I want that wallet to be present as I'm doing tests. However, within these tests there may be some steps that I need to impersonate. e.g. I create a random So I guess you are saying I should not use the wallet at all within the tests, impersonate everything even if I have its wallet? |
One last update, the code below fixes it more correctly (unlike the one at #1918 (comment)): pub async fn anvil_impersonated_tx(
&self,
tx: TransactionRequest,
from: Address,
) -> Result<PendingTransactionBuilder<Http<Client>, Ethereum>> {
let anvil = ProviderBuilder::new()
.on_http(format!("http://localhost:{}", Self::ANVIL_PORT).parse()?);
anvil.anvil_impersonate_account(from).await?;
let pending_tx = anvil.send_transaction(tx.from(from)).await?;
anvil.anvil_stop_impersonating_account(from).await?;
Ok(pending_tx)
} This is done while I create another provider shared all around the application with: let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet)
.on_anvil_with_config(|anvil| {
anvil.fork(rpc_url).port(Self::ANVIL_PORT)
}); This way I can connect to the existing Anvil for impersonated requests just fine, while being able to use wallet for everything else and even create tx's using that and pass them to EDIT: we could use Anvil wallet here as well instead of passing our own, but I prefer this so that I can continue using my private key for forked-tests like I would on the actual application. |
Component
provider, pubsub, signers
What version of Alloy are you on?
alloy v0.8.0
Operating System
macOS (Apple Silicon)
Describe the bug
Using Anvil, we can impersonate accounts without knowing their private keys using
anvil_impersonate_account
,anvil_stop_impersonating_account
andanvil_auto_impersonate_account
.The tests here and here use the following provider:
However, if you use a provider that makes use of a
WalletFiller
filler, impersonations do not work! Consider the following provider:Now, if we try to use an impersonated account within the
from
address, we get the error:I am not yet clear on how one may handle this issue, I guess we have several options:
WalletFiller
so that their signers are not checked explicitlyThe text was updated successfully, but these errors were encountered: