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
use std::ops::{Deref, DerefMut};

use alloy_primitives::Address;
use serde::{Deserialize, Serialize};

use crate::{pair::Pair, FastHashMap, Protocol};

#[derive(Debug, Clone, Default, PartialEq)]
pub struct SubGraphsEntry(pub FastHashMap<u64, Vec<SubGraphEdge>>);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SubGraphEdge {
    pub info: PoolPairInfoDirection,
}
impl Deref for SubGraphEdge {
    type Target = PoolPairInfoDirection;

    fn deref(&self) -> &Self::Target {
        &self.info
    }
}
impl DerefMut for SubGraphEdge {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.info
    }
}

impl SubGraphEdge {
    pub fn new(info: PoolPairInfoDirection) -> Self {
        Self { info }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash, PartialOrd, Ord)]
pub struct PoolPairInformation {
    pub pool_addr: Address,
    pub dex_type:  Protocol,
    pub token_0:   Address,
    pub token_1:   Address,
}

impl PoolPairInformation {
    pub fn new(pool_addr: Address, dex_type: Protocol, token_0: Address, token_1: Address) -> Self {
        Self { pool_addr, dex_type, token_0, token_1 }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PoolPairInfoDirection {
    pub info:       &'static PoolPairInformation,
    pub token_0_in: bool,
}

impl PoolPairInfoDirection {
    pub fn new(info: &'static PoolPairInformation, token_0_in: bool) -> Self {
        Self { info, token_0_in }
    }
}

impl Deref for PoolPairInfoDirection {
    type Target = PoolPairInformation;

    fn deref(&self) -> &Self::Target {
        self.info
    }
}

impl PoolPairInfoDirection {
    fn info(&self) -> &PoolPairInformation {
        self.info
    }

    pub fn get_token_with_direction(&self, outgoing: bool) -> Address {
        if outgoing {
            self.get_base_token()
        } else {
            self.get_quote_token()
        }
    }

    pub fn get_base_token(&self) -> Address {
        if self.token_0_in {
            self.info().token_0
        } else {
            self.info().token_1
        }
    }

    pub fn get_pair(&self) -> Pair {
        if self.token_0_in {
            Pair(self.info().token_0, self.info().token_1)
        } else {
            Pair(self.info().token_1, self.info().token_0)
        }
    }

    pub fn get_quote_token(&self) -> Address {
        if self.token_0_in {
            self.info().token_1
        } else {
            self.info().token_0
        }
    }
}