diff --git a/indexer/services/ender/__tests__/handlers/update-perpetual-handler.test.ts b/indexer/services/ender/__tests__/handlers/update-perpetual-handler.test.ts index f6d67cfcdc..f11b872721 100644 --- a/indexer/services/ender/__tests__/handlers/update-perpetual-handler.test.ts +++ b/indexer/services/ender/__tests__/handlers/update-perpetual-handler.test.ts @@ -12,13 +12,15 @@ import { defaultPreviousHeight, defaultTime, defaultTxHash, - defaultUpdatePerpetualEvent, + defaultUpdatePerpetualEventV1, + defaultUpdatePerpetualEventV2, } from '../helpers/constants'; import { IndexerTendermintBlock, IndexerTendermintEvent, Timestamp, UpdatePerpetualEventV1, + UpdatePerpetualEventV2, } from '@dydxprotocol-indexer/v4-protos'; import { createIndexerTendermintBlock, @@ -57,14 +59,29 @@ describe('update-perpetual-handler', () => { jest.resetAllMocks(); }); - describe('getParallelizationIds', () => { + describe.each([ + [ + 'UpdatePerpetualEventV1', + UpdatePerpetualEventV1.encode(defaultUpdatePerpetualEventV1).finish(), + defaultUpdatePerpetualEventV1, + ], + [ + 'PerpetualMarketCreateEventV2', + UpdatePerpetualEventV2.encode(defaultUpdatePerpetualEventV2).finish(), + defaultUpdatePerpetualEventV2, + ], + ])('%s', ( + _name: string, + updatePerpetualEventBytes: Uint8Array, + event: UpdatePerpetualEventV1 | UpdatePerpetualEventV2, + ) => { it('returns the correct parallelization ids', () => { const transactionIndex: number = 0; const eventIndex: number = 0; const indexerTendermintEvent: IndexerTendermintEvent = createIndexerTendermintEvent( DydxIndexerSubtypes.UPDATE_PERPETUAL, - UpdatePerpetualEventV1.encode(defaultUpdatePerpetualEvent).finish(), + updatePerpetualEventBytes, transactionIndex, eventIndex, ); @@ -80,40 +97,41 @@ describe('update-perpetual-handler', () => { 0, indexerTendermintEvent, 0, - defaultUpdatePerpetualEvent, + event, ); expect(handler.getParallelizationIds()).toEqual([]); }); - }); - it('updates an existing perpetual market', async () => { - const transactionIndex: number = 0; - const kafkaMessage: KafkaMessage = createKafkaMessageFromUpdatePerpetualEvent({ - updatePerpetualEvent: defaultUpdatePerpetualEvent, - transactionIndex, - height: defaultHeight, - time: defaultTime, - txHash: defaultTxHash, - }); - const producerSendMock: jest.SpyInstance = jest.spyOn(producer, 'send'); - await onMessage(kafkaMessage); + it('updates an existing perpetual market', async () => { + const transactionIndex: number = 0; + const kafkaMessage: KafkaMessage = createKafkaMessageFromUpdatePerpetualEvent({ + updatePerpetualEvent: event, + transactionIndex, + height: defaultHeight, + time: defaultTime, + txHash: defaultTxHash, + }); + const producerSendMock: jest.SpyInstance = jest.spyOn(producer, 'send'); + await onMessage(kafkaMessage); - const perpetualMarket: - PerpetualMarketFromDatabase | undefined = await PerpetualMarketTable.findById( - defaultUpdatePerpetualEvent.id.toString(), - ); - expect(perpetualMarket).toEqual(expect.objectContaining({ - id: defaultUpdatePerpetualEvent.id.toString(), - ticker: defaultUpdatePerpetualEvent.ticker, - marketId: defaultUpdatePerpetualEvent.marketId, - atomicResolution: defaultUpdatePerpetualEvent.atomicResolution, - liquidityTierId: defaultUpdatePerpetualEvent.liquidityTier, - })); - expect(perpetualMarket).toEqual( - perpetualMarketRefresher.getPerpetualMarketFromId( - defaultUpdatePerpetualEvent.id.toString())); - expectPerpetualMarketKafkaMessage(producerSendMock, [perpetualMarket!]); + const perpetualMarket: + PerpetualMarketFromDatabase | undefined = await PerpetualMarketTable.findById( + event.id.toString(), + ); + // TODO new fields + expect(perpetualMarket).toEqual(expect.objectContaining({ + id: event.id.toString(), + ticker: event.ticker, + marketId: event.marketId, + atomicResolution: event.atomicResolution, + liquidityTierId: event.liquidityTier, + })); + expect(perpetualMarket).toEqual( + perpetualMarketRefresher.getPerpetualMarketFromId( + event.id.toString())); + expectPerpetualMarketKafkaMessage(producerSendMock, [perpetualMarket!]); + }); }); }); diff --git a/indexer/services/ender/__tests__/helpers/constants.ts b/indexer/services/ender/__tests__/helpers/constants.ts index cb841d2d82..72a4712ccf 100644 --- a/indexer/services/ender/__tests__/helpers/constants.ts +++ b/indexer/services/ender/__tests__/helpers/constants.ts @@ -35,6 +35,7 @@ import { TransferEventV1, UpdateClobPairEventV1, UpdatePerpetualEventV1, + UpdatePerpetualEventV2, OpenInterestUpdateEventV1, OpenInterestUpdate, } from '@dydxprotocol-indexer/v4-protos'; @@ -191,7 +192,7 @@ export const defaultOpenInterestUpdateEvent: OpenInterestUpdateEventV1 = { openInterestUpdates: [defaultOpenInterestUpdate1, defaultOpenInterestUpdate2], }; -export const defaultUpdatePerpetualEvent: UpdatePerpetualEventV1 = { +export const defaultUpdatePerpetualEventV1: UpdatePerpetualEventV1 = { id: 0, ticker: 'BTC-USD2', marketId: 1, @@ -199,6 +200,15 @@ export const defaultUpdatePerpetualEvent: UpdatePerpetualEventV1 = { liquidityTier: 1, }; +export const defaultUpdatePerpetualEventV2: UpdatePerpetualEventV2 = { + id: 0, + ticker: 'BTC-USD2', + marketId: 1, + atomicResolution: -8, + liquidityTier: 1, + marketType: PerpetualMarketType.PERPETUAL_MARKET_TYPE_CROSS, +}; + export const defaultUpdateClobPairEvent: UpdateClobPairEventV1 = { clobPairId: 1, status: ClobPairStatus.CLOB_PAIR_STATUS_ACTIVE, @@ -397,7 +407,7 @@ export const defaultTradeMessage: SingleTradeMessage = contentToSingleTradeMessa testConstants.defaultPerpetualMarket.clobPairId, ); export const defaultTradeKafkaEvent: -ConsolidatedKafkaEvent = createConsolidatedKafkaEventFromTrade(defaultTradeMessage); + ConsolidatedKafkaEvent = createConsolidatedKafkaEventFromTrade(defaultTradeMessage); export const defaultStatefulOrderPlacementEvent: StatefulOrderEventV1 = { orderPlace: { diff --git a/indexer/services/ender/__tests__/validators/update-perpetual-validator.test.ts b/indexer/services/ender/__tests__/validators/update-perpetual-validator.test.ts index 8ba7128494..5cb6bb4c14 100644 --- a/indexer/services/ender/__tests__/validators/update-perpetual-validator.test.ts +++ b/indexer/services/ender/__tests__/validators/update-perpetual-validator.test.ts @@ -3,13 +3,15 @@ import { IndexerTendermintBlock, IndexerTendermintEvent, UpdatePerpetualEventV1, + UpdatePerpetualEventV2, } from '@dydxprotocol-indexer/v4-protos'; import { dbHelpers, testMocks, perpetualMarketRefresher, } from '@dydxprotocol-indexer/postgres'; import { DydxIndexerSubtypes } from '../../src/lib/types'; import { - defaultUpdatePerpetualEvent, + defaultUpdatePerpetualEventV1, + defaultUpdatePerpetualEventV2, defaultHeight, defaultTime, defaultTxHash, @@ -42,11 +44,26 @@ describe('update-perpetual-validator', () => { await dbHelpers.teardown(); }); - describe('validate', () => { + describe.each([ + [ + 'UpdatePerpetualEventV1', + UpdatePerpetualEventV1.encode(defaultUpdatePerpetualEventV1).finish(), + defaultUpdatePerpetualEventV1, + ], + [ + 'PerpetualMarketCreateEventV2', + UpdatePerpetualEventV2.encode(defaultUpdatePerpetualEventV2).finish(), + defaultUpdatePerpetualEventV2, + ], + ])('%s', ( + _name: string, + updatePerpetualEventBytes: Uint8Array, + event: UpdatePerpetualEventV1 | UpdatePerpetualEventV2, + ) => { it('does not throw error on valid perpetual market create event', () => { const validator: UpdatePerpetualValidator = new UpdatePerpetualValidator( - defaultUpdatePerpetualEvent, - createBlock(defaultUpdatePerpetualEvent), + event, + createBlock(updatePerpetualEventBytes), 0, ); @@ -57,10 +74,10 @@ describe('update-perpetual-validator', () => { it('throws error if id does not correspond to an existing perpetual market', () => { const validator: UpdatePerpetualValidator = new UpdatePerpetualValidator( { - ...defaultUpdatePerpetualEvent, + ...event, id: 20, }, - createBlock(defaultUpdatePerpetualEvent), + createBlock(updatePerpetualEventBytes), 0, ); @@ -72,11 +89,11 @@ describe('update-perpetual-validator', () => { }); function createBlock( - updatePerpetualEvent: UpdatePerpetualEventV1, + updatePerpetualEventBytes: Uint8Array, ): IndexerTendermintBlock { const event: IndexerTendermintEvent = createIndexerTendermintEvent( DydxIndexerSubtypes.UPDATE_PERPETUAL, - UpdatePerpetualEventV1.encode(updatePerpetualEvent).finish(), + updatePerpetualEventBytes, 0, 0, ); diff --git a/indexer/services/ender/src/handlers/update-perpetual-handler.ts b/indexer/services/ender/src/handlers/update-perpetual-handler.ts index d17d48e130..abaf22a044 100644 --- a/indexer/services/ender/src/handlers/update-perpetual-handler.ts +++ b/indexer/services/ender/src/handlers/update-perpetual-handler.ts @@ -4,7 +4,7 @@ import { perpetualMarketRefresher, PerpetualMarketModel, } from '@dydxprotocol-indexer/postgres'; -import { UpdatePerpetualEventV1 } from '@dydxprotocol-indexer/v4-protos'; +import { UpdatePerpetualEventV1, UpdatePerpetualEventV2 } from '@dydxprotocol-indexer/v4-protos'; import * as pg from 'pg'; import config from '../config'; @@ -12,8 +12,9 @@ import { generatePerpetualMarketMessage } from '../helpers/kafka-helper'; import { ConsolidatedKafkaEvent } from '../lib/types'; import { Handler } from './handler'; -export class UpdatePerpetualHandler extends Handler { - eventType: string = 'UpdatePerpetualEventV1'; +export class UpdatePerpetualHandler extends Handler< + UpdatePerpetualEventV1 | UpdatePerpetualEventV2> { + eventType: string = 'UpdatePerpetualEvent'; public getParallelizationIds(): string[] { return []; diff --git a/indexer/services/ender/src/helpers/postgres/postgres-functions.ts b/indexer/services/ender/src/helpers/postgres/postgres-functions.ts index 9a82c0eeff..aaae9616ce 100644 --- a/indexer/services/ender/src/helpers/postgres/postgres-functions.ts +++ b/indexer/services/ender/src/helpers/postgres/postgres-functions.ts @@ -44,7 +44,8 @@ const HANDLER_SCRIPTS: string[] = [ 'dydx_trading_rewards_handler.sql', 'dydx_transfer_handler.sql', 'dydx_update_clob_pair_handler.sql', - 'dydx_update_perpetual_handler.sql', + 'dydx_update_perpetual_v1_handler.sql', + 'dydx_update_perpetual_v2_handler.sql', 'dydx_vault_upsert_handler.sql', ]; diff --git a/indexer/services/ender/src/lib/block-processor.ts b/indexer/services/ender/src/lib/block-processor.ts index 6051425b8e..f7a784abb7 100644 --- a/indexer/services/ender/src/lib/block-processor.ts +++ b/indexer/services/ender/src/lib/block-processor.ts @@ -51,6 +51,7 @@ const TXN_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING: Record, ): void { const Initializer: - ValidatorInitializer | undefined = validatorMap[ + ValidatorInitializer | undefined = validatorMap[ serializeSubtypeAndVersion( eventProto.type, eventProto.version, ) - ]; + ]; if (Initializer === undefined) { const message: string = `cannot process subtype ${eventProto.type} and version ${eventProto.version}`; diff --git a/indexer/services/ender/src/lib/helper.ts b/indexer/services/ender/src/lib/helper.ts index 4a2497929a..f510cb8570 100644 --- a/indexer/services/ender/src/lib/helper.ts +++ b/indexer/services/ender/src/lib/helper.ts @@ -21,6 +21,7 @@ import { LiquidityTierUpsertEventV1, LiquidityTierUpsertEventV2, UpdatePerpetualEventV1, + UpdatePerpetualEventV2, UpdateClobPairEventV1, SubaccountMessage, DeleveragingEventV1, @@ -198,13 +199,30 @@ export function indexerTendermintEventToEventProtoWithType( }; } case (DydxIndexerSubtypes.UPDATE_PERPETUAL.toString()): { - return { - type: DydxIndexerSubtypes.UPDATE_PERPETUAL, - eventProto: UpdatePerpetualEventV1.decode(eventDataBinary), - indexerTendermintEvent: event, - version, - blockEventIndex, - }; + if (version === 1) { + return { + type: DydxIndexerSubtypes.UPDATE_PERPETUAL, + eventProto: UpdatePerpetualEventV1.decode(eventDataBinary), + indexerTendermintEvent: event, + version, + blockEventIndex, + }; + } else if (version === 2) { + return { + type: DydxIndexerSubtypes.UPDATE_PERPETUAL, + eventProto: UpdatePerpetualEventV2.decode(eventDataBinary), + indexerTendermintEvent: event, + version, + blockEventIndex, + }; + } else { + const message: string = `Invalid version for update perpetual event: ${version}`; + logger.error({ + at: 'helpers#indexerTendermintEventToEventWithType', + message, + }); + return undefined; + } } case (DydxIndexerSubtypes.UPDATE_CLOB_PAIR.toString()): { return { diff --git a/indexer/services/ender/src/lib/types.ts b/indexer/services/ender/src/lib/types.ts index b13797bf63..7c3a737d3b 100644 --- a/indexer/services/ender/src/lib/types.ts +++ b/indexer/services/ender/src/lib/types.ts @@ -31,6 +31,7 @@ import { LiquidityTierUpsertEventV1, LiquidityTierUpsertEventV2, UpdatePerpetualEventV1, + UpdatePerpetualEventV2, UpdateClobPairEventV1, DeleveragingEventV1, TradingRewardsEventV1, @@ -145,6 +146,12 @@ export type EventProtoWithTypeAndVersion = { indexerTendermintEvent: IndexerTendermintEvent, version: number, blockEventIndex: number, +} | { + type: DydxIndexerSubtypes.UPDATE_PERPETUAL, + eventProto: UpdatePerpetualEventV2, + indexerTendermintEvent: IndexerTendermintEvent, + version: number, + blockEventIndex: number, } | { type: DydxIndexerSubtypes.UPDATE_CLOB_PAIR, eventProto: UpdateClobPairEventV1, @@ -227,7 +234,7 @@ export type FundingEventMessage = { export type SumFields = PerpetualPositionColumns.sumOpen | PerpetualPositionColumns.sumClose; export type PriceFields = PerpetualPositionColumns.entryPrice | -PerpetualPositionColumns.exitPrice; + PerpetualPositionColumns.exitPrice; export type OrderFillEventWithLiquidity = { event: OrderFillEventV1, diff --git a/indexer/services/ender/src/scripts/handlers/dydx_block_processor_ordered_handlers.sql b/indexer/services/ender/src/scripts/handlers/dydx_block_processor_ordered_handlers.sql index 8d75ea9fe3..011776358b 100644 --- a/indexer/services/ender/src/scripts/handlers/dydx_block_processor_ordered_handlers.sql +++ b/indexer/services/ender/src/scripts/handlers/dydx_block_processor_ordered_handlers.sql @@ -62,7 +62,14 @@ BEGIN WHEN '"liquidity_tier"'::jsonb THEN rval[i] = dydx_liquidity_tier_handler(event_data); WHEN '"update_perpetual"'::jsonb THEN - rval[i] = dydx_update_perpetual_handler(event_data); + CASE (event_->'version')::int + WHEN 1 THEN + rval[i] = dydx_update_perpetual_v1_handler(event_data); + WHEN 2 THEN + rval[i] = dydx_update_perpetual_v2_handler(event_data); + ELSE + NULL; + END CASE; WHEN '"update_clob_pair"'::jsonb THEN rval[i] = dydx_update_clob_pair_handler(event_data); WHEN '"funding_values"'::jsonb THEN diff --git a/indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v1_handler.sql b/indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v1_handler.sql new file mode 100644 index 0000000000..1bcb60354a --- /dev/null +++ b/indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v1_handler.sql @@ -0,0 +1,40 @@ +CREATE OR REPLACE FUNCTION dydx_update_perpetual_v1_handler(event_data jsonb) RETURNS jsonb AS $$ +/** + Note: This is a deprecated handler, see `dydx_update_perpetual_v2_handler` for the latest handler. + Parameters: + - event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/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: + - perpetual_market: The updated perpetual market in perpetual-market-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-market-model.ts). + + (Note that no text should exist before the function declaration to ensure that exception line numbers are correct.) +*/ +DECLARE + perpetual_market_id bigint; + perpetual_market_record perpetual_markets%ROWTYPE; +BEGIN + perpetual_market_id = (event_data->'id')::bigint; + perpetual_market_record."ticker" = event_data->>'ticker'; + perpetual_market_record."marketId" = (event_data->'marketId')::integer; + perpetual_market_record."atomicResolution" = (event_data->'atomicResolution')::integer; + perpetual_market_record."liquidityTierId" = (event_data->'liquidityTier')::integer; + + UPDATE perpetual_markets + SET + "ticker" = perpetual_market_record."ticker", + "marketId" = perpetual_market_record."marketId", + "atomicResolution" = perpetual_market_record."atomicResolution", + "liquidityTierId" = perpetual_market_record."liquidityTierId" + WHERE "id" = perpetual_market_id + RETURNING * INTO perpetual_market_record; + + IF NOT FOUND THEN + RAISE EXCEPTION 'Could not find perpetual market with corresponding id %', perpetual_market_id; + END IF; + + RETURN jsonb_build_object( + 'perpetual_market', + dydx_to_jsonb(perpetual_market_record) + ); +END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v2_handler.sql b/indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v2_handler.sql new file mode 100644 index 0000000000..ab96da64a6 --- /dev/null +++ b/indexer/services/ender/src/scripts/handlers/dydx_update_perpetual_v2_handler.sql @@ -0,0 +1,41 @@ +CREATE OR REPLACE FUNCTION dydx_update_perpetual_v2_handler(event_data jsonb) RETURNS jsonb AS $$ +/** + Parameters: + - event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/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: + - perpetual_market: The updated perpetual market in perpetual-market-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-market-model.ts). + + (Note that no text should exist before the function declaration to ensure that exception line numbers are correct.) +*/ +DECLARE + perpetual_market_id bigint; + perpetual_market_record perpetual_markets%ROWTYPE; +BEGIN + perpetual_market_id = (event_data->'id')::bigint; + perpetual_market_record."ticker" = event_data->>'ticker'; + perpetual_market_record."marketId" = (event_data->'marketId')::integer; + perpetual_market_record."atomicResolution" = (event_data->'atomicResolution')::integer; + perpetual_market_record."liquidityTierId" = (event_data->'liquidityTier')::integer; + perpetual_market_record."marketType" = dydx_protocol_market_type_to_perpetual_market_type(event_data->'marketType'); + + UPDATE perpetual_markets + SET + "ticker" = perpetual_market_record."ticker", + "marketId" = perpetual_market_record."marketId", + "atomicResolution" = perpetual_market_record."atomicResolution", + "liquidityTierId" = perpetual_market_record."liquidityTierId" + "marketType" = perpetual_market_record."marketType" + WHERE "id" = perpetual_market_id + RETURNING * INTO perpetual_market_record; + + IF NOT FOUND THEN + RAISE EXCEPTION 'Could not find perpetual market with corresponding id %', perpetual_market_id; + END IF; + + RETURN jsonb_build_object( + 'perpetual_market', + dydx_to_jsonb(perpetual_market_record) + ); +END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/indexer/services/ender/src/validators/update-perpetual-validator.ts b/indexer/services/ender/src/validators/update-perpetual-validator.ts index 25caf65123..4bc50fe744 100644 --- a/indexer/services/ender/src/validators/update-perpetual-validator.ts +++ b/indexer/services/ender/src/validators/update-perpetual-validator.ts @@ -1,11 +1,13 @@ import { perpetualMarketRefresher } from '@dydxprotocol-indexer/postgres'; -import { IndexerTendermintEvent, UpdatePerpetualEventV1 } from '@dydxprotocol-indexer/v4-protos'; +import { IndexerTendermintEvent, UpdatePerpetualEventV1, UpdatePerpetualEventV2 } from '@dydxprotocol-indexer/v4-protos'; import { Handler } from '../handlers/handler'; import { UpdatePerpetualHandler } from '../handlers/update-perpetual-handler'; import { Validator } from './validator'; -export class UpdatePerpetualValidator extends Validator { +export class UpdatePerpetualValidator extends Validator< + UpdatePerpetualEventV1 | UpdatePerpetualEventV2 +> { public validate(): void { if (perpetualMarketRefresher.getPerpetualMarketFromId(this.event.id.toString()) === undefined) { return this.logAndThrowParseMessageError( diff --git a/proto/dydxprotocol/indexer/events/events.proto b/proto/dydxprotocol/indexer/events/events.proto index ed555d527e..b59fd01089 100644 --- a/proto/dydxprotocol/indexer/events/events.proto +++ b/proto/dydxprotocol/indexer/events/events.proto @@ -486,7 +486,37 @@ message UpdateClobPairEventV1 { // UpdatePerpetualEventV1 message contains all the information about an update // to a perpetual on the dYdX chain. +// Deprecated. See UpdatePerpetualEventV2 for the most up to date message +// for the event to update a perpetual. message UpdatePerpetualEventV1 { + option deprecated = true; + // Unique Perpetual id. + // Defined in perpetuals.perpetual + uint32 id = 1; + + // The name of the `Perpetual` (e.g. `BTC-USD`). + // Defined in perpetuals.perpetual + string ticker = 2; + + // Unique id of market param associated with this perpetual market. + // Defined in perpetuals.perpetual + uint32 market_id = 3; + + // The exponent for converting an atomic amount (`size = 1`) + // to a full coin. For example, if `AtomicResolution = -8` + // then a `PerpetualPosition` with `size = 1e8` is equivalent to + // a position size of one full coin. + // Defined in perpetuals.perpetual + sint32 atomic_resolution = 4; + + // The liquidity_tier that this perpetual is associated with. + // Defined in perpetuals.perpetual + uint32 liquidity_tier = 5; +} + +// UpdatePerpetualEventV2 message contains all the information about an update +// to a perpetual on the dYdX chain. +message UpdatePerpetualEventV2 { // Unique Perpetual id. // Defined in perpetuals.perpetual uint32 id = 1; @@ -509,6 +539,9 @@ message UpdatePerpetualEventV1 { // The liquidity_tier that this perpetual is associated with. // Defined in perpetuals.perpetual uint32 liquidity_tier = 5; + + // Market type of the perpetual. + dydxprotocol.indexer.protocol.v1.PerpetualMarketType market_type = 6; } // TradingRewardsEventV1 is communicates all trading rewards for all accounts diff --git a/protocol/indexer/events/constants.go b/protocol/indexer/events/constants.go index 0292aa427e..f0a77cc1dd 100644 --- a/protocol/indexer/events/constants.go +++ b/protocol/indexer/events/constants.go @@ -34,7 +34,7 @@ const ( AssetEventVersion uint32 = 1 PerpetualMarketEventVersion uint32 = 2 LiquidityTierEventVersion uint32 = 2 - UpdatePerpetualEventVersion uint32 = 1 + UpdatePerpetualEventVersion uint32 = 2 UpdateClobPairEventVersion uint32 = 1 DeleveragingEventVersion uint32 = 1 TradingRewardVersion uint32 = 1 diff --git a/protocol/indexer/events/perpetual.go b/protocol/indexer/events/perpetual.go index b23d2c9c99..08ea2fa82b 100644 --- a/protocol/indexer/events/perpetual.go +++ b/protocol/indexer/events/perpetual.go @@ -1,19 +1,25 @@ package events -// NewUpdatePerpetualEventV1 creates a UpdatePerpetualEventV1 representing +import ( + perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" +) + +// NewUpdatePerpetualEvent creates a UpdatePerpetualEventV2 representing // update of a perpetual. -func NewUpdatePerpetualEventV1( +func NewUpdatePerpetualEvent( id uint32, ticker string, marketId uint32, atomicResolution int32, liquidityTier uint32, -) *UpdatePerpetualEventV1 { - return &UpdatePerpetualEventV1{ + marketType perptypes.PerpetualMarketType, +) *UpdatePerpetualEventV2 { + return &UpdatePerpetualEventV2{ Id: id, Ticker: ticker, MarketId: marketId, AtomicResolution: atomicResolution, LiquidityTier: liquidityTier, + MarketType: marketType, } } diff --git a/protocol/indexer/events/perpetual_test.go b/protocol/indexer/events/perpetual_test.go index 2a9cc31077..31af2b5b4a 100644 --- a/protocol/indexer/events/perpetual_test.go +++ b/protocol/indexer/events/perpetual_test.go @@ -3,23 +3,27 @@ package events import ( "testing" + perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" + "github.com/stretchr/testify/require" ) func TestNewUpdatePerpetualEventV1_Success(t *testing.T) { - updatePerpetualEventV1 := NewUpdatePerpetualEventV1( + updatePerpetualEventV1 := NewUpdatePerpetualEvent( 5, "BTC-ETH", 5, -8, 2, + perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, ) - expectedUpdatePerpetualEventV1Proto := &UpdatePerpetualEventV1{ + expectedUpdatePerpetualEventV1Proto := &UpdatePerpetualEventV2{ Id: 5, Ticker: "BTC-ETH", MarketId: 5, AtomicResolution: -8, LiquidityTier: 2, + MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, } require.Equal(t, expectedUpdatePerpetualEventV1Proto, updatePerpetualEventV1) }