loom_execution_multicaller/
swapline_encoder.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
use std::sync::Arc;

use alloy_primitives::{Address, Bytes, U256};
use eyre::{eyre, Result};
use tracing::{trace, warn};

use loom_defi_address_book::TokenAddress;
use loom_types_blockchain::{MulticallerCall, MulticallerCalls};
use loom_types_entities::{PoolClass, PoolWrapper, PreswapRequirement, SwapAmountType, SwapLine, Token};

use crate::helpers::EncoderHelper;
use crate::opcodes_encoder::{OpcodesEncoder, OpcodesEncoderV2};
use crate::poolencoders::{CurveSwapEncoder, StEthSwapEncoder, WstEthSwapEncoder};

#[derive(Clone)]
pub struct SwapLineEncoder {
    multicaller: Address,
}

impl SwapLineEncoder {
    pub fn new(multicaller: Address) -> SwapLineEncoder {
        SwapLineEncoder { multicaller }
    }

    pub fn encode_flash_swap_line_in_amount(
        &self,
        swap_path: &SwapLine,
        inside_swap_opcodes: MulticallerCalls,
        funds_to: Address,
    ) -> Result<MulticallerCalls> {
        trace!("encode_flash_swap_line_in_amount funds_to={}", funds_to);
        let mut flash_swap_opcodes = MulticallerCalls::new();
        let mut inside_opcodes = inside_swap_opcodes.clone();

        let mut reverse_pools: Vec<PoolWrapper> = swap_path.pools().clone();
        reverse_pools.reverse();
        let mut reverse_tokens: Vec<Arc<Token>> = swap_path.tokens().clone();
        reverse_tokens.reverse();

        let mut prev_pool: Option<&PoolWrapper> = None;

        for (pool_idx, flash_pool) in reverse_pools.iter().enumerate() {
            let token_from_address = reverse_tokens[pool_idx + 1].get_address();
            let token_to_address = reverse_tokens[pool_idx].get_address();

            let amount_in = if pool_idx == swap_path.pools().len() - 1 { swap_path.amount_in } else { SwapAmountType::Stack0 };

            let swap_to = match prev_pool {
                Some(prev_pool) => match flash_pool.get_class() {
                    PoolClass::UniswapV2 => match prev_pool.get_encoder().preswap_requirement() {
                        PreswapRequirement::Transfer(transfer_to) => {
                            trace!(
                                "uniswap v2 transfer to previous pool: token={:?}, to={:?}, prev_pool={:?}",
                                token_to_address,
                                transfer_to,
                                prev_pool.get_address()
                            );
                            let mut transfer_opcode =
                                MulticallerCall::new_call(token_to_address, &EncoderHelper::encode_erc20_transfer(transfer_to, U256::ZERO));
                            transfer_opcode.set_call_stack(false, 0, 0x24, 0x20);
                            inside_opcodes.insert(transfer_opcode);
                            transfer_to
                        }
                        _ => self.multicaller,
                    },
                    _ => match prev_pool.get_encoder().preswap_requirement() {
                        PreswapRequirement::Transfer(funds_to) => {
                            trace!("other transfer to previous pool: funds_to={:?}, prev_pool={:?}", funds_to, prev_pool);
                            funds_to
                        }
                        _ => {
                            trace!("other swap to multicaller, prev_pool={:?}", prev_pool);
                            self.multicaller
                        }
                    },
                },
                None => funds_to,
            };

            match flash_pool.get_class() {
                PoolClass::UniswapV2 => {
                    match amount_in {
                        SwapAmountType::Set(amount) => {
                            trace!(
                                "uniswap v2 transfer token={:?}, to={:?}, amount={}",
                                token_from_address,
                                flash_pool.get_address(),
                                amount
                            );
                            inside_opcodes.add(MulticallerCall::new_call(
                                token_from_address,
                                &EncoderHelper::encode_erc20_transfer(flash_pool.get_address(), amount),
                            ));
                        }
                        _ => {
                            warn!("InAmountTypeNotSet")
                        }
                    }

                    if pool_idx == 0 && funds_to != self.multicaller {
                        trace!("uniswap v2 transfer to token_to_address={:?}, funds_to={:?}", token_to_address, funds_to);
                        let mut transfer_opcode =
                            MulticallerCall::new_call(token_to_address, &EncoderHelper::encode_erc20_transfer(funds_to, U256::ZERO));
                        transfer_opcode.set_call_stack(false, 0, 0x24, 0x20);
                        inside_opcodes.insert(transfer_opcode);
                    }
                }
                PoolClass::UniswapV3 => {
                    let transfer_opcode = match amount_in {
                        SwapAmountType::Set(amount) => {
                            trace!(
                                "uniswap v3 transfer token={:?}, to={:?}, amount={:?}",
                                token_to_address,
                                flash_pool.get_address(),
                                amount
                            );

                            MulticallerCall::new_call(
                                token_from_address,
                                &EncoderHelper::encode_erc20_transfer(flash_pool.get_address(), amount),
                            )
                        }
                        _ => {
                            trace!("other transfer token={:?}, to={:?}", token_to_address, flash_pool.get_address());
                            MulticallerCall::new_call(
                                token_from_address,
                                &EncoderHelper::encode_erc20_transfer(flash_pool.get_address(), U256::ZERO),
                            )
                            .set_call_stack(false, 1, 0x24, 0x20)
                            .clone()
                        }
                    };

                    inside_opcodes.add(transfer_opcode);
                }

                _ => {
                    return Err(eyre!("CANNOT_ENCODE_FLASH_CALL"));
                }
            }

            let inside_call_bytes = OpcodesEncoderV2::pack_do_calls_data(&inside_opcodes)?;
            flash_swap_opcodes = MulticallerCalls::new();

            trace!("flash swap_to {:?}", swap_to);

            match flash_pool.get_class() {
                PoolClass::UniswapV2 => {
                    let get_out_amount_opcode = match amount_in {
                        SwapAmountType::Set(amount) => {
                            trace!("uniswap v2 get out amount for pool={:?}, amount={}", flash_pool.get_address(), amount);
                            MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_out_amount(
                                token_from_address,
                                token_to_address,
                                flash_pool.get_address(),
                                amount,
                                flash_pool.get_fee(),
                            ))
                        }
                        _ => {
                            trace!("uniswap v2 get out amount, pool={:?}", flash_pool.get_address());
                            MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_out_amount(
                                token_from_address,
                                token_to_address,
                                flash_pool.get_address(),
                                U256::ZERO,
                                flash_pool.get_fee(),
                            ))
                            .set_call_stack(false, 0, 0x24, 0x20)
                            .clone()
                        }
                    };

                    let mut swap_opcode = MulticallerCall::new_call(
                        flash_pool.get_address(),
                        &flash_pool.get_encoder().encode_swap_out_amount_provided(
                            token_from_address,
                            token_to_address,
                            U256::ZERO,
                            self.multicaller,
                            inside_call_bytes,
                        )?,
                    );
                    swap_opcode.set_call_stack(
                        true,
                        0,
                        flash_pool.get_encoder().swap_out_amount_offset(token_from_address, token_to_address).unwrap(),
                        0x20,
                    );

                    flash_swap_opcodes.add(get_out_amount_opcode).add(swap_opcode);

                    prev_pool = Some(flash_pool);
                    inside_opcodes = flash_swap_opcodes.clone();
                }
                PoolClass::UniswapV3 => {
                    let swap_opcode = match amount_in {
                        SwapAmountType::Set(amount) => {
                            trace!("uniswap v3 swap in amount for pool={:?}, amount={}", flash_pool.get_address(), amount);
                            MulticallerCall::new_call(
                                flash_pool.get_address(),
                                &flash_pool.get_encoder().encode_swap_in_amount_provided(
                                    token_from_address,
                                    token_to_address,
                                    amount,
                                    swap_to,
                                    inside_call_bytes,
                                )?,
                            )
                        }
                        _ => {
                            trace!("uniswap v3 swap in amount for pool={:?}", flash_pool.get_address());
                            MulticallerCall::new_call(
                                flash_pool.get_address(),
                                &flash_pool.get_encoder().encode_swap_in_amount_provided(
                                    token_from_address,
                                    token_to_address,
                                    U256::ZERO,
                                    swap_to,
                                    inside_call_bytes,
                                )?,
                            )
                            .set_call_stack(
                                false,
                                0,
                                flash_pool.get_encoder().swap_in_amount_offset(token_from_address, token_to_address).unwrap(),
                                0x20,
                            )
                            .clone()
                        }
                    };

                    flash_swap_opcodes.add(swap_opcode);

                    prev_pool = Some(flash_pool);
                    inside_opcodes = flash_swap_opcodes.clone();
                }
                _ => {
                    return Err(eyre!("CANNOT_ENCODE_FLASH_CALL"));
                }
            }
        }

        Ok(flash_swap_opcodes)
    }

    pub fn encode_flash_swap_line_out_amount(
        &self,
        swap_path: &SwapLine,
        inside_swap_opcodes: MulticallerCalls,
        _funds_from: Address,
    ) -> Result<MulticallerCalls> {
        trace!("encode_flash_swap_line_out_amount");
        let mut flash_swap_opcodes = MulticallerCalls::new();
        let mut inside_opcodes = inside_swap_opcodes.clone();

        let pools: Vec<PoolWrapper> = swap_path.pools().clone();

        let tokens: Vec<Arc<Token>> = swap_path.tokens().clone();

        for (pool_idx, flash_pool) in pools.iter().enumerate() {
            let token_from_address = tokens[pool_idx].get_address();
            let token_to_address = tokens[pool_idx + 1].get_address();

            let next_pool = if pool_idx < pools.len() - 1 { Some(&pools[pool_idx + 1]) } else { None };

            let amount_out = if pool_idx == pools.len() - 1 { swap_path.amount_out } else { SwapAmountType::Stack0 };

            let swap_to = match next_pool {
                Some(next_pool) => next_pool.get_address(),
                None => self.multicaller,
            };

            match flash_pool.get_class() {
                PoolClass::UniswapV2 => {
                    let mut get_in_amount_opcode =
                        MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_in_amount(
                            token_from_address,
                            token_to_address,
                            flash_pool.get_address(),
                            amount_out.unwrap_or_zero(),
                            flash_pool.get_fee(),
                        ));

                    match amount_out {
                        SwapAmountType::Set(_) => {}
                        _ => {
                            get_in_amount_opcode.set_call_stack(false, 0, 0x24, 20);
                        }
                    }
                    inside_opcodes.insert(get_in_amount_opcode);

                    if pool_idx == 0 && swap_to != flash_pool.get_address() {
                        let mut transfer_opcode = MulticallerCall::new_call(
                            token_from_address,
                            &EncoderHelper::encode_erc20_transfer(flash_pool.get_address(), U256::ZERO),
                        );
                        transfer_opcode.set_call_stack(false, 1, 0x24, 0x20);

                        inside_opcodes.add(transfer_opcode);
                    };

                    if swap_to != self.multicaller {
                        let mut transfer_opcode =
                            MulticallerCall::new_call(token_to_address, &EncoderHelper::encode_erc20_transfer(swap_to, U256::ZERO));
                        transfer_opcode.set_call_stack(false, 0, 0x24, 0x20);
                        inside_opcodes.add(transfer_opcode);
                    }

                    /*
                    match amount_out {
                        SwapAmountType::Set(amount) => {
                            inside_opcodes
                                .add( Opcode::new_call(token_to_address,
                                                       &EncoderHelper::encode_erc20_transfer(flash_pool.get_address(), amount) ) );
                        }
                        _=> {
                            error!("OutAmountTypeNotSet")
                        }
                    }
                     */
                    /*

                    let mut get_in_amount_opcode =
                        match amount_out {
                            InAmountType::Set(amount)=> {
                                Opcode::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_in_amount(
                                    token_from_address,
                                    token_to_address,
                                    flash_pool.get_address(),
                                    amount,
                                    flash_pool.get_fee()
                                ))
                            }
                            _=> {
                                error!("Uni2 In amount not handled");
                                Opcode::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_in_amount(
                                    token_from_address,
                                    token_to_address,
                                    flash_pool.get_address(),
                                    U256::zero(),
                                    flash_pool.get_fee()
                                )).set_call_stack(false, 0, 0x24,0x20).clone()
                            }
                        };

                     */

                    /*
                    if pool_idx == 0 && funds_to != self.multicaller {
                        let mut transfer_opcode = Opcode::new_call(token_to_address,
                                                                   &EncoderHelper::encode_erc20_transfer(funds_to, U256::zero()));
                        transfer_opcode.set_call_stack(false, 0, 0x24, 0x20);
                        inside_opcodes
                            .insert( transfer_opcode);
                    }
                     */
                }
                PoolClass::UniswapV3 => {
                    if pool_idx == 0 {
                        let mut transfer_opcode = MulticallerCall::new_call(
                            token_from_address,
                            &EncoderHelper::encode_erc20_transfer(flash_pool.get_address(), U256::ZERO),
                        );
                        transfer_opcode.set_call_stack(false, 1, 0x24, 0x20);

                        inside_opcodes.add(transfer_opcode);
                    }
                }

                _ => {
                    return Err(eyre!("CANNOT_ENCODE_FLASH_CALL"));
                }
            }

            let inside_call_bytes = OpcodesEncoderV2::pack_do_calls_data(&inside_opcodes)?;
            flash_swap_opcodes = MulticallerCalls::new();

            trace!("flash swap_to {:?}", swap_to);

            match flash_pool.get_class() {
                PoolClass::UniswapV2 => {
                    trace!("uniswap v2 swap out amount provided for pool={:?}, amount_out={:?}", flash_pool.get_address(), amount_out);
                    let mut swap_opcode = MulticallerCall::new_call(
                        flash_pool.get_address(),
                        &flash_pool.get_encoder().encode_swap_out_amount_provided(
                            token_from_address,
                            token_to_address,
                            amount_out.unwrap_or_zero(),
                            self.multicaller,
                            inside_call_bytes,
                        )?,
                    );

                    match amount_out {
                        SwapAmountType::Set(_) => {
                            trace!("uniswap v2 amount out set amount");
                        }
                        _ => {
                            trace!("uniswap v2 amount out else");
                            swap_opcode.set_call_stack(
                                true,
                                0,
                                flash_pool.get_encoder().swap_out_amount_offset(token_from_address, token_to_address).unwrap(),
                                0x20,
                            );
                        }
                    };

                    flash_swap_opcodes.add(swap_opcode);

                    inside_opcodes = flash_swap_opcodes.clone();
                }
                PoolClass::UniswapV3 => {
                    let swap_opcode = match amount_out {
                        SwapAmountType::Set(amount) => {
                            trace!(
                                "uniswap v3 swap out amount provided for pool={:?}, amount_out={:?}",
                                flash_pool.get_address(),
                                amount_out
                            );
                            MulticallerCall::new_call(
                                flash_pool.get_address(),
                                &flash_pool.get_encoder().encode_swap_out_amount_provided(
                                    token_from_address,
                                    token_to_address,
                                    amount,
                                    swap_to,
                                    inside_call_bytes,
                                )?,
                            )
                        }
                        _ => {
                            trace!("uniswap v3 else swap out amount for pool={:?}", flash_pool.get_address());
                            flash_swap_opcodes.add(MulticallerCall::new_calculation_call(&Bytes::from(vec![0x8, 0x2A, 0x00])));

                            MulticallerCall::new_call(
                                flash_pool.get_address(),
                                &flash_pool.get_encoder().encode_swap_out_amount_provided(
                                    token_from_address,
                                    token_to_address,
                                    U256::ZERO,
                                    swap_to,
                                    inside_call_bytes,
                                )?,
                            )
                            .set_call_stack(
                                true,
                                0,
                                flash_pool.get_encoder().swap_out_amount_offset(token_from_address, token_to_address).unwrap(),
                                0x20,
                            )
                            .clone()
                        }
                    };

                    flash_swap_opcodes.add(swap_opcode);

                    inside_opcodes = flash_swap_opcodes.clone();
                }
                _ => {
                    return Err(eyre!("CANNOT_ENCODE_FLASH_CALL"));
                }
            }
        }

        Ok(flash_swap_opcodes)
    }

    pub fn encode_flash_swap_dydx(&self, _inside_swap_opcodes: MulticallerCalls, _funds_from: Address) -> Result<MulticallerCalls> {
        Err(eyre!("NOT_IMPLEMENTED"))
    }

    pub fn encode_swap_line_in_amount(&self, swap_path: &SwapLine, funds_from: Address, funds_to: Address) -> Result<MulticallerCalls> {
        let mut swap_opcodes = MulticallerCalls::new();

        for i in 0..swap_path.pools().len() {
            let token_from_address = swap_path.tokens()[i].get_address();
            let token_to_address = swap_path.tokens()[i + 1].get_address();

            let cur_pool = &swap_path.pools()[i].clone();
            let next_pool: Option<&PoolWrapper> = if i < swap_path.pools().len() - 1 { Some(&swap_path.pools()[i + 1]) } else { None };

            trace!(
                "encode_swap_line_in_amount for from={} to={} pool={}, next_pool={:?}",
                token_from_address,
                token_to_address,
                cur_pool.get_address(),
                next_pool.map(|next_pool| next_pool.get_address())
            );

            let swap_to: Address = if let Some(next_pool) = next_pool {
                match &next_pool.get_encoder().preswap_requirement() {
                    PreswapRequirement::Transfer(next_funds_to) => *next_funds_to,
                    _ => self.multicaller,
                }
            } else {
                funds_to
            };

            trace!("swap_to {:?}", swap_to);

            match cur_pool.get_class() {
                PoolClass::UniswapV2 => {
                    if i == 0 {
                        match swap_path.amount_in {
                            SwapAmountType::Set(value) => {
                                trace!("uniswap v2 i == 0 set amount in {}", value);
                                if funds_from != cur_pool.get_address() {
                                    trace!("transfer token={:?}, to={:?}, amount={}", token_from_address, cur_pool.get_address(), value);
                                    let transfer_opcode = MulticallerCall::new_call(
                                        token_from_address,
                                        &EncoderHelper::encode_erc20_transfer(cur_pool.get_address(), value),
                                    );
                                    swap_opcodes.add(transfer_opcode);
                                }

                                let get_out_amount_opcode =
                                    MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_out_amount(
                                        token_from_address,
                                        token_to_address,
                                        cur_pool.get_address(),
                                        value,
                                        cur_pool.get_fee(),
                                    ));
                                swap_opcodes.add(get_out_amount_opcode);
                            }
                            SwapAmountType::Balance(addr) => {
                                trace!("uniswap v2 i == 0 balance of addr={:?}", addr);
                                let mut balance_opcode =
                                    MulticallerCall::new_static_call(token_from_address, &EncoderHelper::encode_erc20_balance_of(addr));
                                balance_opcode.set_return_stack(true, 0, 0x0, 0x20);
                                swap_opcodes.add(balance_opcode);

                                if funds_from != cur_pool.get_address() {
                                    trace!("transfer token={:?}, to={:?}, amount=from stack", token_from_address, cur_pool.get_address());
                                    let mut transfer_opcode = MulticallerCall::new_call(
                                        token_from_address,
                                        &EncoderHelper::encode_erc20_transfer(cur_pool.get_address(), U256::ZERO),
                                    );
                                    transfer_opcode.set_call_stack(true, 0, 0x24, 0x20);
                                    swap_opcodes.add(transfer_opcode);
                                }
                                trace!(
                                    "uni2 get out amount from={:?}, to={:?}, pool={:?}",
                                    token_from_address,
                                    token_to_address,
                                    cur_pool.get_address()
                                );
                                let mut get_out_amount_opcode =
                                    MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_out_amount(
                                        token_from_address,
                                        token_to_address,
                                        cur_pool.get_address(),
                                        U256::ZERO,
                                        cur_pool.get_fee(),
                                    ));
                                get_out_amount_opcode.set_call_stack(true, 0, 0x24, 0x20);
                                swap_opcodes.add(get_out_amount_opcode);
                            }
                            _ => {
                                trace!("uniswap v2 i == 0");
                                if funds_from != cur_pool.get_address() {
                                    trace!("transfer token={:?}, to={:?}, amount=from stack", token_from_address, cur_pool.get_address());
                                    let mut transfer_opcode = MulticallerCall::new_call(
                                        token_from_address,
                                        &EncoderHelper::encode_erc20_transfer(cur_pool.get_address(), U256::ZERO),
                                    );
                                    transfer_opcode.set_call_stack(false, 0, 0x24, 0x20);
                                    swap_opcodes.add(transfer_opcode);
                                }

                                let mut get_out_amount_opcode =
                                    MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_out_amount(
                                        token_from_address,
                                        token_to_address,
                                        cur_pool.get_address(),
                                        U256::ZERO,
                                        cur_pool.get_fee(),
                                    ));
                                get_out_amount_opcode.set_call_stack(false, 0, 0x24, 0x20);
                                swap_opcodes.add(get_out_amount_opcode);
                            }
                        }
                    } else {
                        trace!("uniswap v2 i != 0 else pool={:?}", cur_pool.get_address());
                        let mut get_out_amount_opcode =
                            MulticallerCall::new_internal_call(&EncoderHelper::encode_multicaller_uni2_get_out_amount(
                                token_from_address,
                                token_to_address,
                                cur_pool.get_address(),
                                U256::from(1),
                                cur_pool.get_fee(),
                            ));
                        get_out_amount_opcode.set_call_stack(true, 0, 0x24, 0x20);
                        swap_opcodes.add(get_out_amount_opcode);
                    };

                    let mut swap_opcode = MulticallerCall::new_call(
                        cur_pool.get_address(),
                        &cur_pool.get_encoder().encode_swap_out_amount_provided(
                            token_from_address,
                            token_to_address,
                            U256::from(1),
                            swap_to,
                            Bytes::new(),
                        )?,
                    );
                    swap_opcode.set_call_stack(
                        true,
                        0,
                        cur_pool.get_encoder().swap_out_amount_offset(token_from_address, token_to_address).unwrap(),
                        0x20,
                    );

                    swap_opcodes.add(swap_opcode);
                }
                PoolClass::UniswapV3 => {
                    let inside_call_payload = Bytes::from(token_from_address.to_vec());

                    let mut swap_opcode = if i == 0 {
                        match swap_path.amount_in {
                            SwapAmountType::Set(amount) => {
                                trace!("uniswap v3 i == 0 set amount in for pool={:?}, amount={}", cur_pool.get_address(), amount);
                                MulticallerCall::new_call(
                                    cur_pool.get_address(),
                                    &cur_pool.get_encoder().encode_swap_in_amount_provided(
                                        token_from_address,
                                        token_to_address,
                                        amount,
                                        swap_to,
                                        inside_call_payload,
                                    )?,
                                )
                            }
                            SwapAmountType::Balance(addr) => {
                                trace!("uniswap v3 i == 0 balance of for pool={:?}, addr={}", cur_pool.get_address(), addr);
                                let mut balance_opcode =
                                    MulticallerCall::new_static_call(token_from_address, &EncoderHelper::encode_erc20_balance_of(addr));
                                balance_opcode.set_return_stack(true, 0, 0x0, 0x20);

                                swap_opcodes.add(balance_opcode);

                                let mut swap_opcode = MulticallerCall::new_call(
                                    cur_pool.get_address(),
                                    &cur_pool.get_encoder().encode_swap_in_amount_provided(
                                        token_from_address,
                                        token_to_address,
                                        U256::ZERO,
                                        swap_to,
                                        inside_call_payload,
                                    )?,
                                );

                                swap_opcode.set_call_stack(
                                    true,
                                    0,
                                    cur_pool.get_encoder().swap_in_amount_offset(token_from_address, token_to_address).unwrap(),
                                    0x20,
                                );
                                swap_opcode
                            }
                            _ => {
                                trace!("uniswap v3 i == 0 else for pool={:?}", cur_pool.get_address());
                                let mut swap_opcode = MulticallerCall::new_call(
                                    cur_pool.get_address(),
                                    &cur_pool.get_encoder().encode_swap_in_amount_provided(
                                        token_from_address,
                                        token_to_address,
                                        U256::ZERO,
                                        swap_to,
                                        inside_call_payload,
                                    )?,
                                );

                                swap_opcode.set_call_stack(
                                    false,
                                    0,
                                    cur_pool.get_encoder().swap_in_amount_offset(token_from_address, token_to_address).unwrap(),
                                    0x20,
                                );
                                swap_opcode
                            }
                        }
                    } else {
                        trace!("uniswap v3 i != 0 else for pool={:?}", cur_pool.get_address());
                        let mut swap_opcode = MulticallerCall::new_call(
                            cur_pool.get_address(),
                            &cur_pool.get_encoder().encode_swap_in_amount_provided(
                                token_from_address,
                                token_to_address,
                                U256::ZERO,
                                swap_to,
                                inside_call_payload,
                            )?,
                        );

                        swap_opcode.set_call_stack(
                            true,
                            0,
                            cur_pool.get_encoder().swap_in_amount_offset(token_from_address, token_to_address).unwrap(),
                            0x20,
                        );
                        swap_opcode
                    };

                    swap_opcode.set_return_stack(
                        true,
                        0,
                        cur_pool.get_encoder().swap_in_amount_return_offset(token_from_address, token_to_address).unwrap(),
                        0x20,
                    );

                    swap_opcodes.add(swap_opcode);

                    if next_pool.is_some() {
                        trace!("has next pool");
                        if let Some(x) = cur_pool.get_encoder().swap_in_amount_return_script(token_from_address, token_to_address) {
                            let calc_opcode = MulticallerCall::new_calculation_call(&x);
                            swap_opcodes.add(calc_opcode);
                        }
                    }
                }
                PoolClass::Curve => {
                    CurveSwapEncoder::encode_swap_in_amount_provided(
                        token_from_address,
                        token_to_address,
                        if i == 0 { swap_path.amount_in } else { SwapAmountType::RelativeStack(0) },
                        &mut swap_opcodes,
                        cur_pool,
                        next_pool,
                        self.multicaller,
                    )?;
                }
                PoolClass::LidoWstEth => {
                    WstEthSwapEncoder::encode_swap_in_amount_provided(
                        token_from_address,
                        token_to_address,
                        if i == 0 { swap_path.amount_in } else { SwapAmountType::RelativeStack(0) },
                        &mut swap_opcodes,
                        cur_pool,
                        next_pool,
                        self.multicaller,
                    )?;
                }

                PoolClass::LidoStEth => {
                    StEthSwapEncoder::encode_swap_in_amount_provided(
                        token_from_address,
                        token_to_address,
                        if i == 0 { swap_path.amount_in } else { SwapAmountType::RelativeStack(0) },
                        &mut swap_opcodes,
                        cur_pool,
                        next_pool,
                        self.multicaller,
                    )?;
                }
                _ => {
                    return Err(eyre!("POOL_TYPE_NOT_SUPPORTED"));
                }
            }
        }
        Ok(swap_opcodes)
    }

    pub fn encode_tips(
        &self,
        swap_opcodes: MulticallerCalls,
        token_address: Address,
        min_balance: U256,
        tips: U256,
        to: Address,
    ) -> Result<MulticallerCalls> {
        let mut tips_opcodes = swap_opcodes.clone();

        let call_data = if token_address == TokenAddress::WETH {
            trace!("encode_multicaller_transfer_tips_weth");
            EncoderHelper::encode_multicaller_transfer_tips_weth(min_balance, tips, to)
        } else {
            trace!("encode_multicaller_transfer_tips");
            EncoderHelper::encode_multicaller_transfer_tips(token_address, min_balance, tips, to)
        };
        tips_opcodes.add(MulticallerCall::new_internal_call(&call_data));
        Ok(tips_opcodes)
    }
}