alloy_provider/ext/
anvil.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
//! This module extends the Ethereum JSON-RPC provider with the Anvil namespace's RPC methods.

use crate::Provider;
use alloy_network::Network;
use alloy_primitives::{Address, Bytes, TxHash, B256, U256};
use alloy_rpc_types_anvil::{Forking, Metadata, MineOptions, NodeInfo};
use alloy_rpc_types_eth::Block;
use alloy_transport::{Transport, TransportResult};

/// Anvil namespace rpc interface that gives access to several non-standard RPC methods.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait AnvilApi<N: Network, T>: Send + Sync {
    // Not implemented:
    // - anvil_enable_traces: Not implemented in the Anvil RPC API.
    // - anvil_set_block: Not implemented / wired correctly in the Anvil RPC API.

    /// Send transactions impersonating specific account and contract addresses.
    async fn anvil_impersonate_account(&self, address: Address) -> TransportResult<()>;

    /// Stops impersonating an account if previously set with `anvil_impersonateAccount`.
    async fn anvil_stop_impersonating_account(&self, address: Address) -> TransportResult<()>;

    /// If set to true will make every account impersonated.
    async fn anvil_auto_impersonate_account(&self, enabled: bool) -> TransportResult<()>;

    /// Returns true if auto mining is enabled, and false.
    async fn anvil_get_auto_mine(&self) -> TransportResult<bool>;

    /// Enables or disables, based on the single boolean argument, the automatic mining of new
    /// blocks with each new transaction submitted to the network.
    async fn anvil_set_auto_mine(&self, enable_automine: bool) -> TransportResult<()>;

    /// Mines a series of blocks.
    async fn anvil_mine(
        &self,
        num_blocks: Option<U256>,
        interval: Option<U256>,
    ) -> TransportResult<()>;

    /// Sets the mining behavior to interval with the given interval (seconds).
    async fn anvil_set_interval_mining(&self, secs: u64) -> TransportResult<()>;

    /// Removes transactions from the pool.
    async fn anvil_drop_transaction(&self, tx_hash: TxHash) -> TransportResult<Option<TxHash>>;

    /// Removes all transactions from the pool.
    async fn anvil_drop_all_transactions(&self) -> TransportResult<()>;

    /// Reset the fork to a fresh forked state, and optionally update the fork config.
    ///
    /// If `forking` is `None` then this will disable forking entirely.
    async fn anvil_reset(&self, forking: Option<Forking>) -> TransportResult<()>;

    /// Sets the chain ID.
    async fn anvil_set_chain_id(&self, chain_id: u64) -> TransportResult<()>;

    /// Modifies the balance of an account.
    async fn anvil_set_balance(&self, address: Address, balance: U256) -> TransportResult<()>;

    /// Sets the code of a contract.
    async fn anvil_set_code(&self, address: Address, code: Bytes) -> TransportResult<()>;

    /// Sets the nonce of an address.
    async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> TransportResult<()>;

    /// Writes a single slot of the account's storage.
    async fn anvil_set_storage_at(
        &self,
        address: Address,
        slot: U256,
        val: B256,
    ) -> TransportResult<bool>;

    /// Enable or disable logging.
    async fn anvil_set_logging(&self, enable: bool) -> TransportResult<()>;

    /// Set the minimum gas price for the node.
    async fn anvil_set_min_gas_price(&self, gas: U256) -> TransportResult<()>;

    /// Sets the base fee of the next block.
    async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: U256) -> TransportResult<()>;

    /// Sets the coinbase address.
    async fn anvil_set_coinbase(&self, address: Address) -> TransportResult<()>;

    /// Create a buffer that represents all state on the chain, which can be loaded to separate
    /// process by calling `anvil_loadState`
    async fn anvil_dump_state(&self) -> TransportResult<Bytes>;

    /// Append chain state buffer to current chain. Will overwrite any conflicting addresses or
    /// storage.
    async fn anvil_load_state(&self, buf: Bytes) -> TransportResult<bool>;

    /// Retrieves the Anvil node configuration params.
    async fn anvil_node_info(&self) -> TransportResult<NodeInfo>;

    /// Retrieves metadata about the Anvil instance.
    async fn anvil_metadata(&self) -> TransportResult<Metadata>;

    /// Removes all transactions from the pool for a specific address.
    async fn anvil_remove_pool_transactions(&self, address: Address) -> TransportResult<()>;

    /// Snapshot the state of the blockchain at the current block.
    async fn anvil_snapshot(&self) -> TransportResult<U256>;

    /// Revert the state of the blockchain to a previous snapshot.
    /// Takes a single parameter, which is the snapshot id to revert to.
    async fn anvil_revert(&self, id: U256) -> TransportResult<bool>;

    /// Jump forward in time by the given amount of time, in seconds.
    async fn anvil_increase_time(&self, seconds: U256) -> TransportResult<i64>;

    /// Similar to `evm_increaseTime` but takes the exact timestamp that you want in the next block.
    async fn anvil_set_next_block_timestamp(&self, timestamp: u64) -> TransportResult<()>;

    /// Sets the specific timestamp and returns the number of seconds between the given timestamp
    /// and the current time.
    async fn anvil_set_time(&self, timestamp: u64) -> TransportResult<u64>;

    /// Set the next block gas limit.
    async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> TransportResult<bool>;

    /// Sets an interval for the block timestamp.
    async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> TransportResult<()>;

    /// Unsets the interval for the block timestamp.
    async fn anvil_remove_block_timestamp_interval(&self) -> TransportResult<bool>;

    /// Mine blocks, instantly.
    /// This will mine the blocks regardless of the configured mining mode.
    async fn evm_mine(&self, opts: Option<MineOptions>) -> TransportResult<String>;

    /// Mine blocks, instantly and return the mined blocks.
    /// This will mine the blocks regardless of the configured mining mode.
    async fn anvil_mine_detailed(&self, opts: Option<MineOptions>) -> TransportResult<Vec<Block>>;

    /// Sets the backend rpc url.
    async fn anvil_set_rpc_url(&self, url: String) -> TransportResult<()>;

    /// Execute a transaction regardless of signature status.
    async fn eth_send_unsigned_transaction(
        &self,
        request: N::TransactionRequest,
    ) -> TransportResult<TxHash>;
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<N, T, P> AnvilApi<N, T> for P
where
    N: Network,
    T: Transport + Clone,
    P: Provider<T, N>,
{
    async fn anvil_impersonate_account(&self, address: Address) -> TransportResult<()> {
        self.client().request("anvil_impersonateAccount", (address,)).await
    }

    async fn anvil_stop_impersonating_account(&self, address: Address) -> TransportResult<()> {
        self.client().request("anvil_stopImpersonatingAccount", (address,)).await
    }

    async fn anvil_auto_impersonate_account(&self, enabled: bool) -> TransportResult<()> {
        self.client().request("anvil_autoImpersonateAccount", (enabled,)).await
    }

    async fn anvil_get_auto_mine(&self) -> TransportResult<bool> {
        self.client().request_noparams("anvil_getAutomine").await
    }

    async fn anvil_set_auto_mine(&self, enabled: bool) -> TransportResult<()> {
        self.client().request("anvil_setAutomine", (enabled,)).await
    }

    async fn anvil_mine(
        &self,
        num_blocks: Option<U256>,
        interval: Option<U256>,
    ) -> TransportResult<()> {
        self.client().request("anvil_mine", (num_blocks, interval)).await
    }

    async fn anvil_set_interval_mining(&self, secs: u64) -> TransportResult<()> {
        self.client().request("anvil_setIntervalMining", (secs,)).await
    }

    async fn anvil_drop_transaction(&self, tx_hash: TxHash) -> TransportResult<Option<TxHash>> {
        self.client().request("anvil_dropTransaction", (tx_hash,)).await
    }

    async fn anvil_drop_all_transactions(&self) -> TransportResult<()> {
        self.client().request_noparams("anvil_dropAllTransactions").await
    }

    async fn anvil_reset(&self, forking: Option<Forking>) -> TransportResult<()> {
        self.client().request("anvil_reset", (forking,)).await
    }

    async fn anvil_set_chain_id(&self, chain_id: u64) -> TransportResult<()> {
        self.client().request("anvil_setChainId", (chain_id,)).await
    }

    async fn anvil_set_balance(&self, address: Address, balance: U256) -> TransportResult<()> {
        self.client().request("anvil_setBalance", (address, balance)).await
    }

    async fn anvil_set_code(&self, address: Address, code: Bytes) -> TransportResult<()> {
        self.client().request("anvil_setCode", (address, code)).await
    }

    async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> TransportResult<()> {
        self.client().request("anvil_setNonce", (address, nonce)).await
    }

    async fn anvil_set_storage_at(
        &self,
        address: Address,
        slot: U256,
        val: B256,
    ) -> TransportResult<bool> {
        self.client().request("anvil_setStorageAt", (address, slot, val)).await
    }

    async fn anvil_set_logging(&self, enable: bool) -> TransportResult<()> {
        self.client().request("anvil_setLoggingEnabled", (enable,)).await
    }

    async fn anvil_set_min_gas_price(&self, gas: U256) -> TransportResult<()> {
        self.client().request("anvil_setMinGasPrice", (gas,)).await
    }

    async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: U256) -> TransportResult<()> {
        self.client().request("anvil_setNextBlockBaseFeePerGas", (basefee,)).await
    }

    async fn anvil_set_coinbase(&self, address: Address) -> TransportResult<()> {
        self.client().request("anvil_setCoinbase", (address,)).await
    }

    async fn anvil_dump_state(&self) -> TransportResult<Bytes> {
        self.client().request_noparams("anvil_dumpState").await
    }

    async fn anvil_load_state(&self, buf: Bytes) -> TransportResult<bool> {
        self.client().request("anvil_loadState", (buf,)).await
    }

    async fn anvil_node_info(&self) -> TransportResult<NodeInfo> {
        self.client().request_noparams("anvil_nodeInfo").await
    }

    async fn anvil_metadata(&self) -> TransportResult<Metadata> {
        self.client().request_noparams("anvil_metadata").await
    }

    async fn anvil_remove_pool_transactions(&self, address: Address) -> TransportResult<()> {
        self.client().request("anvil_removePoolTransactions", (address,)).await
    }

    async fn anvil_snapshot(&self) -> TransportResult<U256> {
        self.client().request_noparams("evm_snapshot").await
    }

    async fn anvil_revert(&self, id: U256) -> TransportResult<bool> {
        self.client().request("evm_revert", (id,)).await
    }

    async fn anvil_increase_time(&self, seconds: U256) -> TransportResult<i64> {
        self.client().request("evm_increaseTime", (seconds,)).await
    }

    async fn anvil_set_next_block_timestamp(&self, seconds: u64) -> TransportResult<()> {
        self.client().request("evm_setNextBlockTimestamp", (seconds,)).await
    }

    async fn anvil_set_time(&self, timestamp: u64) -> TransportResult<u64> {
        self.client().request("evm_setTime", (timestamp,)).await
    }

    async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> TransportResult<bool> {
        self.client().request("evm_setBlockGasLimit", (gas_limit,)).await
    }

    async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> TransportResult<()> {
        self.client().request("anvil_setBlockTimestampInterval", (seconds,)).await
    }

    async fn anvil_remove_block_timestamp_interval(&self) -> TransportResult<bool> {
        self.client().request_noparams("anvil_removeBlockTimestampInterval").await
    }

    async fn evm_mine(&self, opts: Option<MineOptions>) -> TransportResult<String> {
        self.client().request("evm_mine", (opts,)).await
    }

    async fn anvil_mine_detailed(&self, opts: Option<MineOptions>) -> TransportResult<Vec<Block>> {
        self.client().request("evm_mine_detailed", (opts,)).await
    }

    async fn anvil_set_rpc_url(&self, url: String) -> TransportResult<()> {
        self.client().request("anvil_setRpcUrl", (url,)).await
    }

    async fn eth_send_unsigned_transaction(
        &self,
        request: N::TransactionRequest,
    ) -> TransportResult<TxHash> {
        self.client().request("eth_sendUnsignedTransaction", (request,)).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ProviderBuilder;
    use alloy_eips::BlockNumberOrTag;
    use alloy_network::TransactionBuilder;
    use alloy_primitives::B256;
    use alloy_rpc_types_eth::TransactionRequest;

    // use alloy_node_bindings::Anvil; (to be used in `test_anvil_reset`)

    #[tokio::test]
    async fn test_anvil_impersonate_account_stop_impersonating_account() {
        let provider = ProviderBuilder::new().on_anvil();

        let impersonate = Address::random();
        let to = Address::random();
        let val = U256::from(1337);
        let funding = U256::from(1e18 as u64);

        provider.anvil_set_balance(impersonate, funding).await.unwrap();

        let balance = provider.get_balance(impersonate).await.unwrap();
        assert_eq!(balance, funding);

        let tx = TransactionRequest::default().with_from(impersonate).with_to(to).with_value(val);

        let res = provider.send_transaction(tx.clone()).await;
        res.unwrap_err();

        provider.anvil_impersonate_account(impersonate).await.unwrap();
        assert!(provider.get_accounts().await.unwrap().contains(&impersonate));

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

        let nonce = provider.get_transaction_count(impersonate).await.unwrap();
        assert_eq!(nonce, 1);

        let balance = provider.get_balance(to).await.unwrap();
        assert_eq!(balance, val);

        provider.anvil_stop_impersonating_account(impersonate).await.unwrap();
        let res = provider.send_transaction(tx).await;
        res.unwrap_err();
    }

    #[tokio::test]
    async fn test_anvil_auto_impersonate_account() {
        let provider = ProviderBuilder::new().on_anvil();

        let impersonate = Address::random();
        let to = Address::random();
        let val = U256::from(1337);
        let funding = U256::from(1e18 as u64);

        provider.anvil_set_balance(impersonate, funding).await.unwrap();

        let balance = provider.get_balance(impersonate).await.unwrap();
        assert_eq!(balance, funding);

        let tx = TransactionRequest::default().with_from(impersonate).with_to(to).with_value(val);

        let res = provider.send_transaction(tx.clone()).await;
        res.unwrap_err();

        provider.anvil_auto_impersonate_account(true).await.unwrap();

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

        let nonce = provider.get_transaction_count(impersonate).await.unwrap();
        assert_eq!(nonce, 1);

        let balance = provider.get_balance(to).await.unwrap();
        assert_eq!(balance, val);

        provider.anvil_auto_impersonate_account(false).await.unwrap();
        let res = provider.send_transaction(tx).await;
        res.unwrap_err();

        provider.anvil_impersonate_account(impersonate).await.unwrap();
        assert!(provider.get_accounts().await.unwrap().contains(&impersonate));
    }

    #[tokio::test]
    async fn test_anvil_get_auto_mine_set_auto_mine() {
        let provider = ProviderBuilder::new().on_anvil();

        provider.anvil_set_auto_mine(false).await.unwrap();

        let enabled = provider.anvil_get_auto_mine().await.unwrap();
        assert!(!enabled);

        provider.anvil_set_auto_mine(true).await.unwrap();

        let enabled = provider.anvil_get_auto_mine().await.unwrap();
        assert!(enabled);
    }

    #[tokio::test]
    async fn test_anvil_mine() {
        let provider = ProviderBuilder::new().on_anvil();

        let start_num = provider.get_block_number().await.unwrap();

        provider.anvil_mine(Some(U256::from(10)), None).await.unwrap();

        let num = provider.get_block_number().await.unwrap();

        assert_eq!(num, start_num + 10);
    }

    #[tokio::test]
    async fn test_anvil_set_interval_mining() {
        let provider = ProviderBuilder::new().on_anvil();

        provider.anvil_set_interval_mining(1).await.unwrap();

        let start_num = provider.get_block_number().await.unwrap();

        tokio::time::sleep(tokio::time::Duration::from_millis(1500)).await;

        let num = provider.get_block_number().await.unwrap();

        assert_eq!(num, start_num + 1);
    }

    #[tokio::test]
    async fn test_anvil_drop_transaction() {
        let provider = ProviderBuilder::new().on_anvil_with_wallet();

        provider.anvil_set_auto_mine(false).await.unwrap();

        let alice = provider.get_accounts().await.unwrap()[0];
        let bob = provider.get_accounts().await.unwrap()[1];
        let chain_id = provider.get_chain_id().await.unwrap();

        let tx = TransactionRequest::default()
            .with_from(alice)
            .with_to(bob)
            .with_nonce(0)
            .with_chain_id(chain_id)
            .with_value(U256::from(100))
            .with_gas_limit(21_000)
            .with_max_priority_fee_per_gas(1_000_000_000)
            .with_max_fee_per_gas(20_000_000_000);

        let tx_hash =
            provider.send_transaction(tx).await.unwrap().register().await.unwrap().tx_hash;

        let res = provider.anvil_drop_transaction(tx_hash).await.unwrap();

        assert_eq!(res, Some(tx_hash));
    }

    #[tokio::test]
    async fn test_anvil_drop_all_transactions() {
        let provider = ProviderBuilder::new().on_anvil_with_wallet();

        provider.anvil_set_auto_mine(false).await.unwrap();

        let alice = provider.get_accounts().await.unwrap()[0];
        let bob = provider.get_accounts().await.unwrap()[1];
        let chain_id = provider.get_chain_id().await.unwrap();

        let tx = TransactionRequest::default()
            .with_from(alice)
            .with_to(bob)
            .with_nonce(0)
            .with_chain_id(chain_id)
            .with_value(U256::from(100))
            .with_gas_limit(21_000)
            .with_max_priority_fee_per_gas(1_000_000_000)
            .with_max_fee_per_gas(20_000_000_000);

        provider.send_transaction(tx.clone()).await.unwrap().register().await.unwrap();

        let tx = tx.clone().with_nonce(1);

        provider.send_transaction(tx).await.unwrap().register().await.unwrap();

        provider.anvil_drop_all_transactions().await.unwrap();
    }

    // TODO: Fix this test, `chain_id` is not being set correctly.
    // #[tokio::test]
    // async fn test_anvil_reset() {
    //     let fork1 = Anvil::default().chain_id(777).spawn();
    //     let fork2 = Anvil::default().chain_id(888).spawn();

    //     let provider = ProviderBuilder::new()
    //         .on_anvil_with_config(|config| config.fork(fork1.endpoint_url().to_string()));

    //     let chain_id = provider.get_chain_id().await.unwrap();
    //     assert_eq!(chain_id, 777);

    //     provider
    //         .anvil_reset(Some(Forking {
    //             json_rpc_url: Some(fork2.endpoint_url().to_string()),
    //             block_number: Some(0),
    //         }))
    //         .await
    //         .unwrap();

    //     let chain_id = provider.get_chain_id().await.unwrap();
    //     assert_eq!(chain_id, 888);
    // }

    #[tokio::test]
    async fn test_anvil_set_chain_id() {
        let provider = ProviderBuilder::new().on_anvil();

        let chain_id = 1337;
        provider.anvil_set_chain_id(chain_id).await.unwrap();

        let new_chain_id = provider.get_chain_id().await.unwrap();
        assert_eq!(new_chain_id, chain_id);
    }

    #[tokio::test]
    async fn test_anvil_set_balance() {
        let provider = ProviderBuilder::new().on_anvil();

        let address = Address::random();
        let balance = U256::from(1337);
        provider.anvil_set_balance(address, balance).await.unwrap();

        let new_balance = provider.get_balance(address).await.unwrap();
        assert_eq!(new_balance, balance);
    }

    #[tokio::test]
    async fn test_anvil_set_code() {
        let provider = ProviderBuilder::new().on_anvil();

        let address = Address::random();
        provider.anvil_set_code(address, Bytes::from("0xbeef")).await.unwrap();

        let code = provider.get_code_at(address).await.unwrap();
        assert_eq!(code, Bytes::from("0xbeef"));
    }

    #[tokio::test]
    async fn test_anvil_set_nonce() {
        let provider = ProviderBuilder::new().on_anvil();

        let address = Address::random();
        let nonce = U256::from(1337);
        provider.anvil_set_nonce(address, nonce).await.unwrap();

        let new_nonce = provider.get_transaction_count(address).await.unwrap();
        assert_eq!(new_nonce, nonce.to::<u64>());
    }

    #[tokio::test]
    async fn test_anvil_set_storage_at() {
        let provider = ProviderBuilder::new().on_anvil();

        let address = Address::random();
        let slot = U256::from(1337);
        let val = B256::from(U256::from(1337));
        provider.anvil_set_storage_at(address, slot, val).await.unwrap();

        let storage = provider.get_storage_at(address, slot).await.unwrap();
        assert_eq!(B256::from(storage), val);
    }

    #[tokio::test]
    async fn test_anvil_set_logging() {
        let provider = ProviderBuilder::new().on_anvil();

        provider.anvil_set_logging(true).await.unwrap();
    }

    #[tokio::test]
    async fn test_anvil_set_min_gas_price() {
        let provider = ProviderBuilder::new().on_anvil();

        let gas = U256::from(1337);

        if let Err(e) = provider.anvil_set_min_gas_price(gas).await {
            assert_eq!(
                e.to_string(),
                "server returned an error response: error code -32602: anvil_setMinGasPrice is not supported when EIP-1559 is active"
            );
        }
    }

    #[tokio::test]
    async fn test_anvil_set_next_block_base_fee_per_gas() {
        let provider = ProviderBuilder::new().on_anvil();

        let basefee = U256::from(1337);
        provider.anvil_set_next_block_base_fee_per_gas(basefee).await.unwrap();

        provider.evm_mine(None).await.unwrap();

        let block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();

        assert_eq!(block.header.base_fee_per_gas, Some(basefee.to::<u64>()));
    }

    #[tokio::test]
    async fn test_anvil_set_coinbase() {
        let provider = ProviderBuilder::new().on_anvil();

        let coinbase = Address::random();
        provider.anvil_set_coinbase(coinbase).await.unwrap();

        provider.evm_mine(None).await.unwrap();

        let block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();
        assert_eq!(block.header.miner, coinbase);
    }

    #[tokio::test]
    async fn test_anvil_dump_state_load_state() {
        let provider = ProviderBuilder::new().on_anvil();

        let state = provider.anvil_dump_state().await.unwrap();

        assert!(!state.is_empty());

        let res = provider.anvil_load_state(state).await.unwrap();

        assert!(res);
    }

    #[tokio::test]
    async fn test_anvil_node_info() {
        let provider = ProviderBuilder::new().on_anvil();

        let latest_block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();

        provider.evm_mine(None).await.unwrap();

        let node_info = provider.anvil_node_info().await.unwrap();

        assert_eq!(node_info.current_block_number, latest_block.header.number + 1);
    }

    #[tokio::test]
    async fn test_anvil_metadata() {
        let provider = ProviderBuilder::new().on_anvil();

        let client_version = provider.get_client_version().await.unwrap();
        let chain_id = provider.get_chain_id().await.unwrap();

        let metadata = provider.anvil_metadata().await.unwrap();

        assert_eq!(metadata.client_version, client_version);
        assert_eq!(metadata.chain_id, chain_id);
    }

    #[tokio::test]
    async fn test_anvil_remove_pool_transactions() {
        let provider = ProviderBuilder::new().on_anvil_with_wallet();

        provider.anvil_set_auto_mine(false).await.unwrap();

        let alice = provider.get_accounts().await.unwrap()[0];
        let bob = provider.get_accounts().await.unwrap()[1];
        let chain_id = provider.get_chain_id().await.unwrap();

        let tx = TransactionRequest::default()
            .with_from(alice)
            .with_to(bob)
            .with_nonce(0)
            .with_chain_id(chain_id)
            .with_value(U256::from(100))
            .with_gas_limit(21_000)
            .with_max_priority_fee_per_gas(1_000_000_000)
            .with_max_fee_per_gas(20_000_000_000);

        provider.send_transaction(tx.clone()).await.unwrap().register().await.unwrap();

        let tx = tx.clone().with_nonce(1);

        provider.send_transaction(tx).await.unwrap().register().await.unwrap();

        provider.anvil_remove_pool_transactions(alice).await.unwrap();
    }

    #[tokio::test]
    async fn test_anvil_snapshot_revert() {
        let provider = ProviderBuilder::new().on_anvil();

        let snapshot_id = provider.anvil_snapshot().await.unwrap();

        let alice = provider.get_accounts().await.unwrap()[0];
        let bob = provider.get_accounts().await.unwrap()[1];
        let chain_id = provider.get_chain_id().await.unwrap();

        let tx = TransactionRequest::default()
            .with_from(alice)
            .with_to(bob)
            .with_nonce(0)
            .with_chain_id(chain_id)
            .with_value(U256::from(100))
            .with_gas_limit(21_000)
            .with_max_priority_fee_per_gas(1_000_000_000)
            .with_max_fee_per_gas(20_000_000_000);

        provider.send_transaction(tx.clone()).await.unwrap().get_receipt().await.unwrap();

        let tx = tx.clone().with_nonce(1);

        provider.send_transaction(tx).await.unwrap().get_receipt().await.unwrap();

        let tx_count = provider.get_transaction_count(alice).await.unwrap();
        assert_eq!(tx_count, 2);

        let res = provider.anvil_revert(snapshot_id).await.unwrap();
        assert!(res);

        let tx_count = provider.get_transaction_count(alice).await.unwrap();
        assert_eq!(tx_count, 0);
    }

    #[tokio::test]
    async fn test_anvil_increase_time() {
        let provider = ProviderBuilder::new().on_anvil();

        let timestamp = provider
            .get_block_by_number(BlockNumberOrTag::Latest, false)
            .await
            .unwrap()
            .unwrap()
            .header
            .timestamp;

        let seconds = provider.anvil_increase_time(U256::from(1337)).await.unwrap();

        assert_eq!(timestamp as i64 + seconds, timestamp as i64 + 1337_i64);
    }

    #[tokio::test]
    async fn test_anvil_set_next_block_timestamp() {
        let provider = ProviderBuilder::new().on_anvil();

        let timestamp = provider
            .get_block_by_number(BlockNumberOrTag::Latest, false)
            .await
            .unwrap()
            .unwrap()
            .header
            .timestamp;

        provider.anvil_set_next_block_timestamp(timestamp + 1337).await.unwrap();

        provider.evm_mine(None).await.unwrap();

        let latest_block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();
        assert_eq!(latest_block.header.timestamp, timestamp + 1337);
    }

    #[tokio::test]
    async fn test_anvil_set_time() {
        let provider = ProviderBuilder::new().on_anvil();

        provider.anvil_set_time(0).await.unwrap();

        let seconds = provider.anvil_set_time(1001).await.unwrap();

        assert_eq!(seconds, 1);
    }

    #[tokio::test]
    async fn test_anvil_set_block_gas_limit() {
        let provider = ProviderBuilder::new().on_anvil();

        let block_gas_limit = U256::from(1337);
        assert!(provider.anvil_set_block_gas_limit(block_gas_limit).await.unwrap());

        provider.evm_mine(None).await.unwrap();

        let latest_block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();
        assert_eq!(block_gas_limit.to::<u64>(), latest_block.header.gas_limit);
    }

    #[tokio::test]
    async fn test_anvil_block_timestamp_interval() {
        let provider = ProviderBuilder::new().on_anvil();

        provider.anvil_set_block_timestamp_interval(1).await.unwrap();

        let start_timestamp = provider
            .get_block_by_number(BlockNumberOrTag::Latest, false)
            .await
            .unwrap()
            .unwrap()
            .header
            .timestamp;

        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

        provider.evm_mine(None).await.unwrap();

        let latest_block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();

        assert_eq!(latest_block.header.timestamp, start_timestamp + 1);

        provider.anvil_remove_block_timestamp_interval().await.unwrap();

        provider.evm_mine(None).await.unwrap();

        let start_timestamp = provider
            .get_block_by_number(BlockNumberOrTag::Latest, false)
            .await
            .unwrap()
            .unwrap()
            .header
            .timestamp;

        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

        let latest_block =
            provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();

        assert_eq!(latest_block.header.timestamp, start_timestamp);
    }

    #[tokio::test]
    async fn test_evm_mine_single_block() {
        let provider = ProviderBuilder::new().on_anvil();

        let start_num = provider.get_block_number().await.unwrap();

        for (idx, _) in std::iter::repeat(()).take(10).enumerate() {
            provider.evm_mine(None).await.unwrap();
            let num = provider.get_block_number().await.unwrap();
            assert_eq!(num, start_num + idx as u64 + 1);
        }

        let num = provider.get_block_number().await.unwrap();
        assert_eq!(num, start_num + 10);
    }

    // TODO: Fix this test, only a single block is being mined regardless of the `blocks` parameter.
    // #[tokio::test]
    // async fn test_evm_mine_with_configuration() {
    //     let provider = ProviderBuilder::new().on_anvil();

    //     let start_num = provider.get_block_number().await.unwrap();

    //     provider
    //         .evm_mine(Some(MineOptions::Options { timestamp: Some(100), blocks: Some(10) }))
    //         .await
    //         .unwrap();

    //     let num = provider.get_block_number().await.unwrap();
    //     assert_eq!(num, start_num + 10);
    // }

    #[tokio::test]
    async fn test_anvil_mine_detailed_single_block() {
        let provider = ProviderBuilder::new().on_anvil();

        let start_num = provider.get_block_number().await.unwrap();

        for (idx, _) in std::iter::repeat(()).take(10).enumerate() {
            provider.anvil_mine_detailed(None).await.unwrap();
            let num = provider.get_block_number().await.unwrap();
            assert_eq!(num, start_num + idx as u64 + 1);
        }

        let num = provider.get_block_number().await.unwrap();
        assert_eq!(num, start_num + 10);
    }

    // TODO: Fix this test, only a single block is being mined regardless of the `blocks` parameter.
    // #[tokio::test]
    // async fn test_anvil_mine_detailed_with_configuration() {
    //     let provider = ProviderBuilder::new().on_anvil();

    //     let start_num = provider.get_block_number().await.unwrap();

    //     let blocks = provider
    //         .anvil_mine_detailed(Some(MineOptions::Options {
    //             timestamp: Some(100),
    //             blocks: Some(10),
    //         }))
    //         .await
    //         .unwrap();

    //     let num = provider.get_block_number().await.unwrap();
    //     assert_eq!(num, start_num + 10);

    //     for (idx, block) in blocks.iter().enumerate() {
    //         assert_eq!(block.header.number, Some(start_num + idx as u64 + 1));
    //     }
    // }

    #[tokio::test]
    async fn test_anvil_set_rpc_url() {
        let provider = ProviderBuilder::new().on_anvil();

        let url = "https://example.com".to_string();
        provider.anvil_set_rpc_url(url.clone()).await.unwrap();
    }

    #[tokio::test]
    async fn test_eth_send_unsigned_transaction() {
        let provider = ProviderBuilder::new().on_anvil();

        let alice = Address::random();
        let bob = Address::random();
        let chain_id = provider.get_chain_id().await.unwrap();

        provider.anvil_set_balance(alice, U256::from(1e18 as u64)).await.unwrap();

        let tx = TransactionRequest::default()
            .with_from(alice)
            .with_to(bob)
            .with_nonce(0)
            .with_chain_id(chain_id)
            .with_value(U256::from(100))
            .with_gas_limit(21_000)
            .with_max_priority_fee_per_gas(1_000_000_000)
            .with_max_fee_per_gas(20_000_000_000);

        let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();

        provider.evm_mine(None).await.unwrap();

        let res = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
        assert_eq!(res.from, alice);
        assert_eq!(res.to, Some(bob));
    }
}