2021-09-29 01:03:47 +00:00
|
|
|
use std::collections::HashMap;
|
2021-04-09 00:22:17 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-09-29 01:03:47 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::config::Config;
|
|
|
|
use crate::database::Pool;
|
2022-01-30 16:18:05 +00:00
|
|
|
use crate::ethereum::contracts::get_contracts;
|
|
|
|
use crate::ethereum::nft::process_nft_events;
|
2022-01-30 20:58:05 +00:00
|
|
|
use crate::ethereum::subscriptions::check_subscriptions;
|
2021-04-09 00:22:17 +00:00
|
|
|
|
|
|
|
pub fn run(config: Config, db_pool: Pool) -> () {
|
2022-04-12 12:39:32 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(30));
|
2022-01-30 16:18:05 +00:00
|
|
|
let maybe_contract_set = if let Some(blockchain_config) = &config.blockchain {
|
|
|
|
// Create blockchain interface
|
|
|
|
get_contracts(blockchain_config).await
|
2021-11-06 12:48:52 +00:00
|
|
|
.map_err(|err| log::error!("{}", err))
|
|
|
|
.ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
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-01-30 16:18:05 +00:00
|
|
|
if let Some(contract_set) = maybe_contract_set.as_ref() {
|
2021-11-06 12:48:52 +00:00
|
|
|
// Monitor events only if ethereum integration is enabled
|
2022-01-30 16:18:05 +00:00
|
|
|
process_nft_events(
|
|
|
|
&contract_set.web3,
|
|
|
|
&contract_set.collectible,
|
2021-09-29 01:03:47 +00:00
|
|
|
&db_pool,
|
|
|
|
&mut token_waitlist_map,
|
|
|
|
).await.unwrap_or_else(|err| {
|
2021-04-09 00:22:17 +00:00
|
|
|
log::error!("{}", err);
|
|
|
|
});
|
2022-01-30 20:58:05 +00:00
|
|
|
check_subscriptions(
|
|
|
|
&contract_set.web3,
|
|
|
|
&contract_set.subscription,
|
|
|
|
&db_pool,
|
|
|
|
).await.unwrap_or_else(|err| {
|
|
|
|
log::error!("{}", err);
|
|
|
|
});
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|