Skip to content

Commit

Permalink
Fix order handler to return correct updatedAt/updatedAtHeight (#733)
Browse files Browse the repository at this point in the history
Also rename functions to have dydx_ prefix to allow for script deletion to successfully remove and update these functions during ender deployment.
  • Loading branch information
lcwik authored Nov 1, 2023
1 parent cb8e3a1 commit 5534ad8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/helpers/db-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;`;
return rawQuery(sqlFn, {}).catch((error) => {
logger.error({
at: 'dbHelpers#createModelToJsonFunctions',
message: `Failed to create or replace function dydx_to_json for model ${model.tableName}.`,
message: `Failed to create or replace function dydx_to_jsonb for model ${model.tableName}.`,
error,
});
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,8 @@ describe('OrderHandler', () => {
goodTilBlockTime: existingGoodTilBlockTime,
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
clientMetadata: '0',
updatedAt: defaultDateTime.toISO(),
updatedAtHeight: defaultHeight.toString(),
updatedAt: DateTime.fromMillis(0).toISO(),
updatedAtHeight: '0',
}),
// taker order
OrderTable.create({
Expand All @@ -629,8 +629,8 @@ describe('OrderHandler', () => {
goodTilBlockTime: existingGoodTilBlockTime,
orderFlags: ORDER_FLAG_LONG_TERM.toString(),
clientMetadata: '0',
updatedAt: defaultDateTime.toISO(),
updatedAtHeight: defaultHeight.toString(),
updatedAt: DateTime.fromMillis(0).toISO(),
updatedAtHeight: '0',
}),
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
Returns the fee given the liquidity side.
*/
CREATE OR REPLACE FUNCTION get_fee(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
CREATE OR REPLACE FUNCTION dydx_get_fee(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
BEGIN
IF fill_liquidity = 'TAKER' THEN
RETURN dydx_from_jsonlib_long(event_data->'takerFee');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* 4. Short-term Limit & Post-only - If the order is in the CanceledOrdersCache, then it should be
* set to `BEST_EFFORT_CANCELED`, otherwise `OPEN`.
*/
CREATE OR REPLACE FUNCTION get_order_status(total_filled numeric, size numeric, is_cancelled boolean, order_flags bigint, time_in_force text)
CREATE OR REPLACE FUNCTION dydx_get_order_status(total_filled numeric, size numeric, is_cancelled boolean, order_flags bigint, time_in_force text)
RETURNS text AS $$
BEGIN
IF total_filled >= size THEN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
Returns the order total filled amount given the liquidity side.
*/
CREATE OR REPLACE FUNCTION get_total_filled(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
CREATE OR REPLACE FUNCTION dydx_get_total_filled(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
BEGIN
IF fill_liquidity = 'TAKER' THEN
RETURN dydx_from_jsonlib_long(event_data->'totalFilledTaker');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
- transaction_hash: The transaction hash corresponding to this event from the IndexerTendermintBlock 'tx_hashes'.
- fill_liquidity: The liquidity for the fill record.
- fill_type: The type for the fill record.
- usdc_asset_id: The USDC asset id.
- is_cancelled: Whether the order is cancelled.
Returns: JSON object containing fields:
- order: The updated order in order-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/order-model.ts).
- fill: The updated fill in fill-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/fill-model.ts).
- perpetual_market: The perpetual market for the order in perpetual-market-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-market-model.ts).
- perpetual_position: The updated perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/perpetual-position-model.ts).
*/
CREATE OR REPLACE FUNCTION dydx_order_fill_handler_per_order(
field text, block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int,
Expand All @@ -34,6 +36,7 @@ DECLARE
order_side text;
order_size numeric;
order_price numeric;
order_client_metadata bigint;
fee numeric;
fill_amount numeric;
total_filled numeric;
Expand Down Expand Up @@ -77,14 +80,15 @@ BEGIN
power(10, perpetual_market_record."quantumConversionExponent" +
asset_record."atomicResolution" -
perpetual_market_record."atomicResolution")::numeric);
total_filled = dydx_trim_scale(get_total_filled(fill_liquidity, event_data) *
total_filled = dydx_trim_scale(dydx_get_total_filled(fill_liquidity, event_data) *
power(10, perpetual_market_record."atomicResolution")::numeric);
fee = dydx_trim_scale(get_fee(fill_liquidity, event_data) *
fee = dydx_trim_scale(dydx_get_fee(fill_liquidity, event_data) *
power(10, asset_record."atomicResolution")::numeric);

order_uuid = dydx_uuid_from_order_id(order_->'orderId');
subaccount_uuid = dydx_uuid_from_subaccount_id(jsonb_extract_path(order_, 'orderId', 'subaccountId'));
order_side = dydx_from_protocol_order_side(order_->'side');
order_client_metadata = (order_->'clientMetadata')::bigint;

/** Upsert the order, populating the order_record fields with what will be in the database. */
SELECT * INTO order_record FROM orders WHERE "id" = order_uuid;
Expand All @@ -95,11 +99,13 @@ BEGIN
order_record."orderFlags" = jsonb_extract_path(order_, 'orderId', 'orderFlags')::bigint;
order_record."goodTilBlock" = (order_->'goodTilBlock')::bigint;
order_record."goodTilBlockTime" = to_timestamp((order_->'goodTilBlockTime')::double precision);
order_record."clientMetadata" = (order_->'clientMetadata')::bigint;
order_record."clientMetadata" = order_client_metadata;
order_record."updatedAt" = block_time;
order_record."updatedAtHeight" = block_height;

IF FOUND THEN
order_record."totalFilled" = total_filled;
order_record."status" = get_order_status(total_filled, order_record.size, is_cancelled, order_record."orderFlags", order_record."timeInForce");
order_record."status" = dydx_get_order_status(total_filled, order_record.size, is_cancelled, order_record."orderFlags", order_record."timeInForce");

UPDATE orders
SET
Expand All @@ -113,8 +119,8 @@ BEGIN
"timeInForce" = order_record."timeInForce",
"reduceOnly" = order_record."reduceOnly",
"clientMetadata" = order_record."clientMetadata",
"updatedAt" = block_time,
"updatedAtHeight" = block_height
"updatedAt" = order_record."updatedAt",
"updatedAtHeight" = order_record."updatedAtHeight"
WHERE id = order_uuid;
ELSE
order_record."id" = order_uuid;
Expand All @@ -125,10 +131,8 @@ BEGIN
order_record."type" = 'LIMIT'; /* TODO: Add additional order types once we support */

order_record."totalFilled" = fill_amount;
order_record."status" = get_order_status(fill_amount, order_size, is_cancelled, order_record."orderFlags", order_record."timeInForce");
order_record."status" = dydx_get_order_status(fill_amount, order_size, is_cancelled, order_record."orderFlags", order_record."timeInForce");
order_record."createdAtHeight" = block_height;
order_record."updatedAt" = block_time;
order_record."updatedAtHeight" = block_height;
INSERT INTO orders
("id", "subaccountId", "clientId", "clobPairId", "side", "size", "totalFilled", "price", "type",
"status", "timeInForce", "reduceOnly", "orderFlags", "goodTilBlock", "goodTilBlockTime", "createdAtHeight",
Expand Down Expand Up @@ -156,7 +160,7 @@ BEGIN
transaction_hash,
block_time,
block_height,
order_record."clientMetadata",
order_client_metadata,
fee)
RETURNING * INTO fill_record;

Expand Down

0 comments on commit 5534ad8

Please sign in to comment.