Skip to content

Commit

Permalink
[TRA-109] Add parent subaccount websocket (#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed May 9, 2024
1 parent cffdb5f commit 355d9e9
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
getParentSubaccountNum,
} from '../../src/lib/parent-subaccount-helpers';

describe('getParentSubaccountNum', () => {
it('Gets the parent subaccount number from a child subaccount number', () => {
expect(getParentSubaccountNum(0)).toEqual(0);
expect(getParentSubaccountNum(128)).toEqual(0);
expect(getParentSubaccountNum(128 * 999 - 1)).toEqual(127);
});

it('Throws an error if the child subaccount number is greater than the max child subaccount number', () => {
expect(() => getParentSubaccountNum(128001)).toThrowError('Child subaccount number must be less than or equal to 128000');
});
});
1 change: 1 addition & 0 deletions indexer/packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export * as uuid from './helpers/uuid';
export * as protocolTranslations from './lib/protocol-translations';
export * as orderTranslations from './lib/order-translations';
export * as apiTranslations from './lib/api-translations';
export * as parentSubaccountHelpers from './lib/parent-subaccount-helpers';
export * as dbHelpers from './helpers/db-helpers';
export * as storeHelpers from './helpers/stores-helpers';

Expand Down
11 changes: 11 additions & 0 deletions indexer/packages/postgres/src/lib/parent-subaccount-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
CHILD_SUBACCOUNT_MULTIPLIER,
MAX_PARENT_SUBACCOUNTS,
} from '../constants';

export function getParentSubaccountNum(childSubaccountNum: number): number {
if (childSubaccountNum > MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER) {
throw new Error(`Child subaccount number must be less than or equal to ${MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER}`);
}
return childSubaccountNum % MAX_PARENT_SUBACCOUNTS;
}
17 changes: 2 additions & 15 deletions indexer/services/comlink/__tests__/lib/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import {
getSignedNotionalAndRisk,
getTotalUnsettledFunding,
getPerpetualPositionsWithUpdatedFunding,
initializePerpetualPositionsWithFunding, getChildSubaccountNums, getParentSubaccountNum,
initializePerpetualPositionsWithFunding,
getChildSubaccountNums,
} from '../../src/lib/helpers';
import _ from 'lodash';
import Big from 'big.js';
Expand Down Expand Up @@ -720,18 +721,4 @@ describe('helpers', () => {
expect(() => getChildSubaccountNums(128)).toThrowError('Parent subaccount number must be less than 128');
});
});

describe('getParentSubaccountNum', () => {
it('Gets the parent subaccount number from a child subaccount number', () => {
expect(getParentSubaccountNum(0)).toEqual(0);
expect(getParentSubaccountNum(128)).toEqual(0);
expect(getParentSubaccountNum(128 * 999 - 1)).toEqual(127);
});
});

describe('getParentSubaccountNum', () => {
it('Throws an error if the child subaccount number is greater than the max child subaccount number', () => {
expect(() => getParentSubaccountNum(128001)).toThrowError('Child subaccount number must be less than 128000');
});
});
});
13 changes: 0 additions & 13 deletions indexer/services/comlink/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,16 +525,3 @@ export function getChildSubaccountIds(address: string, parentSubaccountNum: numb
(subaccountNumber: number): string => SubaccountTable.uuid(address, subaccountNumber),
);
}

/**
* Gets the parent subaccount number from a child subaccount number
* Parent subaccount = childSubaccount % 128
* @param childSubaccountNum
* @returns
*/
export function getParentSubaccountNum(childSubaccountNum: number): number {
if (childSubaccountNum > MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER) {
throw new Error(`Child subaccount number must be less than ${MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER}`);
}
return childSubaccountNum % MAX_PARENT_SUBACCOUNTS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import {
TradingRewardFromDatabase,
TransferFromDatabase,
TransferType,
parentSubaccountHelpers,
} from '@dydxprotocol-indexer/postgres';
import { OrderbookLevels, PriceLevel } from '@dydxprotocol-indexer/redis';
import { RedisOrder } from '@dydxprotocol-indexer/v4-protos';
import Big from 'big.js';
import _ from 'lodash';

import { getParentSubaccountNum } from '../lib/helpers';
import {
AssetById,
AssetPositionResponseObject,
Expand Down Expand Up @@ -244,12 +244,15 @@ export function transferToParentSubaccountResponseObject(

const senderParentSubaccountNum = transfer.senderWalletAddress
? undefined
: getParentSubaccountNum(subaccountMap[transfer.senderSubaccountId!].subaccountNumber,
: parentSubaccountHelpers.getParentSubaccountNum(
subaccountMap[transfer.senderSubaccountId!].subaccountNumber,
);

const recipientParentSubaccountNum = transfer.recipientWalletAddress
? undefined
: getParentSubaccountNum(subaccountMap[transfer.recipientSubaccountId!].subaccountNumber);
: parentSubaccountHelpers.getParentSubaccountNum(
subaccountMap[transfer.recipientSubaccountId!].subaccountNumber,
);

// Determine transfer type based on parent subaccount number.
let transferType: TransferType = TransferType.TRANSFER_IN;
Expand Down
20 changes: 20 additions & 0 deletions indexer/services/socks/__tests__/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION,
TRADES_WEBSOCKET_MESSAGE_VERSION,
} from '@dydxprotocol-indexer/kafka';
import {
MAX_PARENT_SUBACCOUNTS,
} from '@dydxprotocol-indexer/postgres';
import {
CandleMessage,
CandleMessage_Resolution,
Expand All @@ -28,10 +31,20 @@ export const defaultTxIndex: number = 1;
export const defaultEventIndex: number = 3;
export const defaultOwner: string = 'owner';
export const defaultAccNumber: number = 4;
export const defaultChildAccNumber: number = defaultAccNumber + MAX_PARENT_SUBACCOUNTS;
export const defaultChildAccNumber2: number = defaultAccNumber + 2 * MAX_PARENT_SUBACCOUNTS;
export const defaultSubaccountId: SubaccountId = {
owner: defaultOwner,
number: defaultAccNumber,
};
export const defaultChildSubaccountId: SubaccountId = {
owner: defaultOwner,
number: defaultChildAccNumber,
};
export const defaultChildSubaccountId2: SubaccountId = {
owner: defaultOwner,
number: defaultChildAccNumber2,
};
export const defaultContents: Object = {
prop: 'property',
field: 'field',
Expand Down Expand Up @@ -82,3 +95,10 @@ export const tradesMessage: TradeMessage = {
contents: defaultContentsString,
version: TRADES_WEBSOCKET_MESSAGE_VERSION,
};

export const childSubaccountMessage: SubaccountMessage = {
...commonMsgProps,
subaccountId: defaultChildSubaccountId,
contents: defaultContentsString,
version: SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getChannel, getMessageToForward } from '../../src/helpers/from-kafka-helpers';
import { getChannels, getMessageToForward } from '../../src/helpers/from-kafka-helpers';
import { InvalidForwardMessageError, InvalidTopicError } from '../../src/lib/errors';
import {
Channel,
Expand All @@ -17,7 +17,9 @@ import {
marketsMessage,
orderbookMessage,
subaccountMessage,
childSubaccountMessage,
tradesMessage,
defaultChildAccNumber,
} from '../constants';
import { KafkaMessage } from 'kafkajs';
import { createKafkaMessage } from './kafka';
Expand All @@ -39,17 +41,20 @@ import {
describe('from-kafka-helpers', () => {
describe('getChannel', () => {
it.each([
[WebsocketTopics.TO_WEBSOCKETS_CANDLES, Channel.V4_CANDLES],
[WebsocketTopics.TO_WEBSOCKETS_MARKETS, Channel.V4_MARKETS],
[WebsocketTopics.TO_WEBSOCKETS_ORDERBOOKS, Channel.V4_ORDERBOOK],
[WebsocketTopics.TO_WEBSOCKETS_SUBACCOUNTS, Channel.V4_ACCOUNTS],
[WebsocketTopics.TO_WEBSOCKETS_TRADES, Channel.V4_TRADES],
])('gets correct channel for topic %s', (topic: WebsocketTopics, channel: Channel) => {
expect(getChannel(topic)).toEqual(channel);
[WebsocketTopics.TO_WEBSOCKETS_CANDLES, [Channel.V4_CANDLES]],
[WebsocketTopics.TO_WEBSOCKETS_MARKETS, [Channel.V4_MARKETS]],
[WebsocketTopics.TO_WEBSOCKETS_ORDERBOOKS, [Channel.V4_ORDERBOOK]],
[
WebsocketTopics.TO_WEBSOCKETS_SUBACCOUNTS,
[Channel.V4_ACCOUNTS, Channel.V4_PARENT_ACCOUNTS],
],
[WebsocketTopics.TO_WEBSOCKETS_TRADES, [Channel.V4_TRADES]],
])('gets correct channel for topic %s', (topic: WebsocketTopics, channels: Channel[]) => {
expect(getChannels(topic)).toEqual(channels);
});

it('throws InvalidTopicError for invalid topic', () => {
expect(() => { getChannel(invalidTopic); }).toThrow(new InvalidTopicError(invalidTopic));
expect(() => { getChannels(invalidTopic); }).toThrow(new InvalidTopicError(invalidTopic));
});
});

Expand Down Expand Up @@ -132,6 +137,22 @@ describe('from-kafka-helpers', () => {
expect(messageToForward.contents).toEqual(defaultContents);
});

it('gets correct MessageToForward for subaccount message for parent subaccount channel', () => {
const message: KafkaMessage = createKafkaMessage(
Buffer.from(Uint8Array.from(SubaccountMessage.encode(childSubaccountMessage).finish())),
);
const messageToForward: MessageToForward = getMessageToForward(
Channel.V4_PARENT_ACCOUNTS,
message,
);

expect(messageToForward.channel).toEqual(Channel.V4_PARENT_ACCOUNTS);
expect(messageToForward.id).toEqual(`${defaultOwner}/${defaultAccNumber}`);
expect(messageToForward.contents).toEqual(defaultContents);
expect(messageToForward.subaccountNumber).toBeDefined();
expect(messageToForward.subaccountNumber).toEqual(defaultChildAccNumber);
});

it('throws InvalidForwardMessageError for empty message', () => {
const message: KafkaMessage = createKafkaMessage(null);

Expand Down
Loading

0 comments on commit 355d9e9

Please sign in to comment.