From 54ad20fce39a0f6dac064f472c8ccf9ed55a4382 Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:39:04 -0700 Subject: [PATCH] Add market order/swap closed logic --- ...b9c508e039285194b97b6d96e961dd7edc2557fbd2.json} | 4 ++-- src/rust/aggregator/src/data/user_history.rs | 13 ++++++++++--- src/rust/dependencies/aptos-indexer-processors | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) rename src/rust/.sqlx/{query-a19af3d32ebfdfd481d8a334981d57c3959f401fb7884a37ebbaf76cab817328.json => query-b17a5be1a3797d2bc5bfedb9c508e039285194b97b6d96e961dd7edc2557fbd2.json} (50%) diff --git a/src/rust/.sqlx/query-a19af3d32ebfdfd481d8a334981d57c3959f401fb7884a37ebbaf76cab817328.json b/src/rust/.sqlx/query-b17a5be1a3797d2bc5bfedb9c508e039285194b97b6d96e961dd7edc2557fbd2.json similarity index 50% rename from src/rust/.sqlx/query-a19af3d32ebfdfd481d8a334981d57c3959f401fb7884a37ebbaf76cab817328.json rename to src/rust/.sqlx/query-b17a5be1a3797d2bc5bfedb9c508e039285194b97b6d96e961dd7edc2557fbd2.json index 2e4784545..010e2db41 100644 --- a/src/rust/.sqlx/query-a19af3d32ebfdfd481d8a334981d57c3959f401fb7884a37ebbaf76cab817328.json +++ b/src/rust/.sqlx/query-b17a5be1a3797d2bc5bfedb9c508e039285194b97b6d96e961dd7edc2557fbd2.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n UPDATE aggregator.user_history\n SET\n remaining_size = remaining_size - $1,\n total_filled = total_filled + $1,\n order_status = CASE remaining_size - $1\n WHEN 0 THEN 'closed'\n ELSE order_status\n END,\n last_updated_at = $4\n WHERE order_id = $2 AND market_id = $3\n ", + "query": "\n UPDATE aggregator.user_history\n SET\n remaining_size = remaining_size - $1,\n total_filled = total_filled + $1,\n order_status = CASE order_type\n WHEN 'limit' THEN CASE remaining_size - $1\n WHEN 0 THEN 'closed'\n ELSE order_status\n END\n ELSE 'closed'\n END,\n last_updated_at = $4\n WHERE order_id = $2 AND market_id = $3\n ", "describe": { "columns": [], "parameters": { @@ -13,5 +13,5 @@ }, "nullable": [] }, - "hash": "a19af3d32ebfdfd481d8a334981d57c3959f401fb7884a37ebbaf76cab817328" + "hash": "b17a5be1a3797d2bc5bfedb9c508e039285194b97b6d96e961dd7edc2557fbd2" } diff --git a/src/rust/aggregator/src/data/user_history.rs b/src/rust/aggregator/src/data/user_history.rs index 6c2207fc5..7f515b346 100644 --- a/src/rust/aggregator/src/data/user_history.rs +++ b/src/rust/aggregator/src/data/user_history.rs @@ -383,15 +383,22 @@ async fn aggregate_fill<'a>( market_id: &BigDecimal, time: &DateTime, ) -> DataAggregationResult { + // Only limit orders can remain open after a transaction during which they are filled against, + // so flag market orders and swaps as closed by default: if they end up being cancelled instead + // of closed, the cancel event emitted during the same transaction (aggregated after fills) will + // clean up the order status to cancelled. sqlx::query!( r#" UPDATE aggregator.user_history SET remaining_size = remaining_size - $1, total_filled = total_filled + $1, - order_status = CASE remaining_size - $1 - WHEN 0 THEN 'closed' - ELSE order_status + order_status = CASE order_type + WHEN 'limit' THEN CASE remaining_size - $1 + WHEN 0 THEN 'closed' + ELSE order_status + END + ELSE 'closed' END, last_updated_at = $4 WHERE order_id = $2 AND market_id = $3 diff --git a/src/rust/dependencies/aptos-indexer-processors b/src/rust/dependencies/aptos-indexer-processors index 1a68026c4..c9dd14def 160000 --- a/src/rust/dependencies/aptos-indexer-processors +++ b/src/rust/dependencies/aptos-indexer-processors @@ -1 +1 @@ -Subproject commit 1a68026c49c7b6f9965132850ce4ea289e5f0794 +Subproject commit c9dd14def07cc26ae349028188bfea7131c55d50