-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathpayments.rs
More file actions
123 lines (107 loc) · 4.31 KB
/
payments.rs
File metadata and controls
123 lines (107 loc) · 4.31 KB
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
use std::str::FromStr;
use crate::{
config::Config,
db::Db,
types::{AggregationModePaymentService, AggregationModePaymentServiceContract, RpcProvider},
};
use alloy::{
primitives::Address,
providers::{Provider, ProviderBuilder},
};
use sqlx::types::BigDecimal;
pub struct PaymentsPoller {
db: Db,
proof_aggregation_service: AggregationModePaymentServiceContract,
rpc_provider: RpcProvider,
config: Config,
}
impl PaymentsPoller {
pub fn new(db: Db, config: Config) -> Result<Self, Box<dyn std::error::Error>> {
let rpc_url = config.eth_rpc_url.parse().expect("RPC URL should be valid");
let rpc_provider = ProviderBuilder::new().connect_http(rpc_url);
let proof_aggregation_service = AggregationModePaymentService::new(
Address::from_str(&config.payment_service_address)
.expect("AggregationModePaymentService address should be valid"),
rpc_provider.clone(),
);
// This check is here to catch early failures on last block fetching
let _ = config.get_last_block_fetched()?;
Ok(Self {
db,
proof_aggregation_service,
rpc_provider,
config,
})
}
pub async fn start(&self) {
let seconds_to_wait_between_polls = 12;
loop {
let Ok(last_block_fetched) = self.config.get_last_block_fetched() else {
tracing::warn!("Could not get last block fetched, skipping polling iteration...");
tokio::time::sleep(std::time::Duration::from_secs(
seconds_to_wait_between_polls,
))
.await;
continue;
};
let Ok(current_block) = self.rpc_provider.get_block_number().await else {
tracing::warn!("Could not get current block skipping polling iteration...");
tokio::time::sleep(std::time::Duration::from_secs(
seconds_to_wait_between_polls,
))
.await;
continue;
};
let start_block = last_block_fetched.saturating_sub(5);
tracing::info!("Fetching logs from block {start_block} to {current_block}");
let Ok(logs) = self
.proof_aggregation_service
.UserPayment_filter()
.from_block(start_block)
.to_block(current_block)
.query()
.await
else {
tracing::warn!("Could not get payment log events skipping polling iteration...");
tokio::time::sleep(std::time::Duration::from_secs(
seconds_to_wait_between_polls,
))
.await;
continue;
};
tracing::info!("Logs collected {}", logs.len());
for (payment_event, log) in logs {
let address = format!("{:#x}", payment_event.user);
let Some(tx_hash) = log.transaction_hash else {
tracing::warn!("Skipping payment event for {address}: missing tx hash");
continue;
};
let tx_hash = format!("{tx_hash:#x}");
let Ok(amount) = BigDecimal::from_str(&payment_event.amount.to_string()) else {
continue;
};
let Ok(started_at) = BigDecimal::from_str(&payment_event.from.to_string()) else {
continue;
};
let Ok(valid_until) = BigDecimal::from_str(&payment_event.until.to_string()) else {
continue;
};
if let Err(err) = self
.db
.insert_payment_event(&address, &started_at, &amount, &valid_until, &tx_hash)
.await
{
tracing::error!("Failed to insert payment event for {address}: {err}");
}
}
if let Err(err) = self.config.update_last_block_fetched(current_block) {
tracing::error!("Failed to update the last aggregated block: {err}");
continue;
};
tokio::time::sleep(std::time::Duration::from_secs(
seconds_to_wait_between_polls,
))
.await;
}
}
}