Skip to content

Commit

Permalink
Add parent subaccount subscription.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed May 6, 2024
1 parent db6856c commit c78bc4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
10 changes: 6 additions & 4 deletions indexer/services/socks/__tests__/lib/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Subscriptions } from '../../src/lib/subscription';
import { sendMessage, sendMessageString } from '../../src/helpers/wss';
import { RateLimiter } from '../../src/lib/rate-limit';
import {
dbHelpers, testMocks, perpetualMarketRefresher, CandleResolution,
dbHelpers, testMocks, perpetualMarketRefresher, CandleResolution, MAX_PARENT_SUBACCOUNTS,
} from '@dydxprotocol-indexer/postgres';
import { btcTicker, invalidChannel, invalidTicker } from '../constants';
import { axiosRequest } from '../../src/lib/axios';
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('Subscriptions', () => {
],
[Channel.V4_ORDERBOOK]: [invalidTicker],
[Channel.V4_TRADES]: [invalidTicker],
[Channel.V4_PARENT_ACCOUNTS]: [invalidTicker],
[Channel.V4_PARENT_ACCOUNTS]: [`address/${MAX_PARENT_SUBACCOUNTS}`],
};
const initialResponseUrlPatterns: Record<Channel, string[] | undefined> = {
[Channel.V4_ACCOUNTS]: [
Expand All @@ -58,8 +58,8 @@ describe('Subscriptions', () => {
[Channel.V4_ORDERBOOK]: ['/v4/orderbooks/perpetualMarket/.+'],
[Channel.V4_TRADES]: ['/v4/trades/perpetualMarket/.+'],
[Channel.V4_PARENT_ACCOUNTS]: [
'/v4/addresses/.+/subaccountNumber/.+',
'/v4/orders?.+OPEN,UNTRIGGERED,BEST_EFFORT_OPENED',
'/v4/addresses/.+/parentSubaccountNumber/.+',
'/v4/orders/parentSubaccountNumber?.+OPEN,UNTRIGGERED,BEST_EFFORT_OPENED',
]
};
const initialMessage: Object = { a: 'b' };
Expand Down Expand Up @@ -96,6 +96,7 @@ describe('Subscriptions', () => {
[Channel.V4_MARKETS, validIds[Channel.V4_MARKETS]],
[Channel.V4_ORDERBOOK, validIds[Channel.V4_ORDERBOOK]],
[Channel.V4_TRADES, validIds[Channel.V4_TRADES]],
[Channel.V4_PARENT_ACCOUNTS, validIds[Channel.V4_PARENT_ACCOUNTS]],
])('handles valid subscription request to channel %s', async (
channel: Channel,
id: string,
Expand Down Expand Up @@ -142,6 +143,7 @@ describe('Subscriptions', () => {
[Channel.V4_CANDLES, invalidIdsMap[Channel.V4_CANDLES]],
[Channel.V4_ORDERBOOK, invalidIdsMap[Channel.V4_ORDERBOOK]],
[Channel.V4_TRADES, invalidIdsMap[Channel.V4_TRADES]],
[Channel.V4_PARENT_ACCOUNTS, invalidIdsMap[Channel.V4_PARENT_ACCOUNTS]],
])('sends error message if invalid subscription request to channel %s', async (
channel: Channel,
invalidIds: string[],
Expand Down
29 changes: 25 additions & 4 deletions indexer/services/socks/src/lib/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
logger,
stats,
} from '@dydxprotocol-indexer/base';
import { CandleResolution, perpetualMarketRefresher } from '@dydxprotocol-indexer/postgres';
import { CandleResolution, MAX_PARENT_SUBACCOUNTS, perpetualMarketRefresher } from '@dydxprotocol-indexer/postgres';
import WebSocket from 'ws';

import config from '../config';
Expand Down Expand Up @@ -377,7 +377,6 @@ export class Subscriptions {
return false;
}
switch (channel) {
case (Channel.V4_PARENT_ACCOUNTS):
case (Channel.V4_ACCOUNTS): {
if (id === undefined) {
return false;
Expand Down Expand Up @@ -418,6 +417,25 @@ export class Subscriptions {

return resolution !== undefined;
}
case (Channel.V4_PARENT_ACCOUNTS): {
if (id === undefined) {
return false;
}
const parts: string[] = id.split('/');
if (parts.length !== 2) {
return false;
}

if (isNaN(Number(parts[1]))) {
return false;
}

if (Number(parts[1]) >= MAX_PARENT_SUBACCOUNTS) {
return false;
}

return true
}
default: {
throw new InvalidChannelError(channel);
}
Expand Down Expand Up @@ -574,7 +592,7 @@ export class Subscriptions {
] = await Promise.all([
axiosRequest({
method: RequestMethod.GET,
url: `${COMLINK_URL}/v4/addresses/${address}/subaccountNumber/${subaccountNumber}`,
url: `${COMLINK_URL}/v4/addresses/${address}/parentSubaccountNumber/${subaccountNumber}`,
timeout: config.INITIAL_GET_TIMEOUT_MS,
headers: {
'cf-ipcountry': country,
Expand All @@ -584,7 +602,7 @@ export class Subscriptions {
// TODO(DEC-1462): Use the /active-orders endpoint once it's added.
axiosRequest({
method: RequestMethod.GET,
url: `${COMLINK_URL}/v4/orders?address=${address}&subaccountNumber=${subaccountNumber}&status=OPEN,UNTRIGGERED,BEST_EFFORT_OPENED`,
url: `${COMLINK_URL}/v4/orders/parentSubaccountNumber?address=${address}&subaccountNumber=${subaccountNumber}&status=OPEN,UNTRIGGERED,BEST_EFFORT_OPENED`,
timeout: config.INITIAL_GET_TIMEOUT_MS,
headers: {
'cf-ipcountry': country,
Expand Down Expand Up @@ -651,6 +669,9 @@ export class Subscriptions {
if (channel === Channel.V4_ACCOUNTS) {
return this.getInitialResponseForSubaccountSubscription(id, country);
}
if (channel === Channel.V4_PARENT_ACCOUNTS) {
return this.getInitialResponseForParentSubaccountSubscription(id, country);
}
const endpoint: string | undefined = this.getInitialEndpointForSubscription(channel, id);
// If no endpoint exists, return an empty initial response.
if (endpoint === undefined) {
Expand Down

0 comments on commit c78bc4f

Please sign in to comment.