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-109] Filter out transfers between child subaccounts of same parent. #1544

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions indexer/services/socks/__tests__/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
TRADES_WEBSOCKET_MESSAGE_VERSION,
} from '@dydxprotocol-indexer/kafka';
import {
TransferSubaccountMessageContents,
TransferType,
MAX_PARENT_SUBACCOUNTS,
} from '@dydxprotocol-indexer/postgres';
import {
Expand Down Expand Up @@ -102,3 +104,20 @@ export const childSubaccountMessage: SubaccountMessage = {
contents: defaultContentsString,
version: SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION,
};

export const defaultTransferContents: TransferSubaccountMessageContents = {
sender: {
address: defaultOwner,
subaccountNumber: defaultAccNumber,
},
recipient: {
address: defaultOwner,
subaccountNumber: defaultChildAccNumber,
},
symbol: 'USDC',
size: '1',
type: TransferType.TRANSFER_IN,
transactionHash: '0x1',
createdAt: '2023-10-05T14:48:00.000Z',
createdAtHeight: '10',
};
110 changes: 110 additions & 0 deletions indexer/services/socks/__tests__/helpers/from-kafka-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
childSubaccountMessage,
tradesMessage,
defaultChildAccNumber,
defaultTransferContents,
} from '../constants';
import { KafkaMessage } from 'kafkajs';
import { createKafkaMessage } from './kafka';
Expand All @@ -36,6 +37,8 @@ import {
testMocks,
perpetualMarketRefresher,
CandleResolution,
TransferSubaccountMessageContents,
SubaccountMessageContents, TransferType,
} from '@dydxprotocol-indexer/postgres';

describe('from-kafka-helpers', () => {
Expand Down Expand Up @@ -153,6 +156,113 @@ describe('from-kafka-helpers', () => {
expect(messageToForward.subaccountNumber).toEqual(defaultChildAccNumber);
});

it('filters out transfers between child subaccounts for parent subaccout channel', () => {
vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
const transferContents: SubaccountMessageContents = {
transfers: {
...defaultTransferContents,
sender: {
address: defaultOwner,
subaccountNumber: defaultAccNumber,
},
recipient: {
address: defaultOwner,
subaccountNumber: defaultChildAccNumber,
},
type: TransferType.TRANSFER_IN,
},
};
const message: KafkaMessage = createKafkaMessage(
Buffer.from(Uint8Array.from(SubaccountMessage.encode(
{
...childSubaccountMessage,
contents: JSON.stringify(transferContents),
},
).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({});
expect(messageToForward.subaccountNumber).toBeDefined();
expect(messageToForward.subaccountNumber).toEqual(defaultChildAccNumber);
});

it.each([
[
'transfer between other parent/child subaccount',
{
...defaultTransferContents,
sender: {
address: defaultOwner,
subaccountNumber: defaultAccNumber + 1,
},
recipient: {
address: defaultOwner,
subaccountNumber: defaultChildAccNumber,
},
},
],
[
'deposit',
{
...defaultTransferContents,
sender: {
address: defaultOwner,
subaccountNumber: undefined,
},
recipient: {
address: defaultOwner,
subaccountNumber: defaultChildAccNumber,
},
type: TransferType.DEPOSIT,
},
],
[
'withdraw',
{
...defaultTransferContents,
sender: {
address: defaultOwner,
subaccountNumber: defaultChildAccNumber,
},
recipient: {
address: defaultOwner,
subaccountNumber: undefined,
},
type: TransferType.WITHDRAWAL,
},
],
])('does not filter out transfer message for (%s)', (
_name: string,
transfer: TransferSubaccountMessageContents,
) => {
const transferContents: SubaccountMessageContents = {
transfers: transfer,
};
const message: KafkaMessage = createKafkaMessage(
Buffer.from(Uint8Array.from(SubaccountMessage.encode(
{
...childSubaccountMessage,
contents: JSON.stringify(transferContents),
},
).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(transferContents);
expect(messageToForward.subaccountNumber).toBeDefined();
expect(messageToForward.subaccountNumber).toEqual(defaultChildAccNumber);
});

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

Expand Down
39 changes: 37 additions & 2 deletions indexer/services/socks/src/helpers/from-kafka-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import {
perpetualMarketRefresher,
PROTO_TO_CANDLE_RESOLUTION,
parentSubaccountHelpers,
SubaccountMessageContents,
} from '@dydxprotocol-indexer/postgres';
import { getParentSubaccountNum } from '@dydxprotocol-indexer/postgres/build/src/lib/parent-subaccount-helpers';
import {
CandleMessage,
MarketMessage,
OrderbookMessage,
TradeMessage,
SubaccountMessage, CandleMessage_Resolution,
SubaccountMessage,
CandleMessage_Resolution,
} from '@dydxprotocol-indexer/v4-protos';
import { KafkaMessage } from 'kafkajs';

Expand Down Expand Up @@ -91,7 +94,7 @@ export function getMessageToForward(
channel,
id: getParentSubaccountMessageId(subaccountMessage),
subaccountNumber: subaccountMessage.subaccountId!.number,
contents: JSON.parse(subaccountMessage.contents),
contents: getParentSubaccountContents(subaccountMessage),
version: subaccountMessage.version,
};
}
Expand Down Expand Up @@ -133,3 +136,35 @@ function getCandleMessageId(candleMessage: CandleMessage): string {
}
return `${ticker}/${PROTO_TO_CANDLE_RESOLUTION[candleMessage.resolution]}`;
}

function getParentSubaccountContents(msg: SubaccountMessage): SubaccountMessageContents {
// Filter out transfers between child subaccounts of the same parent subaccount.
const contents: SubaccountMessageContents = JSON.parse(msg.contents) as SubaccountMessageContents;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorta unrelated to this change but why does SubaccountMessageContents have multiples of positions, orders, fills but only a singular transfer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's tech debt that was never fixed.

if (contents.transfers === undefined) {
return contents;
}

const senderAddress: string = contents.transfers.sender.address;
const recipientAddress: string = contents.transfers.recipient.address;

if (senderAddress !== recipientAddress) {
return contents;
}

const senderSubaccount: number | undefined = contents.transfers.sender.subaccountNumber;
const recipientSubaccount: number | undefined = contents.transfers.recipient.subaccountNumber;

if (senderSubaccount === undefined || recipientSubaccount === undefined) {
return contents;
}

const senderParentSubaccountNumber: number = getParentSubaccountNum(senderSubaccount);
const recipientParentSubaccountNumber: number = getParentSubaccountNum(recipientSubaccount);

if (senderParentSubaccountNumber !== recipientParentSubaccountNumber) {
return contents;
}

delete contents.transfers;
return contents;
}
vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
Loading