Skip to content

Commit

Permalink
Refactor out looking up clob pair into SQL function. (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwik authored Nov 9, 2023
1 parent 3cd0a64 commit cfd2be7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 2 additions & 18 deletions indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cfd2be7

Please sign in to comment.