Skip to main content

degenbot_cli_core/
aave.rs

1//! The `aave` command arms (ADR-051 D1;).
2//!
3//! Ports `cli/aave.py`:
4//!
5//! - `aave activate` — [`activate_aave_market`] (the one-time market seed).
6//! - `aave deactivate` — the `(chain, name)` market read + the
7//!   [`deactivate_aave_market`] row flip.
8//! - `aave update` — the active-market walk over
9//!   [`run_aave_update`], with the post-run zero-balance cleanup and the
10//!   opt-in completion backup.
11//! - `aave position show` — the market/user scalar row reads ported onto the
12//!   `degenbot-db::aave` read surface.
13//! - `aave reset` — the market-scoped purge and the cold-boot substrate
14//!   re-seed committed in ONE transaction through the market-reset module,
15//!   so the NEXT update run takes the cold-boot path an empty database
16//!   takes.
17//! - `aave digest` — one market's completed-market record: per-table row
18//!   counts + digests over the market tables, rendered as the manifest
19//!   JSON a drive pins.
20
21use std::sync::Arc;
22
23use alloy::primitives::{address, Address};
24use degenbot_aave::updater::verify::cleanup_zero_balance_positions_on_conn;
25use degenbot_aave::{
26    activate_aave_market, deactivate_aave_market, reset_market_to_cold_boot, run_aave_update,
27    NoProgress, RunError, RunSpec, VerifyPolicy,
28};
29use degenbot_db::{
30    aave_market_digest_for, ops, parse_completed_market_manifest, CompletedMarketDrive,
31    CompletedMarketManifest, DbError, DegenbotDb,
32};
33use degenbot_rpc::provider::AlloyProvider;
34
35use crate::block::{parse_to_block, resolve_to_block};
36use crate::cancel::CancelHandle;
37use crate::context::CliContext;
38use crate::error::CliError;
39use crate::prompt::{PromptPlan, Prompter};
40use crate::report::{
41    AavePositionLine, AaveReinit, AaveReport, AaveUpdateEntry, AaveUpdateOutcome, DeactivateOutcome,
42};
43use degenbot_db::AaveMarketPurgeCount;
44
45/// The RPC retry budget the update arm's per-run transport build uses
46/// (mirrors `crate::pool`'s budget).
47const RPC_MAX_RETRIES: u32 = 5;
48
49/// An Aave V3 deployment (the Python `aave/deployments.py` constants).
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct AaveDeployment {
52    /// The chain id.
53    pub chain_id: u64,
54    /// The human chain label.
55    pub chain_label: &'static str,
56    /// The on-chain `getMarketId()` return — the name the auto-registration
57    /// seam keys the bare inactive row on (and `aave activate` resolves via
58    /// RPC; the two must match for the completion path to reuse the row).
59    pub market_name: &'static str,
60    /// The `PoolAddressProvider` contract.
61    pub pool_address_provider: Address,
62    /// The chain's GHO token.
63    pub gho_token_address: Address,
64}
65
66/// The shipped Aave V3 deployments (Ethereum mainnet only, mirroring Python).
67pub const AAVE_DEPLOYMENTS: &[AaveDeployment] = &[AaveDeployment {
68    chain_id: 1,
69    chain_label: "Ethereum",
70    // The on-chain `getMarketId()` return — the static name the auto-
71    // registration seam keys the bare inactive row on (the RPC-fetched name
72    // `aave activate` resolves must match for the completion path to reuse
73    // the row instead of creating a second one).
74    market_name: "Aave Ethereum Market",
75    pool_address_provider: address!("2f39d218133AFaB8F2B819B1066c7E434Ad94E9e"),
76    gho_token_address: address!("40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f"),
77}];
78
79/// The `aave` command group.
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub enum AaveCommand {
82    /// Activate (or re-activate) the chain's Aave V3 market.
83    Activate {
84        /// The chain id.
85        chain_id: u64,
86    },
87    /// Deactivate a market by chain + name.
88    Deactivate {
89        /// The chain id.
90        chain_id: u64,
91        /// The `aave_v3_markets.name` to flip.
92        market_name: String,
93    },
94    /// Emit one market's completed-market record: per-table row counts +
95    /// digests over the market tables, rendered as the manifest JSON a
96    /// drive pins.
97    Digest {
98        /// The chain id.
99        chain_id: u64,
100        /// The `aave_v3_markets.name` to digest; `None` resolves the
101        /// chain's only registered market.
102        market_name: Option<String>,
103        /// A previous completed-market record whose `market.drive` block is
104        /// carried into the output verbatim (the drive facts are the
105        /// drive's own bookkeeping, not DB state).
106        drive_manifest: Option<std::path::PathBuf>,
107    },
108    /// Update positions for every active market.
109    Update {
110        /// Max blocks per chunk before committing.
111        chunk_size: u64,
112        /// The widest block range a single `eth_getLogs` request may
113        /// span (independent of `chunk_size`).
114        max_block_range: u64,
115        /// The raw `--to-block` identifier.
116        to_block: String,
117        /// The per-chunk touched-position verify.
118        verify_chunk: bool,
119        /// The market-wide interval + completion verify.
120        verify_all: bool,
121        /// The interval for the market-wide gate.
122        verify_all_interval: u64,
123        /// Stop after the first committed chunk.
124        stop_after_one_chunk: bool,
125        /// Preview only (skips the Rust call entirely).
126        dry_run: bool,
127        /// Back up the DB once per market at the end of the run.
128        enable_backup: bool,
129    },
130    /// Purge one market's populated state, then take the cold-boot path a
131    /// fresh empty database takes for it.
132    Reset {
133        /// The chain id.
134        chain_id: u64,
135        /// The `aave_v3_markets.name` to reset; `None` resolves the chain's
136        /// only registered market.
137        market_name: Option<String>,
138        /// Preview the purge's per-relation counts and re-init without writing.
139        dry_run: bool,
140    },
141    /// Display a user's collateral + debt positions.
142    PositionShow {
143        /// The user address.
144        address: String,
145        /// The market name.
146        market: String,
147        /// The chain id.
148        chain_id: u64,
149    },
150}
151
152impl AaveCommand {
153    /// The arm's confirmation policy: no Python aave handler prompts.
154    #[must_use]
155    pub const fn prompt_plan(&self, _ctx: &CliContext<'_>) -> PromptPlan {
156        PromptPlan::None
157    }
158}
159
160/// Resolve the Aave V3 deployment for `chain_id`.
161///
162/// # Errors
163///
164/// [`CliError::UnknownDeployment`] when the chain has no shipped deployment.
165pub fn resolve_aave_deployment(chain_id: u64) -> Result<&'static AaveDeployment, CliError> {
166    AAVE_DEPLOYMENTS
167        .iter()
168        .find(|d| d.chain_id == chain_id)
169        .ok_or_else(|| CliError::UnknownDeployment {
170            chain_id,
171            name: "ethereum_aave_v3".to_string(),
172        })
173}
174
175/// Execute an `aave` command.
176///
177/// # Errors
178///
179/// [`CliError::UnknownDeployment`] / [`CliError::InvalidAddress`] /
180/// [`CliError::InvalidArgument`] for bad inputs, [`CliError::Config`] for an
181/// unresolved driver-domain value, [`CliError::NoActiveAaveMarkets`] when
182/// `aave update` finds nothing, and [`CliError::AaveUpdate`] for a core
183/// failure.
184pub(crate) fn execute(
185    command: &AaveCommand,
186    ctx: &CliContext<'_>,
187    _prompter: &dyn Prompter,
188    cancel: &CancelHandle,
189) -> Result<AaveReport, CliError> {
190    match command {
191        AaveCommand::Activate { chain_id } => activate(ctx, *chain_id),
192        AaveCommand::Deactivate {
193            chain_id,
194            market_name,
195        } => deactivate(ctx, *chain_id, market_name),
196        AaveCommand::Digest {
197            chain_id,
198            market_name,
199            drive_manifest,
200        } => digest(
201            ctx,
202            *chain_id,
203            market_name.as_deref(),
204            drive_manifest.as_deref(),
205        ),
206        AaveCommand::Update {
207            chunk_size,
208            max_block_range,
209            to_block,
210            verify_chunk,
211            verify_all,
212            verify_all_interval,
213            stop_after_one_chunk,
214            dry_run,
215            enable_backup,
216        } => update(
217            ctx,
218            cancel,
219            &UpdateArgs {
220                chunk_size: *chunk_size,
221                max_block_range: *max_block_range,
222                to_block,
223                verify_chunk: *verify_chunk,
224                verify_all: *verify_all,
225                verify_all_interval: *verify_all_interval,
226                stop_after_one_chunk: *stop_after_one_chunk,
227                dry_run: *dry_run,
228                enable_backup: *enable_backup,
229            },
230        ),
231        AaveCommand::Reset {
232            chain_id,
233            market_name,
234            dry_run,
235        } => reset(ctx, cancel, *chain_id, market_name.as_deref(), *dry_run),
236        AaveCommand::PositionShow {
237            address,
238            market,
239            chain_id,
240        } => position_show(ctx, address, market, *chain_id),
241    }
242}
243
244/// `aave activate`.
245fn activate(ctx: &CliContext<'_>, chain_id: u64) -> Result<AaveReport, CliError> {
246    let deployment = resolve_aave_deployment(chain_id)?;
247    let database_path = ctx.database_path()?.value;
248    let rpc_url = ctx.node_request_uri_for(chain_id)?.value;
249    let chain = i64::try_from(chain_id)
250        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
251    // The thin live-provider wrapper (ADR-068 D5): one build per activation on
252    // the shared runtime, and the core only injects it. Mirrors the update
253    // arm's per-market build below.
254    let result = match crate::pool::shared_runtime_block_on(async {
255        AlloyProvider::new(&rpc_url, RPC_MAX_RETRIES).await
256    }) {
257        Ok(Ok(provider)) => activate_aave_market(
258            &database_path,
259            chain,
260            &deployment.pool_address_provider.to_checksum(None),
261            &deployment.gho_token_address.to_checksum(None),
262            &provider,
263        ),
264        Ok(Err(err)) => Err(RunError::from(err)),
265        Err(cli_err) => return Err(cli_err),
266    }
267    .map_err(CliError::AaveUpdate)?;
268    tracing::info!(
269        chain_id,
270        market_id = result.market_id,
271        "activated Aave V3 market"
272    );
273    Ok(AaveReport::Activated {
274        chain_id,
275        chain_label: deployment.chain_label,
276        market_id: result.market_id,
277        market_name: result.market_name,
278        created: result.created,
279    })
280}
281
282/// `aave deactivate`.
283fn deactivate(
284    ctx: &CliContext<'_>,
285    chain_id: u64,
286    market_name: &str,
287) -> Result<AaveReport, CliError> {
288    let database_path = ctx.database_path()?.value;
289    let chain = i64::try_from(chain_id)
290        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
291    let market = {
292        let db = (DegenbotDb::open(&database_path)?).0;
293        db.fetch_aave_market_by_name(chain, market_name)?
294    };
295    let Some(market) = market else {
296        return Ok(AaveReport::Deactivated {
297            chain_id,
298            market_id: None,
299            outcome: DeactivateOutcome::NoEntry,
300        });
301    };
302    if !market.active {
303        return Ok(AaveReport::Deactivated {
304            chain_id,
305            market_id: Some(market.id),
306            outcome: DeactivateOutcome::AlreadyDeactivated,
307        });
308    }
309    deactivate_aave_market(&database_path, market.id).map_err(CliError::AaveUpdate)?;
310    tracing::info!(
311        chain_id,
312        market_id = market.id,
313        "deactivated Aave V3 market"
314    );
315    Ok(AaveReport::Deactivated {
316        chain_id,
317        market_id: Some(market.id),
318        outcome: DeactivateOutcome::Deactivated,
319    })
320}
321
322/// `aave digest`.
323fn digest(
324    ctx: &CliContext<'_>,
325    chain_id: u64,
326    market_name: Option<&str>,
327    drive_manifest: Option<&std::path::Path>,
328) -> Result<AaveReport, CliError> {
329    let database_path = ctx.database_path()?.value;
330    let chain = i64::try_from(chain_id)
331        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
332    let drive = drive_manifest.map(carried_drive).transpose()?;
333    let digest = {
334        // The read handle locks down at open (`query_only=on`): the digest
335        // arm observes the DB, it never writes it.
336        let db = (DegenbotDb::open(&database_path)?).0;
337        let conn = db.lock();
338        aave_market_digest_for(&conn, Some(chain), market_name)?
339    };
340    Ok(AaveReport::Digest {
341        manifest: CompletedMarketManifest { digest, drive },
342    })
343}
344
345/// Parse a previous completed-market record with the typed reader and carry
346/// its `market.drive` block — the one content the record has that the DB
347/// cannot yield.
348///
349/// The record must be one this writer emitted (the serialization stamp is
350/// the version discriminant) and must carry a drive block; a record without
351/// one has nothing to carry.
352fn carried_drive(path: &std::path::Path) -> Result<CompletedMarketDrive, CliError> {
353    let raw = std::fs::read_to_string(path).map_err(CliError::Io)?;
354    let manifest = parse_completed_market_manifest(&raw)
355        .map_err(|error| CliError::InvalidArgument(format!("{}: {error}", path.display())))?;
356    manifest.drive.ok_or_else(|| {
357        CliError::InvalidArgument(format!("{} carries no market.drive block", path.display()))
358    })
359}
360
361/// The `aave update` flag bundle.
362#[expect(clippy::struct_excessive_bools)]
363struct UpdateArgs<'a> {
364    chunk_size: u64,
365    max_block_range: u64,
366    to_block: &'a str,
367    verify_chunk: bool,
368    verify_all: bool,
369    verify_all_interval: u64,
370    stop_after_one_chunk: bool,
371    dry_run: bool,
372    enable_backup: bool,
373}
374
375/// The `aave update` flags' projection onto the run's [`VerifyPolicy`]: the
376/// market-wide interval gate is live only when `--verify-all` is set.
377impl From<&UpdateArgs<'_>> for VerifyPolicy {
378    fn from(args: &UpdateArgs<'_>) -> Self {
379        Self {
380            verify_chunk: args.verify_chunk,
381            verify_all_interval: args.verify_all.then_some(args.verify_all_interval),
382            verify_all_at_completion: args.verify_all,
383        }
384    }
385}
386
387/// `aave update`.
388#[expect(clippy::too_many_lines)]
389fn update(
390    ctx: &CliContext<'_>,
391    cancel: &CancelHandle,
392    args: &UpdateArgs<'_>,
393) -> Result<AaveReport, CliError> {
394    let database_path = ctx.database_path()?.value;
395    // Self-serve registration: every supported Aave market not found in the
396    // DB registers inactive (a bare row awaiting `aave activate`), so the
397    // update never depends on prior CREATEs.
398    crate::registrations::ensure_supported_registrations(&database_path)?;
399    let markets = {
400        let db = (DegenbotDb::open(&database_path)?).0;
401        db.fetch_active_aave_markets()?
402    };
403    if markets.is_empty() {
404        return Err(CliError::NoActiveAaveMarkets);
405    }
406    let spec = parse_to_block(args.to_block)?;
407    let max_chunks = if args.stop_after_one_chunk {
408        Some(1)
409    } else {
410        None
411    };
412    let mut entries: Vec<AaveUpdateEntry> = Vec::new();
413    let mut chain_ids: Vec<i64> = Vec::new();
414    for market in &markets {
415        if !chain_ids.contains(&market.chain_id) {
416            chain_ids.push(market.chain_id);
417        }
418    }
419    for chain in chain_ids {
420        if cancel.is_cancelled() {
421            break;
422        }
423        let chain_unsigned = u64::try_from(chain).map_err(|_| {
424            CliError::InvalidArgument(format!("market chain id {chain} is out of range"))
425        })?;
426        let rpc_url = ctx.node_request_uri_for(chain_unsigned)?.value;
427        let resolved = resolve_to_block(spec, &rpc_url)?;
428        for market in markets.iter().filter(|m| m.chain_id == chain) {
429            if cancel.is_cancelled() {
430                return Ok(AaveReport::Updated { entries });
431            }
432            let Some(last_update_block) = market.last_update_block else {
433                entries.push(AaveUpdateEntry {
434                    chain_id: chain,
435                    market_id: market.id,
436                    market_name: market.name.clone(),
437                    outcome: AaveUpdateOutcome::NeedsBootstrap,
438                });
439                continue;
440            };
441            if args.dry_run {
442                entries.push(AaveUpdateEntry {
443                    chain_id: chain,
444                    market_id: market.id,
445                    market_name: market.name.clone(),
446                    outcome: AaveUpdateOutcome::DryRun {
447                        last_update_block,
448                        to_block: resolved,
449                    },
450                });
451                continue;
452            }
453            // The thin live-provider wrapper (ADR-068 D5): one build per
454            // market run on the shared runtime — the same transport count the
455            // core's old internal `AlloyProvider::new` site produced — and
456            // the core only injects it. A build failure surfaces as the same
457            // `CliError::AaveUpdate` the run's own failures use.
458            let run = match crate::pool::shared_runtime_block_on(async {
459                AlloyProvider::new(&rpc_url, RPC_MAX_RETRIES).await
460            }) {
461                Ok(Ok(provider)) => run_aave_update(
462                    &database_path,
463                    RunSpec {
464                        chain_id: chain,
465                        market_id: market.id,
466                        to_block: resolved,
467                        chunk_size: args.chunk_size,
468                        max_block_range: args.max_block_range,
469                        provider,
470                        cancel: cancel.flag(),
471                        progress: Arc::new(NoProgress),
472                        verify: VerifyPolicy::from(args),
473                        max_chunks,
474                    },
475                ),
476                Ok(Err(err)) => Err(RunError::from(err)),
477                Err(cli_err) => return Err(cli_err),
478            };
479            match run {
480                Ok(report) => {
481                    entries.push(AaveUpdateEntry {
482                        chain_id: chain,
483                        market_id: market.id,
484                        market_name: market.name.clone(),
485                        outcome: AaveUpdateOutcome::Advanced {
486                            from_block: report.from_block,
487                            to_block: report.to_block,
488                            chunks_committed: report.chunks_committed,
489                            total_events_applied: report.total_events_applied,
490                        },
491                    });
492                    cleanup_zero_balance_positions(&database_path, market.id)?;
493                    if args.enable_backup {
494                        let backup = backup_for_block(&database_path, report.to_block)?;
495                        tracing::info!(
496                            chain_id = chain,
497                            to_block = report.to_block,
498                            backup = %backup.display(),
499                            "created Aave database backup at block"
500                        );
501                    }
502                }
503                Err(RunError::Cancelled) => {
504                    entries.push(AaveUpdateEntry {
505                        chain_id: chain,
506                        market_id: market.id,
507                        market_name: market.name.clone(),
508                        outcome: AaveUpdateOutcome::Cancelled,
509                    });
510                    return Ok(AaveReport::Updated { entries });
511                }
512                Err(err) => return Err(CliError::AaveUpdate(err)),
513            }
514        }
515    }
516    Ok(AaveReport::Updated { entries })
517}
518
519/// An exclusive, reset-scoped advisory lock over one database path.
520///
521/// The exclusion is an advisory `flock` (via [`std::fs::File::try_lock`]) on a
522/// file beside the database, which also carries the holder's pid for
523/// diagnostics. The kernel releases the lock when the holding process dies, so
524/// a killed or crashed reset cannot wedge every later reset the way the
525/// previous create-new pid file did: the surviving file carries no lock, and
526/// the next reset takes it over. A live holder still blocks, and the refusal
527/// still reports the path. It guards reset-against-reset on a shared database,
528/// rather than racing a purge against an in-flight re-init. The updater arms
529/// do not take this lock, so it does not by itself exclude a concurrent
530/// `aave update`; the purge's own transaction is what keeps a racing writer
531/// from observing a partially-purged market.
532struct ResetLock {
533    path: std::path::PathBuf,
534    /// Holding the descriptor is what holds the `flock`: dropping it (after
535    /// [`Drop for ResetLock`] unlinks the name) releases the lock. Never read;
536    /// the lifetime IS the lock.
537    _file: std::fs::File,
538}
539
540impl ResetLock {
541    /// Take the lock, or report the holder's file path.
542    fn acquire(database_path: &std::path::Path, market_id: i64) -> Result<Self, CliError> {
543        use std::io::Write as _;
544
545        let mut path = database_path.to_path_buf();
546        let stem = database_path
547            .file_name()
548            .map(|s| s.to_string_lossy().into_owned())
549            .unwrap_or_default();
550        path.set_file_name(format!("{stem}.aave-reset.lock"));
551        // Open-or-create, never `create_new`: a file surviving its holder is
552        // the expected crash case, and the flock — not the file's existence —
553        // is the exclusion.
554        let file = std::fs::OpenOptions::new()
555            .read(true)
556            .write(true)
557            .create(true)
558            .truncate(false)
559            .open(&path)
560            .map_err(CliError::Io)?;
561        file.try_lock().map_err(|err| match err {
562            std::fs::TryLockError::WouldBlock => CliError::AaveMarketMidUpdate {
563                market_id,
564                lock_path: path.display().to_string(),
565            },
566            std::fs::TryLockError::Error(err) => CliError::Io(err),
567        })?;
568        let mut file = file;
569        file.set_len(0).map_err(CliError::Io)?;
570        writeln!(file, "{}", std::process::id()).map_err(CliError::Io)?;
571        Ok(Self { path, _file: file })
572    }
573}
574
575impl Drop for ResetLock {
576    fn drop(&mut self) {
577        // Unlink while the flock is still held (the descriptor drops after
578        // this body), so a waiter that opens the path cannot inherit a lock
579        // on a file whose name is already gone.
580        let _ = std::fs::remove_file(&self.path);
581    }
582}
583
584/// `aave reset`.
585///
586/// Resolves the market the way `deactivate` does, reads and prints the purge's
587/// per-relation counts as the plan, then — under the reset lock — purges the
588/// market and re-seeds its cold-boot substrate in ONE transaction through
589/// [`reset_market_to_cold_boot`]: the purge and the activate seam's completion
590/// commit together, so nothing can land between them. The report renders the
591/// facts that module read back from that transaction. No update run here: the
592/// command returns in seconds, and the next `aave update` takes the cold-boot
593/// path from the stamped cursor.
594fn reset(
595    ctx: &CliContext<'_>,
596    _cancel: &CancelHandle,
597    chain_id: u64,
598    market_name: Option<&str>,
599    dry_run: bool,
600) -> Result<AaveReport, CliError> {
601    let database_path = ctx.database_path()?.value;
602    let chain = i64::try_from(chain_id)
603        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
604    // The re-init completes the purged row through the shipped deployment's
605    // substrate (the address provider + GHO constants), so the chain must be
606    // one the deployment table names — the same precondition `aave
607    // activate` has.
608    let deployment = resolve_aave_deployment(chain_id)?;
609    let market = {
610        let db = (DegenbotDb::open(&database_path)?).0;
611        // No name given: the chain's shipped deployment names the market.
612        let name = market_name.unwrap_or(deployment.market_name);
613        db.fetch_aave_market_by_name(chain, name)?
614    };
615    let Some(market) = market else {
616        let requested =
617            market_name.map_or_else(|| deployment.market_name.to_string(), ToString::to_string);
618        return Err(CliError::UnknownAaveMarket {
619            chain_id,
620            market_name: requested,
621        });
622    };
623
624    // The plan is read first and reported, so the operator sees the blast radius
625    // before the purge. The counts come off a read-only handle.
626    let counts: Vec<AaveMarketPurgeCount> = {
627        let db = (DegenbotDb::open(&database_path)?).0;
628        db.count_aave_market_rows(market.id)?
629    };
630    for count in &counts {
631        tracing::info!(
632            chain_id,
633            market_id = market.id,
634            table = count.table,
635            rows = count.rows,
636            dry_run,
637            "aave reset plan"
638        );
639    }
640    if dry_run {
641        return Ok(AaveReport::Reset {
642            chain_id,
643            market_id: market.id,
644            market_name: market.name,
645            counts,
646            dry_run: true,
647            reinit: None,
648        });
649    }
650
651    // Taken BEFORE the module's transaction and held through it, so two
652    // resets of this database cannot interleave a purge with a re-seed.
653    let _lock = ResetLock::acquire(&database_path, market.id)?;
654
655    // The module's activation RPC needs a live transport; the shipped
656    // deployment constants supply the two addresses it reads (the on-chain
657    // market name + the GHO token's metadata). The build is the same thin
658    // provider wrapper the activate and update arms use.
659    let rpc_url = ctx.node_request_uri_for(chain_id)?.value;
660    let provider = match crate::pool::shared_runtime_block_on(async {
661        AlloyProvider::new(&rpc_url, RPC_MAX_RETRIES).await
662    }) {
663        Ok(Ok(provider)) => provider,
664        Ok(Err(err)) => return Err(CliError::AaveUpdate(RunError::from(err))),
665        Err(cli_err) => return Err(cli_err),
666    };
667
668    // ONE open + ONE transaction: the module purges the market and re-seeds
669    // its cold-boot substrate on this same transaction — the rewind block is
670    // the shipped deployment's bootstrap block, where `aave activate` stamps a
671    // fresh market — and returns the facts it read back from it. A single
672    // commit is what removes the old kill-between-transactions window; a
673    // failure drops the transaction and the database stays byte-identical. The
674    // counts are read on this transaction ahead of the write, so they are the
675    // rows the purge removes.
676    let (removed, facts) = {
677        let (db, _state) = DegenbotDb::open_for_writes(&database_path)?;
678        let mut guard = db.lock();
679        let tx = guard.transaction().map_err(DbError::from)?;
680        let removed = DegenbotDb::count_aave_market_rows_on_conn(&tx, market.id)?;
681        let facts = reset_market_to_cold_boot(
682            &tx,
683            market.id,
684            &deployment.pool_address_provider.to_checksum(None),
685            &deployment.gho_token_address.to_checksum(None),
686            &provider,
687        )
688        .map_err(CliError::AaveUpdate)?;
689        tx.commit().map_err(DbError::from)?;
690        (removed, facts)
691    };
692    tracing::info!(
693        chain_id,
694        market_id = market.id,
695        cursor = facts.last_update_block.unwrap_or_default(),
696        "reset Aave V3 market and re-stamped the cold-boot substrate"
697    );
698    Ok(AaveReport::Reset {
699        chain_id,
700        market_id: market.id,
701        market_name: market.name,
702        counts: removed,
703        dry_run: false,
704        reinit: Some(AaveReinit {
705            market_activated: facts.market_activated,
706            contract_row_present: facts.contract_row_present,
707            last_update_block: facts
708                .last_update_block
709                .map(|block| i64::try_from(block).unwrap_or(i64::MAX)),
710        }),
711    })
712}
713
714/// Delete the market's zero-balance collateral + debt rows under one transaction.
715fn cleanup_zero_balance_positions(
716    database_path: &std::path::Path,
717    market_id: i64,
718) -> Result<(), CliError> {
719    let (db, _state) = DegenbotDb::open_for_writes(database_path)?;
720    let mut guard = db.lock();
721    let tx = guard.transaction().map_err(DbError::from)?;
722    cleanup_zero_balance_positions_on_conn(&tx, market_id)?;
723    tx.commit().map_err(DbError::from)?;
724    Ok(())
725}
726
727/// The `<stem>-<to_block>.db.bak` sibling the Python completion backup writes.
728fn backup_for_block(
729    database_path: &std::path::Path,
730    to_block: u64,
731) -> Result<std::path::PathBuf, CliError> {
732    let stem = database_path
733        .file_stem()
734        .map(|s| s.to_string_lossy().into_owned())
735        .unwrap_or_default();
736    let mut backup = database_path.to_path_buf();
737    backup.set_file_name(format!("{stem}-{to_block}.db.bak"));
738    ops::backup_database(database_path, &backup)?;
739    Ok(backup)
740}
741
742/// `aave position show`.
743fn position_show(
744    ctx: &CliContext<'_>,
745    raw_address: &str,
746    market: &str,
747    chain_id: u64,
748) -> Result<AaveReport, CliError> {
749    let address: Address = raw_address
750        .parse()
751        .map_err(|_| CliError::InvalidAddress(raw_address.to_string()))?;
752    let user_address = address.to_checksum(None);
753    let chain = i64::try_from(chain_id)
754        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
755    let database_path = ctx.database_path()?.value;
756    let db = (DegenbotDb::open(&database_path)?).0;
757    let Some(market_row) = db.fetch_aave_market_by_name(chain, market)? else {
758        return Ok(AaveReport::PositionNoMarket {
759            market: market.to_string(),
760            chain_id,
761        });
762    };
763    let Some(user) = db.fetch_aave_user_by_address(market_row.id, &user_address)? else {
764        return Ok(AaveReport::PositionNoUser {
765            user_address,
766            market: market.to_string(),
767            chain_id,
768        });
769    };
770    let collateral = db
771        .fetch_aave_collateral_positions(user.id)?
772        .into_iter()
773        .map(|p| AavePositionLine {
774            symbol: p.underlying_symbol.unwrap_or_else(|| "Unknown".to_string()),
775            balance: p.balance,
776        })
777        .collect();
778    let debt = db
779        .fetch_aave_debt_positions(user.id)?
780        .into_iter()
781        .map(|p| AavePositionLine {
782            symbol: p.underlying_symbol.unwrap_or_else(|| "Unknown".to_string()),
783            balance: p.balance,
784        })
785        .collect();
786    Ok(AaveReport::Position {
787        user_address,
788        market: market.to_string(),
789        chain_id,
790        collateral,
791        debt,
792    })
793}
794
795#[cfg(test)]
796mod tests {
797    #![expect(clippy::unwrap_used)]
798
799    use std::fs;
800    use std::path::PathBuf;
801    use std::time::{Duration, Instant};
802
803    use super::ResetLock;
804    use crate::error::CliError;
805
806    /// Spawn the `#[ignore]` child test as a fake holder and wait until it
807    /// reports the lock armed.
808    fn spawn_holder(db: &PathBuf, dir: &std::path::Path) -> std::process::Child {
809        let ready = dir.join("holder-ready");
810        let child = std::process::Command::new(std::env::current_exe().unwrap())
811            .args([
812                "--exact",
813                "aave::tests::reset_lock_holder_child",
814                "--ignored",
815                "--nocapture",
816            ])
817            .env("RESET_LOCK_TEST_DB", db)
818            .env("RESET_LOCK_TEST_READY", &ready)
819            .spawn()
820            .unwrap();
821        let deadline = Instant::now() + Duration::from_secs(30);
822        while !ready.exists() {
823            assert!(
824                Instant::now() < deadline,
825                "holder child never armed the reset lock"
826            );
827            std::thread::sleep(Duration::from_millis(20));
828        }
829        child
830    }
831
832    #[test]
833    fn stale_lock_file_does_not_block_a_reset() {
834        let dir = tempfile::tempdir().unwrap();
835        let db = dir.path().join("degenbot.db");
836        let lock_path = dir.path().join("degenbot.db.aave-reset.lock");
837        // A lock file outliving its holder: dead-or-garbage pid, no flock.
838        fs::write(&lock_path, "999999999 dead holder\n").unwrap();
839        let lock = ResetLock::acquire(&db, 7).unwrap();
840        // The takeover rewrites the pid file with the new holder's pid.
841        assert_eq!(
842            fs::read_to_string(&lock_path).unwrap().trim(),
843            std::process::id().to_string()
844        );
845        drop(lock);
846        assert!(!lock_path.exists());
847    }
848
849    #[test]
850    fn a_live_holder_blocks_until_it_releases() {
851        let dir = tempfile::tempdir().unwrap();
852        let db = dir.path().join("degenbot.db");
853        let holder = ResetLock::acquire(&db, 7).unwrap();
854        // A second open file description conflicts with the held flock even
855        // inside one process — the same refusal a concurrent reset sees.
856        assert!(matches!(
857            ResetLock::acquire(&db, 7),
858            Err(CliError::AaveMarketMidUpdate { .. })
859        ));
860        drop(holder);
861        ResetLock::acquire(&db, 7).unwrap();
862    }
863
864    #[test]
865    fn a_killed_holder_releases_the_reset_lock() {
866        let dir = tempfile::tempdir().unwrap();
867        let db = dir.path().join("degenbot.db");
868        let mut child = spawn_holder(&db, dir.path());
869        // The live fake holder blocks.
870        assert!(matches!(
871            ResetLock::acquire(&db, 7),
872            Err(CliError::AaveMarketMidUpdate { .. })
873        ));
874        // SIGKILL: the kernel drops the flock with the process, the pid file
875        // survives, and the next reset proceeds.
876        child.kill().unwrap();
877        child.wait().unwrap();
878        ResetLock::acquire(&db, 7).unwrap();
879    }
880
881    /// The fake holder: spawned as a child test process by
882    /// `a_killed_holder_releases_the_reset_lock`, killed mid-hold.
883    #[test]
884    #[ignore = "spawned as the fake holder by a_killed_holder_releases_the_reset_lock"]
885    fn reset_lock_holder_child() {
886        let db = PathBuf::from(std::env::var("RESET_LOCK_TEST_DB").unwrap());
887        let ready = PathBuf::from(std::env::var("RESET_LOCK_TEST_READY").unwrap());
888        let _lock = ResetLock::acquire(&db, 7).unwrap();
889        fs::write(&ready, b"armed").unwrap();
890        // Hold until the parent kills us; the deadline only bounds a leak if
891        // the parent dies before it can.
892        let deadline = Instant::now() + Duration::from_secs(120);
893        while Instant::now() < deadline {
894            std::thread::sleep(Duration::from_millis(100));
895        }
896    }
897}