2021-09-29 01:03:47 +00:00
|
|
|
use std::collections::HashMap;
|
2021-04-09 00:22:17 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2022-06-21 16:17:30 +00:00
|
|
|
use anyhow::Error;
|
2021-09-29 01:03:47 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-07-14 13:34:50 +00:00
|
|
|
use crate::config::{Config, Instance};
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::database::Pool;
|
2022-06-24 23:27:09 +00:00
|
|
|
use crate::ethereum::contracts::Blockchain;
|
2022-01-30 16:18:05 +00:00
|
|
|
use crate::ethereum::nft::process_nft_events;
|
2022-08-28 12:01:26 +00:00
|
|
|
use crate::ethereum::subscriptions::{
|
|
|
|
check_ethereum_subscriptions,
|
|
|
|
update_expired_subscriptions,
|
|
|
|
};
|
2022-08-19 19:21:42 +00:00
|
|
|
use crate::monero::subscriptions::check_monero_subscriptions;
|
2021-04-09 00:22:17 +00:00
|
|
|
|
2022-06-21 16:17:30 +00:00
|
|
|
#[derive(Debug, Eq, Hash, PartialEq)]
|
2022-06-20 20:28:46 +00:00
|
|
|
enum Task {
|
|
|
|
NftMonitor,
|
2022-08-28 12:01:26 +00:00
|
|
|
EthereumSubscriptionMonitor,
|
|
|
|
SubscriptionExpirationMonitor,
|
2022-08-19 19:21:42 +00:00
|
|
|
MoneroPaymentMonitor,
|
2022-06-20 20:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Task {
|
|
|
|
/// Returns task period (in seconds)
|
|
|
|
fn period(&self) -> i64 {
|
|
|
|
match self {
|
|
|
|
Self::NftMonitor => 30,
|
2022-08-28 12:01:26 +00:00
|
|
|
Self::EthereumSubscriptionMonitor => 300,
|
|
|
|
Self::SubscriptionExpirationMonitor => 300,
|
2022-08-19 19:21:42 +00:00
|
|
|
Self::MoneroPaymentMonitor => 30,
|
2022-06-20 20:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_task_ready(last_run: &Option<DateTime<Utc>>, period: i64) -> bool {
|
|
|
|
match last_run {
|
|
|
|
Some(last_run) => {
|
|
|
|
let time_passed = Utc::now() - *last_run;
|
|
|
|
time_passed.num_seconds() >= period
|
|
|
|
},
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:30:36 +00:00
|
|
|
async fn nft_monitor_task(
|
|
|
|
maybe_blockchain: Option<&mut Blockchain>,
|
|
|
|
db_pool: &Pool,
|
|
|
|
token_waitlist_map: &mut HashMap<Uuid, DateTime<Utc>>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let blockchain = match maybe_blockchain {
|
|
|
|
Some(blockchain) => blockchain,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
2022-06-28 23:02:31 +00:00
|
|
|
let collectible = match &blockchain.contract_set.collectible {
|
|
|
|
Some(contract) => contract,
|
|
|
|
None => return Ok(()), // feature not enabled
|
|
|
|
};
|
2022-06-29 17:30:36 +00:00
|
|
|
process_nft_events(
|
|
|
|
&blockchain.contract_set.web3,
|
2022-07-08 19:54:12 +00:00
|
|
|
collectible,
|
2022-06-29 17:30:36 +00:00
|
|
|
&mut blockchain.sync_state,
|
|
|
|
db_pool,
|
|
|
|
token_waitlist_map,
|
|
|
|
).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-28 12:01:26 +00:00
|
|
|
async fn ethereum_subscription_monitor_task(
|
2022-07-14 13:34:50 +00:00
|
|
|
instance: &Instance,
|
2022-06-29 17:30:36 +00:00
|
|
|
maybe_blockchain: Option<&mut Blockchain>,
|
|
|
|
db_pool: &Pool,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let blockchain = match maybe_blockchain {
|
|
|
|
Some(blockchain) => blockchain,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
2022-06-28 23:02:31 +00:00
|
|
|
let subscription = match &blockchain.contract_set.subscription {
|
|
|
|
Some(contract) => contract,
|
|
|
|
None => return Ok(()), // feature not enabled
|
|
|
|
};
|
2022-08-28 12:01:26 +00:00
|
|
|
check_ethereum_subscriptions(
|
2022-08-30 20:33:45 +00:00
|
|
|
&blockchain.config,
|
2022-07-14 13:34:50 +00:00
|
|
|
instance,
|
2022-06-29 17:30:36 +00:00
|
|
|
&blockchain.contract_set.web3,
|
2022-07-08 19:54:12 +00:00
|
|
|
subscription,
|
2022-06-29 17:30:36 +00:00
|
|
|
&mut blockchain.sync_state,
|
|
|
|
db_pool,
|
|
|
|
).await.map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
2022-08-19 19:21:42 +00:00
|
|
|
async fn monero_payment_monitor_task(
|
|
|
|
config: &Config,
|
|
|
|
db_pool: &Pool,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let maybe_monero_config = config.blockchain()
|
|
|
|
.and_then(|conf| conf.monero_config());
|
|
|
|
let monero_config = match maybe_monero_config {
|
|
|
|
Some(monero_config) => monero_config,
|
|
|
|
None => return Ok(()), // not configured
|
|
|
|
};
|
2022-08-30 23:56:10 +00:00
|
|
|
check_monero_subscriptions(
|
|
|
|
&config.instance(),
|
|
|
|
monero_config,
|
|
|
|
db_pool,
|
|
|
|
).await?;
|
2022-08-19 19:21:42 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:29:14 +00:00
|
|
|
pub fn run(
|
2022-07-14 13:34:50 +00:00
|
|
|
config: Config,
|
2022-06-24 23:27:09 +00:00
|
|
|
mut maybe_blockchain: Option<Blockchain>,
|
2022-06-14 10:29:14 +00:00
|
|
|
db_pool: Pool,
|
|
|
|
) -> () {
|
2022-04-12 12:39:32 +00:00
|
|
|
tokio::spawn(async move {
|
2022-06-20 20:28:46 +00:00
|
|
|
let mut scheduler_state = HashMap::new();
|
|
|
|
scheduler_state.insert(Task::NftMonitor, None);
|
2022-08-28 12:01:26 +00:00
|
|
|
scheduler_state.insert(Task::EthereumSubscriptionMonitor, None);
|
|
|
|
scheduler_state.insert(Task::SubscriptionExpirationMonitor, None);
|
2022-08-19 19:21:42 +00:00
|
|
|
scheduler_state.insert(Task::MoneroPaymentMonitor, None);
|
2022-06-20 20:28:46 +00:00
|
|
|
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(5));
|
2021-09-29 01:03:47 +00:00
|
|
|
let mut token_waitlist_map: HashMap<Uuid, DateTime<Utc>> = HashMap::new();
|
2021-04-09 00:22:17 +00:00
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
2021-11-06 12:48:52 +00:00
|
|
|
|
2022-06-20 20:28:46 +00:00
|
|
|
for (task, last_run) in scheduler_state.iter_mut() {
|
|
|
|
if !is_task_ready(last_run, task.period()) {
|
|
|
|
continue;
|
|
|
|
};
|
2022-06-21 16:17:30 +00:00
|
|
|
let task_result = match task {
|
2022-06-20 20:28:46 +00:00
|
|
|
Task::NftMonitor => {
|
2022-06-29 17:30:36 +00:00
|
|
|
nft_monitor_task(
|
|
|
|
maybe_blockchain.as_mut(),
|
|
|
|
&db_pool,
|
|
|
|
&mut token_waitlist_map,
|
|
|
|
).await
|
2022-06-20 20:28:46 +00:00
|
|
|
},
|
2022-08-28 12:01:26 +00:00
|
|
|
Task::EthereumSubscriptionMonitor => {
|
|
|
|
ethereum_subscription_monitor_task(
|
2022-07-14 13:34:50 +00:00
|
|
|
&config.instance(),
|
2022-06-29 17:30:36 +00:00
|
|
|
maybe_blockchain.as_mut(),
|
|
|
|
&db_pool,
|
|
|
|
).await
|
2022-06-20 20:28:46 +00:00
|
|
|
},
|
2022-08-28 12:01:26 +00:00
|
|
|
Task::SubscriptionExpirationMonitor => {
|
|
|
|
update_expired_subscriptions(
|
|
|
|
&config.instance(),
|
|
|
|
&db_pool,
|
|
|
|
).await.map_err(Error::from)
|
|
|
|
},
|
2022-08-19 19:21:42 +00:00
|
|
|
Task::MoneroPaymentMonitor => {
|
|
|
|
monero_payment_monitor_task(&config, &db_pool).await
|
|
|
|
},
|
2022-06-20 20:28:46 +00:00
|
|
|
};
|
2022-06-21 16:17:30 +00:00
|
|
|
task_result.unwrap_or_else(|err| {
|
|
|
|
log::error!("{:?}: {}", task, err);
|
|
|
|
});
|
2022-06-20 20:28:46 +00:00
|
|
|
*last_run = Some(Utc::now());
|
|
|
|
};
|
2022-08-28 12:01:26 +00:00
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
});
|
|
|
|
}
|