polynode 0.7.0

Rust SDK for the PolyNode API — real-time Polymarket data
Documentation
use crate::error::Result;
use crate::types::events::SettlementEvent;
use super::types::*;

/// Storage backend trait for the cache.
/// SQLite is the built-in implementation. Users can implement this
/// for Postgres or other databases.
pub trait StorageBackend: Send + Sync {
    fn open(&mut self) -> Result<()>;
    fn close(&mut self);

    // ── Writes ──

    fn upsert_settlement(&self, event: &SettlementEvent) -> Result<()>;
    fn upsert_trade(&self, trade: &TradeRow) -> Result<()>;
    fn upsert_trades(&self, trades: &[TradeRow]) -> Result<()>;
    fn upsert_positions(&self, wallet: &str, positions: &[serde_json::Value]) -> Result<()>;
    fn upsert_onchain_positions(&self, wallet: &str, positions: &[crate::types::rest::OnchainPosition]) -> Result<()>;

    // ── Reads ──

    fn wallet_trades(&self, wallet: &str, opts: &QueryOptions) -> Result<Vec<TradeRow>>;
    fn wallet_positions(&self, wallet: &str) -> Result<Vec<PositionSummary>>;
    fn multi_wallet_positions(&self, wallets: &[String]) -> Result<std::collections::HashMap<String, Vec<PositionSummary>>>;
    fn market_trades(&self, condition_id: &str, opts: &QueryOptions) -> Result<Vec<TradeRow>>;
    fn market_positions(&self, condition_id: &str) -> Result<Vec<PositionSummary>>;
    fn token_trades(&self, token_id: &str, opts: &QueryOptions) -> Result<Vec<TradeRow>>;
    fn wallet_settlements(&self, wallet: &str, opts: &QueryOptions) -> Result<Vec<SettlementRow>>;
    fn trade_by_tx_hash(&self, tx_hash: &str) -> Result<Vec<TradeRow>>;

    // ── Backfill state ──

    fn get_backfill_state(&self, entity_type: &str, entity_id: &str) -> Result<Option<BackfillStateRow>>;
    fn get_pending_backfills(&self) -> Result<Vec<BackfillStateRow>>;
    fn set_backfill_state(&self, state: &BackfillStateRow) -> Result<()>;
    fn update_backfill_progress(&self, entity_type: &str, entity_id: &str, offset: i64, fetched: i64) -> Result<()>;
    fn complete_backfill(&self, entity_type: &str, entity_id: &str) -> Result<()>;
    fn fail_backfill(&self, entity_type: &str, entity_id: &str, error: &str) -> Result<()>;

    // ── Watchlist snapshot ──

    fn get_watchlist_snapshot(&self) -> Result<Vec<WatchlistSnapshotRow>>;
    fn set_watchlist_snapshot(&self, entries: &[WatchlistSnapshotRow]) -> Result<()>;

    // ── P&L queries ──

    fn wallet_token_ids(&self, _wallet: &str) -> Vec<String> { Vec::new() }
    fn wallet_token_trades(&self, _wallet: &str, _token_id: &str) -> Vec<super::types::TradeRow> { Vec::new() }
    fn wallet_backfill_complete(&self, _wallet: &str) -> bool { false }

    // ── Maintenance ──

    fn prune(&self, max_age_seconds: u64) -> Result<usize>;
    fn purge_entity(&self, entity_type: &str, entity_id: &str) -> Result<usize>;
    fn analyze(&self) -> Result<()>;
    fn stats(&self) -> Result<CacheStats>;
}