use crate::{ database::Database, fuel_core_graphql_api::ports::{ DatabaseBlocks, DatabaseChain, DatabaseContracts, DatabaseMessages, OnChainDatabase, }, }; use fuel_core_importer::ports::ImporterDatabase; use fuel_core_storage::{ iter::{ BoxedIter, IntoBoxedIter, IterDirection, IteratorOverTable, }, not_found, tables::FuelBlocks, Error as StorageError, Result as StorageResult, }; use fuel_core_txpool::types::ContractId; use fuel_core_types::{ blockchain::{ block::CompressedBlock, primitives::DaBlockHeight, }, entities::relayer::message::Message, fuel_tx::AssetId, fuel_types::{ BlockHeight, Nonce, }, services::graphql_api::ContractBalance, }; use itertools::Itertools; impl DatabaseBlocks for Database { fn blocks( &self, height: Option, direction: IterDirection, ) -> BoxedIter<'_, StorageResult> { self.iter_all_by_start::(height.as_ref(), Some(direction)) .map(|result| result.map(|(_, block)| block)) .into_boxed() } fn latest_height(&self) -> StorageResult { self.latest_block_height() .transpose() .ok_or(not_found!("BlockHeight"))? } fn latest_genesis_height(&self) -> StorageResult { self.genesis_block()? .map(|block| *block.header().height()) .ok_or(not_found!("BlockHeight")) } } impl DatabaseMessages for Database { fn all_messages( &self, start_message_id: Option, direction: IterDirection, ) -> BoxedIter<'_, StorageResult> { self.all_messages(start_message_id, Some(direction)) .map(|result| result.map_err(StorageError::from)) .into_boxed() } fn message_exists(&self, nonce: &Nonce) -> StorageResult { self.message_exists(nonce) } } impl DatabaseContracts for Database { fn contract_balances( &self, contract: ContractId, start_asset: Option, direction: IterDirection, ) -> BoxedIter> { self.filter_contract_balances(contract, start_asset, Some(direction)) .map_ok(|entry| ContractBalance { owner: *entry.key.contract_id(), amount: entry.value, asset_id: *entry.key.asset_id(), }) .map(|res| res.map_err(StorageError::from)) .into_boxed() } } impl DatabaseChain for Database { fn da_height(&self) -> StorageResult { self.latest_compressed_block()? .map(|block| block.header().da_height) .ok_or(not_found!("DaBlockHeight")) } } impl OnChainDatabase for Database {}