Skip to content

Commit 159f91f

Browse files
authored
Merge pull request #61 from lfglabs-dev/fix/add_logs
fix: last_renewal check
2 parents 4828a6d + c645a04 commit 159f91f

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

bot/src/bot.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bigdecimal::{
88
BigDecimal,
99
};
1010
use bson::doc;
11+
use chrono::Utc;
1112
use futures::stream::{self, StreamExt};
1213
use mongodb::options::FindOneOptions;
1314
use starknet::accounts::ConnectedAccount;
@@ -36,17 +37,33 @@ lazy_static::lazy_static! {
3637
static ref RENEW_TIME: FieldElement = FieldElement::from_dec_str("365").unwrap();
3738
}
3839

40+
/// Filters out entries that were renewed less than 364 days ago
41+
fn filter_recent_renewals(results: &mut Vec<DomainAggregateResult>) {
42+
let now_timestamp = Utc::now().timestamp();
43+
let min_renewal_age = 364 * 24 * 60 * 60; // 364 days in seconds
44+
45+
results.retain(|result| {
46+
let last_renewal = result.last_renewal.unwrap_or(0);
47+
now_timestamp - last_renewal > min_renewal_age
48+
});
49+
}
50+
3951
pub async fn get_domains_ready_for_renewal(
4052
config: &Config,
4153
state: &Arc<AppState>,
4254
logger: &Logger,
4355
) -> Result<HashMap<FieldElement, AggregateResults>> {
4456
let mut results = get_auto_renewal_data(config, state).await?;
45-
let results_altcoins = get_auto_renewal_altcoins_data(config, state).await?;
57+
let mut results_altcoins = get_auto_renewal_altcoins_data(config, state).await?;
58+
59+
// Filter out entries that were renewed less than 364 days ago
60+
filter_recent_renewals(&mut results);
61+
filter_recent_renewals(&mut results_altcoins);
4662

4763
let mut grouped_results: HashMap<FieldElement, AggregateResults> = HashMap::new();
4864

4965
if results.is_empty() && results_altcoins.is_empty() {
66+
logger.warning("No domains to renew".to_string());
5067
grouped_results.insert(
5168
config.contract.erc20,
5269
AggregateResults {
@@ -69,9 +86,9 @@ pub async fn get_domains_ready_for_renewal(
6986
.iter()
7087
.map(|result| {
7188
let test = config
72-
.renewers_mapping
73-
.get(&result.auto_renew_contract)
74-
.unwrap();
89+
.renewers_mapping
90+
.get(&result.auto_renew_contract)
91+
.unwrap();
7592
(
7693
result.renewer_address.clone(),
7794
// get the erc20 address for the given auto_renew_contract
@@ -274,7 +291,8 @@ async fn process_aggregate_result(
274291
.unwrap();
275292
println!(
276293
"[OK] Domain {}.stark can be renewed by {}",
277-
domain_name, to_hex(renewer_addr)
294+
domain_name,
295+
to_hex(renewer_addr)
278296
);
279297
Some(AggregateResult {
280298
domain: domain_encoded,

bot/src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct DomainAggregateResult {
5858
pub meta_hash: Option<String>,
5959
pub _cursor: Cursor,
6060
pub auto_renew_contract: FieldElement,
61-
pub approval_values: Vec<AutoRenewAllowance>
61+
pub approval_values: Vec<AutoRenewAllowance>,
6262
}
6363

6464
pub struct AggregateResult {

0 commit comments

Comments
 (0)