-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IND-475] Update ender
update clob pair
handler to execute via a SQ…
…L function. (#752) * [IND-475] Update ender `update clob pair` handler to execute via a SQL function.
- Loading branch information
1 parent
b205524
commit e4b45de
Showing
7 changed files
with
162 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
indexer/services/ender/src/scripts/dydx_clob_pair_status_to_market_status.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
Returns the market status (https://github.com/dydxprotocol/v4-chain/blob/ea4f6895a73627aaa9bc5e21eed1ba51313b1ce4/indexer/packages/postgres/src/types/perpetual-market-types.ts#L60) | ||
from the clob pair status (https://github.com/dydxprotocol/v4-chain/blob/ea4f6895a73627aaa9bc5e21eed1ba51313b1ce4/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L157). | ||
The conversion is equivalent to https://github.com/dydxprotocol/v4-chain/blob/ea4f6895a73627aaa9bc5e21eed1ba51313b1ce4/indexer/packages/postgres/src/lib/protocol-translations.ts#L351. | ||
Parameters: | ||
- status: the ClobPairStatus (https://github.com/dydxprotocol/v4-chain/blob/ea4f6895a73627aaa9bc5e21eed1ba51313b1ce4/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L157) | ||
*/ | ||
CREATE OR REPLACE FUNCTION dydx_clob_pair_status_to_market_status(status jsonb) | ||
RETURNS text AS $$ | ||
BEGIN | ||
CASE status | ||
WHEN '1'::jsonb THEN RETURN 'ACTIVE'; /** CLOB_PAIR_STATUS_ACTIVE */ | ||
WHEN '2'::jsonb THEN RETURN 'PAUSED'; /** CLOB_PAIR_STATUS_PAUSED */ | ||
WHEN '3'::jsonb THEN RETURN 'CANCEL_ONLY'; /** CLOB_PAIR_STATUS_CANCEL_ONLY */ | ||
WHEN '4'::jsonb THEN RETURN 'POST_ONLY'; /** CLOB_PAIR_STATUS_POST_ONLY */ | ||
WHEN '5'::jsonb THEN RETURN 'INITIALIZING'; /** CLOB_PAIR_STATUS_INITIALIZING */ | ||
ELSE RAISE EXCEPTION 'Invalid clob pair status: %', status; | ||
END CASE; | ||
END; | ||
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE; |
38 changes: 38 additions & 0 deletions
38
indexer/services/ender/src/scripts/dydx_update_clob_pair_handler.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
Parameters: | ||
- event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-proto/blob/8d35c86/dydxprotocol/indexer/indexer_manager/event.proto#L25) | ||
converted to JSON format. Conversion to JSON is expected to be done by JSON.stringify. | ||
Returns: JSON object containing fields: | ||
- perpetual_market: The updated perpetual market in perpetual-market-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-market-model.ts). | ||
*/ | ||
CREATE OR REPLACE FUNCTION dydx_update_clob_pair_handler(event_data jsonb) RETURNS jsonb AS $$ | ||
DECLARE | ||
row_count integer; | ||
clob_pair_id bigint; | ||
perpetual_market_record perpetual_markets%ROWTYPE; | ||
BEGIN | ||
clob_pair_id = (event_data->'clobPairId')::bigint; | ||
perpetual_market_record."status" = dydx_clob_pair_status_to_market_status(event_data->'status'); | ||
perpetual_market_record."quantumConversionExponent" = (event_data->'quantumConversionExponent')::integer; | ||
perpetual_market_record."subticksPerTick" = (event_data->'subticksPerTick')::integer; | ||
perpetual_market_record."stepBaseQuantums" = dydx_from_jsonlib_long(event_data->'stepBaseQuantums'); | ||
|
||
UPDATE perpetual_markets | ||
SET | ||
"status" = perpetual_market_record."status", | ||
"quantumConversionExponent" = perpetual_market_record."quantumConversionExponent", | ||
"subticksPerTick" = perpetual_market_record."subticksPerTick", | ||
"stepBaseQuantums" = perpetual_market_record."stepBaseQuantums" | ||
WHERE "clobPairId" = clob_pair_id | ||
RETURNING * INTO perpetual_market_record; | ||
|
||
IF NOT FOUND THEN | ||
RAISE EXCEPTION 'Could not find perpetual market with corresponding clobPairId %', event_data; | ||
END IF; | ||
|
||
RETURN jsonb_build_object( | ||
'perpetual_market', | ||
dydx_to_jsonb(perpetual_market_record) | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |