From 8666dda63a4b0f1cccc9ec2303a66b465be32509 Mon Sep 17 00:00:00 2001 From: Lukasz Cwik Date: Thu, 9 Nov 2023 14:40:26 -0800 Subject: [PATCH] Refactor out looking up clob pair into SQL function. --- .../helpers/postgres/postgres-functions.ts | 1 + ...ydx_get_perpetual_market_for_clob_pair.sql | 23 +++++++++++++++++++ ...ydx_liquidation_fill_handler_per_order.sql | 10 +------- .../dydx_order_fill_handler_per_order.sql | 10 +------- .../scripts/dydx_stateful_order_handler.sql | 20 ++-------------- ...te_perpetual_position_aggregate_fields.sql | 2 +- 6 files changed, 29 insertions(+), 37 deletions(-) create mode 100644 indexer/services/ender/src/scripts/dydx_get_perpetual_market_for_clob_pair.sql diff --git a/indexer/services/ender/src/helpers/postgres/postgres-functions.ts b/indexer/services/ender/src/helpers/postgres/postgres-functions.ts index d822bc411f..fe5abaa1ce 100644 --- a/indexer/services/ender/src/helpers/postgres/postgres-functions.ts +++ b/indexer/services/ender/src/helpers/postgres/postgres-functions.ts @@ -42,6 +42,7 @@ const scripts: string[] = [ 'dydx_from_serializable_int.sql', 'dydx_funding_handler.sql', 'dydx_get_fee_from_liquidity.sql', + 'dydx_get_perpetual_market_for_clob_pair.sql', 'dydx_get_order_status.sql', 'dydx_get_total_filled_from_liquidity.sql', 'dydx_get_weighted_average.sql', diff --git a/indexer/services/ender/src/scripts/dydx_get_perpetual_market_for_clob_pair.sql b/indexer/services/ender/src/scripts/dydx_get_perpetual_market_for_clob_pair.sql new file mode 100644 index 0000000000..451c87601b --- /dev/null +++ b/indexer/services/ender/src/scripts/dydx_get_perpetual_market_for_clob_pair.sql @@ -0,0 +1,23 @@ +/** + Returns the perpetual market record for the provided clob pair. + + Parameters: + - clob_pair_id: The clob pair id. + Returns: the only perpetual market for the clob pair. Throws an exception if not exactly one row is found. +*/ +CREATE OR REPLACE FUNCTION dydx_get_perpetual_market_for_clob_pair( + clob_pair_id bigint +) RETURNS perpetual_markets AS $$ +DECLARE + perpetual_market_record perpetual_markets%ROWTYPE; +BEGIN + SELECT * INTO STRICT perpetual_market_record FROM perpetual_markets WHERE "clobPairId" = clob_pair_id; + RETURN perpetual_market_record; +EXCEPTION + WHEN NO_DATA_FOUND THEN + RAISE EXCEPTION 'Unable to find perpetual market with clobPairId: %', clob_pair_id; + WHEN TOO_MANY_ROWS THEN + /** This should never happen and if it ever were to would indicate that the table has malformed data. */ + RAISE EXCEPTION 'Found multiple perpetual markets with clobPairId: %', clob_pair_id; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql b/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql index f78888b04f..53b94b1858 100644 --- a/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql +++ b/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql @@ -52,15 +52,7 @@ BEGIN clob_pair_id = jsonb_extract_path(order_, 'clobPairId')::bigint; END IF; - BEGIN - SELECT * INTO STRICT perpetual_market_record FROM perpetual_markets WHERE "clobPairId" = clob_pair_id; - EXCEPTION - WHEN NO_DATA_FOUND THEN - RAISE EXCEPTION 'Unable to find perpetual market with clobPairId %', clob_pair_id; - WHEN TOO_MANY_ROWS THEN - /** This should never happen and if it ever were to would indicate that the table has malformed data. */ - RAISE EXCEPTION 'Found multiple perpetual markets with clobPairId %', clob_pair_id; - END; + perpetual_market_record = dydx_get_perpetual_market_for_clob_pair(clob_pair_id); BEGIN SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id; 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 70515bd62b..afdbfd0e8c 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 @@ -46,15 +46,7 @@ BEGIN order_ = event_data->field; maker_order = event_data->'makerOrder'; clob_pair_id = jsonb_extract_path(order_, 'orderId', 'clobPairId')::bigint; - BEGIN - SELECT * INTO STRICT perpetual_market_record FROM perpetual_markets WHERE "clobPairId" = clob_pair_id; - EXCEPTION - WHEN NO_DATA_FOUND THEN - RAISE EXCEPTION 'Unable to find perpetual market with clobPairId %', clob_pair_id; - WHEN TOO_MANY_ROWS THEN - /** This should never happen and if it ever were to would indicate that the table has malformed data. */ - RAISE EXCEPTION 'Found multiple perpetual markets with clobPairId %', clob_pair_id; - END; + perpetual_market_record = dydx_get_perpetual_market_for_clob_pair(clob_pair_id); BEGIN SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id; diff --git a/indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql b/indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql index 3adfb095a7..1355f46ccc 100644 --- a/indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql +++ b/indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql @@ -25,15 +25,7 @@ BEGIN order_ = COALESCE(event_data->'orderPlace'->'order', event_data->'longTermOrderPlacement'->'order', event_data->'conditionalOrderPlacement'->'order'); clob_pair_id = (order_->'orderId'->'clobPairId')::bigint; - BEGIN - SELECT * INTO STRICT perpetual_market_record FROM perpetual_markets WHERE "clobPairId" = clob_pair_id; - EXCEPTION - WHEN NO_DATA_FOUND THEN - RAISE EXCEPTION 'Unable to find perpetual market with clobPairId: %', clob_pair_id; - WHEN TOO_MANY_ROWS THEN - /** This should never happen and if it ever were to would indicate that the table has malformed data. */ - RAISE EXCEPTION 'Found multiple perpetual markets with clobPairId: %', clob_pair_id; - END; + perpetual_market_record = dydx_get_perpetual_market_for_clob_pair(clob_pair_id); /** Calculate sizes, prices, and fill amounts. @@ -113,15 +105,7 @@ BEGIN END CASE; clob_pair_id = (order_id->'clobPairId')::bigint; - BEGIN - SELECT * INTO STRICT perpetual_market_record FROM perpetual_markets WHERE "clobPairId" = clob_pair_id; - EXCEPTION - WHEN NO_DATA_FOUND THEN - RAISE EXCEPTION 'Unable to find perpetual market with clobPairId: %', clob_pair_id; - WHEN TOO_MANY_ROWS THEN - /** This should never happen and if it ever were to would indicate that the table has malformed data. */ - RAISE EXCEPTION 'Found multiple perpetual markets with clobPairId: %', clob_pair_id; - END; + perpetual_market_record = dydx_get_perpetual_market_for_clob_pair(clob_pair_id); subaccount_id = dydx_uuid_from_subaccount_id(order_id->'subaccountId'); SELECT * INTO subaccount_record FROM subaccounts WHERE "id" = subaccount_id; diff --git a/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql b/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql index 6f314eeae8..5bbb57f0d6 100644 --- a/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql +++ b/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql @@ -15,7 +15,7 @@ CREATE OR REPLACE FUNCTION dydx_update_perpetual_position_aggregate_fields( price numeric ) RETURNS perpetual_positions AS $$ DECLARE - perpetual_position_record RECORD; + perpetual_position_record perpetual_positions%ROWTYPE; sum_open numeric; entry_price numeric; sum_close numeric;