Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-507] Add Indexer changes for UpgradeIsolatedPerpetualToCross #2597

Merged
merged 18 commits into from
Nov 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -1249,8 +1249,12 @@ export interface UpdateClobPairEventV1SDKType {
/**
* 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.
*/

/** @deprecated */

export interface UpdatePerpetualEventV1 {
/**
* Unique Perpetual id.
Expand Down Expand Up @@ -1288,8 +1292,12 @@ export interface UpdatePerpetualEventV1 {
/**
* 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.
*/

/** @deprecated */

export interface UpdatePerpetualEventV1SDKType {
/**
* Unique Perpetual id.
Expand Down Expand Up @@ -1324,6 +1332,90 @@ export interface UpdatePerpetualEventV1SDKType {

liquidity_tier: number;
}
/**
* UpdatePerpetualEventV2 message contains all the information about an update
* to a perpetual on the dYdX chain.
*/

export interface UpdatePerpetualEventV2 {
/**
* Unique Perpetual id.
* Defined in perpetuals.perpetual
*/
id: number;
/**
* The name of the `Perpetual` (e.g. `BTC-USD`).
* Defined in perpetuals.perpetual
*/

ticker: string;
/**
* Unique id of market param associated with this perpetual market.
* Defined in perpetuals.perpetual
*/

marketId: number;
/**
* 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
*/

atomicResolution: number;
/**
* The liquidity_tier that this perpetual is associated with.
* Defined in perpetuals.perpetual
*/

liquidityTier: number;
/** Market type of the perpetual. */

marketType: PerpetualMarketType;
}
/**
* UpdatePerpetualEventV2 message contains all the information about an update
* to a perpetual on the dYdX chain.
*/

export interface UpdatePerpetualEventV2SDKType {
/**
* Unique Perpetual id.
* Defined in perpetuals.perpetual
*/
id: number;
/**
* The name of the `Perpetual` (e.g. `BTC-USD`).
* Defined in perpetuals.perpetual
*/

ticker: string;
/**
* Unique id of market param associated with this perpetual market.
* Defined in perpetuals.perpetual
*/

market_id: number;
/**
* 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
*/

atomic_resolution: number;
/**
* The liquidity_tier that this perpetual is associated with.
* Defined in perpetuals.perpetual
*/

liquidity_tier: number;
/** Market type of the perpetual. */

market_type: PerpetualMarketTypeSDKType;
}
/**
* TradingRewardsEventV1 is communicates all trading rewards for all accounts
* that receive trade rewards in the block.
Expand Down Expand Up @@ -3485,6 +3577,101 @@ export const UpdatePerpetualEventV1 = {

};

function createBaseUpdatePerpetualEventV2(): UpdatePerpetualEventV2 {
return {
id: 0,
ticker: "",
marketId: 0,
atomicResolution: 0,
liquidityTier: 0,
marketType: 0
};
}

export const UpdatePerpetualEventV2 = {
encode(message: UpdatePerpetualEventV2, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.id !== 0) {
writer.uint32(8).uint32(message.id);
}

if (message.ticker !== "") {
writer.uint32(18).string(message.ticker);
}

if (message.marketId !== 0) {
writer.uint32(24).uint32(message.marketId);
}

if (message.atomicResolution !== 0) {
writer.uint32(32).sint32(message.atomicResolution);
}

if (message.liquidityTier !== 0) {
writer.uint32(40).uint32(message.liquidityTier);
}

if (message.marketType !== 0) {
writer.uint32(48).int32(message.marketType);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): UpdatePerpetualEventV2 {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseUpdatePerpetualEventV2();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.id = reader.uint32();
break;

case 2:
message.ticker = reader.string();
break;

case 3:
message.marketId = reader.uint32();
break;

case 4:
message.atomicResolution = reader.sint32();
break;

case 5:
message.liquidityTier = reader.uint32();
break;

case 6:
message.marketType = (reader.int32() as any);
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<UpdatePerpetualEventV2>): UpdatePerpetualEventV2 {
const message = createBaseUpdatePerpetualEventV2();
message.id = object.id ?? 0;
message.ticker = object.ticker ?? "";
message.marketId = object.marketId ?? 0;
message.atomicResolution = object.atomicResolution ?? 0;
message.liquidityTier = object.liquidityTier ?? 0;
message.marketType = object.marketType ?? 0;
return message;
}

};

function createBaseTradingRewardsEventV1(): TradingRewardsEventV1 {
return {
tradingRewards: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import {
defaultPreviousHeight,
defaultTime,
defaultTxHash,
defaultUpdatePerpetualEvent,
defaultUpdatePerpetualEventV1,
defaultUpdatePerpetualEventV2,
} from '../helpers/constants';
import {
IndexerTendermintBlock,
IndexerTendermintEvent,
Timestamp,
UpdatePerpetualEventV1,
UpdatePerpetualEventV2,
} from '@dydxprotocol-indexer/v4-protos';
import {
createIndexerTendermintBlock,
createIndexerTendermintEvent,
eventPerpetualMarketTypeToIndexerPerpetualMarketType,
expectPerpetualMarketKafkaMessage,
} from '../helpers/indexer-proto-helpers';
import { DydxIndexerSubtypes } from '../../src/lib/types';
Expand Down Expand Up @@ -57,14 +60,29 @@ describe('update-perpetual-handler', () => {
jest.resetAllMocks();
});

describe('getParallelizationIds', () => {
describe.each([
[
'UpdatePerpetualEventV1',
UpdatePerpetualEventV1.encode(defaultUpdatePerpetualEventV1).finish(),
defaultUpdatePerpetualEventV1,
],
[
'UpdatePerpetualEventV2',
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,
);
Expand All @@ -80,51 +98,56 @@ 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({
updatePerpetualEventBytes,
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(
event.id.toString(),
);

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!]);
expect(perpetualMarket).toEqual(expect.objectContaining({
id: event.id.toString(),
ticker: event.ticker,
marketId: event.marketId,
atomicResolution: event.atomicResolution,
liquidityTierId: event.liquidityTier,
// Add V2-specific field expectations when testing V2 events
...('marketType' in event && {
marketType: eventPerpetualMarketTypeToIndexerPerpetualMarketType(event.marketType),
}),
}));
expect(perpetualMarket).toEqual(
perpetualMarketRefresher.getPerpetualMarketFromId(
event.id.toString()));
expectPerpetualMarketKafkaMessage(producerSendMock, [perpetualMarket!]);
});
});
});

function createKafkaMessageFromUpdatePerpetualEvent({
updatePerpetualEvent,
updatePerpetualEventBytes,
transactionIndex,
height,
time,
txHash,
}: {
updatePerpetualEvent: UpdatePerpetualEventV1,
updatePerpetualEventBytes: Uint8Array,
transactionIndex: number,
height: number,
time: Timestamp,
Expand All @@ -134,7 +157,7 @@ function createKafkaMessageFromUpdatePerpetualEvent({
events.push(
createIndexerTendermintEvent(
DydxIndexerSubtypes.UPDATE_PERPETUAL,
UpdatePerpetualEventV1.encode(updatePerpetualEvent).finish(),
updatePerpetualEventBytes,
transactionIndex,
0,
),
Expand Down
Loading
Loading