From 5534ad805b9705532ef63959d50e719961a25231 Mon Sep 17 00:00:00 2001 From: Lukasz Cwik <126621805+lcwik@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:30:14 -0700 Subject: [PATCH] Fix order handler to return correct updatedAt/updatedAtHeight (#733) Also rename functions to have dydx_ prefix to allow for script deletion to successfully remove and update these functions during ender deployment. --- .../postgres/src/helpers/db-helpers.ts | 2 +- .../order-fills/order-handler.test.ts | 8 +++---- .../scripts/dydx_get_fee_from_liquidity.sql | 2 +- .../src/scripts/dydx_get_order_status.sql | 2 +- .../dydx_get_total_filled_from_liquidity.sql | 2 +- .../dydx_order_fill_handler_per_order.sql | 24 +++++++++++-------- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/indexer/packages/postgres/src/helpers/db-helpers.ts b/indexer/packages/postgres/src/helpers/db-helpers.ts index 032909b184..a70f4ada0b 100644 --- a/indexer/packages/postgres/src/helpers/db-helpers.ts +++ b/indexer/packages/postgres/src/helpers/db-helpers.ts @@ -70,7 +70,7 @@ $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;`; return rawQuery(sqlFn, {}).catch((error) => { logger.error({ at: 'dbHelpers#createModelToJsonFunctions', - message: `Failed to create or replace function dydx_to_json for model ${model.tableName}.`, + message: `Failed to create or replace function dydx_to_jsonb for model ${model.tableName}.`, error, }); throw error; diff --git a/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts b/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts index 8fb3468971..a8bf586610 100644 --- a/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts +++ b/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts @@ -609,8 +609,8 @@ describe('OrderHandler', () => { goodTilBlockTime: existingGoodTilBlockTime, orderFlags: ORDER_FLAG_SHORT_TERM.toString(), clientMetadata: '0', - updatedAt: defaultDateTime.toISO(), - updatedAtHeight: defaultHeight.toString(), + updatedAt: DateTime.fromMillis(0).toISO(), + updatedAtHeight: '0', }), // taker order OrderTable.create({ @@ -629,8 +629,8 @@ describe('OrderHandler', () => { goodTilBlockTime: existingGoodTilBlockTime, orderFlags: ORDER_FLAG_LONG_TERM.toString(), clientMetadata: '0', - updatedAt: defaultDateTime.toISO(), - updatedAtHeight: defaultHeight.toString(), + updatedAt: DateTime.fromMillis(0).toISO(), + updatedAtHeight: '0', }), ]); diff --git a/indexer/services/ender/src/scripts/dydx_get_fee_from_liquidity.sql b/indexer/services/ender/src/scripts/dydx_get_fee_from_liquidity.sql index 6dc226c3f9..b961433273 100644 --- a/indexer/services/ender/src/scripts/dydx_get_fee_from_liquidity.sql +++ b/indexer/services/ender/src/scripts/dydx_get_fee_from_liquidity.sql @@ -1,7 +1,7 @@ /** Returns the fee given the liquidity side. */ -CREATE OR REPLACE FUNCTION get_fee(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$ +CREATE OR REPLACE FUNCTION dydx_get_fee(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$ BEGIN IF fill_liquidity = 'TAKER' THEN RETURN dydx_from_jsonlib_long(event_data->'takerFee'); diff --git a/indexer/services/ender/src/scripts/dydx_get_order_status.sql b/indexer/services/ender/src/scripts/dydx_get_order_status.sql index 34748b731d..286e1dbef9 100644 --- a/indexer/services/ender/src/scripts/dydx_get_order_status.sql +++ b/indexer/services/ender/src/scripts/dydx_get_order_status.sql @@ -14,7 +14,7 @@ * 4. Short-term Limit & Post-only - If the order is in the CanceledOrdersCache, then it should be * set to `BEST_EFFORT_CANCELED`, otherwise `OPEN`. */ -CREATE OR REPLACE FUNCTION get_order_status(total_filled numeric, size numeric, is_cancelled boolean, order_flags bigint, time_in_force text) +CREATE OR REPLACE FUNCTION dydx_get_order_status(total_filled numeric, size numeric, is_cancelled boolean, order_flags bigint, time_in_force text) RETURNS text AS $$ BEGIN IF total_filled >= size THEN diff --git a/indexer/services/ender/src/scripts/dydx_get_total_filled_from_liquidity.sql b/indexer/services/ender/src/scripts/dydx_get_total_filled_from_liquidity.sql index f01fd26b6a..b85c0c7545 100644 --- a/indexer/services/ender/src/scripts/dydx_get_total_filled_from_liquidity.sql +++ b/indexer/services/ender/src/scripts/dydx_get_total_filled_from_liquidity.sql @@ -1,7 +1,7 @@ /** Returns the order total filled amount given the liquidity side. */ -CREATE OR REPLACE FUNCTION get_total_filled(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$ +CREATE OR REPLACE FUNCTION dydx_get_total_filled(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$ BEGIN IF fill_liquidity = 'TAKER' THEN RETURN dydx_from_jsonlib_long(event_data->'totalFilledTaker'); diff --git a/indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql b/indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql index 402a068075..7c5e0eb1e0 100644 --- a/indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql +++ b/indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql @@ -11,11 +11,13 @@ - transaction_hash: The transaction hash corresponding to this event from the IndexerTendermintBlock 'tx_hashes'. - fill_liquidity: The liquidity for the fill record. - fill_type: The type for the fill record. + - usdc_asset_id: The USDC asset id. - is_cancelled: Whether the order is cancelled. Returns: JSON object containing fields: - order: The updated order in order-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/order-model.ts). - fill: The updated 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 order in perpetual-market-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-market-model.ts). + - perpetual_position: The updated perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-position-model.ts). */ CREATE OR REPLACE FUNCTION dydx_order_fill_handler_per_order( field text, block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int, @@ -34,6 +36,7 @@ DECLARE order_side text; order_size numeric; order_price numeric; + order_client_metadata bigint; fee numeric; fill_amount numeric; total_filled numeric; @@ -77,14 +80,15 @@ BEGIN power(10, perpetual_market_record."quantumConversionExponent" + asset_record."atomicResolution" - perpetual_market_record."atomicResolution")::numeric); - total_filled = dydx_trim_scale(get_total_filled(fill_liquidity, event_data) * + total_filled = dydx_trim_scale(dydx_get_total_filled(fill_liquidity, event_data) * power(10, perpetual_market_record."atomicResolution")::numeric); - fee = dydx_trim_scale(get_fee(fill_liquidity, event_data) * + fee = dydx_trim_scale(dydx_get_fee(fill_liquidity, event_data) * power(10, asset_record."atomicResolution")::numeric); order_uuid = dydx_uuid_from_order_id(order_->'orderId'); subaccount_uuid = dydx_uuid_from_subaccount_id(jsonb_extract_path(order_, 'orderId', 'subaccountId')); order_side = dydx_from_protocol_order_side(order_->'side'); + order_client_metadata = (order_->'clientMetadata')::bigint; /** Upsert the order, populating the order_record fields with what will be in the database. */ SELECT * INTO order_record FROM orders WHERE "id" = order_uuid; @@ -95,11 +99,13 @@ BEGIN order_record."orderFlags" = jsonb_extract_path(order_, 'orderId', 'orderFlags')::bigint; order_record."goodTilBlock" = (order_->'goodTilBlock')::bigint; order_record."goodTilBlockTime" = to_timestamp((order_->'goodTilBlockTime')::double precision); - order_record."clientMetadata" = (order_->'clientMetadata')::bigint; + order_record."clientMetadata" = order_client_metadata; + order_record."updatedAt" = block_time; + order_record."updatedAtHeight" = block_height; IF FOUND THEN order_record."totalFilled" = total_filled; - order_record."status" = get_order_status(total_filled, order_record.size, is_cancelled, order_record."orderFlags", order_record."timeInForce"); + order_record."status" = dydx_get_order_status(total_filled, order_record.size, is_cancelled, order_record."orderFlags", order_record."timeInForce"); UPDATE orders SET @@ -113,8 +119,8 @@ BEGIN "timeInForce" = order_record."timeInForce", "reduceOnly" = order_record."reduceOnly", "clientMetadata" = order_record."clientMetadata", - "updatedAt" = block_time, - "updatedAtHeight" = block_height + "updatedAt" = order_record."updatedAt", + "updatedAtHeight" = order_record."updatedAtHeight" WHERE id = order_uuid; ELSE order_record."id" = order_uuid; @@ -125,10 +131,8 @@ BEGIN order_record."type" = 'LIMIT'; /* TODO: Add additional order types once we support */ order_record."totalFilled" = fill_amount; - order_record."status" = get_order_status(fill_amount, order_size, is_cancelled, order_record."orderFlags", order_record."timeInForce"); + order_record."status" = dydx_get_order_status(fill_amount, order_size, is_cancelled, order_record."orderFlags", order_record."timeInForce"); order_record."createdAtHeight" = block_height; - order_record."updatedAt" = block_time; - order_record."updatedAtHeight" = block_height; INSERT INTO orders ("id", "subaccountId", "clientId", "clobPairId", "side", "size", "totalFilled", "price", "type", "status", "timeInForce", "reduceOnly", "orderFlags", "goodTilBlock", "goodTilBlockTime", "createdAtHeight", @@ -156,7 +160,7 @@ BEGIN transaction_hash, block_time, block_height, - order_record."clientMetadata", + order_client_metadata, fee) RETURNING * INTO fill_record;