Skip to content

Commit

Permalink
fix su handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill committed Jun 11, 2024
1 parent 02a935f commit 5b1213b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
8 changes: 6 additions & 2 deletions indexer/packages/postgres/__tests__/db/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ describe('helpers', () => {
);

const unrealizedPnl: string = getUnrealizedPnl(
perpetualPosition, defaultPerpetualMarket, marketIdToMarket[defaultPerpetualMarket.marketId],
perpetualPosition,
defaultPerpetualMarket,
marketIdToMarket[defaultPerpetualMarket.marketId],
);

expect(unrealizedPnl).toEqual(Big(-50000).toFixed());
Expand All @@ -125,7 +127,9 @@ describe('helpers', () => {
);

const unrealizedPnl: string = getUnrealizedPnl(
perpetualPosition, defaultPerpetualMarket, marketIdToMarket[defaultPerpetualMarket.marketId],
perpetualPosition,
defaultPerpetualMarket,
marketIdToMarket[defaultPerpetualMarket.marketId],
);

expect(unrealizedPnl).toEqual(Big(50000).toFixed());
Expand Down
16 changes: 6 additions & 10 deletions indexer/services/ender/src/handlers/subaccount-update-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
AssetPositionModel,
assetRefresher,
AssetsMap,
MarketColumns,
MarketFromDatabase,
MarketModel,
MarketsMap,
MarketTable,
perpetualMarketRefresher,
PerpetualMarketsMap,
PerpetualPositionModel,
Expand Down Expand Up @@ -36,6 +35,7 @@ export class SubaccountUpdateHandler extends Handler<SubaccountUpdate> {
];
}

// eslint-disable-next-line @typescript-eslint/require-await
public async internalHandle(resultRow: pg.QueryResultRow): Promise<ConsolidatedKafkaEvent[]> {
const updateObjects: UpdatedPerpetualPositionSubaccountKafkaObject[] = _.map(
resultRow.perpetual_positions,
Expand All @@ -46,15 +46,11 @@ export class SubaccountUpdateHandler extends Handler<SubaccountUpdate> {
resultRow.asset_positions,
(value) => AssetPositionModel.fromJson(value) as AssetPositionFromDatabase,
);
const markets: MarketFromDatabase[] = await MarketTable.findAll(
{},
[],
{ txId: this.txId },
);
const marketIdToMarket: MarketsMap = _.keyBy(
markets,
MarketColumns.id,
const marketIdToMarket: MarketsMap = _.mapValues(
resultRow.markets,
(value) => MarketModel.fromJson(value) as MarketFromDatabase,
);

for (let i = 0; i < updateObjects.length; i++) {
const marketId: number = perpetualMarketRefresher.getPerpetualMarketsMap()[
updateObjects[i].perpetualId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CREATE OR REPLACE FUNCTION dydx_subaccount_update_handler(
- subaccount: The upserted subaccount in subaccount-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/subaccount-model.ts).
- perpetual_positions: A JSON array of upserted perpetual positions in perpetual-position-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-position-model.ts).
- asset_positions: A JSON array of upserted asset positions in asset-position-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/asset-position-model.ts).
- markets: A JSON object mapping market ids to market records.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
Expand All @@ -25,6 +26,7 @@ DECLARE
perpetual_position_record_updates jsonb[];
asset_position_update jsonb;
asset_position_record_updates jsonb[];
market_map jsonb;
BEGIN
event_id = dydx_event_id_from_parts(
block_height, transaction_index, event_index);
Expand Down Expand Up @@ -167,10 +169,13 @@ BEGIN
END;
END LOOP;

-- Fetch all markets
market_map := (SELECT jsonb_object_agg(id, dydx_to_jsonb(markets)) FROM markets);
RETURN jsonb_build_object(
'subaccount', dydx_to_jsonb(subaccount_record),
'perpetual_positions', to_jsonb(perpetual_position_record_updates),
'asset_positions', to_jsonb(asset_position_record_updates)
'asset_positions', to_jsonb(asset_position_record_updates),
'markets', market_map
);
END;
$$ LANGUAGE plpgsql;

0 comments on commit 5b1213b

Please sign in to comment.