Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill committed Nov 7, 2023
1 parent c812739 commit 5148966
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,42 +165,12 @@ BEGIN
RETURNING * INTO fill_record;

/* Upsert the perpetual_position record for this order_fill event. */
SELECT * INTO perpetual_position_record FROM perpetual_positions WHERE "subaccountId" = subaccount_uuid
AND "perpetualId" = perpetual_market_record."id"
ORDER BY "createdAtHeight" DESC;
IF NOT FOUND THEN
RAISE EXCEPTION 'Unable to find existing perpetual position, subaccountId: %, perpetualId: %', subaccount_uuid, perpetual_market_record."id";
END IF;
DECLARE
sum_open numeric = perpetual_position_record."sumOpen";
entry_price numeric = perpetual_position_record."entryPrice";
sum_close numeric = perpetual_position_record."sumClose";
exit_price numeric = perpetual_position_record."exitPrice";
BEGIN
IF dydx_perpetual_position_and_order_side_matching(
perpetual_position_record."side", order_side) THEN
sum_open = dydx_trim_scale(perpetual_position_record."sumOpen" + fill_amount);
entry_price = dydx_get_weighted_average(
perpetual_position_record."entryPrice", perpetual_position_record."sumOpen",
maker_price, fill_amount);
perpetual_position_record."sumOpen" = sum_open;
perpetual_position_record."entryPrice" = entry_price;
ELSE
sum_close = dydx_trim_scale(perpetual_position_record."sumClose" + fill_amount);
exit_price = dydx_get_weighted_average(
perpetual_position_record."exitPrice", perpetual_position_record."sumClose",
maker_price, fill_amount);
perpetual_position_record."sumClose" = sum_close;
perpetual_position_record."exitPrice" = exit_price;
END IF;
UPDATE perpetual_positions
SET
"sumOpen" = sum_open,
"entryPrice" = entry_price,
"sumClose" = sum_close,
"exitPrice" = exit_price
WHERE "id" = perpetual_position_record.id;
END;
perpetual_position_record = dydx_update_perpetual_position(
subaccount_uuid,
perpetual_market_record."id",
order_side,
fill_amount,
maker_price);

RETURN jsonb_build_object(
'order',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ CREATE OR REPLACE FUNCTION dydx_update_perpetual_position(
) RETURNS perpetual_positions AS $$
DECLARE
perpetual_position_record RECORD;
sum_open numeric;
entry_price numeric;
sum_close numeric;
exit_price numeric;
BEGIN
-- Retrieve the latest perpetual position record
SELECT * INTO perpetual_position_record
Expand All @@ -21,26 +25,35 @@ BEGIN
RAISE EXCEPTION 'Unable to find existing perpetual position, subaccountId: %, perpetualId: %', subaccount_uuid, perpetual_id;
END IF;

sum_open = perpetual_position_record."sumOpen";
entry_price = perpetual_position_record."entryPrice";
sum_close = perpetual_position_record."sumClose";
exit_price = perpetual_position_record."exitPrice";

-- Update the perpetual position record based on the side
IF dydx_perpetual_position_and_order_side_matching(perpetual_position_record."side", side) THEN
perpetual_position_record."sumOpen" := dydx_trim_scale(perpetual_position_record."sumOpen" + size);
perpetual_position_record."entryPrice" := dydx_get_weighted_average(
sum_open := dydx_trim_scale(perpetual_position_record."sumOpen" + size);
entry_price := dydx_get_weighted_average(
perpetual_position_record."entryPrice", perpetual_position_record."sumOpen", price, size
);
perpetual_position_record."sumOpen" = sum_open;
perpetual_position_record."entryPrice" = entry_price;
ELSE
perpetual_position_record."sumClose" := dydx_trim_scale(perpetual_position_record."sumClose" + size);
perpetual_position_record."exitPrice" := dydx_get_weighted_average(
sum_close := dydx_trim_scale(perpetual_position_record."sumClose" + size);
exit_price := dydx_get_weighted_average(
perpetual_position_record."exitPrice", perpetual_position_record."sumClose", price, size
);
perpetual_position_record."sumClose" = sum_close;
perpetual_position_record."exitPrice" = exit_price;
END IF;

-- Perform the actual update in the database
UPDATE perpetual_positions
SET
"sumOpen" = perpetual_position_record."sumOpen",
"entryPrice" = perpetual_position_record."entryPrice",
"sumClose" = perpetual_position_record."sumClose",
"exitPrice" = perpetual_position_record."exitPrice"
"sumOpen" = sum_open,
"entryPrice" = entry_price,
"sumClose" = sum_close,
"exitPrice" = exit_price
WHERE "id" = perpetual_position_record.id;

-- Return the updated perpetual position record as jsonb
Expand Down

0 comments on commit 5148966

Please sign in to comment.