From 25ba7be2188c839ad324c916b8e088b4bb7c8b7f Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 8 Nov 2023 16:06:36 -0500 Subject: [PATCH] address cmts --- .../ender/src/handlers/deleveraging-handler.ts | 2 +- .../order-fills/abstract-order-fill-handler.ts | 3 ++- .../src/helpers/postgres/postgres-functions.ts | 1 - .../src/scripts/dydx_deleveraging_handler.sql | 16 ++++++++-------- .../src/scripts/dydx_from_protocol_is_buy.sql | 12 ------------ .../scripts/dydx_update_perpetual_position.sql | 11 ++++++++++- 6 files changed, 21 insertions(+), 24 deletions(-) delete mode 100644 indexer/services/ender/src/scripts/dydx_from_protocol_is_buy.sql diff --git a/indexer/services/ender/src/handlers/deleveraging-handler.ts b/indexer/services/ender/src/handlers/deleveraging-handler.ts index e959765aa1..e2d712991a 100644 --- a/indexer/services/ender/src/handlers/deleveraging-handler.ts +++ b/indexer/services/ender/src/handlers/deleveraging-handler.ts @@ -120,7 +120,7 @@ export class DeleveragingHandler extends Handler { { txId: this.txId }, ).catch((error: Error) => { logger.error({ - at: 'deleveragingHandler#handleViaSqlFunction', + at: 'DeleveragingHandler#handleViaSqlFunction', message: 'Failed to handle DeleveragingEventV1', error, }); diff --git a/indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts b/indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts index 4e51de73de..0c0c864b67 100644 --- a/indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts +++ b/indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts @@ -44,7 +44,8 @@ import { DateTime } from 'luxon'; import { generateFillSubaccountMessage, generateOrderSubaccountMessage, - generatePerpetualPositionsContents, isDeleveraging, + generatePerpetualPositionsContents, + isDeleveraging, isLiquidation, } from '../../helpers/kafka-helper'; import { diff --git a/indexer/services/ender/src/helpers/postgres/postgres-functions.ts b/indexer/services/ender/src/helpers/postgres/postgres-functions.ts index 743ee78172..810bd05ddd 100644 --- a/indexer/services/ender/src/helpers/postgres/postgres-functions.ts +++ b/indexer/services/ender/src/helpers/postgres/postgres-functions.ts @@ -38,7 +38,6 @@ const scripts: string[] = [ 'dydx_event_id_from_parts.sql', 'dydx_event_to_transaction_index.sql', 'dydx_from_jsonlib_long.sql', - 'dydx_from_protocol_is_buy.sql', 'dydx_from_protocol_order_side.sql', 'dydx_from_protocol_time_in_force.sql', 'dydx_from_serializable_int.sql', diff --git a/indexer/services/ender/src/scripts/dydx_deleveraging_handler.sql b/indexer/services/ender/src/scripts/dydx_deleveraging_handler.sql index 95a07675f3..2a4479cc10 100644 --- a/indexer/services/ender/src/scripts/dydx_deleveraging_handler.sql +++ b/indexer/services/ender/src/scripts/dydx_deleveraging_handler.sql @@ -9,8 +9,8 @@ account the block_event (https://github.com/dydxprotocol/indexer/blob/cc70982/services/ender/src/lib/helper.ts#L33) - transaction_hash: The transaction hash corresponding to this event from the IndexerTendermintBlock 'tx_hashes'. Returns: JSON object containing fields: - - liquidated_fill: The updated liquidated fill in fill-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/fill-model.ts). - - offsetting_fill: The updated liquidated fill in fill-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/fill-model.ts). + - liquidated_fill: The created liquidated fill in fill-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/fill-model.ts). + - offsetting_fill: The created offsetting fill in fill-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/fill-model.ts). - perpetual_market: The perpetual market for the deleveraging in perpetual-market-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-market-model.ts). - liquidated_perpetual_position: The updated liquidated perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-position-model.ts). - offsetting_perpetual_position: The updated offsetting perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-position-model.ts). @@ -58,7 +58,7 @@ BEGIN liquidated_subaccount_uuid = dydx_uuid_from_subaccount_id(event_data->'liquidated'); offsetting_subaccount_uuid = dydx_uuid_from_subaccount_id(event_data->'offsetting'); - offsetting_side = dydx_from_protocol_is_buy((event_data->'isBuy')::bool); + offsetting_side = CASE WHEN (event_data->'isBuy')::bool THEN 'BUY' ELSE 'SELL' END; liquidated_side = CASE WHEN offsetting_side = 'BUY' THEN 'SELL' ELSE 'BUY' END; clob_pair_id = perpetual_market_record."clobPairId"; @@ -105,17 +105,17 @@ BEGIN /* Upsert the perpetual_position records for this deleveraging event. */ liquidated_perpetual_position_record = dydx_update_perpetual_position( - liquidated_subaccount_uuid, + liquidated_subaccount_uuid, perpetual_id, liquidated_side, size, price); offsetting_perpetual_position_record = dydx_update_perpetual_position( offsetting_subaccount_uuid, - perpetual_id, - offsetting_side, - size, - price); + perpetual_id, + offsetting_side, + size, + price); RETURN jsonb_build_object( diff --git a/indexer/services/ender/src/scripts/dydx_from_protocol_is_buy.sql b/indexer/services/ender/src/scripts/dydx_from_protocol_is_buy.sql deleted file mode 100644 index c85606cdc8..0000000000 --- a/indexer/services/ender/src/scripts/dydx_from_protocol_is_buy.sql +++ /dev/null @@ -1,12 +0,0 @@ -/** - Converts a boolean 'isBuy' field to 'BUY' or 'SELL'. - */ -CREATE OR REPLACE FUNCTION dydx_from_protocol_is_buy(is_buy boolean) RETURNS text AS $$ -BEGIN - IF is_buy THEN - RETURN 'BUY'; - ELSE - RETURN 'SELL'; - END IF; -END; -$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE; diff --git a/indexer/services/ender/src/scripts/dydx_update_perpetual_position.sql b/indexer/services/ender/src/scripts/dydx_update_perpetual_position.sql index 5a3f9fc673..ef8cf27aaa 100644 --- a/indexer/services/ender/src/scripts/dydx_update_perpetual_position.sql +++ b/indexer/services/ender/src/scripts/dydx_update_perpetual_position.sql @@ -1,3 +1,12 @@ +/** + Parameters: + - subaccount_uuid: The subaccount uuid of the updated perpetual position. + - perpetual_id: The perpetual id of the updated perpetual position. + - side: The side of the fill. + - size: The size of the fill. + - price: The price of the fill. + Returns: the updated perpetual position. +*/ CREATE OR REPLACE FUNCTION dydx_update_perpetual_position( subaccount_uuid uuid, perpetual_id bigint, @@ -53,7 +62,7 @@ BEGIN "entryPrice" = entry_price, "sumClose" = sum_close, "exitPrice" = exit_price - WHERE "id" = perpetual_position_record.id; + WHERE "id" = perpetual_position_record."id"; -- Return the updated perpetual position record RETURN perpetual_position_record;