@@ -65,6 +65,93 @@ ENGINE = MergeTree
6565PARTITION BY toYYYYMM(parseDateTimeBestEffortOrZero(broker_observed_timestamp))
6666ORDER BY (exchange, symbol, broker_observed_timestamp);
6767
68+ -- CEX value movements: withdrawals, deposits, and sub<->master internal transfers.
69+ --
70+ -- Column names/types/ORDER BY match the fiet-maker consumer contract
71+ -- (docs/CEX_EXECUTION_ARCHIVE_CONTRACT.md + scripts/sql/clickhouse-schema.sql):
72+ -- MergeTree with a 90-day TTL, DateTime64(3,'UTC') timestamps, string quantities,
73+ -- result_index UInt32, error_summary. Two ADDITIVE columns not in the consumer
74+ -- contract: fee_amount / fee_currency (the ccxt withdrawal object exposes the fee,
75+ -- the dominant small-commit cost). broker_observed_timestamp is emitted as an
76+ -- ISO-8601 UTC string and parsed on insert via the forwarder's
77+ -- date_time_input_format=best_effort (see services/archive-forwarder/index.ts).
78+ --
79+ -- Engine is plain MergeTree (contract), so re-observed rows are NOT collapsed;
80+ -- dedup, when needed, is at read time (GROUP BY / argMax over exchange,
81+ -- account_selector, symbol, external_id, lifecycle_action).
82+ CREATE TABLE IF NOT EXISTS broker_execution .transfer_events
83+ (
84+ broker_observed_timestamp DateTime64(3 , ' UTC' ),
85+ source LowCardinality(String),
86+ deployment_id LowCardinality(String),
87+ schema_version LowCardinality(String),
88+ account_selector LowCardinality(String),
89+ exchange LowCardinality(String),
90+ symbol LowCardinality(String),
91+ event_kind LowCardinality(String),
92+ lifecycle_action LowCardinality(String),
93+ status LowCardinality(String) DEFAULT ' ' ,
94+ asset_symbol LowCardinality(String) DEFAULT ' ' ,
95+ amount String DEFAULT ' ' ,
96+ address String DEFAULT ' ' ,
97+ network LowCardinality(String) DEFAULT ' ' ,
98+ external_id String DEFAULT ' ' ,
99+ txid String DEFAULT ' ' ,
100+ result_index UInt32 DEFAULT 0 ,
101+ fee_amount String DEFAULT ' ' ,
102+ fee_currency LowCardinality(String) DEFAULT ' ' ,
103+ exchange_timestamp Nullable(DateTime64(3 , ' UTC' )),
104+ error_summary String DEFAULT ' ' ,
105+ payload_json String DEFAULT ' '
106+ )
107+ ENGINE = MergeTree
108+ PARTITION BY toDate(broker_observed_timestamp)
109+ ORDER BY (account_selector, broker_observed_timestamp, exchange, symbol, event_kind, lifecycle_action)
110+ TTL toDateTime(broker_observed_timestamp) + toIntervalDay(90 )
111+ SETTINGS ttl_only_drop_parts = 1 ;
112+
113+ -- Per-fill execution facts from the venue trade-history endpoint (fetchMyTrades),
114+ -- captured by the broker-internal fill poller. GetOrderDetails/createOrder payloads
115+ -- carry no per-trade breakdown and no fee on most venues, so per-fill truth
116+ -- (incl. fee) requires this endpoint; hence event_kind is stamped
117+ -- "trade_history_fill" rather than the contract fixture's "create_order_fill".
118+ --
119+ -- Column names/types/ORDER BY match the fiet-maker consumer contract: MergeTree +
120+ -- 90-day TTL, DateTime64 timestamps, string quantities, fill_index UInt32. Plain
121+ -- MergeTree (contract): the poller re-scans a lookback window after a restart, so
122+ -- the same trade can be re-inserted; dedup is at read time (GROUP BY / argMax over
123+ -- exchange, account_selector, symbol, order_id, fill_id).
124+ CREATE TABLE IF NOT EXISTS broker_execution .fill_events
125+ (
126+ broker_observed_timestamp DateTime64(3 , ' UTC' ),
127+ source LowCardinality(String),
128+ deployment_id LowCardinality(String),
129+ schema_version LowCardinality(String),
130+ account_selector LowCardinality(String),
131+ exchange LowCardinality(String),
132+ symbol LowCardinality(String),
133+ event_kind LowCardinality(String),
134+ order_id String,
135+ client_order_id String DEFAULT ' ' ,
136+ fill_id String DEFAULT ' ' ,
137+ fill_index UInt32 DEFAULT 0 ,
138+ side LowCardinality(String) DEFAULT ' ' ,
139+ order_type LowCardinality(String) DEFAULT ' ' ,
140+ price String DEFAULT ' ' ,
141+ base_quantity String DEFAULT ' ' ,
142+ quote_quantity String DEFAULT ' ' ,
143+ fee_amount String DEFAULT ' ' ,
144+ fee_currency LowCardinality(String) DEFAULT ' ' ,
145+ fee_rate String DEFAULT ' ' ,
146+ exchange_timestamp Nullable(DateTime64(3 , ' UTC' )),
147+ payload_json String DEFAULT ' '
148+ )
149+ ENGINE = MergeTree
150+ PARTITION BY toDate(broker_observed_timestamp)
151+ ORDER BY (symbol, account_selector, broker_observed_timestamp, exchange, order_id, fill_index)
152+ TTL toDateTime(broker_observed_timestamp) + toIntervalDay(90 )
153+ SETTINGS ttl_only_drop_parts = 1 ;
154+
68155-- Pre-order top-of-book snapshots captured immediately before an order action,
69156-- joinable to order_events via market_metadata_hash and the order identifiers.
70157CREATE TABLE IF NOT EXISTS broker_execution .market_metadata_snapshots
0 commit comments