loom_types_entities/
swap_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
use crate::tips::Tips;
use crate::Swap;
use alloy_primitives::{Address, BlockNumber, Bytes, U256};
use eyre::Result;
use std::ops::Deref;
use std::sync::Arc;

pub trait SwapEncoder {
    /// Encodes Swap
    ///
    /// - next_block_number - number of the next block
    /// - next_block_gas_price - base_fee + priority fee for transaction
    /// - sender_address - EOA of of the transaction
    /// - sender_eth_balance - balance of EOA
    ///
    /// returns (to. value, call_data) for transaction

    fn encode(
        &self,
        swap: Swap,
        tips_pct: Option<u32>,
        next_block_number: Option<BlockNumber>,
        gas_cost: Option<U256>,
        sender_address: Option<Address>,
        sender_eth_balance: Option<U256>,
    ) -> Result<(Address, Option<U256>, Bytes, Vec<Tips>)>
    where
        Self: Sized;
}

#[derive(Clone)]
pub struct SwapEncoderWrapper {
    pub inner: Arc<dyn SwapEncoder>,
}

impl SwapEncoderWrapper {
    pub fn new(encoder: Arc<dyn SwapEncoder>) -> Self {
        SwapEncoderWrapper { inner: encoder }
    }
}

impl<T: 'static + SwapEncoder + Clone> From<T> for SwapEncoderWrapper {
    fn from(pool: T) -> Self {
        Self { inner: Arc::new(pool) }
    }
}

impl Deref for SwapEncoderWrapper {
    type Target = dyn SwapEncoder;
    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}