{"record":{"id":"523690437d807d35","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-invalid-token-addresses-e","errorCode":null,"errorMessage":"Failed to load invalid token addresses: {e}","messagePattern":"Failed to load invalid token addresses: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":1339,"sourceCode":"    /// Retrieves all invalid token addresses for a given chain.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the database query fails or address validation fails.\n    pub async fn load_invalid_token_addresses(\n        &self,\n        chain_id: u32,\n    ) -> anyhow::Result<Vec<Address>> {\n        sqlx::query_as::<_, (String,)>(\n            \"SELECT address FROM token WHERE chain_id = $1 AND error IS NOT NULL\",\n        )\n        .bind(chain_id as i32)\n        .fetch_all(&self.pool)\n        .await?\n        .into_iter()\n        .map(|(address,)| validate_address(&address))\n        .collect::<Result<Vec<_>, _>>()\n        .map_err(|e| anyhow::anyhow!(\"Failed to load invalid token addresses: {e}\"))\n    }\n\n    /// Loads pool data from the database for the specified chain and DEX.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the database query fails, the connection to the database is lost, or the query parameters are invalid.\n    pub async fn load_pools(\n        &self,\n        chain: SharedChain,\n        dex_id: &str,\n    ) -> anyhow::Result<Vec<PoolRow>> {\n        sqlx::query_as::<_, PoolRow>(AssertSqlSafe(format!(\n            \"SELECT {POOL_ROW_COLUMNS} FROM pool WHERE chain_id = $1 AND dex_name = $2 ORDER BY creation_block ASC\"\n        )))\n        .bind(chain.chain_id as i32)\n        .bind(dex_id)\n        .fetch_all(&self.pool)","sourceCodeStart":1321,"sourceCodeEnd":1357,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/database.rs#L1321-L1357","documentation":"Wraps failures while loading invalid token addresses for a chain: either the SELECT of addresses fails, or any returned address string fails `validate_address`. Both paths are funneled into this single anyhow error.","triggerScenarios":"Calling the invalid-address loading method (database.rs) when the SELECT errors (connection loss, missing table) or when a stored address fails `validate_address`, e.g. a row containing an empty string, wrong-length hex, or non-hex characters.","commonSituations":"Legacy rows written by an older adapter version that stored addresses without checksum/validation; manual data edits in the DB; database connectivity problems during startup cache warm-up.","solutions":["Identify the failing address from the error and delete or correct the invalid row.","Re-validate stored addresses against checksum format before relying on the invalid-token set.","Check that the underlying table exists and migrations are current.","Retry if the embedded error indicates a transient connection failure."],"exampleFix":"// before\n.map_err(|e| anyhow::anyhow!(\"Failed to load invalid token addresses: {e}\"))\n// after: purge rows that cannot validate\n// DELETE FROM invalid_token WHERE address !~ '^0x[0-9a-fA-F]{40}$';","handlingStrategy":"validation","validationCode":"// pre-filter addresses that cannot pass validation\nlet valid_hex = |a: &str| a.len() == 42 && a.starts_with(\"0x\") && a[2..].chars().all(|c| c.is_ascii_hexdigit());","typeGuard":null,"tryCatchPattern":"let invalid = self.load_invalid_addresses(chain).await.unwrap_or_else(|e| {\n    log::warn!(\"invalid-token set unavailable, treating as empty: {e}\");\n    HashSet::new()\n});","preventionTips":["Normalize and checksum addresses before persisting them to the invalid set.","Purge legacy rows written by older adapter versions.","Retry transient connection errors during startup warm-up.","Log which specific address failed validation to ease data repair."],"tags":["database","sqlx","postgres","address-validation"],"backgroundTag":"database-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}