multiplexer-evm 0.1.1

A Rust library and Solidity contracts for building and executing complex EVM transaction sequences, including flash loans.
Documentation
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149

use crate::{FlowBuilder, DELEGATE_PROXY_INIT, EXECUTOR_INIT};
use alloy::{
    hex,
    network::TransactionBuilder, // Added Ethereum
    primitives::{address, bytes, uint, Address, U256},
    // Removed EthereumSigner, node_bindings::Anvil
    providers::{
        ext::AnvilApi,
        fillers::{BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller}, // Corrected filler paths
        layers::AnvilProvider,
        Identity, Provider, ProviderBuilder, RootProvider,
    },
    rpc::types::TransactionRequest,
    sol,
    sol_types::{SolCall, SolConstructor},
};
use core::str;

// Type alias for the complex provider type using LocalWallet as signer
type AnvilTestProvider = FillProvider<
    JoinFill<
        Identity,
        JoinFill<GasFiller, JoinFill<BlobGasFiller, JoinFill<NonceFiller, ChainIdFiller>>>
        >,
    AnvilProvider<RootProvider>,
>;

// Constants
const BUDGET: U256 = uint!(1000000000000000000000_U256); // 1000e18
const TWO_ETH: U256 = uint!(2000000000000000000_U256); // 2e18
const ONEHUNDRED_ETH: U256 = uint!(10000000000000000000_U256);
const WALLET: Address = Address::repeat_byte(0x41);
const BOB: Address = Address::repeat_byte(0x42);
const WETH9: Address = address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
const MORPHO: Address = address!("BBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb");

// Test helpers
async fn setup_provider() -> AnvilTestProvider {
    let provider = get_provider();
    provider
        .anvil_set_balance(WALLET, BUDGET + U256::from(10u64.pow(18)))
        .await
        .unwrap();
    provider
        .anvil_set_balance(BOB, BUDGET + U256::from(10u64.pow(18)))
        .await
        .unwrap();
    provider
}

async fn deploy_executor(provider: &AnvilTestProvider) -> Address {
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(EXECUTOR_INIT)
        .with_nonce(0);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    receipt.contract_address.unwrap()
}
fn get_provider() -> AnvilTestProvider {
    ProviderBuilder::new().on_anvil_with_config(|anvil| {
        anvil
            .fork(std::env::var("ETH_RPC_URL").expect("failed to retrieve ETH_RPC_URL url from env"))
            .fork_block_number(20_000_000)
    })
}

sol! {
    #[sol(rpc)]
    interface IERC20 {
        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
        function totalSupply() external view returns (uint256);
        function balanceOf(address account) external view returns (uint256);
        function transfer(address to, uint256 value) external returns (bool);
        function allowance(address owner, address spender) external view returns (uint256);
        function approve(address spender, uint256 value) external returns (bool);
        function transferFrom(address from, address to, uint256 value) external returns (bool);
    }
}

sol! {
    #[sol(rpc)]
    contract IProxy {
        constructor(address _target, bytes memory constructorData) payable;
    }
}

sol! {
    #[sol(rpc)]
    interface IWETH {
        function deposit() external payable;
        function transfer(address to, uint value) external returns (bool);
        function withdraw(uint amount) external;
    }
}

sol! {
    interface IMorpho {
        function flashLoan(address token, uint256 assets, bytes calldata data) external;
    }      
}

#[test]
fn test_flow_builder_create() {
    let calldata = FlowBuilder::empty()
        .create(Address::ZERO, "LALA".as_bytes(), U256::from(10))
        .optimize()
        .build();
    assert_eq!(
        calldata,
        hex!("c94f554d04000000000000000000000000000000000000000000000000000000000000000a01000402000000044c414c4107")
    );
}

#[test]
fn test_flow_builder_call() {
    let addr_a = Address::repeat_byte(0x41);
    let calldata = FlowBuilder::empty()
        .call(addr_a, &vec![98, 99], U256::ZERO)
        .optimize()
        .build();
    assert_eq!(
        calldata,
        hex!("c94f554d0341414141414141414141414141414141414141410100020200000002626306")
    );
}

#[test]
fn test_flow_builder_delegatecall() {
    let addr_b = Address::repeat_byte(0x42);
    let calldata = FlowBuilder::empty()
        .delegatecall(addr_b, &vec![70, 71])
        .optimize()
        .build();
    assert_eq!(
        calldata,
        hex!("c94f554d0342424242424242424242424242424242424242420100020200000002464708")
    );
}

#[test]
fn test_flow_builder_combined_operations() {
    let addr_a = Address::repeat_byte(0x41);
    let addr_b = Address::repeat_byte(0x42);
    let calldata = FlowBuilder::empty()
        .create(Address::ZERO, "LALA".as_bytes(), U256::from(10))
        .call(addr_a, &vec![98, 99], U256::ZERO)
        .delegatecall(addr_b, &vec![70, 71])
        .optimize()
        .build();
    assert_eq!(
        calldata,
        hex!("c94f554d04000000000000000000000000000000000000000000000000000000000000000a01000402000000044c414c410703414141414141414141414141414141414141414101000202000000026263060342424242424242424242424242424242424242420200000002464708")
    );
}

#[tokio::test]
async fn test_bob_cannot_interact() {
    // A random account can not interact with multiplexer
    let provider = ProviderBuilder::new().on_anvil();
    provider
        .anvil_set_balance(WALLET, BUDGET + U256::from(10u64.pow(18)))
        .await
        .unwrap();
    provider
        .anvil_set_balance(BOB, BUDGET + U256::from(10u64.pow(18)))
        .await
        .unwrap();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(EXECUTOR_INIT)
        .with_nonce(0);

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

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

    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    let executor_wallet = receipt.contract_address.unwrap();

    // Executor address is deterministic because we use always same WALLET and nonce.
    assert_eq!(
        address!("c088f75b5733d097f266010c1502399a53bdfdbd"),
        executor_wallet
    );

    let tx = TransactionRequest::default()
        .with_from(BOB)
        .with_to(executor_wallet)
        .with_nonce(1)
        .with_value(BUDGET);

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

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

    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap();

    assert!(receipt.is_none()); // Tx can not be send from bob
}

#[tokio::test]
async fn test_wallet_can_interact() {
    let provider = ProviderBuilder::new().on_anvil();
    provider
        .anvil_set_balance(WALLET, BUDGET + U256::from(10u64.pow(18)))
        .await
        .unwrap();
    provider
        .anvil_set_balance(BOB, BUDGET + U256::from(10u64.pow(18)))
        .await
        .unwrap();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(EXECUTOR_INIT)
        .with_nonce(0);

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

    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    let executor_wallet = receipt.contract_address.unwrap();
    // Executor address is deterministic because we use always same WALLET and nonce.
    assert_eq!(
        address!("c088f75b5733d097f266010c1502399a53bdfdbd"),
        executor_wallet
    );

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor_wallet)
        .with_nonce(1)
        .with_value(BUDGET);

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

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

    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap();

    assert!(receipt.is_some()); // Tx succeed from WALLET

    let account_balance = provider.get_balance(executor_wallet).await.unwrap();
    assert_eq!(account_balance, BUDGET); // executor has the money sent in empty tx
}

#[tokio::test]
async fn test_weth_deposit_through_executor() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;
    
    // Initial balance check
    let executor_balance = provider.get_balance(executor).await.unwrap();
    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_balance, U256::ZERO);
    assert_eq!(executor_weth_balance, U256::ZERO);

    // Deposit ETH to get WETH
    let fb = FlowBuilder::empty().call(WETH9, &bytes!(""), TWO_ETH).optimize().build();
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(TWO_ETH)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());

    // Verify balances after deposit
    let executor_balance = provider.get_balance(executor).await.unwrap();
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_balance, U256::ZERO);
    assert_eq!(executor_weth_balance, TWO_ETH);
}

#[tokio::test]
async fn test_weth_withdraw_through_executor() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;
    
    // First deposit WETH
    let fb = FlowBuilder::empty().call(WETH9, &bytes!(""), TWO_ETH).optimize().build();
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(TWO_ETH)
        .with_input(fb);
    let _tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    
    // Then withdraw it back to ETH
    let withdraw_calldata = IWETH::withdrawCall { amount: TWO_ETH }.abi_encode();
    let fb = FlowBuilder::empty().call(WETH9, &withdraw_calldata, U256::ZERO).optimize().build();
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());

    // Verify final balances
    let executor_balance = provider.get_balance(executor).await.unwrap();
    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_balance, TWO_ETH);
    assert_eq!(executor_weth_balance, U256::ZERO);
}

#[tokio::test]
async fn test_wallet_can_proxy_create_small() {
    let provider = get_provider();

    // reality check
    let weth_balance = provider.get_balance(WETH9).await.unwrap();
    assert_eq!(format!("{}", weth_balance), "2933633723194923479377016");

    // test WALLETs
    // 0x4141414141..4141414141  with 1001 eth
    // 0x4242424242..4242424242  with 1001 eth
    provider
        .anvil_set_balance(WALLET, BUDGET + U256::from(1e18 as u64))
        .await
        .unwrap();

    let wallet_balance = provider.get_balance(WALLET).await.unwrap();
    assert_eq!(wallet_balance, BUDGET + U256::from(1e18 as u64)); // executor shoud shave sent the value to WETH9

    provider
        .anvil_set_balance(BOB, BUDGET + U256::from(1e18 as u64))
        .await
        .unwrap();
    // Make the Executor contract (WALLET is the owner)
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(EXECUTOR_INIT)
        .with_nonce(0);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    let executor = receipt.contract_address.unwrap();

    // Create dellegate proxy
    // let mut calldata = DELEGATE_PROXY_INIT;
    // calldata.extend(hex!("00").repeat(12));
    // calldata.extend(executor.as_slice());
    let mut calldata = DELEGATE_PROXY_INIT.to_vec();
    calldata.extend(
        IProxy::constructorCall {
            _target: executor,
            constructorData: "".into(),
        }
        .abi_encode(),
    );

    let fb = FlowBuilder::empty()
        .create(executor.create(1), &calldata, U256::ZERO)
        .call(
            executor.create(1),
            &FlowBuilder::empty()
                .call(WETH9, &vec![], TWO_ETH)
                .optimize().build(),
            TWO_ETH,
        )
        .optimize().build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(TWO_ETH)
        .with_input(fb);

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

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

    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();

    assert!(receipt.status());

    let executor_balance = provider.get_balance(executor).await.unwrap();
    assert_eq!(executor_balance, U256::ZERO); // executor shoud shave sent the value to WETH9
    assert_eq!(
        address!("c84f9705070281e8c800c57d92dbab053a80a2d0"),
        executor.create(1)
    );

    // Executor has
    // 0 eth
    // 0 weth
    let executor_balance = provider.get_balance(executor).await.unwrap();
    assert_eq!(executor_balance, U256::ZERO); // executor shoud shave sent the value to WETH9

    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_weth_balance, U256::ZERO); // executor should have 2 eth worth of weth

    // Proxy created via executor that points to the executor ?? ?AHHH
    // 0 eth
    // 2 weth

    let proxy_balance = provider.get_balance(executor.create(1)).await.unwrap();
    assert_eq!(proxy_balance, U256::ZERO); // executor shoud shave sent the value to WETH9

    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let proxy_weth_balance = weth9_contract
        .balanceOf(executor.create(1))
        .call()
        .await
        .unwrap();
    assert_eq!(proxy_weth_balance, TWO_ETH); // executor should have 2 eth worth of weth

    // Test ownership in the created proxy
    // WALLET -> executor -> proxy mint some weth

    let withdraw_calldata = IWETH::withdrawCall { amount: TWO_ETH }.abi_encode();
    let multiplexed_withdraw_calldata = FlowBuilder::empty()
        .call(WETH9, &withdraw_calldata, U256::ZERO)
        .optimize().build(); // multiplexed withdraw from weth

    let fb = FlowBuilder::empty().call(
        executor.create(1),
        &multiplexed_withdraw_calldata,
        U256::ZERO,
    ).optimize().build(); // this should send 2 eth to weth and assign the same weth value to the executor

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

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

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

    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();

    assert!(receipt.status());

    // Executor has
    // 0 eth
    // 0 weth
    let executor_balance = provider.get_balance(executor).await.unwrap();
    assert_eq!(executor_balance, U256::ZERO); // executor shoud shave sent the value to WETH9

    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_weth_balance, U256::ZERO);

    // Proxy created via executor that points to the executor ?? ?AHHH
    // 2 eth
    // 0 weth

    let proxy_balance = provider.get_balance(executor.create(1)).await.unwrap();
    assert_eq!(proxy_balance, TWO_ETH); // executor shoud shave sent the value to WETH9

    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let proxy_weth_balance = weth9_contract
        .balanceOf(executor.create(1))
        .call()
        .await
        .unwrap();
    assert_eq!(proxy_weth_balance, U256::ZERO); 

    // bob -> executor -> ?? :fail:
    // bob -> proxy  :fail:
}

#[tokio::test]
async fn test_wallet_can_proxy_create_ultimate() {
    let provider = get_provider();

    // reality check
    let weth9_contract = IERC20::new(WETH9, provider.clone());
    let weth_balance = provider.get_balance(WETH9).await.unwrap();
    assert_eq!(format!("{}", weth_balance), "2933633723194923479377016");

    // test WALLETs
    // 0x4141414141..4141414141  with 1001 eth
    // 0x4242424242..4242424242  with 1001 eth
    provider
        .anvil_set_balance(WALLET, BUDGET + U256::from(1e18 as u64))
        .await
        .unwrap();
    provider
        .anvil_set_balance(BOB, BUDGET + U256::from(1e18 as u64))
        .await
        .unwrap();

    ////////////////////////////////////////////////////////////
    // Make the Executor contract (WALLET is the owner)
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(EXECUTOR_INIT)
        .with_nonce(0);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();

    assert!(receipt.status());
    let executor = receipt.contract_address.unwrap();

    ////////////////////////////////////////////////////////////
    // Make the Proxy(Executor) contract (WALLET is the owner)
    // Link the proxy to the executor but do not use the delegatecall in the constructor

    let mut deploy_proxy_executor = DELEGATE_PROXY_INIT.to_vec();
    deploy_proxy_executor.extend(
        IProxy::constructorCall {
            _target: executor,
            constructorData: "".into(),
        }
        .abi_encode(),
    );

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(deploy_proxy_executor)
        .with_nonce(1);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    let proxy_executor = receipt.contract_address.unwrap();
    assert_eq!(proxy_executor, WALLET.create(1));

    ////////////////////////////////////////////////////////////
    // Deposit weth in the proxy account
    // Use the deployed Proxy(Executor) contract (WALLET is the owner) to deposit weth
    let deposit_calldata = [];
    let fb = FlowBuilder::empty().call(WETH9, &deposit_calldata, TWO_ETH).optimize().build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(proxy_executor)
        .with_value(TWO_ETH)
        .with_input(fb);

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

    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());

    // Executor account has no assets
    // 0 eth
    // 0 weth
    let executor_balance = provider.get_balance(executor).await.unwrap();
    assert_eq!(executor_balance, U256::ZERO); // executor shoud shave sent the value to WETH9
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_weth_balance, U256::ZERO); // executor should have 2 eth worth of weth

    // Proxy(Executor) account has 2 weth
    // 0 eth
    // 2 weth
    let proxy_executor_balance = provider.get_balance(proxy_executor).await.unwrap();
    assert_eq!(proxy_executor_balance, U256::ZERO); // executor shoud shave sent the value to WETH9
    let proxy_executor_weth_balance = weth9_contract
        .balanceOf(proxy_executor)
        .call()
        .await
        .unwrap();
    assert_eq!(proxy_executor_weth_balance, TWO_ETH); // executor should have 2 eth worth of weth

    ////////////////////////////////////////////////////////////
    // Whithdraw weth from the proxy account
    // Use the deployed Proxy(Executor) contract (WALLET is the owner) to deposit weth
    let withdraw_calldata = IWETH::withdrawCall { amount: TWO_ETH }.abi_encode();
    let fb = FlowBuilder::empty().call(WETH9, &withdraw_calldata, U256::ZERO).optimize().build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(proxy_executor)
        .with_value(U256::ZERO)
        .with_input(fb);

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

    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());

    // Executor account has no assets
    // 0 eth
    // 0 weth
    let executor_balance = provider.get_balance(executor).await.unwrap();
    assert_eq!(executor_balance, U256::ZERO); // executor shoud shave sent the value to WETH9
    let executor_weth_balance = weth9_contract.balanceOf(executor).call().await.unwrap();
    assert_eq!(executor_weth_balance, U256::ZERO); // executor should have 2 eth worth of weth

    // Proxy(Executor) account has 2 weth
    // 2 eth
    // 0 weth
    let proxy_executor_balance = provider.get_balance(proxy_executor).await.unwrap();
    assert_eq!(proxy_executor_balance, TWO_ETH);
    let proxy_executor_weth_balance = weth9_contract
        .balanceOf(proxy_executor)
        .call()
        .await
        .unwrap();
    assert_eq!(proxy_executor_weth_balance, U256::ZERO);
}

#[tokio::test]
async fn test_extcodecopy() {
    // Flipper.sol::
    // contract proxy {
    //     bool flag;
    //     function flip() external {
    //         flag = !flag;
    //     }
    // }

    let flipper_init = hex!("6080604052348015600e575f80fd5b50608f80601a5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c8063cde4efa914602a575b5f80fd5b60306032565b005b5f8054906101000a900460ff16155f806101000a81548160ff02191690831515021790555056fea264697066735822122054836815366ebd9b068e7694d59a986fb0267bc2cc7c9ec20ffdccea97c00a3b64736f6c634300081a0033");
    let flipper_runtime = hex!("6080604052348015600e575f80fd5b50600436106026575f3560e01c8063cde4efa914602a575b5f80fd5b60306032565b005b5f8054906101000a900460ff16155f806101000a81548160ff02191690831515021790555056fea264697066735822122054836815366ebd9b068e7694d59a986fb0267bc2cc7c9ec20ffdccea97c00a3b64736f6c634300081a0033");
    let flipper_prolog = hex!("6080604052348015600e575f80fd5b50608f80601a5f395ff3fe");
    let provider = get_provider();

    // reality check
    let weth_balance = provider.get_balance(WETH9).await.unwrap();
    assert_eq!(format!("{}", weth_balance), "2933633723194923479377016");

    // test WALLETs
    // 0x4141414141..4141414141  with 1001 eth
    // 0x4242424242..4242424242  with 1001 eth
    provider
        .anvil_set_balance(WALLET, BUDGET + U256::from(1e18 as u64))
        .await
        .unwrap();
    provider
        .anvil_set_balance(BOB, BUDGET + U256::from(1e18 as u64))
        .await
        .unwrap();
    // Make the Executor contract (WALLET is the owner)
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(EXECUTOR_INIT)
        .with_nonce(0);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    let executor = receipt.contract_address.unwrap();

    // create normal flipper account.
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_deploy_code(flipper_init);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    let flipper = receipt.contract_address.unwrap();

    let created_flipper_runtime = provider.get_code_at(flipper).await.unwrap();

    // The created flipper has the expected runtime bytecode. duh
    assert_eq!(created_flipper_runtime.to_vec(), flipper_runtime.to_vec());

    let flipper1 = executor.create(1);
    let fb = FlowBuilder::empty()
        .set_cleardata_op(flipper_init.len() as u16)
        .set_data_op(0, &flipper_init)
        .create_op(flipper1)
        .optimize().build();

    // create normal flipper account. Using data ops
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    println!("NO excodecopy gas used {:?}", receipt.gas_used);

    assert_eq!(
        address!("c84f9705070281e8c800c57d92dbab053a80a2d0"),
        flipper1
    );

    let created_flipper1_runtime = provider.get_code_at(flipper1).await.unwrap();
    assert_eq!(created_flipper1_runtime, created_flipper_runtime);

    // create normal flipper account. Using data Extcodecopy
    let flipper2 = executor.create(2);
    let fb = FlowBuilder::empty()
        .set_cleardata_op(flipper_init.len() as u16)
        .set_data_op(0, &flipper_prolog)
        //  .set_data_op(flipper_prolog.len() as u16, &flipper_runtime)
        //.set_data_op(0, &flipper_init)
        .set_extcodecopy_op(
            flipper1,
            flipper_prolog.len() as u16,
            0,
            created_flipper_runtime.len() as u16,
        )
        .create_op(flipper2)
        .optimize().build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider
        .get_transaction_receipt(tx_hash)
        .await
        .unwrap()
        .unwrap();
    assert!(receipt.status());
    println!("excodecopy gas used {:?}", receipt.gas_used);
    assert_eq!(
        address!("6266c8947cb0834202f2a3be9e0b5f97e0089fda"),
        flipper2
    );

    let created_flipper2_runtime = provider.get_code_at(flipper2).await.unwrap();
    assert_eq!(created_flipper2_runtime, created_flipper_runtime);
}

// This test test a simple flashloan with morpho 
#[tokio::test]
async fn test_flashloan_success_with_callback() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    let approve_calldata = IERC20::approveCall {
        spender: MORPHO,
        value: ONEHUNDRED_ETH,
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb.into(),
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());
}

#[tokio::test]
async fn test_flashloan_fails_without_callback() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    let approve_calldata = IERC20::approveCall {
        spender: MORPHO,
        value: ONEHUNDRED_ETH,
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb.into(),
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .call(MORPHO, &flashloan_calldata, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(!receipt.status());
}

#[tokio::test]
async fn test_multiple_flashloans_with_callback_reset() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    let approve_calldata = IERC20::approveCall {
        spender: MORPHO,
        value: ONEHUNDRED_ETH,
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb.into(),
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata, U256::ZERO)
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());
}

#[tokio::test]
async fn test_multiple_flashloans_fails_without_callback_reset() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    let approve_calldata = IERC20::approveCall {
        spender: MORPHO,
        value: ONEHUNDRED_ETH,
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb.into(),
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata, U256::ZERO)
        .call(MORPHO, &flashloan_calldata, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(!receipt.status());
}

#[tokio::test]
async fn test_nested_flashloan_success() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    let approve_calldata = IERC20::approveCall {
        spender: MORPHO,
        value: ONEHUNDRED_ETH,
    }.abi_encode();

    let fb_approve = FlowBuilder::empty()
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata_inner = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb_approve.into(),
    }.abi_encode();

    let fb_inner = FlowBuilder::empty()
        .set_fail()
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata_inner, U256::ZERO)
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata_outer = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb_inner.into(),
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata_outer, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());
}

#[tokio::test]
async fn test_nested_flashloan_fails_without_callback() {
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    let approve_calldata = IERC20::approveCall {
        spender: MORPHO,
        value: ONEHUNDRED_ETH,
    }.abi_encode();

    let fb_approve = FlowBuilder::empty()
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata_inner = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb_approve.into(),
    }.abi_encode();

    let fb_inner = FlowBuilder::empty()
        .set_fail()
        .call(MORPHO, &flashloan_calldata_inner, U256::ZERO)
        .call(WETH9, &approve_calldata, U256::ZERO)
        .optimize()
        .build_raw();

    let flashloan_calldata_outer = IMorpho::flashLoanCall {
        token: WETH9,
        assets: ONEHUNDRED_ETH,
        data: fb_inner.into(),
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .set_callback(MORPHO)
        .call(MORPHO, &flashloan_calldata_outer, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(!receipt.status());
}

#[tokio::test]
/// Tests Aave V3 flash loan functionality with callback
///
/// This test verifies:
/// 1. Flash loan execution with proper callback setup
/// 2. Premium calculation and payment (0.05%)
/// 3. WETH wrapping/unwrapping during the process
/// 4. Successful transaction completion and balance verification
async fn test_flashloan_aave3_success_with_callback() {
    // Aave V3 Pool contract on mainnet
    const AAVE3_POOL: Address = address!("87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2");
    // Flash loan premium rate (0.05%)
    const PREMIUM_FACTOR: U256 = uint!(500000000000000_U256);
    
    let provider = setup_provider().await;
    let executor = deploy_executor(&provider).await;

    // Calculate premium for 100 ETH flash loan
    let premium = ONEHUNDRED_ETH * PREMIUM_FACTOR / uint!(1000000000000000000_U256);

    // First send premium amount to executor so it can repay the flash loan
    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(premium);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());

    sol! {
        interface IAavePool {
            function flashLoanSimple(
                address receiverAddress,
                address asset,
                uint256 amount,
                bytes calldata params,
                uint16 referralCode
            ) external;
        }
    }

    // Build the repayment flow that will be executed in the callback
    let repay_fb = FlowBuilder::empty()
        // First deposit ETH to get WETH for the premium
        .call(WETH9, &[], premium)
        // Then transfer full amount back to Aave
        .call(WETH9, &IERC20::approveCall {
            spender: AAVE3_POOL,
            value: ONEHUNDRED_ETH + premium,
        }.abi_encode(), U256::ZERO)
        .optimize()
        .build_raw();

    // Build flash loan call using proper ABI encoding
    let flashloan_calldata = IAavePool::flashLoanSimpleCall {
        receiverAddress: executor,
        asset: WETH9,
        amount: ONEHUNDRED_ETH,
        params: repay_fb.into(),
        referralCode: 0
    }.abi_encode();

    let fb = FlowBuilder::empty()
        .set_fail()
        .set_callback(AAVE3_POOL)
        .call(AAVE3_POOL, &flashloan_calldata, U256::ZERO)
        .optimize()
        .build();

    let tx = TransactionRequest::default()
        .with_from(WALLET)
        .with_to(executor)
        .with_value(U256::ZERO)
        .with_input(fb);

    let tx_hash = provider.eth_send_unsigned_transaction(tx).await.unwrap();
    provider.evm_mine(None).await.unwrap();
    let receipt = provider.get_transaction_receipt(tx_hash).await.unwrap().unwrap();
    assert!(receipt.status());
}