-
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-470] Update asset create handler to use SQL function. (#748)
- Loading branch information
Showing
8 changed files
with
170 additions
and
42 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
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
34 changes: 34 additions & 0 deletions
34
indexer/services/ender/src/scripts/dydx_asset_create_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,34 @@ | ||
/** | ||
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: | ||
- asset: The created asset in asset-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/asset-model.ts). | ||
*/ | ||
CREATE OR REPLACE FUNCTION dydx_asset_create_handler(event_data jsonb) RETURNS jsonb AS $$ | ||
DECLARE | ||
market_record_id integer; | ||
asset_record assets%ROWTYPE; | ||
BEGIN | ||
asset_record."id" = event_data->>'id'; | ||
asset_record."atomicResolution" = (event_data->'atomicResolution')::integer; | ||
asset_record."symbol" = event_data->>'symbol'; | ||
|
||
asset_record."hasMarket" = (event_data->'hasMarket')::bool; | ||
if asset_record."hasMarket" THEN | ||
market_record_id = (event_data->'marketId')::integer; | ||
SELECT "id" INTO asset_record."marketId" FROM markets WHERE "id" = market_record_id; | ||
|
||
IF NOT FOUND THEN | ||
RAISE EXCEPTION 'Unable to find market with id: %', market_record_id; | ||
END IF; | ||
END IF; | ||
|
||
INSERT INTO assets VALUES (asset_record.*); | ||
|
||
RETURN jsonb_build_object( | ||
'asset', | ||
dydx_to_jsonb(asset_record) | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |