pub struct BlockchainCacheDatabase { /* private fields */ }
Expand description
Database interface for persisting and retrieving blockchain entities and domain objects.
Implementations§
Source§impl BlockchainCacheDatabase
impl BlockchainCacheDatabase
Sourcepub async fn init(pg_options: PgConnectOptions) -> Self
pub async fn init(pg_options: PgConnectOptions) -> Self
Initializes a new database instance by establishing a connection to PostgreSQL.
§Panics
Panics if unable to connect to PostgreSQL with the provided options.
Sourcepub async fn seed_chain(&self, chain: &Chain) -> Result<()>
pub async fn seed_chain(&self, chain: &Chain) -> Result<()>
Seeds the database with a blockchain chain record.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn create_block_partition(&self, chain: &Chain) -> Result<String>
pub async fn create_block_partition(&self, chain: &Chain) -> Result<String>
Creates a table partition for the block table specific to the given chain
by calling the existing PostgreSQL function create_block_partition
.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn create_token_partition(&self, chain: &Chain) -> Result<String>
pub async fn create_token_partition(&self, chain: &Chain) -> Result<String>
Creates a table partition for the token table specific to the given chain
by calling the existing PostgreSQL function create_token_partition
.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn get_block_consistency_status(
&self,
chain: &Chain,
) -> Result<CachedBlocksConsistencyStatus>
pub async fn get_block_consistency_status( &self, chain: &Chain, ) -> Result<CachedBlocksConsistencyStatus>
Returns the highest block number that maintains data continuity in the database.
§Errors
Returns an error if the database query fails.
Sourcepub async fn add_block(&self, chain_id: u32, block: &Block) -> Result<()>
pub async fn add_block(&self, chain_id: u32, block: &Block) -> Result<()>
Inserts or updates a block record in the database.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_blocks_batch(
&self,
chain_id: u32,
blocks: &[Block],
) -> Result<()>
pub async fn add_blocks_batch( &self, chain_id: u32, blocks: &[Block], ) -> Result<()>
Inserts multiple blocks in a single database operation using UNNEST for optimal performance.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_blocks_copy(
&self,
chain_id: u32,
blocks: &[Block],
) -> Result<()>
pub async fn add_blocks_copy( &self, chain_id: u32, blocks: &[Block], ) -> Result<()>
Inserts blocks using PostgreSQL COPY BINARY for maximum performance.
This method is significantly faster than INSERT for bulk operations as it bypasses SQL parsing and uses PostgreSQL’s native binary protocol.
§Errors
Returns an error if the COPY operation fails.
Sourcepub async fn add_pool_swaps_copy(
&self,
chain_id: u32,
swaps: &[PoolSwap],
) -> Result<()>
pub async fn add_pool_swaps_copy( &self, chain_id: u32, swaps: &[PoolSwap], ) -> Result<()>
Inserts pool swaps using PostgreSQL COPY BINARY for maximum performance.
This method is significantly faster than INSERT for bulk operations as it bypasses SQL parsing and uses PostgreSQL’s native binary protocol.
§Errors
Returns an error if the COPY operation fails.
Sourcepub async fn add_pool_liquidity_updates_copy(
&self,
chain_id: u32,
updates: &[PoolLiquidityUpdate],
) -> Result<()>
pub async fn add_pool_liquidity_updates_copy( &self, chain_id: u32, updates: &[PoolLiquidityUpdate], ) -> Result<()>
Inserts pool liquidity updates using PostgreSQL COPY BINARY for maximum performance.
This method is significantly faster than INSERT for bulk operations as it bypasses SQL parsing and uses PostgreSQL’s native binary protocol.
§Errors
Returns an error if the COPY operation fails.
Sourcepub async fn copy_pool_fee_collects_batch(
&self,
chain_id: u32,
collects: &[PoolFeeCollect],
) -> Result<()>
pub async fn copy_pool_fee_collects_batch( &self, chain_id: u32, collects: &[PoolFeeCollect], ) -> Result<()>
Inserts pool fee collect events using PostgreSQL COPY BINARY for maximum performance.
This method is significantly faster than INSERT for bulk operations as it bypasses SQL parsing and most database validation checks.
§Errors
Returns an error if the COPY operation fails.
Sourcepub async fn load_block_timestamps(
&self,
chain: SharedChain,
from_block: u64,
) -> Result<Vec<BlockTimestampRow>>
pub async fn load_block_timestamps( &self, chain: SharedChain, from_block: u64, ) -> Result<Vec<BlockTimestampRow>>
Retrieves block timestamps for a given chain starting from a specific block number.
§Errors
Returns an error if the database query fails.
Sourcepub async fn add_dex(&self, dex: SharedDex) -> Result<()>
pub async fn add_dex(&self, dex: SharedDex) -> Result<()>
Adds or updates a DEX (Decentralized Exchange) record in the database.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_pool(&self, pool: &Pool) -> Result<()>
pub async fn add_pool(&self, pool: &Pool) -> Result<()>
Adds or updates a liquidity pool/pair record in the database.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_pools_batch(&self, pools: &[Pool]) -> Result<()>
pub async fn add_pools_batch(&self, pools: &[Pool]) -> Result<()>
Inserts multiple pools in a single database operation using UNNEST for optimal performance.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_pool_swaps_batch(
&self,
chain_id: u32,
swaps: &[PoolSwap],
) -> Result<()>
pub async fn add_pool_swaps_batch( &self, chain_id: u32, swaps: &[PoolSwap], ) -> Result<()>
Inserts multiple pool swaps in a single database operation using UNNEST for optimal performance.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_pool_liquidity_updates_batch(
&self,
chain_id: u32,
updates: &[PoolLiquidityUpdate],
) -> Result<()>
pub async fn add_pool_liquidity_updates_batch( &self, chain_id: u32, updates: &[PoolLiquidityUpdate], ) -> Result<()>
Inserts multiple pool liquidity updates in a single database operation using UNNEST for optimal performance.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_token(&self, token: &Token) -> Result<()>
pub async fn add_token(&self, token: &Token) -> Result<()>
Adds or updates a token record in the database.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_invalid_token(
&self,
chain_id: u32,
address: &Address,
error_string: &str,
) -> Result<()>
pub async fn add_invalid_token( &self, chain_id: u32, address: &Address, error_string: &str, ) -> Result<()>
Records an invalid token address with associated error information.
§Errors
Returns an error if the database insertion fails.
Sourcepub async fn add_swap(&self, chain_id: u32, swap: &PoolSwap) -> Result<()>
pub async fn add_swap(&self, chain_id: u32, swap: &PoolSwap) -> Result<()>
Persists a token swap transaction event to the pool_swap
table.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn add_pool_liquidity_update(
&self,
chain_id: u32,
liquidity_update: &PoolLiquidityUpdate,
) -> Result<()>
pub async fn add_pool_liquidity_update( &self, chain_id: u32, liquidity_update: &PoolLiquidityUpdate, ) -> Result<()>
Persists a liquidity position change (mint/burn) event to the pool_liquidity
table.
§Errors
Returns an error if the database operation fails.
Sourcepub async fn load_tokens(&self, chain: SharedChain) -> Result<Vec<Token>>
pub async fn load_tokens(&self, chain: SharedChain) -> Result<Vec<Token>>
Retrieves all valid token records for the given chain and converts them into Token
domain objects.
Only returns tokens that do not contain error information, filtering out invalid tokens that were previously recorded with error details.
§Errors
Returns an error if the database query fails.
Sourcepub async fn load_invalid_token_addresses(
&self,
chain_id: u32,
) -> Result<Vec<Address>>
pub async fn load_invalid_token_addresses( &self, chain_id: u32, ) -> Result<Vec<Address>>
Retrieves all invalid token addresses for a given chain.
§Errors
Returns an error if the database query fails or address validation fails.
Sourcepub async fn load_pools(
&self,
chain: SharedChain,
dex_id: &str,
) -> Result<Vec<PoolRow>>
pub async fn load_pools( &self, chain: SharedChain, dex_id: &str, ) -> Result<Vec<PoolRow>>
Loads pool data from the database for the specified chain and DEX.
§Errors
Returns an error if the database query fails, the connection to the database is lost, or the query parameters are invalid.
Sourcepub async fn toggle_perf_sync_settings(&self, enable: bool) -> Result<()>
pub async fn toggle_perf_sync_settings(&self, enable: bool) -> Result<()>
Toggles performance optimization settings for sync operations.
When enabled (true), applies settings for maximum write performance:
synchronous_commit
= OFFwork_mem
increased for bulk operations
When disabled (false), restores default safe settings:
synchronous_commit
= ON (data safety)work_mem
back to default
§Errors
Returns an error if the database operations fail.
Sourcepub async fn update_dex_last_synced_block(
&self,
chain_id: u32,
dex: &DexType,
block_number: u64,
) -> Result<()>
pub async fn update_dex_last_synced_block( &self, chain_id: u32, dex: &DexType, block_number: u64, ) -> Result<()>
Saves the checkpoint block number indicating the last completed pool synchronization for a specific DEX.
§Errors
Returns an error if the database operation fails.
pub async fn update_pool_last_synced_block( &self, chain_id: u32, dex: &DexType, pool_address: &Address, block_number: u64, ) -> Result<()>
Sourcepub async fn get_dex_last_synced_block(
&self,
chain_id: u32,
dex: &DexType,
) -> Result<Option<u64>>
pub async fn get_dex_last_synced_block( &self, chain_id: u32, dex: &DexType, ) -> Result<Option<u64>>
Retrieves the saved checkpoint block number from the last completed pool synchronization for a specific DEX.
§Errors
Returns an error if the database query fails.
pub async fn get_pool_last_synced_block( &self, chain_id: u32, dex: &DexType, pool_address: &Address, ) -> Result<Option<u64>>
Sourcepub async fn get_table_last_block(
&self,
chain_id: u32,
table_name: &str,
pool_address: &Address,
) -> Result<Option<u64>>
pub async fn get_table_last_block( &self, chain_id: u32, table_name: &str, pool_address: &Address, ) -> Result<Option<u64>>
Retrieves the maximum block number from a specific table for a given pool. This is useful to detect orphaned data where events were inserted but progress wasn’t updated.
§Errors
Returns an error if the database query fails.
Sourcepub async fn add_pool_collects_batch(
&self,
chain_id: u32,
collects: &[PoolFeeCollect],
) -> Result<()>
pub async fn add_pool_collects_batch( &self, chain_id: u32, collects: &[PoolFeeCollect], ) -> Result<()>
Adds a batch of pool fee collect events to the database using batch operations.
§Errors
Returns an error if the database operation fails.
pub async fn update_pool_initial_price_tick( &self, chain_id: u32, initialize_event: &InitializeEvent, ) -> Result<()>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BlockchainCacheDatabase
impl !RefUnwindSafe for BlockchainCacheDatabase
impl Send for BlockchainCacheDatabase
impl Sync for BlockchainCacheDatabase
impl Unpin for BlockchainCacheDatabase
impl !UnwindSafe for BlockchainCacheDatabase
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more