-
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-472] Update ender liquidity tier upsert to execute updates via a…
… SQL function. (#761)
- Loading branch information
Showing
8 changed files
with
188 additions
and
61 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_liquidity_tier_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: | ||
- liquidy_tier: The upserted liquidity tier in liquidity-tiers-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/liquidity-tiers-model.ts). | ||
*/ | ||
CREATE OR REPLACE FUNCTION dydx_liquidity_tier_handler(event_data jsonb) RETURNS jsonb AS $$ | ||
DECLARE | ||
liquidity_tier_record liquidity_tiers%ROWTYPE; | ||
BEGIN | ||
liquidity_tier_record."id" = (event_data->'id')::integer; | ||
liquidity_tier_record."name" = event_data->>'name'; | ||
liquidity_tier_record."initialMarginPpm" = (event_data->'initialMarginPpm')::bigint; | ||
liquidity_tier_record."maintenanceFractionPpm" = (event_data->'maintenanceFractionPpm')::bigint; | ||
liquidity_tier_record."basePositionNotional" = power(10, -6) * dydx_from_jsonlib_long(event_data->'basePositionNotional'); | ||
|
||
INSERT INTO liquidity_tiers | ||
VALUES (liquidity_tier_record.*) | ||
ON CONFLICT ("id") DO | ||
UPDATE | ||
SET | ||
"name" = liquidity_tier_record."name", | ||
"initialMarginPpm" = liquidity_tier_record."initialMarginPpm", | ||
"maintenanceFractionPpm" = liquidity_tier_record."maintenanceFractionPpm", | ||
"basePositionNotional" = liquidity_tier_record."basePositionNotional" | ||
RETURNING * INTO liquidity_tier_record; | ||
|
||
RETURN jsonb_build_object( | ||
'liquidity_tier', | ||
dydx_to_jsonb(liquidity_tier_record) | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |