Skip to content

Commit 47610ab

Browse files
authored
Migrate evm_ (impersonate, automine, etc) APIs to alloy (#4004)
# Description Discussed in this thread: #4001 (comment) This PR moves the impersonation mechanisms to alloy, the migrated ones are only impersonated in the specific transaction, no changes to the actual tests were needed. Exposed the previous string oriented API under a trait to as discussed in a comment below. Anvil provides an anvil specific API that we're not using unless necessary as these ones are more general # Changes - [ ] Migrate the last `mine_pending_block` - [ ] Migrate the other evm calls to the new trait - [ ] Migrate the last impersonations - [ ] Remove the forked_node and local_node modules ## How to test Existing tests
1 parent d4f3810 commit 47610ab

File tree

12 files changed

+155
-230
lines changed

12 files changed

+155
-230
lines changed

crates/driver/src/tests/setup/blockchain.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use {
2424
},
2525
ethrpc::{
2626
Web3,
27-
alloy::{CallBuilderExt, ProviderExt},
27+
alloy::{CallBuilderExt, EvmProviderExt, ProviderExt},
2828
},
2929
futures::Future,
3030
solvers_dto::solution::Flashloan,
@@ -859,11 +859,7 @@ impl Blockchain {
859859
}
860860

861861
pub async fn set_auto_mining(&self, enabled: bool) {
862-
self.web3
863-
.alloy
864-
.raw_request::<_, ()>("evm_setAutomine".into(), (enabled,))
865-
.await
866-
.unwrap();
862+
self.web3.alloy.evm_set_automine(enabled).await.unwrap();
867863
}
868864
}
869865

crates/e2e/src/nodes/forked_node.rs

Lines changed: 0 additions & 64 deletions
This file was deleted.

crates/e2e/src/nodes/local_node.rs

Lines changed: 0 additions & 81 deletions
This file was deleted.

crates/e2e/src/nodes/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
pub mod forked_node;
2-
pub mod local_node;
3-
41
/// The default node URL that should be used for e2e tests.
52
pub const NODE_HOST: &str = "http://127.0.0.1:8545";
63

crates/e2e/src/setup/onchain_components/mod.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use {
2-
crate::{
3-
nodes::forked_node::ForkedNodeApi,
4-
setup::{DeployedContracts, deploy::Contracts},
5-
},
2+
crate::setup::{DeployedContracts, deploy::Contracts},
63
::alloy::{
74
network::{Ethereum, NetworkWallet, TransactionBuilder},
85
primitives::{Address, U256},
9-
providers::{Provider, ext::AnvilApi},
6+
providers::{
7+
Provider,
8+
ext::{AnvilApi, ImpersonateConfig},
9+
},
1010
rpc::types::TransactionRequest,
1111
signers::local::PrivateKeySigner,
1212
},
@@ -16,11 +16,7 @@ use {
1616
GPv2AllowListAuthentication::GPv2AllowListAuthentication,
1717
test::CowProtocolToken,
1818
},
19-
ethrpc::alloy::{
20-
CallBuilderExt,
21-
ProviderSignerExt,
22-
conversions::{IntoAlloy, IntoLegacy},
23-
},
19+
ethrpc::alloy::{CallBuilderExt, ProviderSignerExt},
2420
hex_literal::hex,
2521
model::{
2622
DomainSeparator,
@@ -283,22 +279,9 @@ impl OnchainComponents {
283279
with_wei: U256,
284280
) -> [TestAccount; N] {
285281
let authenticator = &self.contracts.gp_authenticator;
282+
let auth_manager = authenticator.manager().call().await.unwrap();
286283

287-
let auth_manager = authenticator.manager().call().await.unwrap().into_legacy();
288-
289-
let forked_node_api = self.web3.api::<ForkedNodeApi<_>>();
290-
291-
forked_node_api
292-
.set_balance(&auth_manager, 100u64.eth().into_legacy())
293-
.await
294-
.expect("could not set auth_manager balance");
295-
296-
let impersonated_authenticator = {
297-
forked_node_api
298-
.impersonate(&auth_manager)
299-
.await
300-
.expect("could not impersonate auth_manager");
301-
284+
let gpv2_auth = {
302285
// we create a new provider without a wallet so that
303286
// alloy does not try to sign the tx with it and instead
304287
// forwards the tx to the node for signing. This will
@@ -310,21 +293,43 @@ impl OnchainComponents {
310293
let solvers = self.make_accounts::<N>(with_wei).await;
311294

312295
for solver in &solvers {
313-
impersonated_authenticator
314-
.addSolver(solver.address())
315-
.from(auth_manager.into_alloy())
316-
.send_and_watch()
296+
self.web3
297+
.alloy
298+
.anvil_send_impersonated_transaction_with_config(
299+
gpv2_auth
300+
.addSolver(solver.address())
301+
.from(auth_manager)
302+
.into_transaction_request(),
303+
ImpersonateConfig {
304+
fund_amount: Some(100u64.eth()),
305+
stop_impersonate: true,
306+
},
307+
)
317308
.await
318-
.expect("failed to add solver");
309+
.unwrap()
310+
.watch()
311+
.await
312+
.unwrap();
319313
}
320314

321315
if let Some(router) = &self.contracts.flashloan_router {
322-
impersonated_authenticator
323-
.addSolver(*router.address())
324-
.from(auth_manager.into_alloy())
325-
.send_and_watch()
316+
self.web3
317+
.alloy
318+
.anvil_send_impersonated_transaction_with_config(
319+
gpv2_auth
320+
.addSolver(*router.address())
321+
.from(auth_manager)
322+
.into_transaction_request(),
323+
ImpersonateConfig {
324+
fund_amount: Some(100u64.eth()),
325+
stop_impersonate: true,
326+
},
327+
)
326328
.await
327-
.expect("failed to add flashloan wrapper");
329+
.unwrap()
330+
.watch()
331+
.await
332+
.unwrap();
328333
}
329334

330335
solvers

crates/e2e/tests/e2e/place_order_with_quote.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use {
22
::alloy::primitives::U256,
33
driver::domain::eth::NonZeroU256,
4-
e2e::{nodes::local_node::TestNodeApi, setup::*},
5-
ethrpc::alloy::CallBuilderExt,
4+
e2e::setup::*,
5+
ethrpc::alloy::{CallBuilderExt, EvmProviderExt},
66
model::{
77
order::{OrderCreation, OrderKind},
88
quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount},
@@ -57,8 +57,8 @@ async fn place_order_with_quote(web3: Web3) {
5757
services.start_protocol(solver.clone()).await;
5858

5959
// Disable auto-mine so we don't accidentally mine a settlement
60-
web3.api::<TestNodeApi<_>>()
61-
.set_automine_enabled(false)
60+
web3.alloy
61+
.evm_set_automine(false)
6262
.await
6363
.expect("Must be able to disable automine");
6464

@@ -140,8 +140,8 @@ async fn disabled_same_sell_and_buy_token_order_feature(web3: Web3) {
140140
services.start_protocol(solver.clone()).await;
141141

142142
// Disable auto-mine so we don't accidentally mine a settlement
143-
web3.api::<TestNodeApi<_>>()
144-
.set_automine_enabled(false)
143+
web3.alloy
144+
.evm_set_automine(false)
145145
.await
146146
.expect("Must be able to disable automine");
147147

crates/e2e/tests/e2e/refunder.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use {
22
crate::ethflow::{EthFlowOrderOnchainStatus, ExtendedEthFlowOrder},
3-
::alloy::primitives::Address,
3+
::alloy::{primitives::Address, providers::ext::AnvilApi},
44
chrono::{TimeZone, Utc},
5-
e2e::{nodes::local_node::TestNodeApi, setup::*},
6-
ethrpc::{Web3, block_stream::timestamp_of_current_block_in_seconds},
5+
e2e::setup::*,
6+
ethrpc::{Web3, alloy::EvmProviderExt, block_stream::timestamp_of_current_block_in_seconds},
77
model::quote::{OrderQuoteRequest, OrderQuoteSide, QuoteSigningScheme, Validity},
88
number::{nonzero::NonZeroU256, units::EthUnit},
99
refunder::refund_service::RefundService,
@@ -106,16 +106,21 @@ async fn refunder_tx(web3: Web3) {
106106
.unwrap();
107107

108108
let time_after_expiration = valid_to as i64 + 60;
109-
web3.api::<TestNodeApi<_>>()
110-
.set_next_block_timestamp(
111-
&Utc.timestamp_millis_opt(time_after_expiration * 1_000)
112-
.unwrap(),
109+
web3.alloy
110+
.evm_set_next_block_timestamp(
111+
u64::try_from(
112+
Utc.timestamp_millis_opt(time_after_expiration * 1_000)
113+
.unwrap()
114+
.timestamp(),
115+
)
116+
.expect("timestamp should be positive"),
113117
)
114118
.await
115119
.expect("Must be able to set block timestamp");
120+
116121
// mine next block to push time forward
117-
web3.api::<TestNodeApi<_>>()
118-
.mine_pending_block()
122+
web3.alloy
123+
.evm_mine(None)
119124
.await
120125
.expect("Unable to mine next block");
121126

crates/e2e/tests/e2e/replace_order.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {
22
::alloy::primitives::U256,
3-
e2e::{nodes::local_node::TestNodeApi, setup::*},
4-
ethrpc::alloy::CallBuilderExt,
3+
e2e::setup::*,
4+
ethrpc::alloy::{CallBuilderExt, EvmProviderExt},
55
model::{
66
order::{OrderCreation, OrderCreationAppData, OrderKind, OrderStatus},
77
signature::EcdsaSigningScheme,
@@ -105,10 +105,7 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) {
105105
.unwrap();
106106

107107
// disable auto mining to prevent order being immediately executed
108-
web3.api::<TestNodeApi<_>>()
109-
.set_automine_enabled(false)
110-
.await
111-
.unwrap();
108+
web3.alloy.evm_set_automine(false).await.unwrap();
112109

113110
// Place Orders
114111
let services = Services::new(&onchain).await;
@@ -181,8 +178,8 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) {
181178
assert_eq!(error_message, expected_body);
182179

183180
// Continue automining so our order can be executed
184-
web3.api::<TestNodeApi<_>>()
185-
.set_automine_enabled(true)
181+
web3.alloy
182+
.evm_set_automine(true)
186183
.await
187184
.expect("Must be able to disable auto-mining");
188185

0 commit comments

Comments
 (0)