BlockchainCacheDatabase

Struct BlockchainCacheDatabase 

Source
pub struct BlockchainCacheDatabase { /* private fields */ }
Expand description

Database interface for persisting and retrieving blockchain entities and domain objects.

Implementations§

Source§

impl BlockchainCacheDatabase

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 = OFF
  • work_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.

Source

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.

Source

pub async fn update_pool_last_synced_block( &self, chain_id: u32, dex: &DexType, pool_address: &Address, block_number: u64, ) -> Result<()>

Source

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.

Source

pub async fn get_pool_last_synced_block( &self, chain_id: u32, dex: &DexType, pool_address: &Address, ) -> Result<Option<u64>>

Source

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.

Source

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.

Source

pub async fn update_pool_initial_price_tick( &self, chain_id: u32, initialize_event: &InitializeEvent, ) -> Result<()>

Trait Implementations§

Source§

impl Debug for BlockchainCacheDatabase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> Ungil for T
where T: Send,