kabu_types_entities/
swap_direction.rs

1use crate::PoolId;
2use alloy_primitives::Address;
3use std::fmt::Debug;
4use std::hash::{DefaultHasher, Hash, Hasher};
5
6#[derive(Clone, Debug)]
7pub struct SwapDirection {
8    token_from: Address,
9    token_to: Address,
10}
11
12impl SwapDirection {
13    #[inline]
14    pub fn new(token_from: Address, token_to: Address) -> Self {
15        Self { token_from, token_to }
16    }
17
18    #[inline]
19    pub fn from(&self) -> &Address {
20        &self.token_from
21    }
22    #[inline]
23    pub fn to(&self) -> &Address {
24        &self.token_to
25    }
26
27    #[inline]
28    pub fn get_hash(&self) -> u64 {
29        let mut hasher = DefaultHasher::new();
30        self.hash(&mut hasher);
31        hasher.finish()
32    }
33
34    #[inline]
35    pub fn get_hash_with_pool(&self, pool_id: &PoolId) -> u64 {
36        let mut hasher = DefaultHasher::new();
37        self.hash(&mut hasher);
38        pool_id.hash(&mut hasher);
39        hasher.finish()
40    }
41}
42
43impl Hash for SwapDirection {
44    fn hash<H: Hasher>(&self, state: &mut H) {
45        self.token_from.hash(state);
46        self.token_to.hash(state);
47    }
48}
49
50impl PartialEq for SwapDirection {
51    fn eq(&self, other: &Self) -> bool {
52        self.token_from.eq(&other.token_from) && self.token_to.eq(&other.token_to)
53    }
54}
55
56impl Eq for SwapDirection {}
57
58impl From<(Address, Address)> for SwapDirection {
59    fn from(value: (Address, Address)) -> Self {
60        Self { token_from: value.0, token_to: value.1 }
61    }
62}