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
use std::time::{SystemTime, UNIX_EPOCH};

use bridge_e2e_traits::{
    error::{E2EClientError, E2EClientResult},
    strategy::RelayStrategy,
};
use client_contracts::FeeMarket;
use web3::types::{Address, U256};

#[derive(Debug, Clone)]
pub struct FeeMarketRelayStrategy {
    pub fee_market: FeeMarket,
    account: Address,
}

impl FeeMarketRelayStrategy {
    pub fn new(fee_market: FeeMarket, account: Address) -> Self {
        Self {
            fee_market,
            account,
        }
    }
}

#[async_trait::async_trait]
impl RelayStrategy for FeeMarketRelayStrategy {
    async fn decide(&mut self, encoded_key: U256) -> E2EClientResult<bool> {
        let (order, exts) = self
            .fee_market
            .order(encoded_key)
            .await
            .map_err(|e| E2EClientError::Custom(format!("[feemarket]: {:?}", e)))?;

        let is_assigned_relayer = exts.iter().any(|x| x.assigned_relayer == self.account);
        if is_assigned_relayer {
            tracing::info!(
                target: "feemarket",
                "[feemarket] You are assigned relayer, you must relay this message: {:?}",
                encoded_key
            );
            return Ok(true);
        }

        let relay_time = self
            .fee_market
            .relay_time()
            .await
            .map_err(|e| E2EClientError::Custom(format!("[feemarket]: {:?}", e)))?;
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| E2EClientError::Custom(format!("[feemarket]: {:?}", e)))?
            .as_secs();
        if current_time - order.assigned_time as u64 > relay_time {
            tracing::info!(
                target: "feemarket",
                "[feemarket] You aren't assigned relayer, but this message is timeout. Decide to relay this message"
            );
            return Ok(true);
        }

        tracing::info!(
            target: "feemarket",
            "[feemarket] You aren't assigned relayer, and nonce({:?}) is on-time. so don't relay this",
            encoded_key
        );

        Ok(false)
    }
}