VaultSettlementDatabase
Documentation for eth_defi.vault.settlement_data.VaultSettlementDatabase Python class.
- class VaultSettlementDatabase
Bases:
objectMini DuckDB interface for vault settlement events.
The schema has no primary key because DuckDB ART indexes have caused stability issues elsewhere in this project. Idempotence is handled with a delete-then-insert transaction keyed by
(chain_id, address, tx_hash, event_name). Different transactions in the same block and different settlement events in the same transaction are separate rows.- Parameters
path – DuckDB database path.
Open the database and create the schema if needed.
- Parameters
path – DuckDB file path.
Methods summary
__init__(path)Open the database and create the schema if needed.
close()Close the database connection.
get_latest_block_number(chain_id, address)Return the latest stored settlement block for a vault.
get_latest_scanned_block_number(chain_id, ...)Return the latest successfully scanned settlement block for a vault.
Return the number of stored settlement rows.
get_settlements([chain_id, address, protocol])Read settlement rows as a DataFrame.
save()Checkpoint the database.
upsert_scan_state(scan_states)Update settlement scan watermarks.
upsert_settlements(settlements)Insert settlement rows idempotently.
- __init__(path)
Open the database and create the schema if needed.
- Parameters
path (pathlib.Path) – DuckDB file path.
- close()
Close the database connection.
- Return type
None
- save()
Checkpoint the database.
- Return type
None
- upsert_settlements(settlements)
Insert settlement rows idempotently.
Existing rows with the same
(chain_id, address, tx_hash, event_name)are replaced. This lets protocol readers rescan overlapping block ranges without collapsing different settlement transactions in the same block or different event logs in the same transaction.- Parameters
settlements (list[eth_defi.vault.settlement_data.VaultSettlement]) – Settlement rows to store.
- Returns
Number of rows inserted.
- Return type
- get_settlements(chain_id=None, address=None, protocol=None)
Read settlement rows as a DataFrame.
- Parameters
- Returns
DataFrame sorted by
chain_id,address,timestampandtx_hash.- Return type
- get_settlement_count()
Return the number of stored settlement rows.
- Return type
- get_latest_block_number(chain_id, address)
Return the latest stored settlement block for a vault.
- get_latest_scanned_block_number(chain_id, address)
Return the latest successfully scanned settlement block for a vault.
Settlement events are sparse, so the event table cannot tell whether a vault has no new events or whether it has never been scanned. This scan-state table records successful empty scans as well.
- upsert_scan_state(scan_states)
Update settlement scan watermarks.
The stored watermark never moves backwards. Forced historical backfills may scan old ranges, but they must not erase knowledge of newer ranges that already completed successfully.