Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor out looking up clob pair into SQL function. #784

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines 51 to 52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query to retrieve the asset record is not wrapped in a try-catch block. If the query fails for any reason (e.g., the asset with the given ID does not exist), it will cause the entire function to fail. Consider adding error handling here.

+    BEGIN
        SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
+    EXCEPTION
+        WHEN NO_DATA_FOUND THEN
+            RAISE EXCEPTION 'Unable to find asset with id %', usdc_asset_id;
+    END;

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
BEGIN
SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
BEGIN
SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE EXCEPTION 'Unable to find asset with id %', usdc_asset_id;
END;

Expand Down
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